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

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.     public function __construct(
  26.         AuthorizationValidatorInterface $decorated,
  27.         Connection $connection,
  28.         Configuration $configuration
  29.     ) {
  30.         $this->decorated $decorated;
  31.         $this->connection $connection;
  32.         $this->configuration $configuration;
  33.     }
  34.     /**
  35.      * @return ServerRequestInterface
  36.      */
  37.     public function validateAuthorization(ServerRequestInterface $request)
  38.     {
  39.         $request $this->decorated->validateAuthorization($request);
  40.         $header $request->getHeader('authorization');
  41.         $jwt trim(preg_replace('/^(?:\s+)?Bearer\s/'''$header[0]) ?? '');
  42.         /** @var UnencryptedToken $token */
  43.         $token $this->configuration->parser()->parse($jwt);
  44.         if ($userId $request->getAttribute(PlatformRequest::ATTRIBUTE_OAUTH_USER_ID)) {
  45.             $this->validateAccessTokenIssuedAt($token->claims()->get('iat'0), $userId);
  46.         }
  47.         return $request;
  48.     }
  49.     /**
  50.      * @throws OAuthServerException
  51.      * @throws \Doctrine\DBAL\DBALException
  52.      */
  53.     private function validateAccessTokenIssuedAt(\DateTimeImmutable $tokenIssuedAtstring $userId): void
  54.     {
  55.         $lastUpdatedPasswordAt $this->connection->createQueryBuilder()
  56.             ->select(['last_updated_password_at'])
  57.             ->from('user')
  58.             ->where('id = :userId')
  59.             ->setParameter('userId'Uuid::fromHexToBytes($userId))
  60.             ->execute()
  61.             ->fetchColumn();
  62.         if ($lastUpdatedPasswordAt === false) {
  63.             throw OAuthServerException::accessDenied('Access token is invalid');
  64.         }
  65.         if ($lastUpdatedPasswordAt === null) {
  66.             return;
  67.         }
  68.         $lastUpdatedPasswordAt strtotime($lastUpdatedPasswordAt);
  69.         if ($tokenIssuedAt->getTimestamp() <= $lastUpdatedPasswordAt) {
  70.             throw OAuthServerException::accessDenied('Access token is expired');
  71.         }
  72.     }
  73. }