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

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