1818use OCP \AppFramework \Http ;
1919use OCP \AppFramework \Utility \ITimeFactory ;
2020use OCP \Authentication \Exceptions \InvalidTokenException ;
21+ use OCP \Authentication \Token \IToken ;
2122use OCP \DB \QueryBuilder \IQueryBuilder ;
2223use OCP \Http \Client \IClientService ;
2324use OCP \ICache ;
@@ -537,10 +538,23 @@ protected function validateToken(int $tokenId, int $maxAge): bool {
537538 try {
538539 // Check if the token is still valid...
539540 $ token = $ this ->tokenProvider ->getTokenById ($ tokenId );
540- $ this ->cache ->set ('t ' . $ tokenId , $ token ->getLastCheck (), 600 );
541+ $ type = $ this ->callSafelyForToken ($ token , 'getType ' );
542+ if ($ type === IToken::WIPE_TOKEN ) {
543+ // Token does not exist any more, should drop the push device entry
544+ $ this ->printInfo ('Device token is marked for remote wipe ' );
545+ $ this ->deletePushToken ($ tokenId );
546+ $ this ->cache ->set ('t ' . $ tokenId , 0 , 600 );
547+ return false ;
548+ }
549+
541550 $ age = $ token ->getLastCheck ();
551+ $ lastActivity = $ this ->callSafelyForToken ($ token , 'getLastActivity ' );
552+ if ($ lastActivity ) {
553+ $ age = max ($ age , $ lastActivity );
554+ }
555+ $ this ->cache ->set ('t ' . $ tokenId , $ age , 600 );
542556 } catch (InvalidTokenException ) {
543- // Token does not exist anymore , should drop the push device entry
557+ // Token does not exist any more , should drop the push device entry
544558 $ this ->printInfo ('InvalidTokenException is thrown ' );
545559 $ this ->deletePushToken ($ tokenId );
546560 $ this ->cache ->set ('t ' . $ tokenId , 0 , 600 );
@@ -557,6 +571,25 @@ protected function validateToken(int $tokenId, int $maxAge): bool {
557571 return false ;
558572 }
559573
574+ /**
575+ * The functions are not part of public API so we are a bit more careful
576+ * @param IToken $token
577+ * @param 'getLastActivity'|'getType' $method
578+ * @return int|null
579+ */
580+ protected function callSafelyForToken (IToken $ token , string $ method ): ?int {
581+ if (method_exists ($ token , $ method ) || method_exists ($ token , '__call ' )) {
582+ try {
583+ $ result = $ token ->$ method ();
584+ if (is_int ($ result )) {
585+ return $ result ;
586+ }
587+ } catch (\BadFunctionCallException ) {
588+ }
589+ }
590+ return null ;
591+ }
592+
560593 /**
561594 * @param Key $userKey
562595 * @param array $device
0 commit comments