From 748f004b676a7975d3a527b7433ddb57a1bddd5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Barou=C5=A1?= Date: Mon, 7 Aug 2023 18:38:51 +0200 Subject: [PATCH] Allow modifying scopes in AuthorizationRequestResolveEvent --- src/Controller/AuthorizationController.php | 9 +++++++++ src/Event/AuthorizationRequestResolveEvent.php | 10 ++++++++++ src/Resources/config/services.php | 1 + 3 files changed, 20 insertions(+) diff --git a/src/Controller/AuthorizationController.php b/src/Controller/AuthorizationController.php index 7b5215e4..bd7b4880 100644 --- a/src/Controller/AuthorizationController.php +++ b/src/Controller/AuthorizationController.php @@ -4,6 +4,7 @@ namespace League\Bundle\OAuth2ServerBundle\Controller; +use League\Bundle\OAuth2ServerBundle\Converter\ScopeConverterInterface; use League\Bundle\OAuth2ServerBundle\Converter\UserConverterInterface; use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEventFactory; use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface; @@ -40,6 +41,11 @@ final class AuthorizationController */ private $userConverter; + /** + * @var ScopeConverterInterface + */ + private $scopeConverter; + /** * @var ClientManagerInterface */ @@ -65,6 +71,7 @@ public function __construct( EventDispatcherInterface $eventDispatcher, AuthorizationRequestResolveEventFactory $eventFactory, UserConverterInterface $userConverter, + ScopeConverterInterface $scopeConverter, ClientManagerInterface $clientManager, HttpMessageFactoryInterface $httpMessageFactory, HttpFoundationFactoryInterface $httpFoundationFactory, @@ -74,6 +81,7 @@ public function __construct( $this->eventDispatcher = $eventDispatcher; $this->eventFactory = $eventFactory; $this->userConverter = $userConverter; + $this->scopeConverter = $scopeConverter; $this->clientManager = $clientManager; $this->httpMessageFactory = $httpMessageFactory; $this->httpFoundationFactory = $httpFoundationFactory; @@ -102,6 +110,7 @@ public function indexAction(Request $request): Response ); $authRequest->setUser($this->userConverter->toLeague($event->getUser())); + $authRequest->setScopes($this->scopeConverter->toLeagueArray($event->getScopes())); if ($response = $event->getResponse()) { return $response; diff --git a/src/Event/AuthorizationRequestResolveEvent.php b/src/Event/AuthorizationRequestResolveEvent.php index dd431f29..fb708326 100644 --- a/src/Event/AuthorizationRequestResolveEvent.php +++ b/src/Event/AuthorizationRequestResolveEvent.php @@ -122,6 +122,16 @@ public function getScopes(): array return $this->scopes; } + /** + * @param Scope[] $scopes + */ + public function setScopes(array $scopes): self + { + $this->scopes = $scopes; + + return $this; + } + public function isAuthorizationApproved(): bool { return $this->authorizationRequest->isAuthorizationApproved(); diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index 89810e0d..2b988537 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -196,6 +196,7 @@ service(EventDispatcherInterface::class), service(AuthorizationRequestResolveEventFactory::class), service(UserConverterInterface::class), + service(ScopeConverterInterface::class), service(ClientManagerInterface::class), service('league.oauth2_server.factory.psr_http'), service('league.oauth2_server.factory.http_foundation'),