Skip to content

Commit ee475ce

Browse files
pringelmannbackportbot[bot]
authored andcommitted
fix(auth): rotate the stored token and invalidate the old hash
Signed-off-by: Peter Ringelmann <peter.ringelmann@nextcloud.com> Assisted-by: ClaudeCode:claude-opus-5-5
1 parent acfee6c commit ee475ce

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

‎core/Controller/AppPasswordController.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ public function rotateAppPassword(): DataResponse {
143143

144144
$appPassword = $this->session->get('app_password');
145145

146+
$newToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
147+
146148
try {
147149
$token = $this->tokenProvider->getToken($appPassword);
150+
$this->tokenProvider->rotate($token, $appPassword, $newToken);
148151
} catch (InvalidTokenException $e) {
149152
throw new OCSForbiddenException('could not rotate apptoken');
150153
}
151154

152-
$newToken = $this->random->generate(72, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
153-
$this->tokenProvider->rotate($token, $appPassword, $newToken);
154-
155155
return new DataResponse([
156156
'apppassword' => $newToken,
157157
]);

‎lib/private/Authentication/Token/IProvider.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ public function setPassword(OCPIToken $token, string $tokenId, string $password)
150150
* @param OCPIToken $token
151151
* @param string $oldTokenId
152152
* @param string $newTokenId
153-
* @return OCPIToken
153+
* @return OCPIToken the rotated token as it is stored, not necessarily the given instance
154+
* @throws InvalidTokenException when the token does not exist (anymore)
154155
* @throws \RuntimeException when OpenSSL reports a problem
155156
*/
156157
public function rotate(OCPIToken $token, string $oldTokenId, string $newTokenId): OCPIToken;

‎lib/private/Authentication/Token/PublicKeyTokenProvider.php‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function getToken(string $tokenId): OCPIToken {
160160
} catch (DoesNotExistException $ex) {
161161
try {
162162
$token = $this->mapper->getToken($this->hashTokenWithEmptySecret($tokenId));
163-
$this->rotate($token, $tokenId, $tokenId);
163+
$token = $this->rotate($token, $tokenId, $tokenId);
164164
} catch (DoesNotExistException) {
165165
$this->cacheInvalidHash($tokenHash);
166166
throw new InvalidTokenException('Token does not exist: ' . $ex->getMessage(), 0, $ex);
@@ -377,14 +377,33 @@ public function rotate(OCPIToken $token, string $oldTokenId, string $newTokenId)
377377
throw new InvalidTokenException('Invalid token type');
378378
}
379379

380+
// The given token might come from the cache, so rotate what is stored:
381+
// it might have been invalidated, rotated or changed in the meantime
382+
$oldTokenHash = $token->getToken();
383+
try {
384+
$token = $this->mapper->getToken($oldTokenHash);
385+
} catch (DoesNotExistException $ex) {
386+
$this->cacheInvalidHash($oldTokenHash);
387+
throw new InvalidTokenException('Token does not exist: ' . $ex->getMessage(), 0, $ex);
388+
}
389+
if ($token->getType() === OCPIToken::WIPE_TOKEN) {
390+
$this->cacheToken($token);
391+
throw new WipeTokenException($token);
392+
}
393+
380394
// Decrypt private key with oldTokenId
381395
$privateKey = $this->decrypt($token->getPrivateKey(), $oldTokenId);
382396
// Encrypt with the new token
383397
$token->setPrivateKey($this->encrypt($privateKey, $newTokenId));
384398

385-
$token->setToken($this->hashToken($newTokenId));
399+
$newTokenHash = $this->hashToken($newTokenId);
400+
$token->setToken($newTokenHash);
386401
$this->updateToken($token);
387402

403+
if ($oldTokenHash !== $newTokenHash) {
404+
$this->cacheInvalidHash($oldTokenHash);
405+
}
406+
388407
return $token;
389408
}
390409

0 commit comments

Comments
 (0)