EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Validates scopes in authorization start
Yurii9618bf4aea9f
diff --git a/application/Espo/Tools/OAuthServer/AuthorizationService.php b/application/Espo/Tools/OAuthServer/AuthorizationService.phpindex 8fbc5679ff..88be6abd61 100644--- a/application/Espo/Tools/OAuthServer/AuthorizationService.php+++ b/application/Espo/Tools/OAuthServer/AuthorizationService.php@@ -39,10 +39,12 @@ use Espo\Tools\OAuthServer\League\AuthorizationRequestStorage;use Espo\Tools\OAuthServer\League\AuthorizationServerFactory;use Espo\Tools\OAuthServer\League\Entities\ScopeEntity;use Espo\Tools\OAuthServer\League\Entities\UserEntity;+use Espo\Tools\OAuthServer\Repository\ClientRepository as ClientRepositoryInternal;use Espo\Tools\OAuthServer\Scope\ScopeSorter;use Espo\Tools\OAuthServer\Utils\UriUtil;use League\OAuth2\Server\Exception\OAuthServerException;use League\OAuth2\Server\RequestTypes\AuthorizationRequest;+use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;use Psr\Http\Message\ResponseInterface;use Psr\Http\Message\ServerRequestInterface;@@ -53,6 +55,7 @@ class AuthorizationServiceprivate ApplicationState $applicationState,private AuthorizationRequestStorage $authorizationRequestStorage,private ScopeSorter $scopeSorter,+ private ClientRepositoryInternal $clientRepository,) {}/**@@ -64,12 +67,14 @@ class AuthorizationServicetry {$authRequest = $server->validateAuthorizationRequest($request);++ $clientId = $authRequest->getClient()->getIdentifier();++ $this->validateScopes($clientId, $request, $authRequest);} catch (OAuthServerException $e) {return $e->generateHttpResponse($response);}- $clientId = $authRequest->getClient()->getIdentifier();-$this->authorizationRequestStorage->store($clientId, $authRequest);return null;@@ -182,4 +187,26 @@ class AuthorizationServicethrow new Forbidden("Scope mismatch.");}}++ /**+ * @throws OAuthServerException+ */+ private function validateScopes(+ string $clientId,+ ServerRequestInterface $request,+ AuthorizationRequestInterface $authRequest,+ ): void {++ $client = $this->clientRepository->getActiveByIdentifier($clientId);++ if (!$client) {+ throw OAuthServerException::invalidClient($request);+ }++ foreach ($authRequest->getScopes() as $scope) {+ if (!in_array($scope->getIdentifier(), $client->getScopes())) {+ throw OAuthServerException::invalidScope($scope->getIdentifier());+ }+ }+ }}diff --git a/tests/integration/Espo/Tools/OAuthServer/AuthorizationServerTest.php b/tests/integration/Espo/Tools/OAuthServer/AuthorizationServerTest.phpindex c4260d0f9d..87518f5279 100644--- a/tests/integration/Espo/Tools/OAuthServer/AuthorizationServerTest.php+++ b/tests/integration/Espo/Tools/OAuthServer/AuthorizationServerTest.php@@ -196,6 +196,28 @@ class AuthorizationServerTest extends BaseTestCase$this->authorizeCompleteScopeMismatch($client);}+ public function testStartExtraScopesNotAllowed(): void+ {+ $redirectUri = self::REDIRECT_URI;++ $client = $this->createClient();++ $user = $this->createUserForTest();++ //++ $codeChallenge = PkceUtil::generateCodeVerifier();++ $this->obtainCodeInitial(+ client: $client,+ redirectUri: $redirectUri,+ codeChallenge: $codeChallenge,+ user: $user,+ scopes: [...$this->getScopes(), Contact::ENTITY_TYPE],+ expectedErrorCode: 400,+ );+ }+/*** @noinspection PhpUnhandledExceptionInspection*/@@ -1413,6 +1435,7 @@ class AuthorizationServerTest extends BaseTestCase}/**+ * @param ?string[] $scopes* @noinspection PhpUnhandledExceptionInspection*/private function obtainCodeInitial(@@ -1420,6 +1443,8 @@ class AuthorizationServerTest extends BaseTestCasestring $redirectUri,string $codeChallenge,User $user,+ ?array $scopes = null,+ ?int $expectedErrorCode = null,): void {$session = $this->createMock(Session::class);@@ -1434,13 +1459,19 @@ class AuthorizationServerTest extends BaseTestCase//+ if ($scopes !== null) {+ $scopesString = implode(' ', $scopes);+ } else {+ $scopesString = $this->getScopesString();+ }+$request = $this->createRequest(method: Method::GET,queryParams: ['client_id' => $client->getIdentifier(),'redirect_uri' => $redirectUri,'response_type' => 'code',- 'scope' => $this->getScopesString(),+ 'scope' => $scopesString,'code_challenge' => PkceUtil::hashAndEncodeCodeVerifier($codeChallenge),'code_challenge_method' => 'S256',],@@ -1472,6 +1503,12 @@ class AuthorizationServerTest extends BaseTestCase$authorizeEntryPoint->run($request, $response);+ if ($expectedErrorCode !== null) {+ $this->assertEquals($expectedErrorCode, $response->getStatusCode());++ return;+ }+//$session