vendor/symfony/symfony/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php line 36

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\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  16. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  23. use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
  24. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  25. use Symfony\Component\Security\Http\SecurityEvents;
  26. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  27. /**
  28.  * SimplePreAuthenticationListener implements simple proxying to an authenticator.
  29.  *
  30.  * @author Jordi Boggiano <j.boggiano@seld.be>
  31.  */
  32. class SimplePreAuthenticationListener implements ListenerInterface
  33. {
  34.     private $tokenStorage;
  35.     private $authenticationManager;
  36.     private $providerKey;
  37.     private $simpleAuthenticator;
  38.     private $logger;
  39.     private $dispatcher;
  40.     private $sessionStrategy;
  41.     /**
  42.      * @param TokenStorageInterface           $tokenStorage          A TokenStorageInterface instance
  43.      * @param AuthenticationManagerInterface  $authenticationManager An AuthenticationManagerInterface instance
  44.      * @param string                          $providerKey
  45.      * @param SimplePreAuthenticatorInterface $simpleAuthenticator   A SimplePreAuthenticatorInterface instance
  46.      * @param LoggerInterface|null            $logger                A LoggerInterface instance
  47.      * @param EventDispatcherInterface|null   $dispatcher            An EventDispatcherInterface instance
  48.      */
  49.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManager$providerKeySimplePreAuthenticatorInterface $simpleAuthenticatorLoggerInterface $logger nullEventDispatcherInterface $dispatcher null)
  50.     {
  51.         if (empty($providerKey)) {
  52.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  53.         }
  54.         $this->tokenStorage $tokenStorage;
  55.         $this->authenticationManager $authenticationManager;
  56.         $this->providerKey $providerKey;
  57.         $this->simpleAuthenticator $simpleAuthenticator;
  58.         $this->logger $logger;
  59.         $this->dispatcher $dispatcher;
  60.     }
  61.     /**
  62.      * Call this method if your authentication token is stored to a session.
  63.      *
  64.      * @final
  65.      */
  66.     public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
  67.     {
  68.         $this->sessionStrategy $sessionStrategy;
  69.     }
  70.     /**
  71.      * Handles basic authentication.
  72.      */
  73.     public function handle(GetResponseEvent $event)
  74.     {
  75.         $request $event->getRequest();
  76.         if (null !== $this->logger) {
  77.             $this->logger->info('Attempting SimplePreAuthentication.', ['key' => $this->providerKey'authenticator' => \get_class($this->simpleAuthenticator)]);
  78.         }
  79.         if (null !== $this->tokenStorage->getToken() && !$this->tokenStorage->getToken() instanceof AnonymousToken) {
  80.             return;
  81.         }
  82.         try {
  83.             $token $this->simpleAuthenticator->createToken($request$this->providerKey);
  84.             // allow null to be returned to skip authentication
  85.             if (null === $token) {
  86.                 return;
  87.             }
  88.             $token $this->authenticationManager->authenticate($token);
  89.             $this->migrateSession($request$token);
  90.             $this->tokenStorage->setToken($token);
  91.             if (null !== $this->dispatcher) {
  92.                 $loginEvent = new InteractiveLoginEvent($request$token);
  93.                 $this->dispatcher->dispatch(SecurityEvents::INTERACTIVE_LOGIN$loginEvent);
  94.             }
  95.         } catch (AuthenticationException $e) {
  96.             $this->tokenStorage->setToken(null);
  97.             if (null !== $this->logger) {
  98.                 $this->logger->info('SimplePreAuthentication request failed.', ['exception' => $e'authenticator' => \get_class($this->simpleAuthenticator)]);
  99.             }
  100.             if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
  101.                 $response $this->simpleAuthenticator->onAuthenticationFailure($request$e);
  102.                 if ($response instanceof Response) {
  103.                     $event->setResponse($response);
  104.                 } elseif (null !== $response) {
  105.                     throw new \UnexpectedValueException(sprintf('The %s::onAuthenticationFailure method must return null or a Response object', \get_class($this->simpleAuthenticator)));
  106.                 }
  107.             }
  108.             return;
  109.         }
  110.         if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
  111.             $response $this->simpleAuthenticator->onAuthenticationSuccess($request$token);
  112.             if ($response instanceof Response) {
  113.                 $event->setResponse($response);
  114.             } elseif (null !== $response) {
  115.                 throw new \UnexpectedValueException(sprintf('The %s::onAuthenticationSuccess method must return null or a Response object', \get_class($this->simpleAuthenticator)));
  116.             }
  117.         }
  118.     }
  119.     private function migrateSession(Request $requestTokenInterface $token)
  120.     {
  121.         if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
  122.             return;
  123.         }
  124.         $this->sessionStrategy->onAuthentication($request$token);
  125.     }
  126. }