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

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