1212use OC \Authentication \Exceptions \ExpiredTokenException ;
1313use OC \Authentication \Exceptions \InvalidTokenException ;
1414use OC \Authentication \Exceptions \PasswordlessTokenException ;
15+ use OC \Authentication \Exceptions \WipeTokenException ;
1516use OC \Authentication \Token \PublicKeyToken ;
1617use OC \Authentication \Token \PublicKeyTokenMapper ;
1718use OC \Authentication \Token \PublicKeyTokenProvider ;
19+ use OC \Memcache \ArrayCache ;
1820use OCP \AppFramework \Db \DoesNotExistException ;
1921use OCP \AppFramework \Utility \ITimeFactory ;
2022use 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