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

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. /**
  20.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  21.  */
  22. class ApiAuthenticationListener implements EventSubscriberInterface
  23. {
  24.     use RouteScopeCheckTrait;
  25.     private ResourceServer $resourceServer;
  26.     private AuthorizationServer $authorizationServer;
  27.     private UserRepositoryInterface $userRepository;
  28.     private RefreshTokenRepositoryInterface $refreshTokenRepository;
  29.     private PsrHttpFactory $psrHttpFactory;
  30.     private RouteScopeRegistry $routeScopeRegistry;
  31.     /**
  32.      * @internal
  33.      */
  34.     public function __construct(
  35.         ResourceServer $resourceServer,
  36.         AuthorizationServer $authorizationServer,
  37.         UserRepositoryInterface $userRepository,
  38.         RefreshTokenRepositoryInterface $refreshTokenRepository,
  39.         PsrHttpFactory $psrHttpFactory,
  40.         RouteScopeRegistry $routeScopeRegistry
  41.     ) {
  42.         $this->resourceServer $resourceServer;
  43.         $this->authorizationServer $authorizationServer;
  44.         $this->userRepository $userRepository;
  45.         $this->refreshTokenRepository $refreshTokenRepository;
  46.         $this->psrHttpFactory $psrHttpFactory;
  47.         $this->routeScopeRegistry $routeScopeRegistry;
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             KernelEvents::REQUEST => [
  53.                 ['setupOAuth'128],
  54.             ],
  55.             KernelEvents::CONTROLLER => [
  56.                 ['validateRequest'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_PRIORITY_AUTH_VALIDATE],
  57.             ],
  58.         ];
  59.     }
  60.     public function setupOAuth(RequestEvent $event): void
  61.     {
  62.         if (!$event->isMainRequest()) {
  63.             return;
  64.         }
  65.         $tenMinuteInterval = new \DateInterval('PT10M');
  66.         $oneWeekInterval = new \DateInterval('P1W');
  67.         $passwordGrant = new PasswordGrant($this->userRepository$this->refreshTokenRepository);
  68.         $passwordGrant->setRefreshTokenTTL($oneWeekInterval);
  69.         $refreshTokenGrant = new RefreshTokenGrant($this->refreshTokenRepository);
  70.         $refreshTokenGrant->setRefreshTokenTTL($oneWeekInterval);
  71.         $this->authorizationServer->enableGrantType($passwordGrant$tenMinuteInterval);
  72.         $this->authorizationServer->enableGrantType($refreshTokenGrant$tenMinuteInterval);
  73.         $this->authorizationServer->enableGrantType(new ClientCredentialsGrant(), $tenMinuteInterval);
  74.     }
  75.     public function validateRequest(ControllerEvent $event): void
  76.     {
  77.         $request $event->getRequest();
  78.         if (!$request->attributes->get('auth_required'true)) {
  79.             return;
  80.         }
  81.         if (!$this->isRequestScoped($requestApiContextRouteScopeDependant::class)) {
  82.             return;
  83.         }
  84.         $psr7Request $this->psrHttpFactory->createRequest($event->getRequest());
  85.         $psr7Request $this->resourceServer->validateAuthenticatedRequest($psr7Request);
  86.         $request->attributes->add($psr7Request->getAttributes());
  87.     }
  88.     protected function getScopeRegistry(): RouteScopeRegistry
  89.     {
  90.         return $this->routeScopeRegistry;
  91.     }
  92. }