vendor/trikoder/oauth2-bundle/Controller/AuthorizationController.php line 60

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Trikoder\Bundle\OAuth2Bundle\Controller;
  4. use League\OAuth2\Server\AuthorizationServer;
  5. use League\OAuth2\Server\Exception\OAuthServerException;
  6. use Psr\Http\Message\ResponseFactoryInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Psr\Http\Message\ServerRequestInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Trikoder\Bundle\OAuth2Bundle\Converter\UserConverterInterface;
  11. use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent;
  12. use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEventFactory;
  13. use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
  14. use Trikoder\Bundle\OAuth2Bundle\OAuth2Events;
  15. final class AuthorizationController
  16. {
  17.     /**
  18.      * @var AuthorizationServer
  19.      */
  20.     private $server;
  21.     /**
  22.      * @var EventDispatcherInterface
  23.      */
  24.     private $eventDispatcher;
  25.     /**
  26.      * @var AuthorizationRequestResolveEventFactory
  27.      */
  28.     private $eventFactory;
  29.     /**
  30.      * @var UserConverterInterface
  31.      */
  32.     private $userConverter;
  33.     /**
  34.      * @var ClientManagerInterface
  35.      */
  36.     private $clientManager;
  37.     public function __construct(
  38.         AuthorizationServer $server,
  39.         EventDispatcherInterface $eventDispatcher,
  40.         AuthorizationRequestResolveEventFactory $eventFactory,
  41.         UserConverterInterface $userConverter,
  42.         ClientManagerInterface $clientManager
  43.     ) {
  44.         $this->server $server;
  45.         $this->eventDispatcher $eventDispatcher;
  46.         $this->eventFactory $eventFactory;
  47.         $this->userConverter $userConverter;
  48.         $this->clientManager $clientManager;
  49.     }
  50.     public function indexAction(ServerRequestInterface $serverRequestResponseFactoryInterface $responseFactory): ResponseInterface
  51.     {
  52.         $serverResponse $responseFactory->createResponse();
  53.         try {
  54.             $authRequest $this->server->validateAuthorizationRequest($serverRequest);
  55.             if ('plain' === $authRequest->getCodeChallengeMethod()) {
  56.                 $client $this->clientManager->find($authRequest->getClient()->getIdentifier());
  57.                 if (!$client->isPlainTextPkceAllowed()) {
  58.                     return OAuthServerException::invalidRequest(
  59.                         'code_challenge_method',
  60.                         'Plain code challenge method is not allowed for this client'
  61.                     )->generateHttpResponse($serverResponse);
  62.                 }
  63.             }
  64.             /** @var AuthorizationRequestResolveEvent $event */
  65.             $event $this->eventDispatcher->dispatch(
  66.                 $this->eventFactory->fromAuthorizationRequest($authRequest),
  67.                 OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE
  68.             );
  69.             $authRequest->setUser($this->userConverter->toLeague($event->getUser()));
  70.             if ($event->hasResponse()) {
  71.                 return $event->getResponse();
  72.             }
  73.             $authRequest->setAuthorizationApproved($event->getAuthorizationResolution());
  74.             return $this->server->completeAuthorizationRequest($authRequest$serverResponse);
  75.         } catch (OAuthServerException $e) {
  76.             return $e->generateHttpResponse($serverResponse);
  77.         }
  78.     }
  79. }