]> source.dussan.org Git - nextcloud-server.git/commitdiff
public interface to invalidate tokens of user
authorArtur Neumann <artur@jankaritech.com>
Mon, 21 Nov 2022 11:43:21 +0000 (17:28 +0545)
committerCôme Chilliet <come.chilliet@nextcloud.com>
Tue, 14 Mar 2023 16:13:29 +0000 (17:13 +0100)
Signed-off-by: Artur Neumann <artur@jankaritech.com>
apps/oauth2/lib/Controller/SettingsController.php
lib/private/Authentication/Token/Manager.php
lib/private/Server.php
lib/public/Authentication/Token/IProvider.php [new file with mode: 0644]

index 872288064ddf2842e8f198e514e627b7f9317fe3..c24308140ecbb81a1bc58878039a7ee0d3d187b5 100644 (file)
@@ -30,7 +30,7 @@ declare(strict_types=1);
  */
 namespace OCA\OAuth2\Controller;
 
-use OC\Authentication\Token\IProvider as IAuthTokenProvider;
+use OCP\Authentication\Token\IProvider as IAuthTokenProvider;
 use OCA\OAuth2\Db\AccessTokenMapper;
 use OCA\OAuth2\Db\Client;
 use OCA\OAuth2\Db\ClientMapper;
@@ -106,14 +106,7 @@ class SettingsController extends Controller {
                $client = $this->clientMapper->getByUid($id);
 
                $this->userManager->callForAllUsers(function (IUser $user) use ($client) {
-                       $tokens = $this->tokenProvider->getTokenByUser($user->getUID());
-                       foreach ($tokens as $token) {
-                               if ($token->getName() === $client->getName()) {
-                                       $this->tokenProvider->invalidateTokenById(
-                                               $user->getUID(), $token->getId()
-                                       );
-                               }
-                       }
+                       $this->tokenProvider->invalidateTokensOfUser($user->getUID(), $client->getName());
                });
 
                $this->accessTokenMapper->deleteByClientId($id);
index 59c7ca714c6329f8e844d21125444906ee833413..761e799d298351158efe71fdbf771554260d1b5e 100644 (file)
@@ -32,8 +32,9 @@ use OC\Authentication\Exceptions\ExpiredTokenException;
 use OC\Authentication\Exceptions\InvalidTokenException;
 use OC\Authentication\Exceptions\PasswordlessTokenException;
 use OC\Authentication\Exceptions\WipeTokenException;
+use OCP\Authentication\Token\IProvider as OCPIProvider;
 
-class Manager implements IProvider {
+class Manager implements IProvider, OCPIProvider {
        /** @var PublicKeyTokenProvider */
        private $publicKeyTokenProvider;
 
@@ -239,4 +240,13 @@ class Manager implements IProvider {
        public function updatePasswords(string $uid, string $password) {
                $this->publicKeyTokenProvider->updatePasswords($uid, $password);
        }
+
+       public function invalidateTokensOfUser(string $uid, ?string $clientName) {
+               $tokens = $this->getTokenByUser($uid);
+               foreach ($tokens as $token) {
+                       if ($clientName === null || ($token->getName() === $clientName)) {
+                               $this->invalidateTokenById($uid, $token->getId());
+                       }
+               }
+       }
 }
index 9a4ee0da198823ca21e0d8b8862ce560fefb3a33..f1e96170886627063472ad6bca786276d9612c16 100644 (file)
@@ -163,6 +163,7 @@ use OCA\Theming\Util;
 use OCP\Accounts\IAccountManager;
 use OCP\App\IAppManager;
 use OCP\Authentication\LoginCredentials\IStore;
+use OCP\Authentication\Token\IProvider as OCPIProvider;
 use OCP\BackgroundJob\IJobList;
 use OCP\Collaboration\AutoComplete\IManager;
 use OCP\Collaboration\Reference\IReferenceManager;
@@ -550,6 +551,7 @@ class Server extends ServerContainer implements IServerContainer {
                });
                $this->registerAlias(IStore::class, Store::class);
                $this->registerAlias(IProvider::class, Authentication\Token\Manager::class);
+               $this->registerAlias(OCPIProvider::class, Authentication\Token\Manager::class);
 
                $this->registerService(\OC\User\Session::class, function (Server $c) {
                        $manager = $c->get(IUserManager::class);
diff --git a/lib/public/Authentication/Token/IProvider.php b/lib/public/Authentication/Token/IProvider.php
new file mode 100644 (file)
index 0000000..9000868
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022 Artur Neumann <artur@jankaritech.com>
+ *
+ * @author Artur Neumann <artur@jankaritech.com>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+namespace OCP\Authentication\Token;
+
+interface IProvider {
+       /**
+        * invalidates all tokens of a specific user
+        * if a client name is given only tokens of that client will be invalidated
+        *
+        * @param string $uid
+        * @param string|null $clientName
+        * @return void
+        */
+       public function invalidateTokensOfUser(string $uid, ?string $clientName);
+}