Skip to content

Commit f0d1822

Browse files
committed
public interface to invalidate tokens of user
Signed-off-by: Artur Neumann <artur@jankaritech.com>
1 parent 91b599f commit f0d1822

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

apps/oauth2/lib/Controller/SettingsController.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
namespace OCA\OAuth2\Controller;
3232

33-
use OC\Authentication\Token\IProvider as IAuthTokenProvider;
33+
use OCP\Authentication\Token\IProvider as IAuthTokenProvider;
3434
use OCA\OAuth2\Db\AccessTokenMapper;
3535
use OCA\OAuth2\Db\Client;
3636
use OCA\OAuth2\Db\ClientMapper;
@@ -115,14 +115,7 @@ public function deleteClient(int $id): JSONResponse {
115115
$client = $this->clientMapper->getByUid($id);
116116

117117
$this->userManager->callForAllUsers(function (IUser $user) use ($client) {
118-
$tokens = $this->tokenProvider->getTokenByUser($user->getUID());
119-
foreach ($tokens as $token) {
120-
if ($token->getName() === $client->getName()) {
121-
$this->tokenProvider->invalidateTokenById(
122-
$user->getUID(), $token->getId()
123-
);
124-
}
125-
}
118+
$this->tokenProvider->invalidateTokensOfUser($user->getUID(), $client->getName());
126119
});
127120

128121
$this->accessTokenMapper->deleteByClientId($id);

lib/private/Authentication/Token/Manager.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
use OC\Authentication\Exceptions\InvalidTokenException;
3333
use OC\Authentication\Exceptions\PasswordlessTokenException;
3434
use OC\Authentication\Exceptions\WipeTokenException;
35+
use OCP\Authentication\Token\IProvider as OCPIProvider;
3536

36-
class Manager implements IProvider {
37-
37+
class Manager implements IProvider, OCPIProvider {
3838
/** @var PublicKeyTokenProvider */
3939
private $publicKeyTokenProvider;
4040

@@ -240,4 +240,13 @@ public function markPasswordInvalid(IToken $token, string $tokenId) {
240240
public function updatePasswords(string $uid, string $password) {
241241
$this->publicKeyTokenProvider->updatePasswords($uid, $password);
242242
}
243+
244+
public function invalidateTokensOfUser(string $uid, ?string $clientName) {
245+
$tokens = $this->getTokenByUser($uid);
246+
foreach ($tokens as $token) {
247+
if ($clientName === null || ($token->getName() === $clientName)) {
248+
$this->invalidateTokenById($uid, $token->getId());
249+
}
250+
}
251+
}
243252
}

lib/private/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
use OCP\Accounts\IAccountManager;
162162
use OCP\App\IAppManager;
163163
use OCP\Authentication\LoginCredentials\IStore;
164+
use OCP\Authentication\Token\IProvider as OCPIProvider;
164165
use OCP\BackgroundJob\IJobList;
165166
use OCP\Collaboration\AutoComplete\IManager;
166167
use OCP\Collaboration\Reference\IReferenceManager;
@@ -278,7 +279,6 @@
278279
* TODO: hookup all manager classes
279280
*/
280281
class Server extends ServerContainer implements IServerContainer {
281-
282282
/** @var string */
283283
private $webRoot;
284284

@@ -547,6 +547,7 @@ public function __construct($webRoot, \OC\Config $config) {
547547
});
548548
$this->registerAlias(IStore::class, Store::class);
549549
$this->registerAlias(IProvider::class, Authentication\Token\Manager::class);
550+
$this->registerAlias(OCPIProvider::class, Authentication\Token\Manager::class);
550551

551552
$this->registerService(\OC\User\Session::class, function (Server $c) {
552553
$manager = $c->get(IUserManager::class);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Artur Neumann <artur@jankaritech.com>
7+
*
8+
* @author Artur Neumann <artur@jankaritech.com>
9+
*
10+
* @license AGPL-3.0
11+
*
12+
* This code is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License, version 3,
14+
* as published by the Free Software Foundation.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License, version 3,
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>
23+
*
24+
*/
25+
namespace OCP\Authentication\Token;
26+
27+
interface IProvider {
28+
/**
29+
* invalidates all tokens of a specific user
30+
* if a client name is given only tokens of that client will be invalidated
31+
*
32+
* @param string $uid
33+
* @param string|null $clientName
34+
* @return void
35+
*/
36+
public function invalidateTokensOfUser(string $uid, ?string $clientName);
37+
}

0 commit comments

Comments
 (0)