Skip to content

Commit ccaaffe

Browse files
committed
chore(cleanup): deprecated secureRandom->generate
See nextcloud/server#61538 Signed-off-by: Max <max@nextcloud.com>
1 parent ccd88bc commit ccaaffe

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

lib/Service/SessionService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class SessionService {
2626
public const int SESSION_VALID_TIME = 5 * 60;
2727

2828
private SessionMapper $sessionMapper;
29-
private ISecureRandom $secureRandom;
3029
private ITimeFactory $timeFactory;
3130
private IUserManager $userManager;
3231
private IAvatarManager $avatarManager;
@@ -38,7 +37,6 @@ class SessionService {
3837

3938
public function __construct(
4039
SessionMapper $sessionMapper,
41-
ISecureRandom $secureRandom,
4240
ITimeFactory $timeFactory,
4341
IUserManager $userManager,
4442
IAvatarManager $avatarManager,
@@ -48,7 +46,6 @@ public function __construct(
4846
ICacheFactory $cacheFactory,
4947
) {
5048
$this->sessionMapper = $sessionMapper;
51-
$this->secureRandom = $secureRandom;
5249
$this->timeFactory = $timeFactory;
5350
$this->userManager = $userManager;
5451
$this->avatarManager = $avatarManager;
@@ -72,7 +69,7 @@ public function initSession(int $documentId, ?string $guestName = null): Session
7269
$session = new Session();
7370
$session->setDocumentId($documentId);
7471
$session->setUserId($this->userId);
75-
$session->setToken($this->secureRandom->generate(64));
72+
$session->setToken($this->generateRandomString());
7673
$session->setColor($this->getColor());
7774
if ($this->userId === null) {
7875
$session->setGuestName($guestName ?? '');
@@ -242,11 +239,16 @@ private function getColor(string $guestName = ''): string {
242239
private function getColorForGuestName(string $guestName = ''): string {
243240
$uniqueGuestId = !empty($guestName)
244241
? $guestName . '(guest)' // make it harder to impersonate users.
245-
: $this->secureRandom->generate(12);
242+
: $this->generateRandomString(12);
246243
$color = $this->avatarManager->getGuestAvatar($uniqueGuestId)->avatarBackgroundColor($uniqueGuestId);
247244
return $color->name();
248245
}
249246

247+
private function generateRandomString(int $length = 64): string {
248+
$randomizer = new \Random\Randomizer();
249+
return $randomizer->getBytesFromString(ISecureRandom::CHAR_ALPHANUMERIC . '+/', $length);
250+
}
251+
250252
public function isUserInDocument(int $documentId, string $mention): bool {
251253
return $this->sessionMapper->isUserInDocument($documentId, $mention);
252254
}

0 commit comments

Comments
 (0)