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 <[email protected]>
  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 <[email protected]>
  25.  *
  26.  * @final
  27.  */
  28. class BasicAuthenticationListener extends AbstractListener
  29. {
  30.     private $tokenStorage;
  31.     private $authenticationManager;
  32.     private $providerKey;
  33.     private $authenticationEntryPoint;
  34.     private $logger;
  35.     private $ignoreFailure;
  36.     private $sessionStrategy;
  37.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerstring $providerKeyAuthenticationEntryPointInterface $authenticationEntryPointLoggerInterface $logger null)
  38.     {
  39.         if (empty($providerKey)) {
  40.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  41.         }
  42.         $this->tokenStorage $tokenStorage;
  43.         $this->authenticationManager $authenticationManager;
  44.         $this->providerKey $providerKey;
  45.         $this->authenticationEntryPoint $authenticationEntryPoint;
  46.         $this->logger $logger;
  47.         $this->ignoreFailure false;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function supports(Request $request): ?bool
  53.     {
  54.         return null !== $request->headers->get('PHP_AUTH_USER');
  55.     }
  56.     /**
  57.      * Handles basic authentication.
  58.      */
  59.     public function authenticate(RequestEvent $event)
  60.     {
  61.         $request $event->getRequest();
  62.         if (null === $username $request->headers->get('PHP_AUTH_USER')) {
  63.             return;
  64.         }
  65.         if (null !== $token $this->tokenStorage->getToken()) {
  66.             if ($token instanceof UsernamePasswordToken && $token->isAuthenticated() && $token->getUsername() === $username) {
  67.                 return;
  68.             }
  69.         }
  70.         if (null !== $this->logger) {
  71.             $this->logger->info('Basic authentication Authorization header found for user.', ['username' => $username]);
  72.         }
  73.         try {
  74.             $token $this->authenticationManager->authenticate(new UsernamePasswordToken($username$request->headers->get('PHP_AUTH_PW'), $this->providerKey));
  75.             $this->migrateSession($request$token);
  76.             $this->tokenStorage->setToken($token);
  77.         } catch (AuthenticationException $e) {
  78.             $token $this->tokenStorage->getToken();
  79.             if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
  80.                 $this->tokenStorage->setToken(null);
  81.             }
  82.             if (null !== $this->logger) {
  83.                 $this->logger->info('Basic authentication failed for user.', ['username' => $username'exception' => $e]);
  84.             }
  85.             if ($this->ignoreFailure) {
  86.                 return;
  87.             }
  88.             $event->setResponse($this->authenticationEntryPoint->start($request$e));
  89.         }
  90.     }
  91.     /**
  92.      * Call this method if your authentication token is stored to a session.
  93.      *
  94.      * @final
  95.      */
  96.     public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
  97.     {
  98.         $this->sessionStrategy $sessionStrategy;
  99.     }
  100.     private function migrateSession(Request $requestTokenInterface $token)
  101.     {
  102.         if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
  103.             return;
  104.         }
  105.         $this->sessionStrategy->onAuthentication($request$token);
  106.     }
  107. }