vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/BasicAuthenticationListener.php line 30

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