EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Inactivate tokens
Yurii8b5cdd8e175b
diff --git a/application/Espo/Core/Authentication/AuthToken/EspoManager.php b/application/Espo/Core/Authentication/AuthToken/EspoManager.phpindex 6736019624..0db3d84cb4 100644--- a/application/Espo/Core/Authentication/AuthToken/EspoManager.php+++ b/application/Espo/Core/Authentication/AuthToken/EspoManager.php@@ -127,13 +127,29 @@ class EspoManager implements Manager$this->repository->save($authToken);}- public function inactiveOther(AuthToken $authToken): void+ public function inactivateOther(AuthToken $authToken): void{foreach ($this->findConcurrent($authToken) as $other) {$this->inactivate($other);}}+ public function inactivateAllForUser(string $userId): void+ {+ $tokens = $this->repository+ ->select([Attribute::ID])+ ->where([+ AuthTokenEntity::ATTR_USER_ID => $userId,+ AuthTokenEntity::FIELD_IS_ACTIVE => true,+ ])+ ->sth()+ ->find();++ foreach ($tokens as $token) {+ $this->inactivate($token);+ }+ }+private function validate(AuthToken $authToken): void{if (!$authToken->getToken()) {diff --git a/application/Espo/Core/Authentication/AuthToken/Manager.php b/application/Espo/Core/Authentication/AuthToken/Manager.phpindex 74c99b1d6f..7c3b85374e 100644--- a/application/Espo/Core/Authentication/AuthToken/Manager.php+++ b/application/Espo/Core/Authentication/AuthToken/Manager.php@@ -29,6 +29,8 @@namespace Espo\Core\Authentication\AuthToken;+use Espo\Entities\User;+/*** Fetches and stores auth tokens.*@@ -70,5 +72,12 @@ interface Manager* @param TAuthToken $authToken* @since 10.1.0*/- public function inactiveOther(AuthToken $authToken): void;+ public function inactivateOther(AuthToken $authToken): void;++ /**+ * Inactivate all auth tokens for a user.+ *+ * @since 10.1.0+ */+ public function inactivateAllForUser(string $userId): void;}diff --git a/application/Espo/Core/Authentication/Authentication.php b/application/Espo/Core/Authentication/Authentication.phpindex 978a714ccb..28fd9d31c9 100644--- a/application/Espo/Core/Authentication/Authentication.php+++ b/application/Espo/Core/Authentication/Authentication.php@@ -408,7 +408,7 @@ class Authentication}if ($this->configDataProvider->preventConcurrentAuthToken()) {- $this->authTokenManager->inactiveOther($authToken);+ $this->authTokenManager->inactivateOther($authToken);}return $authToken;diff --git a/application/Espo/Resources/i18n/en_US/User.json b/application/Espo/Resources/i18n/en_US/User.jsonindex b649b2d38f..3086447bfb 100644--- a/application/Espo/Resources/i18n/en_US/User.json+++ b/application/Espo/Resources/i18n/en_US/User.json@@ -171,5 +171,8 @@},"actions": {"changePosition": "Change Position"+ },+ "strings": {+ "revokeAllAuthenticationTokens": "Revoke all authentication tokens"}}diff --git a/application/Espo/Resources/routes.json b/application/Espo/Resources/routes.jsonindex 66d05cfd74..55764ec6a5 100644--- a/application/Espo/Resources/routes.json+++ b/application/Espo/Resources/routes.json@@ -476,6 +476,12 @@"actionClassName": "Espo\\Tools\\UserSecurity\\Api\\DeleteConnectedApp","apiAuthForbidden": true},+ {+ "route": "/User/:id/revokeAllAuthenticationTokens",+ "method": "post",+ "actionClassName": "Espo\\Tools\\UserSecurity\\Api\\PostRevokeAllAuthenticationTokens",+ "apiAuthForbidden": true+ },{"route": "/UserSecurity/apiKey/generate","method": "post",diff --git a/application/Espo/Tools/UserSecurity/Api/PostRevokeAllAuthenticationTokens.php b/application/Espo/Tools/UserSecurity/Api/PostRevokeAllAuthenticationTokens.phpnew file mode 100644index 0000000000..aa740a2825--- /dev/null+++ b/application/Espo/Tools/UserSecurity/Api/PostRevokeAllAuthenticationTokens.php@@ -0,0 +1,77 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * 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+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Tools\UserSecurity\Api;++use Espo\Core\Api\Action;+use Espo\Core\Api\Request;+use Espo\Core\Api\Response;+use Espo\Core\Api\ResponseComposer;+use Espo\Core\Exceptions\BadRequest;+use Espo\Core\Exceptions\Forbidden;+use Espo\Core\Exceptions\NotFound;+use Espo\Core\Record\EntityProvider;+use Espo\Entities\User;++/**+ * @noinspection PhpUnused+ */+class PostRevokeAllAuthenticationTokens implements Action+{+ public function __construct(+ private EntityProvider $entityProvider,+ private User $user,+ private RevokeAuthenticationTokensService $service,+ ) {}++ public function process(Request $request): Response+ {+ $user = $this->fetchUser($request);++ $this->service->revokeAll($user);++ return ResponseComposer::json(true);+ }++ /**+ * @throws BadRequest+ * @throws Forbidden+ * @throws NotFound+ */+ private function fetchUser(Request $request): User+ {+ $id = $request->getRouteParam('id') ?? throw new BadRequest();++ if ($id !== $this->user->getId() && !$this->user->isEffectiveAdmin()) {+ throw new Forbidden();+ }++ return $this->entityProvider->getByClass(User::class, $id);+ }+}diff --git a/application/Espo/Tools/UserSecurity/Api/RevokeAuthenticationTokensService.php b/application/Espo/Tools/UserSecurity/Api/RevokeAuthenticationTokensService.phpnew file mode 100644index 0000000000..479c949132--- /dev/null+++ b/application/Espo/Tools/UserSecurity/Api/RevokeAuthenticationTokensService.php@@ -0,0 +1,52 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * 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+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Tools\UserSecurity\Api;++use Espo\Core\Authentication\AuthToken\Manager;+use Espo\Entities\User;+use Espo\Tools\OAuthServer\Record\RevokeService;++/**+ * @internal+ */+class RevokeAuthenticationTokensService+{+ public function __construct(+ private Manager $authTokenManager,+ private RevokeService $oAuthRevokeService,+ ) {}++ public function revokeAll(User $user): void+ {+ $this->authTokenManager->inactivateAllForUser($user->getId());++ $this->oAuthRevokeService->revoke(new RevokeService\Params(userName: $user->getUserName()));+ }+}diff --git a/client/src/views/user/modals/security.js b/client/src/views/user/modals/security.jsindex 06ea0b993d..b4acf2c490 100644--- a/client/src/views/user/modals/security.js+++ b/client/src/views/user/modals/security.js@@ -1,3 +1,4 @@+/************************************************************************/************************************************************************* This file is part of EspoCRM.*@@ -29,6 +30,8 @@import ModalView from 'views/modal';import Model from 'model';import Utils from 'utils';+import Ajax from 'ajax';+import Ui from 'ui';class UserSecurityModalView extends ModalView {@@ -85,6 +88,13 @@ class UserSecurityModalView extends ModalView {},];+ this.addButton({+ name: 'revokeAllTokens',+ labelTranslation: 'User.strings.revokeAllAuthenticationTokens',+ position: 'right',+ onClick: () => this.processRevokeAllTokens(),+ });+this.userModel = this.options.userModel;this.headerHtml =@@ -313,6 +323,27 @@ class UserSecurityModalView extends ModalView {}).catch(() => this.showActionItem('apply'));}++ /**+ * @private+ */+ async processRevokeAllTokens() {+ await this.confirm({+ message: this.translate('confirmation', 'messages'),+ });++ await this.disableButton('revokeAllTokens');++ try {+ await Ajax.postRequest(`User/${this.userModel.id}/revokeAllAuthenticationTokens`);+ } catch (e) {+ await this.enableButton('revokeAllTokens');++ return;+ }++ Ui.success(this.translate('Done'));+ }}export default UserSecurityModalView;