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

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