vendor/symfony/security-http/Firewall/AbstractAuthenticationListener.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\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  19. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  20. use Symfony\Component\Security\Core\Exception\SessionUnavailableException;
  21. use Symfony\Component\Security\Core\Security;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  24. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  25. use Symfony\Component\Security\Http\HttpUtils;
  26. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  27. use Symfony\Component\Security\Http\SecurityEvents;
  28. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  29. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  30. trigger_deprecation('symfony/security-http''5.3''The "%s" class is deprecated, use the new authenticator system instead.'AbstractAuthenticationListener::class);
  31. /**
  32.  * The AbstractAuthenticationListener is the preferred base class for all
  33.  * browser-/HTTP-based authentication requests.
  34.  *
  35.  * Subclasses likely have to implement the following:
  36.  * - an TokenInterface to hold authentication related data
  37.  * - an AuthenticationProvider to perform the actual authentication of the
  38.  *   token, retrieve the UserInterface implementation from a database, and
  39.  *   perform the specific account checks using the UserChecker
  40.  *
  41.  * By default, this listener only is active for a specific path, e.g.
  42.  * /login_check. If you want to change this behavior, you can overwrite the
  43.  * requiresAuthentication() method.
  44.  *
  45.  * @author Fabien Potencier <fabien@symfony.com>
  46.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  47.  *
  48.  * @deprecated since Symfony 5.3, use the new authenticator system instead
  49.  */
  50. abstract class AbstractAuthenticationListener extends AbstractListener
  51. {
  52.     protected $options;
  53.     protected $logger;
  54.     protected $authenticationManager;
  55.     protected $providerKey;
  56.     protected $httpUtils;
  57.     private $tokenStorage;
  58.     private $sessionStrategy;
  59.     private $dispatcher;
  60.     private $successHandler;
  61.     private $failureHandler;
  62.     private $rememberMeServices;
  63.     /**
  64.      * @throws \InvalidArgumentException
  65.      */
  66.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerSessionAuthenticationStrategyInterface $sessionStrategyHttpUtils $httpUtilsstring $providerKeyAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandler, array $options = [], LoggerInterface $logger nullEventDispatcherInterface $dispatcher null)
  67.     {
  68.         if (empty($providerKey)) {
  69.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  70.         }
  71.         $this->tokenStorage $tokenStorage;
  72.         $this->authenticationManager $authenticationManager;
  73.         $this->sessionStrategy $sessionStrategy;
  74.         $this->providerKey $providerKey;
  75.         $this->successHandler $successHandler;
  76.         $this->failureHandler $failureHandler;
  77.         $this->options array_merge([
  78.             'check_path' => '/login_check',
  79.             'login_path' => '/login',
  80.             'always_use_default_target_path' => false,
  81.             'default_target_path' => '/',
  82.             'target_path_parameter' => '_target_path',
  83.             'use_referer' => false,
  84.             'failure_path' => null,
  85.             'failure_forward' => false,
  86.             'require_previous_session' => true,
  87.         ], $options);
  88.         $this->logger $logger;
  89.         $this->dispatcher $dispatcher;
  90.         $this->httpUtils $httpUtils;
  91.     }
  92.     /**
  93.      * Sets the RememberMeServices implementation to use.
  94.      */
  95.     public function setRememberMeServices(RememberMeServicesInterface $rememberMeServices)
  96.     {
  97.         $this->rememberMeServices $rememberMeServices;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function supports(Request $request): ?bool
  103.     {
  104.         return $this->requiresAuthentication($request);
  105.     }
  106.     /**
  107.      * Handles form based authentication.
  108.      *
  109.      * @throws \RuntimeException
  110.      * @throws SessionUnavailableException
  111.      */
  112.     public function authenticate(RequestEvent $event)
  113.     {
  114.         $request $event->getRequest();
  115.         if (!$request->hasSession()) {
  116.             throw new \RuntimeException('This authentication method requires a session.');
  117.         }
  118.         try {
  119.             if ($this->options['require_previous_session'] && !$request->hasPreviousSession()) {
  120.                 throw new SessionUnavailableException('Your session has timed out, or you have disabled cookies.');
  121.             }
  122.             if (null === $returnValue $this->attemptAuthentication($request)) {
  123.                 return;
  124.             }
  125.             if ($returnValue instanceof TokenInterface) {
  126.                 $this->sessionStrategy->onAuthentication($request$returnValue);
  127.                 $response $this->onSuccess($request$returnValue);
  128.             } elseif ($returnValue instanceof Response) {
  129.                 $response $returnValue;
  130.             } else {
  131.                 throw new \RuntimeException('attemptAuthentication() must either return a Response, an implementation of TokenInterface, or null.');
  132.             }
  133.         } catch (AuthenticationException $e) {
  134.             $response $this->onFailure($request$e);
  135.         }
  136.         $event->setResponse($response);
  137.     }
  138.     /**
  139.      * Whether this request requires authentication.
  140.      *
  141.      * The default implementation only processes requests to a specific path,
  142.      * but a subclass could change this to only authenticate requests where a
  143.      * certain parameters is present.
  144.      *
  145.      * @return bool
  146.      */
  147.     protected function requiresAuthentication(Request $request)
  148.     {
  149.         return $this->httpUtils->checkRequestPath($request$this->options['check_path']);
  150.     }
  151.     /**
  152.      * Performs authentication.
  153.      *
  154.      * @return TokenInterface|Response|null The authenticated token, null if full authentication is not possible, or a Response
  155.      *
  156.      * @throws AuthenticationException if the authentication fails
  157.      */
  158.     abstract protected function attemptAuthentication(Request $request);
  159.     private function onFailure(Request $requestAuthenticationException $failed): Response
  160.     {
  161.         if (null !== $this->logger) {
  162.             $this->logger->info('Authentication request failed.', ['exception' => $failed]);
  163.         }
  164.         $token $this->tokenStorage->getToken();
  165.         if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getFirewallName()) {
  166.             $this->tokenStorage->setToken(null);
  167.         }
  168.         $response $this->failureHandler->onAuthenticationFailure($request$failed);
  169.         if (!$response instanceof Response) {
  170.             throw new \RuntimeException('Authentication Failure Handler did not return a Response.');
  171.         }
  172.         return $response;
  173.     }
  174.     private function onSuccess(Request $requestTokenInterface $token): Response
  175.     {
  176.         if (null !== $this->logger) {
  177.             // @deprecated since Symfony 5.3, change to $token->getUserIdentifier() in 6.0
  178.             $this->logger->info('User has been authenticated successfully.', ['username' => method_exists($token'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername()]);
  179.         }
  180.         $this->tokenStorage->setToken($token);
  181.         $session $request->getSession();
  182.         $session->remove(Security::AUTHENTICATION_ERROR);
  183.         $session->remove(Security::LAST_USERNAME);
  184.         if (null !== $this->dispatcher) {
  185.             $loginEvent = new InteractiveLoginEvent($request$token);
  186.             $this->dispatcher->dispatch($loginEventSecurityEvents::INTERACTIVE_LOGIN);
  187.         }
  188.         $response $this->successHandler->onAuthenticationSuccess($request$token);
  189.         if (!$response instanceof Response) {
  190.             throw new \RuntimeException('Authentication Success Handler did not return a Response.');
  191.         }
  192.         if (null !== $this->rememberMeServices) {
  193.             $this->rememberMeServices->loginSuccess($request$response$token);
  194.         }
  195.         return $response;
  196.     }
  197. }