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

  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.      * @var bool
  70.      */
  71.     private $revokeRefreshTokens true;
  72.     /**
  73.      * New server instance.
  74.      *
  75.      * @param ClientRepositoryInterface      $clientRepository
  76.      * @param AccessTokenRepositoryInterface $accessTokenRepository
  77.      * @param ScopeRepositoryInterface       $scopeRepository
  78.      * @param CryptKey|string                $privateKey
  79.      * @param string|Key                     $encryptionKey
  80.      * @param null|ResponseTypeInterface     $responseType
  81.      */
  82.     public function __construct(
  83.         ClientRepositoryInterface $clientRepository,
  84.         AccessTokenRepositoryInterface $accessTokenRepository,
  85.         ScopeRepositoryInterface $scopeRepository,
  86.         $privateKey,
  87.         $encryptionKey,
  88.         ResponseTypeInterface $responseType null
  89.     ) {
  90.         $this->clientRepository $clientRepository;
  91.         $this->accessTokenRepository $accessTokenRepository;
  92.         $this->scopeRepository $scopeRepository;
  93.         if ($privateKey instanceof CryptKey === false) {
  94.             $privateKey = new CryptKey($privateKey);
  95.         }
  96.         $this->privateKey $privateKey;
  97.         $this->encryptionKey $encryptionKey;
  98.         if ($responseType === null) {
  99.             $responseType = new BearerTokenResponse();
  100.         } else {
  101.             $responseType = clone $responseType;
  102.         }
  103.         $this->responseType $responseType;
  104.     }
  105.     /**
  106.      * Enable a grant type on the server.
  107.      *
  108.      * @param GrantTypeInterface $grantType
  109.      * @param null|DateInterval  $accessTokenTTL
  110.      */
  111.     public function enableGrantType(GrantTypeInterface $grantTypeDateInterval $accessTokenTTL null)
  112.     {
  113.         if ($accessTokenTTL === null) {
  114.             $accessTokenTTL = new DateInterval('PT1H');
  115.         }
  116.         $grantType->setAccessTokenRepository($this->accessTokenRepository);
  117.         $grantType->setClientRepository($this->clientRepository);
  118.         $grantType->setScopeRepository($this->scopeRepository);
  119.         $grantType->setDefaultScope($this->defaultScope);
  120.         $grantType->setPrivateKey($this->privateKey);
  121.         $grantType->setEmitter($this->getEmitter());
  122.         $grantType->setEncryptionKey($this->encryptionKey);
  123.         $grantType->revokeRefreshTokens($this->revokeRefreshTokens);
  124.         $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
  125.         $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
  126.     }
  127.     /**
  128.      * Validate an authorization request
  129.      *
  130.      * @param ServerRequestInterface $request
  131.      *
  132.      * @throws OAuthServerException
  133.      *
  134.      * @return AuthorizationRequest
  135.      */
  136.     public function validateAuthorizationRequest(ServerRequestInterface $request)
  137.     {
  138.         foreach ($this->enabledGrantTypes as $grantType) {
  139.             if ($grantType->canRespondToAuthorizationRequest($request)) {
  140.                 return $grantType->validateAuthorizationRequest($request);
  141.             }
  142.         }
  143.         throw OAuthServerException::unsupportedGrantType();
  144.     }
  145.     /**
  146.      * Complete an authorization request
  147.      *
  148.      * @param AuthorizationRequest $authRequest
  149.      * @param ResponseInterface    $response
  150.      *
  151.      * @return ResponseInterface
  152.      */
  153.     public function completeAuthorizationRequest(AuthorizationRequest $authRequestResponseInterface $response)
  154.     {
  155.         return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
  156.             ->completeAuthorizationRequest($authRequest)
  157.             ->generateHttpResponse($response);
  158.     }
  159.     /**
  160.      * Return an access token response.
  161.      *
  162.      * @param ServerRequestInterface $request
  163.      * @param ResponseInterface      $response
  164.      *
  165.      * @throws OAuthServerException
  166.      *
  167.      * @return ResponseInterface
  168.      */
  169.     public function respondToAccessTokenRequest(ServerRequestInterface $requestResponseInterface $response)
  170.     {
  171.         foreach ($this->enabledGrantTypes as $grantType) {
  172.             if (!$grantType->canRespondToAccessTokenRequest($request)) {
  173.                 continue;
  174.             }
  175.             $tokenResponse $grantType->respondToAccessTokenRequest(
  176.                 $request,
  177.                 $this->getResponseType(),
  178.                 $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
  179.             );
  180.             if ($tokenResponse instanceof ResponseTypeInterface) {
  181.                 return $tokenResponse->generateHttpResponse($response);
  182.             }
  183.         }
  184.         throw OAuthServerException::unsupportedGrantType();
  185.     }
  186.     /**
  187.      * Get the token type that grants will return in the HTTP response.
  188.      *
  189.      * @return ResponseTypeInterface
  190.      */
  191.     protected function getResponseType()
  192.     {
  193.         $responseType = clone $this->responseType;
  194.         if ($responseType instanceof AbstractResponseType) {
  195.             $responseType->setPrivateKey($this->privateKey);
  196.         }
  197.         $responseType->setEncryptionKey($this->encryptionKey);
  198.         return $responseType;
  199.     }
  200.     /**
  201.      * Set the default scope for the authorization server.
  202.      *
  203.      * @param string $defaultScope
  204.      */
  205.     public function setDefaultScope($defaultScope)
  206.     {
  207.         $this->defaultScope $defaultScope;
  208.     }
  209.     /**
  210.      * Sets whether to revoke refresh tokens or not (for all grant types).
  211.      *
  212.      * @param bool $revokeRefreshTokens
  213.      */
  214.     public function revokeRefreshTokens(bool $revokeRefreshTokens): void
  215.     {
  216.         $this->revokeRefreshTokens $revokeRefreshTokens;
  217.     }
  218. }