vendor/league/oauth2-server/src/AuthorizationServer.php line 189

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author      Alex Bilbie <hello@alexbilbie.com>
  4.  * @copyright   Copyright (c) Alex Bilbie
  5.  * @license     http://mit-license.org/
  6.  *
  7.  * @link        https://github.com/thephpleague/oauth2-server
  8.  */
  9. namespace League\OAuth2\Server;
  10. use DateInterval;
  11. use Defuse\Crypto\Key;
  12. use League\Event\EmitterAwareInterface;
  13. use League\Event\EmitterAwareTrait;
  14. use League\OAuth2\Server\Exception\OAuthServerException;
  15. use League\OAuth2\Server\Grant\GrantTypeInterface;
  16. use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
  17. use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
  18. use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
  19. use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
  20. use League\OAuth2\Server\ResponseTypes\AbstractResponseType;
  21. use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
  22. use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
  23. use Psr\Http\Message\ResponseInterface;
  24. use Psr\Http\Message\ServerRequestInterface;
  25. class AuthorizationServer implements EmitterAwareInterface
  26. {
  27.     use EmitterAwareTrait;
  28.     /**
  29.      * @var GrantTypeInterface[]
  30.      */
  31.     protected $enabledGrantTypes = [];
  32.     /**
  33.      * @var DateInterval[]
  34.      */
  35.     protected $grantTypeAccessTokenTTL = [];
  36.     /**
  37.      * @var CryptKey
  38.      */
  39.     protected $privateKey;
  40.     /**
  41.      * @var CryptKey
  42.      */
  43.     protected $publicKey;
  44.     /**
  45.      * @var ResponseTypeInterface
  46.      */
  47.     protected $responseType;
  48.     /**
  49.      * @var ClientRepositoryInterface
  50.      */
  51.     private $clientRepository;
  52.     /**
  53.      * @var AccessTokenRepositoryInterface
  54.      */
  55.     private $accessTokenRepository;
  56.     /**
  57.      * @var ScopeRepositoryInterface
  58.      */
  59.     private $scopeRepository;
  60.     /**
  61.      * @var string|Key
  62.      */
  63.     private $encryptionKey;
  64.     /**
  65.      * @var string
  66.      */
  67.     private $defaultScope '';
  68.     /**
  69.      * New server instance.
  70.      *
  71.      * @param ClientRepositoryInterface      $clientRepository
  72.      * @param AccessTokenRepositoryInterface $accessTokenRepository
  73.      * @param ScopeRepositoryInterface       $scopeRepository
  74.      * @param CryptKey|string                $privateKey
  75.      * @param string|Key                     $encryptionKey
  76.      * @param null|ResponseTypeInterface     $responseType
  77.      */
  78.     public function __construct(
  79.         ClientRepositoryInterface $clientRepository,
  80.         AccessTokenRepositoryInterface $accessTokenRepository,
  81.         ScopeRepositoryInterface $scopeRepository,
  82.         $privateKey,
  83.         $encryptionKey,
  84.         ResponseTypeInterface $responseType null
  85.     ) {
  86.         $this->clientRepository $clientRepository;
  87.         $this->accessTokenRepository $accessTokenRepository;
  88.         $this->scopeRepository $scopeRepository;
  89.         if ($privateKey instanceof CryptKey === false) {
  90.             $privateKey = new CryptKey($privateKey);
  91.         }
  92.         $this->privateKey $privateKey;
  93.         $this->encryptionKey $encryptionKey;
  94.         if ($responseType === null) {
  95.             $responseType = new BearerTokenResponse();
  96.         } else {
  97.             $responseType = clone $responseType;
  98.         }
  99.         $this->responseType $responseType;
  100.     }
  101.     /**
  102.      * Enable a grant type on the server.
  103.      *
  104.      * @param GrantTypeInterface $grantType
  105.      * @param null|DateInterval  $accessTokenTTL
  106.      */
  107.     public function enableGrantType(GrantTypeInterface $grantTypeDateInterval $accessTokenTTL null)
  108.     {
  109.         if ($accessTokenTTL instanceof DateInterval === false) {
  110.             $accessTokenTTL = new DateInterval('PT1H');
  111.         }
  112.         $grantType->setAccessTokenRepository($this->accessTokenRepository);
  113.         $grantType->setClientRepository($this->clientRepository);
  114.         $grantType->setScopeRepository($this->scopeRepository);
  115.         $grantType->setDefaultScope($this->defaultScope);
  116.         $grantType->setPrivateKey($this->privateKey);
  117.         $grantType->setEmitter($this->getEmitter());
  118.         $grantType->setEncryptionKey($this->encryptionKey);
  119.         $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
  120.         $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
  121.     }
  122.     /**
  123.      * Validate an authorization request
  124.      *
  125.      * @param ServerRequestInterface $request
  126.      *
  127.      * @throws OAuthServerException
  128.      *
  129.      * @return AuthorizationRequest
  130.      */
  131.     public function validateAuthorizationRequest(ServerRequestInterface $request)
  132.     {
  133.         foreach ($this->enabledGrantTypes as $grantType) {
  134.             if ($grantType->canRespondToAuthorizationRequest($request)) {
  135.                 return $grantType->validateAuthorizationRequest($request);
  136.             }
  137.         }
  138.         throw OAuthServerException::unsupportedGrantType();
  139.     }
  140.     /**
  141.      * Complete an authorization request
  142.      *
  143.      * @param AuthorizationRequest $authRequest
  144.      * @param ResponseInterface    $response
  145.      *
  146.      * @return ResponseInterface
  147.      */
  148.     public function completeAuthorizationRequest(AuthorizationRequest $authRequestResponseInterface $response)
  149.     {
  150.         return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
  151.             ->completeAuthorizationRequest($authRequest)
  152.             ->generateHttpResponse($response);
  153.     }
  154.     /**
  155.      * Return an access token response.
  156.      *
  157.      * @param ServerRequestInterface $request
  158.      * @param ResponseInterface      $response
  159.      *
  160.      * @throws OAuthServerException
  161.      *
  162.      * @return ResponseInterface
  163.      */
  164.     public function respondToAccessTokenRequest(ServerRequestInterface $requestResponseInterface $response)
  165.     {
  166.         foreach ($this->enabledGrantTypes as $grantType) {
  167.             if (!$grantType->canRespondToAccessTokenRequest($request)) {
  168.                 continue;
  169.             }
  170.             $tokenResponse $grantType->respondToAccessTokenRequest(
  171.                 $request,
  172.                 $this->getResponseType(),
  173.                 $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
  174.             );
  175.             if ($tokenResponse instanceof ResponseTypeInterface) {
  176.                 return $tokenResponse->generateHttpResponse($response);
  177.             }
  178.         }
  179.         throw OAuthServerException::unsupportedGrantType();
  180.     }
  181.     /**
  182.      * Get the token type that grants will return in the HTTP response.
  183.      *
  184.      * @return ResponseTypeInterface
  185.      */
  186.     protected function getResponseType()
  187.     {
  188.         $responseType = clone $this->responseType;
  189.         if ($responseType instanceof AbstractResponseType) {
  190.             $responseType->setPrivateKey($this->privateKey);
  191.         }
  192.         $responseType->setEncryptionKey($this->encryptionKey);
  193.         return $responseType;
  194.     }
  195.     /**
  196.      * Set the default scope for the authorization server.
  197.      *
  198.      * @param string $defaultScope
  199.      */
  200.     public function setDefaultScope($defaultScope)
  201.     {
  202.         $this->defaultScope $defaultScope;
  203.     }
  204. }