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

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.     public function __construct(
  47.         ResourceServer $resourceServer,
  48.         AuthorizationServer $authorizationServer,
  49.         UserRepositoryInterface $userRepository,
  50.         RefreshTokenRepositoryInterface $refreshTokenRepository,
  51.         PsrHttpFactory $psrHttpFactory,
  52.         RouteScopeRegistry $routeScopeRegistry
  53.     ) {
  54.         $this->resourceServer $resourceServer;
  55.         $this->authorizationServer $authorizationServer;
  56.         $this->userRepository $userRepository;
  57.         $this->refreshTokenRepository $refreshTokenRepository;
  58.         $this->psrHttpFactory $psrHttpFactory;
  59.         $this->routeScopeRegistry $routeScopeRegistry;
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             KernelEvents::REQUEST => [
  65.                 ['setupOAuth'128],
  66.             ],
  67.             KernelEvents::CONTROLLER => [
  68.                 ['validateRequest'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_PRIORITY_AUTH_VALIDATE],
  69.             ],
  70.         ];
  71.     }
  72.     public function setupOAuth(RequestEvent $event): void
  73.     {
  74.         if (!$event->isMasterRequest()) {
  75.             return;
  76.         }
  77.         $tenMinuteInterval = new \DateInterval('PT10M');
  78.         $oneWeekInterval = new \DateInterval('P1W');
  79.         $passwordGrant = new PasswordGrant($this->userRepository$this->refreshTokenRepository);
  80.         $passwordGrant->setRefreshTokenTTL($oneWeekInterval);
  81.         $refreshTokenGrant = new RefreshTokenGrant($this->refreshTokenRepository);
  82.         $refreshTokenGrant->setRefreshTokenTTL($oneWeekInterval);
  83.         $this->authorizationServer->enableGrantType($passwordGrant$tenMinuteInterval);
  84.         $this->authorizationServer->enableGrantType($refreshTokenGrant$tenMinuteInterval);
  85.         $this->authorizationServer->enableGrantType(new ClientCredentialsGrant(), $tenMinuteInterval);
  86.     }
  87.     public function validateRequest(ControllerEvent $event): void
  88.     {
  89.         $request $event->getRequest();
  90.         if (!$request->attributes->get('auth_required'true)) {
  91.             return;
  92.         }
  93.         if (!$this->isRequestScoped($requestApiContextRouteScopeDependant::class)) {
  94.             return;
  95.         }
  96.         $psr7Request $this->psrHttpFactory->createRequest($event->getRequest());
  97.         $psr7Request $this->resourceServer->validateAuthenticatedRequest($psr7Request);
  98.         $request->attributes->add($psr7Request->getAttributes());
  99.     }
  100.     protected function getScopeRegistry(): RouteScopeRegistry
  101.     {
  102.         return $this->routeScopeRegistry;
  103.     }
  104. }