vendor/shopware/core/Framework/Api/EventListener/Authentication/SalesChannelAuthenticationListener.php line 42

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\EventListener\Authentication;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Api\Util\AccessKeyHelper;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\Framework\Routing\Exception\SalesChannelNotFoundException;
  7. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  8. use Shopware\Core\Framework\Routing\RouteScopeCheckTrait;
  9. use Shopware\Core\Framework\Routing\RouteScopeRegistry;
  10. use Shopware\Core\Framework\Routing\StoreApiRouteScope;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. use Shopware\Core\PlatformRequest;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  15. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. /**
  18.  * @internal
  19.  */
  20. #[Package('core')]
  21. class SalesChannelAuthenticationListener implements EventSubscriberInterface
  22. {
  23.     use RouteScopeCheckTrait;
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(private readonly Connection $connection, private readonly RouteScopeRegistry $routeScopeRegistry)
  28.     {
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::CONTROLLER => ['validateRequest'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_PRIORITY_AUTH_VALIDATE],
  34.         ];
  35.     }
  36.     public function validateRequest(ControllerEvent $event): void
  37.     {
  38.         $request $event->getRequest();
  39.         if (!$request->attributes->get('auth_required'true)) {
  40.             return;
  41.         }
  42.         if (!$this->isRequestScoped($requestStoreApiRouteScope::class)) {
  43.             return;
  44.         }
  45.         $accessKey $request->headers->get(PlatformRequest::HEADER_ACCESS_KEY);
  46.         if (!$accessKey) {
  47.             throw new UnauthorizedHttpException('header'sprintf('Header "%s" is required.'PlatformRequest::HEADER_ACCESS_KEY));
  48.         }
  49.         $origin AccessKeyHelper::getOrigin($accessKey);
  50.         if ($origin !== 'sales-channel') {
  51.             throw new SalesChannelNotFoundException();
  52.         }
  53.         $salesChannelId $this->getSalesChannelId($accessKey);
  54.         $request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID$salesChannelId);
  55.     }
  56.     protected function getScopeRegistry(): RouteScopeRegistry
  57.     {
  58.         return $this->routeScopeRegistry;
  59.     }
  60.     private function getSalesChannelId(string $accessKey): string
  61.     {
  62.         $builder $this->connection->createQueryBuilder();
  63.         $salesChannelId $builder->select(['sales_channel.id'])
  64.             ->from('sales_channel')
  65.             ->where('sales_channel.access_key = :accessKey')
  66.             ->setParameter('accessKey'$accessKey)
  67.             ->executeQuery()
  68.             ->fetchOne();
  69.         if (!$salesChannelId) {
  70.             throw new SalesChannelNotFoundException();
  71.         }
  72.         return Uuid::fromBytesToHex($salesChannelId);
  73.     }
  74. }