Skip to content

Commit 59e92dd

Browse files
authored
Merge pull request #46666 from nextcloud/backport/46640/stable26
[stable26] fix(Token): take over scope in token refresh with login by cookie
2 parents 7826984 + c6d8aff commit 59e92dd

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

lib/private/Authentication/Token/IProvider.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ interface IProvider {
4848
* @return IToken
4949
* @throws \RuntimeException when OpenSSL reports a problem
5050
*/
51-
public function generateToken(string $token,
52-
string $uid,
53-
string $loginName,
54-
?string $password,
55-
string $name,
56-
int $type = IToken::TEMPORARY_TOKEN,
57-
int $remember = IToken::DO_NOT_REMEMBER): IToken;
51+
public function generateToken(
52+
string $token,
53+
string $uid,
54+
string $loginName,
55+
?string $password,
56+
string $name,
57+
int $type = IToken::TEMPORARY_TOKEN,
58+
int $remember = IToken::DO_NOT_REMEMBER,
59+
?array $scope = null,
60+
): IToken;
5861

5962
/**
6063
* Get a token by token id

lib/private/Authentication/Token/Manager.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ public function __construct(PublicKeyTokenProvider $publicKeyTokenProvider) {
5454
* @param int $remember whether the session token should be used for remember-me
5555
* @return IToken
5656
*/
57-
public function generateToken(string $token,
58-
string $uid,
59-
string $loginName,
60-
$password,
61-
string $name,
62-
int $type = IToken::TEMPORARY_TOKEN,
63-
int $remember = IToken::DO_NOT_REMEMBER): IToken {
57+
public function generateToken(
58+
string $token,
59+
string $uid,
60+
string $loginName,
61+
$password,
62+
string $name,
63+
int $type = IToken::TEMPORARY_TOKEN,
64+
int $remember = IToken::DO_NOT_REMEMBER,
65+
?array $scope = null,
66+
): IToken {
6467
if (mb_strlen($name) > 128) {
6568
$name = mb_substr($name, 0, 120) . '';
6669
}
@@ -73,7 +76,8 @@ public function generateToken(string $token,
7376
$password,
7477
$name,
7578
$type,
76-
$remember
79+
$remember,
80+
$scope,
7781
);
7882
} catch (UniqueConstraintViolationException $e) {
7983
// It's rare, but if two requests of the same session (e.g. env-based SAML)

lib/private/Authentication/Token/PublicKeyTokenProvider.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ public function __construct(PublicKeyTokenMapper $mapper,
9393
/**
9494
* {@inheritDoc}
9595
*/
96-
public function generateToken(string $token,
97-
string $uid,
98-
string $loginName,
99-
?string $password,
100-
string $name,
101-
int $type = IToken::TEMPORARY_TOKEN,
102-
int $remember = IToken::DO_NOT_REMEMBER): IToken {
96+
public function generateToken(
97+
string $token,
98+
string $uid,
99+
string $loginName,
100+
?string $password,
101+
string $name,
102+
int $type = IToken::TEMPORARY_TOKEN,
103+
int $remember = IToken::DO_NOT_REMEMBER,
104+
?array $scope = null,
105+
): IToken {
103106
if (strlen($token) < self::TOKEN_MIN_LENGTH) {
104107
$exception = new InvalidTokenException('Token is too short, minimum of ' . self::TOKEN_MIN_LENGTH . ' characters is required, ' . strlen($token) . ' characters given');
105108
$this->logger->error('Invalid token provided when generating new token', ['exception' => $exception]);
@@ -121,6 +124,10 @@ public function generateToken(string $token,
121124
$dbToken->setPasswordHash($randomOldToken->getPasswordHash());
122125
}
123126

127+
if ($scope !== null) {
128+
$dbToken->setScope($scope);
129+
}
130+
124131
$this->mapper->insert($dbToken);
125132

126133
if (!$oldTokenMatches && $password !== null) {
@@ -233,16 +240,18 @@ public function renewSessionToken(string $oldSessionId, string $sessionId): ITok
233240
$privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId);
234241
$password = $this->decryptPassword($token->getPassword(), $privateKey);
235242
}
243+
244+
$scope = $token->getScope() === '' ? null : $token->getScopeAsArray();
236245
$newToken = $this->generateToken(
237246
$sessionId,
238247
$token->getUID(),
239248
$token->getLoginName(),
240249
$password,
241250
$token->getName(),
242251
IToken::TEMPORARY_TOKEN,
243-
$token->getRemember()
252+
$token->getRemember(),
253+
$scope,
244254
);
245-
$newToken->setScope($token->getScopeAsArray());
246255

247256
$this->mapper->delete($token);
248257

0 commit comments

Comments
 (0)