Skip to content

EspoCRM – Open Source CRM Application

Oauth check user non API

Yurii404cb63f3eeb
diff --git a/application/Espo/Tools/OAuthServer/AuthorizationService.php b/application/Espo/Tools/OAuthServer/AuthorizationService.php
index 0972f457f0..0b28a9bb24 100644
--- a/application/Espo/Tools/OAuthServer/AuthorizationService.php
+++ b/application/Espo/Tools/OAuthServer/AuthorizationService.php
@@ -56,6 +56,7 @@ class AuthorizationService
private AuthorizationRequestStorage $authorizationRequestStorage,
private ScopeSorter $scopeSorter,
private ClientRepositoryInternal $clientRepository,
+ private UserValidation $userValidation,
) {}
/**
@@ -157,13 +158,17 @@ class AuthorizationService
*/
private function assertUser(User $user, AuthorizationRequest $authorizationRequest): void
{
+ try {
+ $this->userValidation->assertValid($user);
+ } catch (Forbidden) {
+ $this->throwUserIsNotAllowed($authorizationRequest);
+ }
+
if ($user->isRegular() || $user->getType() === User::TYPE_ADMIN) {
return;
}
- $redirectUri = $this->prepareRedirectUri($authorizationRequest);
-
- throw OAuthServerException::accessDenied("User is not allowed.", $redirectUri);
+ $this->throwUserIsNotAllowed($authorizationRequest);
}
/**
@@ -216,4 +221,14 @@ class AuthorizationService
return $redirectUri;
}
+
+ /**
+ * @throws OAuthServerException
+ */
+ private function throwUserIsNotAllowed(AuthorizationRequest $authorizationRequest): void
+ {
+ $redirectUri = $this->prepareRedirectUri($authorizationRequest);
+
+ throw OAuthServerException::accessDenied("User is not allowed.", $redirectUri);
+ }
}
diff --git a/application/Espo/Tools/OAuthServer/ConsentDataService.php b/application/Espo/Tools/OAuthServer/ConsentDataService.php
index ae5165c6a5..c54cb957e7 100644
--- a/application/Espo/Tools/OAuthServer/ConsentDataService.php
+++ b/application/Espo/Tools/OAuthServer/ConsentDataService.php
@@ -31,6 +31,7 @@ namespace Espo\Tools\OAuthServer;
use Espo\Core\Acl\Scope;
use Espo\Core\Exceptions\Error;
+use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Utils\Config\ApplicationConfig;
use Espo\Core\Utils\Language;
@@ -53,14 +54,18 @@ class ConsentDataService
private AuthorizationRequestStorage $authorizationRequestStorage,
private UserAvailableScopesFilter $userAvailableScopesFilter,
private ScopeSorter $scopeSorter,
+ private UserValidation $userValidation,
) {}
/**
* @throws NotFound
* @throws Error
+ * @throws Forbidden
*/
public function getData(string $clientId): stdClass
{
+ $this->userValidation->assertValid($this->user);
+
$client = $this->getClient($clientId);
return (object) [
diff --git a/application/Espo/Tools/OAuthServer/UserValidation.php b/application/Espo/Tools/OAuthServer/UserValidation.php
new file mode 100644
index 0000000000..c704ac8a2d
--- /dev/null
+++ b/application/Espo/Tools/OAuthServer/UserValidation.php
@@ -0,0 +1,53 @@
+<?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\OAuthServer;
+
+use Espo\Core\Exceptions\Forbidden;
+use Espo\Entities\User;
+
+/**
+ * @internal
+ */
+class UserValidation
+{
+ /**
+ * Allowing only users logged in with basic authorization,
+ * preventing issuing a new refresh token by having a stolen
+ * auth token.
+ *
+ * @throws Forbidden
+ */
+ public function assertValid(User $user): void
+ {
+ if ($user->getScopes() !== null || $user->isApi()) {
+ throw new Forbidden();
+ }
+ }
+}