vendor/symfony/security-http/Firewall/SimplePreAuthenticationListener.php line 34

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