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

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