Skip to content

Commit 3b3e56d

Browse files
ArtificialOwlBackportbot
authored andcommitted
token instead of uuid
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 62369ef commit 3b3e56d

3 files changed

Lines changed: 20 additions & 22 deletions

File tree

lib/Circles/FileSharingBroadcaster.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,13 @@ public function createShareToMember(SharingFrame $frame, Member $member) {
181181
$password = '';
182182

183183
if ($this->configService->enforcePasswordProtection()) {
184-
$password = $this->miscService->uuid(15);
184+
$password = $this->miscService->token(15);
185185
}
186186
$token = $this->tokensRequest->generateTokenForMember($member, $share->getId(), $password);
187187
if ($token !== '') {
188188
$this->sharedByMail($circle, $share, $member->getUserId(), $token, $password);
189189
}
190-
} catch (TokenDoesNotExistException $e) {
191-
} catch (NotFoundException $e) {
190+
} catch (Exception $e) {
192191
}
193192
}
194193

@@ -465,7 +464,11 @@ private function generateEmailTemplate($subject, $text, $fileName, $link, $autho
465464
private function sendMailExitingShares(array $unknownShares, $author, Member $member, $circleName) {
466465
$password = '';
467466
if ($this->configService->enforcePasswordProtection()) {
468-
$password = $this->miscService->uuid(15);
467+
try {
468+
$password = $this->miscService->token(15);
469+
} catch (Exception $e) {
470+
return;
471+
}
469472
}
470473

471474
$data = [];

lib/Db/TokensRequest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public function getTokensFromMember(Member $member) {
109109
}
110110

111111

112-
113112
/**
114113
* @param Member $member
115114
* @param int $shareId
@@ -119,11 +118,12 @@ public function getTokensFromMember(Member $member) {
119118
* @return string
120119
*/
121120
public function generateTokenForMember(Member $member, int $shareId, string $password = '') {
122-
$token = $this->miscService->uuid(15);
123-
124-
$hasher = \OC::$server->getHasher();
125-
$password = ($password !== '') ? $hasher->hash($password) : '';
126121
try {
122+
$token = $this->miscService->token(15);
123+
124+
$hasher = \OC::$server->getHasher();
125+
$password = ($password !== '') ? $hasher->hash($password) : '';
126+
127127
$qb = $this->getTokensInsertSql();
128128
$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
129129
->setValue('user_id', $qb->createNamedParameter($member->getUserId()))

lib/Service/MiscService.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,18 @@ public function asyncAndLeaveClientOutOfThis($result = '') {
307307
* @param int $length
308308
*
309309
* @return string
310+
* @throws Exception
310311
*/
311-
public function uuid(int $length = 0): string {
312-
$uuid = sprintf(
313-
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff),
314-
mt_rand(0, 0xffff), mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000,
315-
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
316-
);
317-
318-
if ($length > 0) {
319-
if ($length <= 16) {
320-
$uuid = str_replace('-', '', $uuid);
321-
}
312+
public function token(int $length = 0): string {
313+
$chars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
322314

323-
$uuid = substr($uuid, 0, $length);
315+
$str = '';
316+
$max = strlen($chars);
317+
for ($i = 0; $i <= $length; $i++) {
318+
$str .= $chars[random_int(0, $max)];
324319
}
325320

326-
return $uuid;
321+
return $str;
327322
}
328323

329324
}

0 commit comments

Comments
 (0)