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 extends AbstractListener 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.      * {@inheritdoc}
  52.      */
  53.     public function supports(Request $request): ?bool
  54.     {
  55.         return null !== $request->headers->get('PHP_AUTH_USER');
  56.     }
  57.     /**
  58.      * Handles basic authentication.
  59.      */
  60.     public function authenticate(RequestEvent $event)
  61.     {
  62.         $request $event->getRequest();
  63.         if (null === $username $request->headers->get('PHP_AUTH_USER')) {
  64.             return;
  65.         }
  66.         if (null !== $token $this->tokenStorage->getToken()) {
  67.             if ($token instanceof UsernamePasswordToken && $token->isAuthenticated() && $token->getUsername() === $username) {
  68.                 return;
  69.             }
  70.         }
  71.         if (null !== $this->logger) {
  72.             $this->logger->info('Basic authentication Authorization header found for user.', ['username' => $username]);
  73.         }
  74.         try {
  75.             $token $this->authenticationManager->authenticate(new UsernamePasswordToken($username$request->headers->get('PHP_AUTH_PW'), $this->providerKey));
  76.             $this->migrateSession($request$token);
  77.             $this->tokenStorage->setToken($token);
  78.         } catch (AuthenticationException $e) {
  79.             $token $this->tokenStorage->getToken();
  80.             if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
  81.                 $this->tokenStorage->setToken(null);
  82.             }
  83.             if (null !== $this->logger) {
  84.                 $this->logger->info('Basic authentication failed for user.', ['username' => $username'exception' => $e]);
  85.             }
  86.             if ($this->ignoreFailure) {
  87.                 return;
  88.             }
  89.             $event->setResponse($this->authenticationEntryPoint->start($request$e));
  90.         }
  91.     }
  92.     /**
  93.      * Call this method if your authentication token is stored to a session.
  94.      *
  95.      * @final
  96.      */
  97.     public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
  98.     {
  99.         $this->sessionStrategy $sessionStrategy;
  100.     }
  101.     private function migrateSession(Request $requestTokenInterface $token)
  102.     {
  103.         if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
  104.             return;
  105.         }
  106.         $this->sessionStrategy->onAuthentication($request$token);
  107.     }
  108. }