1515use OCP \AppFramework \Db \DoesNotExistException ;
1616use OCP \AppFramework \Db \TTransactional ;
1717use OCP \AppFramework \Utility \ITimeFactory ;
18+ use OCP \Authentication \Events \TokenInvalidatedEvent ;
1819use OCP \Authentication \Token \IToken as OCPIToken ;
20+ use OCP \EventDispatcher \IEventDispatcher ;
1921use OCP \ICache ;
2022use OCP \ICacheFactory ;
2123use OCP \IConfig ;
@@ -55,14 +57,18 @@ class PublicKeyTokenProvider implements IProvider {
5557 /** @var IHasher */
5658 private $ hasher ;
5759
60+ private IEventDispatcher $ eventDispatcher ;
61+
5862 public function __construct (PublicKeyTokenMapper $ mapper ,
5963 ICrypto $ crypto ,
6064 IConfig $ config ,
6165 IDBConnection $ db ,
6266 LoggerInterface $ logger ,
6367 ITimeFactory $ time ,
6468 IHasher $ hasher ,
65- ICacheFactory $ cacheFactory ) {
69+ ICacheFactory $ cacheFactory ,
70+ IEventDispatcher $ eventDispatcher ,
71+ ) {
6672 $ this ->mapper = $ mapper ;
6773 $ this ->crypto = $ crypto ;
6874 $ this ->config = $ config ;
@@ -74,6 +80,7 @@ public function __construct(PublicKeyTokenMapper $mapper,
7480 ? $ cacheFactory ->createLocal ('authtoken_ ' )
7581 : $ cacheFactory ->createInMemory ();
7682 $ this ->hasher = $ hasher ;
83+ $ this ->eventDispatcher = $ eventDispatcher ;
7784 }
7885
7986 /**
@@ -263,9 +270,17 @@ public function renewSessionToken(string $oldSessionId, string $sessionId): OCPI
263270
264271 public function invalidateToken (string $ token ) {
265272 $ tokenHash = $ this ->hashToken ($ token );
273+ $ tokenEntry = null ;
274+ try {
275+ $ tokenEntry = $ this ->mapper ->getToken ($ tokenHash );
276+ } catch (DoesNotExistException ) {
277+ }
266278 $ this ->mapper ->invalidate ($ this ->hashToken ($ token ));
267279 $ this ->mapper ->invalidate ($ this ->hashTokenWithEmptySecret ($ token ));
268280 $ this ->cacheInvalidHash ($ tokenHash );
281+ if ($ tokenEntry !== null ) {
282+ $ this ->eventDispatcher ->dispatchTyped (new TokenInvalidatedEvent ($ tokenEntry ));
283+ }
269284 }
270285
271286 public function invalidateTokenById (string $ uid , int $ id ) {
@@ -275,7 +290,7 @@ public function invalidateTokenById(string $uid, int $id) {
275290 }
276291 $ this ->mapper ->invalidate ($ token ->getToken ());
277292 $ this ->cacheInvalidHash ($ token ->getToken ());
278-
293+ $ this -> eventDispatcher -> dispatchTyped ( new TokenInvalidatedEvent ( $ token ));
279294 }
280295
281296 public function invalidateOldTokens () {
0 commit comments