Skip to content

Commit

Permalink
Merge pull request #46668 from nextcloud/backport/46640/stable28
Browse files Browse the repository at this point in the history
[stable28] fix(Token): take over scope in token refresh with login by cookie
  • Loading branch information
AndyScherzinger authored Jul 25, 2024
2 parents f02bbf8 + 79dcb5a commit 9c94775
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/private/Authentication/Token/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public function generateToken(string $token,
?string $password,
string $name,
int $type = OCPIToken::TEMPORARY_TOKEN,
int $remember = OCPIToken::DO_NOT_REMEMBER): OCPIToken;
int $remember = OCPIToken::DO_NOT_REMEMBER,
?array $scope = null,
): OCPIToken;

/**
* Get a token by token id
Expand Down
7 changes: 5 additions & 2 deletions lib/private/Authentication/Token/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public function generateToken(string $token,
$password,
string $name,
int $type = OCPIToken::TEMPORARY_TOKEN,
int $remember = OCPIToken::DO_NOT_REMEMBER): OCPIToken {
int $remember = OCPIToken::DO_NOT_REMEMBER,
?array $scope = null,
): OCPIToken {
if (mb_strlen($name) > 128) {
$name = mb_substr($name, 0, 120) . '';
}
Expand All @@ -75,7 +77,8 @@ public function generateToken(string $token,
$password,
$name,
$type,
$remember
$remember,
$scope,
);
} catch (UniqueConstraintViolationException $e) {
// It's rare, but if two requests of the same session (e.g. env-based SAML)
Expand Down
14 changes: 11 additions & 3 deletions lib/private/Authentication/Token/PublicKeyTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public function generateToken(string $token,
?string $password,
string $name,
int $type = OCPIToken::TEMPORARY_TOKEN,
int $remember = OCPIToken::DO_NOT_REMEMBER): OCPIToken {
int $remember = OCPIToken::DO_NOT_REMEMBER,
?array $scope = null,
): OCPIToken {
if (strlen($token) < self::TOKEN_MIN_LENGTH) {
$exception = new InvalidTokenException('Token is too short, minimum of ' . self::TOKEN_MIN_LENGTH . ' characters is required, ' . strlen($token) . ' characters given');
$this->logger->error('Invalid token provided when generating new token', ['exception' => $exception]);
Expand All @@ -129,6 +131,10 @@ public function generateToken(string $token,
$dbToken->setPasswordHash($randomOldToken->getPasswordHash());
}

if ($scope !== null) {
$dbToken->setScope($scope);
}

$this->mapper->insert($dbToken);

if (!$oldTokenMatches && $password !== null) {
Expand Down Expand Up @@ -256,16 +262,18 @@ public function renewSessionToken(string $oldSessionId, string $sessionId): OCPI
$privateKey = $this->decrypt($token->getPrivateKey(), $oldSessionId);
$password = $this->decryptPassword($token->getPassword(), $privateKey);
}

$scope = $token->getScope() === '' ? null : $token->getScopeAsArray();
$newToken = $this->generateToken(
$sessionId,
$token->getUID(),
$token->getLoginName(),
$password,
$token->getName(),
OCPIToken::TEMPORARY_TOKEN,
$token->getRemember()
$token->getRemember(),
$scope,
);
$newToken->setScope($token->getScopeAsArray());
$this->cacheToken($newToken);

$this->cacheInvalidHash($token->getToken());
Expand Down

0 comments on commit 9c94775

Please sign in to comment.