vendor/shopware/core/Framework/Api/OAuth/BearerTokenValidator.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\OAuth;
  3. use Doctrine\DBAL\Connection;
  4. use Lcobucci\JWT\Configuration;
  5. use Lcobucci\JWT\UnencryptedToken;
  6. use League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface;
  7. use League\OAuth2\Server\Exception\OAuthServerException;
  8. use Psr\Http\Message\ServerRequestInterface;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\PlatformRequest;
  11. class BearerTokenValidator implements AuthorizationValidatorInterface
  12. {
  13.     /**
  14.      * @var Connection
  15.      */
  16.     private $connection;
  17.     /**
  18.      * @var AuthorizationValidatorInterface
  19.      */
  20.     private $decorated;
  21.     /**
  22.      * @var Configuration
  23.      */
  24.     private $configuration;
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(
  29.         AuthorizationValidatorInterface $decorated,
  30.         Connection $connection,
  31.         Configuration $configuration
  32.     ) {
  33.         $this->decorated $decorated;
  34.         $this->connection $connection;
  35.         $this->configuration $configuration;
  36.     }
  37.     /**
  38.      * @return ServerRequestInterface
  39.      */
  40.     public function validateAuthorization(ServerRequestInterface $request)
  41.     {
  42.         $request $this->decorated->validateAuthorization($request);
  43.         $header $request->getHeader('authorization');
  44.         $jwt trim(preg_replace('/^(?:\s+)?Bearer\s/'''$header[0]) ?? '');
  45.         /** @var UnencryptedToken $token */
  46.         $token $this->configuration->parser()->parse($jwt);
  47.         if ($userId $request->getAttribute(PlatformRequest::ATTRIBUTE_OAUTH_USER_ID)) {
  48.             $this->validateAccessTokenIssuedAt($token->claims()->get('iat'0), $userId);
  49.         }
  50.         return $request;
  51.     }
  52.     /**
  53.      * @throws OAuthServerException
  54.      * @throws \Doctrine\DBAL\DBALException
  55.      */
  56.     private function validateAccessTokenIssuedAt(\DateTimeImmutable $tokenIssuedAtstring $userId): void
  57.     {
  58.         $lastUpdatedPasswordAt $this->connection->createQueryBuilder()
  59.             ->select(['last_updated_password_at'])
  60.             ->from('user')
  61.             ->where('id = :userId')
  62.             ->setParameter('userId'Uuid::fromHexToBytes($userId))
  63.             ->execute()
  64.             ->fetchColumn();
  65.         if ($lastUpdatedPasswordAt === false) {
  66.             throw OAuthServerException::accessDenied('Access token is invalid');
  67.         }
  68.         if ($lastUpdatedPasswordAt === null) {
  69.             return;
  70.         }
  71.         $lastUpdatedPasswordAt strtotime($lastUpdatedPasswordAt);
  72.         if ($tokenIssuedAt->getTimestamp() <= $lastUpdatedPasswordAt) {
  73.             throw OAuthServerException::accessDenied('Access token is expired');
  74.         }
  75.     }
  76. }