vendor/league/oauth2-server/src/ResourceServer.php line 84

  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 League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface;
  11. use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator;
  12. use League\OAuth2\Server\Exception\OAuthServerException;
  13. use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
  14. use Psr\Http\Message\ServerRequestInterface;
  15. class ResourceServer
  16. {
  17.     /**
  18.      * @var AccessTokenRepositoryInterface
  19.      */
  20.     private $accessTokenRepository;
  21.     /**
  22.      * @var CryptKey
  23.      */
  24.     private $publicKey;
  25.     /**
  26.      * @var null|AuthorizationValidatorInterface
  27.      */
  28.     private $authorizationValidator;
  29.     /**
  30.      * New server instance.
  31.      *
  32.      * @param AccessTokenRepositoryInterface       $accessTokenRepository
  33.      * @param CryptKey|string                      $publicKey
  34.      * @param null|AuthorizationValidatorInterface $authorizationValidator
  35.      */
  36.     public function __construct(
  37.         AccessTokenRepositoryInterface $accessTokenRepository,
  38.         $publicKey,
  39.         AuthorizationValidatorInterface $authorizationValidator null
  40.     ) {
  41.         $this->accessTokenRepository $accessTokenRepository;
  42.         if ($publicKey instanceof CryptKey === false) {
  43.             $publicKey = new CryptKey($publicKey);
  44.         }
  45.         $this->publicKey $publicKey;
  46.         $this->authorizationValidator $authorizationValidator;
  47.     }
  48.     /**
  49.      * @return AuthorizationValidatorInterface
  50.      */
  51.     protected function getAuthorizationValidator()
  52.     {
  53.         if ($this->authorizationValidator instanceof AuthorizationValidatorInterface === false) {
  54.             $this->authorizationValidator = new BearerTokenValidator($this->accessTokenRepository);
  55.         }
  56.         if ($this->authorizationValidator instanceof BearerTokenValidator === true) {
  57.             $this->authorizationValidator->setPublicKey($this->publicKey);
  58.         }
  59.         return $this->authorizationValidator;
  60.     }
  61.     /**
  62.      * Determine the access token validity.
  63.      *
  64.      * @param ServerRequestInterface $request
  65.      *
  66.      * @throws OAuthServerException
  67.      *
  68.      * @return ServerRequestInterface
  69.      */
  70.     public function validateAuthenticatedRequest(ServerRequestInterface $request)
  71.     {
  72.         return $this->getAuthorizationValidator()->validateAuthorization($request);
  73.     }
  74. }