Skip to content

Commit 8ffd30b

Browse files
committed
feat(auth): dispatch new TokenInvalidatedEvent when PublicKeyTokenProvider::invalidateTokenById is called
Signed-off-by: Julien Veyssier <[email protected]>
1 parent d5417d6 commit 8ffd30b

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
'OCP\\App\\ManagerEvent' => $baseDir . '/lib/public/App/ManagerEvent.php',
158158
'OCP\\Authentication\\Events\\AnyLoginFailedEvent' => $baseDir . '/lib/public/Authentication/Events/AnyLoginFailedEvent.php',
159159
'OCP\\Authentication\\Events\\LoginFailedEvent' => $baseDir . '/lib/public/Authentication/Events/LoginFailedEvent.php',
160+
'OCP\\Authentication\\Events\\TokenInvalidatedEvent' => $baseDir . '/lib/public/Authentication/Events/TokenInvalidatedEvent.php',
160161
'OCP\\Authentication\\Exceptions\\CredentialsUnavailableException' => $baseDir . '/lib/public/Authentication/Exceptions/CredentialsUnavailableException.php',
161162
'OCP\\Authentication\\Exceptions\\ExpiredTokenException' => $baseDir . '/lib/public/Authentication/Exceptions/ExpiredTokenException.php',
162163
'OCP\\Authentication\\Exceptions\\InvalidTokenException' => $baseDir . '/lib/public/Authentication/Exceptions/InvalidTokenException.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
198198
'OCP\\App\\ManagerEvent' => __DIR__ . '/../../..' . '/lib/public/App/ManagerEvent.php',
199199
'OCP\\Authentication\\Events\\AnyLoginFailedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/AnyLoginFailedEvent.php',
200200
'OCP\\Authentication\\Events\\LoginFailedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/LoginFailedEvent.php',
201+
'OCP\\Authentication\\Events\\TokenInvalidatedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/TokenInvalidatedEvent.php',
201202
'OCP\\Authentication\\Exceptions\\CredentialsUnavailableException' => __DIR__ . '/../../..' . '/lib/public/Authentication/Exceptions/CredentialsUnavailableException.php',
202203
'OCP\\Authentication\\Exceptions\\ExpiredTokenException' => __DIR__ . '/../../..' . '/lib/public/Authentication/Exceptions/ExpiredTokenException.php',
203204
'OCP\\Authentication\\Exceptions\\InvalidTokenException' => __DIR__ . '/../../..' . '/lib/public/Authentication/Exceptions/InvalidTokenException.php',

lib/private/Authentication/Token/PublicKeyTokenProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use OCP\AppFramework\Db\DoesNotExistException;
1616
use OCP\AppFramework\Db\TTransactional;
1717
use OCP\AppFramework\Utility\ITimeFactory;
18+
use OCP\Authentication\Events\TokenInvalidatedEvent;
1819
use OCP\Authentication\Token\IToken as OCPIToken;
20+
use OCP\EventDispatcher\IEventDispatcher;
1921
use OCP\ICache;
2022
use OCP\ICacheFactory;
2123
use 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
/**
@@ -275,7 +282,7 @@ public function invalidateTokenById(string $uid, int $id) {
275282
}
276283
$this->mapper->invalidate($token->getToken());
277284
$this->cacheInvalidHash($token->getToken());
278-
285+
$this->eventDispatcher->dispatchTyped(new TokenInvalidatedEvent($uid, $id));
279286
}
280287

281288
public function invalidateOldTokens() {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCP\Authentication\Events;
10+
11+
use OCP\EventDispatcher\Event;
12+
13+
/**
14+
* Emitted when an authentication token is invalidated
15+
*
16+
* @since 32.0.0
17+
*/
18+
class TokenInvalidatedEvent extends Event {
19+
20+
/**
21+
* @since 32.0.0
22+
*/
23+
public function __construct(
24+
private string $userId,
25+
private int $tokenId,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
/**
31+
* returns the uid of the user associated with the invalidated token
32+
*
33+
* @since 32.0.0
34+
*/
35+
public function getUserId(): string {
36+
return $this->userId;
37+
}
38+
39+
/**
40+
* returns the ID of the token that is being invalidated
41+
*
42+
* @since 32.0.0
43+
*/
44+
public function getTokenId(): int {
45+
return $this->tokenId;
46+
}
47+
}

0 commit comments

Comments
 (0)