Skip to content

Commit f82bdbe

Browse files
committed
test(auth): cover rotation of stored and invalidated tokens
Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Peter Ringelmann <peter.ringelmann@nextcloud.com>
1 parent ee475ce commit f82bdbe

2 files changed

Lines changed: 166 additions & 0 deletions

File tree

‎tests/Core/Controller/AppPasswordControllerTest.php‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,63 @@ public function testDeleteAppPasswordSuccess(): void {
246246

247247
$this->assertEquals(new DataResponse(), $result);
248248
}
249+
250+
public function testRotateAppPasswordNoAppPassword(): void {
251+
$this->session->method('exists')
252+
->with('app_password')
253+
->willReturn(false);
254+
255+
$this->expectException(OCSForbiddenException::class);
256+
257+
$this->controller->rotateAppPassword();
258+
}
259+
260+
public function testRotateAppPasswordFails(): void {
261+
$this->session->method('exists')
262+
->with('app_password')
263+
->willReturn(true);
264+
$this->session->method('get')
265+
->with('app_password')
266+
->willReturn('myAppPassword');
267+
$this->random->method('generate')
268+
->willReturn('myNewAppPassword');
269+
270+
$token = $this->createMock(IToken::class);
271+
$this->tokenProvider->method('getToken')
272+
->with('myAppPassword')
273+
->willReturn($token);
274+
$this->tokenProvider->expects($this->once())
275+
->method('rotate')
276+
->with($token, 'myAppPassword', 'myNewAppPassword')
277+
->willThrowException(new InvalidTokenException());
278+
279+
$this->expectException(OCSForbiddenException::class);
280+
281+
$this->controller->rotateAppPassword();
282+
}
283+
284+
public function testRotateAppPasswordSuccess(): void {
285+
$this->session->method('exists')
286+
->with('app_password')
287+
->willReturn(true);
288+
$this->session->method('get')
289+
->with('app_password')
290+
->willReturn('myAppPassword');
291+
$this->random->method('generate')
292+
->willReturn('myNewAppPassword');
293+
294+
$token = $this->createMock(IToken::class);
295+
$this->tokenProvider->method('getToken')
296+
->with('myAppPassword')
297+
->willReturn($token);
298+
$this->tokenProvider->expects($this->once())
299+
->method('rotate')
300+
->with($token, 'myAppPassword', 'myNewAppPassword')
301+
->willReturn($token);
302+
303+
$result = $this->controller->rotateAppPassword();
304+
305+
$this->assertEquals(new DataResponse(['apppassword' => 'myNewAppPassword']), $result);
306+
}
307+
249308
}

‎tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
use OC\Authentication\Exceptions\ExpiredTokenException;
1313
use OC\Authentication\Exceptions\InvalidTokenException;
1414
use OC\Authentication\Exceptions\PasswordlessTokenException;
15+
use OC\Authentication\Exceptions\WipeTokenException;
1516
use OC\Authentication\Token\PublicKeyToken;
1617
use OC\Authentication\Token\PublicKeyTokenMapper;
1718
use OC\Authentication\Token\PublicKeyTokenProvider;
19+
use OC\Memcache\ArrayCache;
1820
use OCP\AppFramework\Db\DoesNotExistException;
1921
use OCP\AppFramework\Utility\ITimeFactory;
2022
use OCP\Authentication\Token\IToken;
@@ -545,6 +547,9 @@ public function testRotate(): void {
545547
['auth.storeCryptedPassword', true, true],
546548
]);
547549
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
550+
$this->mapper->method('getToken')
551+
->with($actual->getToken())
552+
->willReturn($actual);
548553

549554
$new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken');
550555

@@ -560,6 +565,9 @@ public function testRotateNoPassword(): void {
560565
$type = IToken::PERMANENT_TOKEN;
561566

562567
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
568+
$this->mapper->method('getToken')
569+
->with($actual->getToken())
570+
->willReturn($actual);
563571

564572
$oldPrivate = $actual->getPrivateKey();
565573

@@ -571,6 +579,105 @@ public function testRotateNoPassword(): void {
571579
$this->assertNull($new->getPassword());
572580
}
573581

582+
public function testRotateInvalidatesOldTokenInCache(): void {
583+
$oldToken = 'oldtokentokentokentokentoken';
584+
$newToken = 'newtokentokentokentokentoken';
585+
586+
$this->cacheFactory->method('createInMemory')
587+
->willReturn(new ArrayCache());
588+
$tokenProvider = new PublicKeyTokenProvider(
589+
$this->mapper,
590+
$this->crypto,
591+
$this->config,
592+
$this->db,
593+
$this->logger,
594+
$this->timeFactory,
595+
$this->hasher,
596+
$this->cacheFactory,
597+
);
598+
599+
$actual = $tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
600+
$oldHash = $actual->getToken();
601+
$this->assertSame($oldHash, $tokenProvider->getToken($oldToken)->getToken());
602+
603+
// Only the cache knows the token after the rotation
604+
$stored = true;
605+
$this->mapper->method('getToken')
606+
->willReturnCallback(function (string $hash) use (&$stored, $oldHash, $actual) {
607+
if ($stored && $hash === $oldHash) {
608+
return $actual;
609+
}
610+
throw new DoesNotExistException('');
611+
});
612+
613+
$tokenProvider->rotate($actual, $oldToken, $newToken);
614+
$stored = false;
615+
616+
$this->assertSame($actual->getToken(), $tokenProvider->getToken($newToken)->getToken());
617+
618+
$this->expectException(InvalidTokenException::class);
619+
$tokenProvider->getToken($oldToken);
620+
}
621+
622+
public function testRotateStoredToken(): void {
623+
$oldToken = 'oldtokentokentokentokentoken';
624+
$newToken = 'newtokentokentokentokentoken';
625+
626+
// What the caller has, e.g. from the cache, is older than what is stored
627+
$outdated = $this->tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
628+
$stored = clone $outdated;
629+
$stored->setScope([IToken::SCOPE_FILESYSTEM => false]);
630+
631+
$this->mapper->expects($this->once())
632+
->method('getToken')
633+
->with($outdated->getToken())
634+
->willReturn($stored);
635+
$this->mapper->expects($this->once())
636+
->method('update')
637+
->with($stored);
638+
639+
$new = $this->tokenProvider->rotate($outdated, $oldToken, $newToken);
640+
641+
$this->assertSame($stored, $new);
642+
$this->assertSame([IToken::SCOPE_FILESYSTEM => false], $new->getScopeAsArray());
643+
}
644+
645+
public function testRotateDeletedToken(): void {
646+
$oldToken = 'oldtokentokentokentokentoken';
647+
$newToken = 'newtokentokentokentokentoken';
648+
649+
$actual = $this->tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
650+
651+
$this->mapper->expects($this->once())
652+
->method('getToken')
653+
->with($actual->getToken())
654+
->willThrowException(new DoesNotExistException(''));
655+
$this->mapper->expects($this->never())
656+
->method('update');
657+
658+
$this->expectException(InvalidTokenException::class);
659+
$this->tokenProvider->rotate($actual, $oldToken, $newToken);
660+
}
661+
662+
public function testRotateTokenMarkedForWipe(): void {
663+
$oldToken = 'oldtokentokentokentokentoken';
664+
$newToken = 'newtokentokentokentokentoken';
665+
666+
$actual = $this->tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
667+
668+
$stored = clone $actual;
669+
$stored->setType(IToken::WIPE_TOKEN);
670+
$this->mapper->expects($this->once())
671+
->method('getToken')
672+
->with($actual->getToken())
673+
->willReturn($stored);
674+
$this->mapper->expects($this->never())
675+
->method('update');
676+
677+
$this->expectException(WipeTokenException::class);
678+
$this->tokenProvider->rotate($actual, $oldToken, $newToken);
679+
}
680+
574681
public function testMarkPasswordInvalidInvalidToken(): void {
575682
$token = $this->createMock(IToken::class);
576683

0 commit comments

Comments
 (0)