vendor/shopware/core/Framework/Api/EventListener/Authentication/ApiAuthenticationListener.php line 87

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\EventListener\Authentication;
  3. use League\OAuth2\Server\AuthorizationServer;
  4. use League\OAuth2\Server\Grant\ClientCredentialsGrant;
  5. use League\OAuth2\Server\Grant\PasswordGrant;
  6. use League\OAuth2\Server\Grant\RefreshTokenGrant;
  7. use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
  8. use League\OAuth2\Server\Repositories\UserRepositoryInterface;
  9. use League\OAuth2\Server\ResourceServer;
  10. use Shopware\Core\Framework\Routing\ApiContextRouteScopeDependant;
  11. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  12. use Shopware\Core\Framework\Routing\RouteScopeCheckTrait;
  13. use Shopware\Core\Framework\Routing\RouteScopeRegistry;
  14. use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. class ApiAuthenticationListener implements EventSubscriberInterface
  20. {
  21.     use RouteScopeCheckTrait;
  22.     /**
  23.      * @var ResourceServer
  24.      */
  25.     private $resourceServer;
  26.     /**
  27.      * @var AuthorizationServer
  28.      */
  29.     private $authorizationServer;
  30.     /**
  31.      * @var UserRepositoryInterface
  32.      */
  33.     private $userRepository;
  34.     /**
  35.      * @var RefreshTokenRepositoryInterface
  36.      */
  37.     private $refreshTokenRepository;
  38.     /**
  39.      * @var PsrHttpFactory
  40.      */
  41.     private $psrHttpFactory;
  42.     /**
  43.      * @var RouteScopeRegistry
  44.      */
  45.     private $routeScopeRegistry;
  46.     /**
  47.      * @internal
  48.      */
  49.     public function __construct(
  50.         ResourceServer $resourceServer,
  51.         AuthorizationServer $authorizationServer,
  52.         UserRepositoryInterface $userRepository,
  53.         RefreshTokenRepositoryInterface $refreshTokenRepository,
  54.         PsrHttpFactory $psrHttpFactory,
  55.         RouteScopeRegistry $routeScopeRegistry
  56.     ) {
  57.         $this->resourceServer $resourceServer;
  58.         $this->authorizationServer $authorizationServer;
  59.         $this->userRepository $userRepository;
  60.         $this->refreshTokenRepository $refreshTokenRepository;
  61.         $this->psrHttpFactory $psrHttpFactory;
  62.         $this->routeScopeRegistry $routeScopeRegistry;
  63.     }
  64.     public static function getSubscribedEvents(): array
  65.     {
  66.         return [
  67.             KernelEvents::REQUEST => [
  68.                 ['setupOAuth'128],
  69.             ],
  70.             KernelEvents::CONTROLLER => [
  71.                 ['validateRequest'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_PRIORITY_AUTH_VALIDATE],
  72.             ],
  73.         ];
  74.     }
  75.     public function setupOAuth(RequestEvent $event): void
  76.     {
  77.         if (!$event->isMainRequest()) {
  78.             return;
  79.         }
  80.         $tenMinuteInterval = new \DateInterval('PT10M');
  81.         $oneWeekInterval = new \DateInterval('P1W');
  82.         $passwordGrant = new PasswordGrant($this->userRepository$this->refreshTokenRepository);
  83.         $passwordGrant->setRefreshTokenTTL($oneWeekInterval);
  84.         $refreshTokenGrant = new RefreshTokenGrant($this->refreshTokenRepository);
  85.         $refreshTokenGrant->setRefreshTokenTTL($oneWeekInterval);
  86.         $this->authorizationServer->enableGrantType($passwordGrant$tenMinuteInterval);
  87.         $this->authorizationServer->enableGrantType($refreshTokenGrant$tenMinuteInterval);
  88.         $this->authorizationServer->enableGrantType(new ClientCredentialsGrant(), $tenMinuteInterval);
  89.     }
  90.     public function validateRequest(ControllerEvent $event): void
  91.     {
  92.         $request $event->getRequest();
  93.         if (!$request->attributes->get('auth_required'true)) {
  94.             return;
  95.         }
  96.         if (!$this->isRequestScoped($requestApiContextRouteScopeDependant::class)) {
  97.             return;
  98.         }
  99.         $psr7Request $this->psrHttpFactory->createRequest($event->getRequest());
  100.         $psr7Request $this->resourceServer->validateAuthenticatedRequest($psr7Request);
  101.         $request->attributes->add($psr7Request->getAttributes());
  102.     }
  103.     protected function getScopeRegistry(): RouteScopeRegistry
  104.     {
  105.         return $this->routeScopeRegistry;
  106.     }
  107. }