custom/static-plugins/SamsonCustomer/src/Api/Soap/Subscriber/AuthSubscriber.php line 64

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Api\Soap\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2020
  11.  *
  12.  ***/
  13. use Samson\Api\Soap\Controller\ApiController;
  14. use Samson\Api\Soap\Service\AuthService;
  15. use Samson\Service\LoggerService;
  16. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  19. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  22. /**
  23.  * Class AuthSubscriber
  24.  * @package Samson\Api\BasicAuth
  25.  * @author Artur Seitz <artur.seitz@dkd.de>
  26.  */
  27. class AuthSubscriber implements EventSubscriberInterface
  28. {
  29.     protected AuthService $authService;
  30.     protected LoggerService $logger;
  31.     /**
  32.      * AuthSubscriber constructor.
  33.      *
  34.      * @param AuthService $authService
  35.      * @param LoggerService $logger
  36.      */
  37.     public function __construct(
  38.         AuthService $authService,
  39.         LoggerService $logger
  40.     ) {
  41.         $this->authService $authService;
  42.         $this->logger $logger;
  43.     }
  44.     /** {@inheritDoc} */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             KernelEvents::CONTROLLER => [
  49.                 ['resolveContext'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_PRIORITY_AUTH_VALIDATE],
  50.             ],
  51.         ];
  52.     }
  53.     /**
  54.      * KernelEvents::CONTROLLER event listener.
  55.      *
  56.      * @param ControllerEvent $event
  57.      */
  58.     public function resolveContext(ControllerEvent $event): void
  59.     {
  60.         $controller $event->getController();
  61.         // when a controller class defines multiple action methods, the controller
  62.         // is returned as [$controllerInstance, 'methodName']
  63.         if (is_array($controller)) {
  64.             $controller $controller[0];
  65.         }
  66.         if ($controller instanceof ApiController) {
  67.             if (!$event->getRequest()->headers->has('Authorization')) {
  68.                 throw new UnauthorizedHttpException('header''Header "Authorization" is required.');
  69.             }
  70.             if (!$this->authService->isAuthenticated($event->getRequest())) {
  71.                 $this->logger->authenticationFailed($event->getRequest());
  72.                 throw new AuthenticationException('Basic authorization failed. The user is unknown.');
  73.             }
  74.         }
  75.     }
  76. }