vendor/symfony/security-http/Firewall/BasicAuthenticationListener.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Firewall;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  18. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  19. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  20. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  21. /**
  22.  * BasicAuthenticationListener implements Basic HTTP authentication.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  *
  26.  * @final since Symfony 4.3
  27.  */
  28. class BasicAuthenticationListener implements ListenerInterface
  29. {
  30.     use LegacyListenerTrait;
  31.     private $tokenStorage;
  32.     private $authenticationManager;
  33.     private $providerKey;
  34.     private $authenticationEntryPoint;
  35.     private $logger;
  36.     private $ignoreFailure;
  37.     private $sessionStrategy;
  38.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerstring $providerKeyAuthenticationEntryPointInterface $authenticationEntryPointLoggerInterface $logger null)
  39.     {
  40.         if (empty($providerKey)) {
  41.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  42.         }
  43.         $this->tokenStorage $tokenStorage;
  44.         $this->authenticationManager $authenticationManager;
  45.         $this->providerKey $providerKey;
  46.         $this->authenticationEntryPoint $authenticationEntryPoint;
  47.         $this->logger $logger;
  48.         $this->ignoreFailure false;
  49.     }
  50.     /**
  51.      * Handles basic authentication.
  52.      */
  53.     public function __invoke(RequestEvent $event)
  54.     {
  55.         $request $event->getRequest();
  56.         if (null === $username $request->headers->get('PHP_AUTH_USER')) {
  57.             return;
  58.         }
  59.         if (null !== $token $this->tokenStorage->getToken()) {
  60.             if ($token instanceof UsernamePasswordToken && $token->isAuthenticated() && $token->getUsername() === $username) {
  61.                 return;
  62.             }
  63.         }
  64.         if (null !== $this->logger) {
  65.             $this->logger->info('Basic authentication Authorization header found for user.', ['username' => $username]);
  66.         }
  67.         try {
  68.             $token $this->authenticationManager->authenticate(new UsernamePasswordToken($username$request->headers->get('PHP_AUTH_PW'), $this->providerKey));
  69.             $this->migrateSession($request$token);
  70.             $this->tokenStorage->setToken($token);
  71.         } catch (AuthenticationException $e) {
  72.             $token $this->tokenStorage->getToken();
  73.             if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
  74.                 $this->tokenStorage->setToken(null);
  75.             }
  76.             if (null !== $this->logger) {
  77.                 $this->logger->info('Basic authentication failed for user.', ['username' => $username'exception' => $e]);
  78.             }
  79.             if ($this->ignoreFailure) {
  80.                 return;
  81.             }
  82.             $event->setResponse($this->authenticationEntryPoint->start($request$e));
  83.         }
  84.     }
  85.     /**
  86.      * Call this method if your authentication token is stored to a session.
  87.      *
  88.      * @final
  89.      */
  90.     public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
  91.     {
  92.         $this->sessionStrategy $sessionStrategy;
  93.     }
  94.     private function migrateSession(Request $requestTokenInterface $token)
  95.     {
  96.         if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
  97.             return;
  98.         }
  99.         $this->sessionStrategy->onAuthentication($request$token);
  100.     }
  101. }