vendor/league/oauth2-server-bundle/src/Controller/AuthorizationController.php line 89

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