vendor/symfony/security/Http/Firewall/SimpleFormAuthenticationListener.php line 32

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\HttpKernel\Exception\BadRequestHttpException;
  15. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  18. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  19. use Symfony\Component\Security\Core\Security;
  20. use Symfony\Component\Security\Csrf\CsrfToken;
  21. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  24. use Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface;
  25. use Symfony\Component\Security\Http\HttpUtils;
  26. use Symfony\Component\Security\Http\ParameterBagUtils;
  27. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  28. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use Guard instead.'SimpleFormAuthenticationListener::class), \E_USER_DEPRECATED);
  29. /**
  30.  * @author Jordi Boggiano <j.boggiano@seld.be>
  31.  *
  32.  * @deprecated since Symfony 4.2, use Guard instead.
  33.  */
  34. class SimpleFormAuthenticationListener extends AbstractAuthenticationListener
  35. {
  36.     private $simpleAuthenticator;
  37.     private $csrfTokenManager;
  38.     /**
  39.      * @throws \InvalidArgumentException In case no simple authenticator is provided
  40.      */
  41.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerSessionAuthenticationStrategyInterface $sessionStrategyHttpUtils $httpUtilsstring $providerKeyAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandler, array $options = [], LoggerInterface $logger nullEventDispatcherInterface $dispatcher nullCsrfTokenManagerInterface $csrfTokenManager nullSimpleFormAuthenticatorInterface $simpleAuthenticator null)
  42.     {
  43.         if (!$simpleAuthenticator) {
  44.             throw new \InvalidArgumentException('Missing simple authenticator.');
  45.         }
  46.         $this->simpleAuthenticator $simpleAuthenticator;
  47.         $this->csrfTokenManager $csrfTokenManager;
  48.         $options array_merge([
  49.             'username_parameter' => '_username',
  50.             'password_parameter' => '_password',
  51.             'csrf_parameter' => '_csrf_token',
  52.             'csrf_token_id' => 'authenticate',
  53.             'post_only' => true,
  54.         ], $options);
  55.         parent::__construct($tokenStorage$authenticationManager$sessionStrategy$httpUtils$providerKey$successHandler$failureHandler$options$logger$dispatcher);
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     protected function requiresAuthentication(Request $request)
  61.     {
  62.         if ($this->options['post_only'] && !$request->isMethod('POST')) {
  63.             return false;
  64.         }
  65.         return parent::requiresAuthentication($request);
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     protected function attemptAuthentication(Request $request)
  71.     {
  72.         if (null !== $this->csrfTokenManager) {
  73.             $csrfToken ParameterBagUtils::getRequestParameterValue($request$this->options['csrf_parameter']);
  74.             if (!\is_string($csrfToken) || false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['csrf_token_id'], $csrfToken))) {
  75.                 throw new InvalidCsrfTokenException('Invalid CSRF token.');
  76.             }
  77.         }
  78.         if ($this->options['post_only']) {
  79.             $username ParameterBagUtils::getParameterBagValue($request->request$this->options['username_parameter']);
  80.             $password ParameterBagUtils::getParameterBagValue($request->request$this->options['password_parameter']);
  81.         } else {
  82.             $username ParameterBagUtils::getRequestParameterValue($request$this->options['username_parameter']);
  83.             $password ParameterBagUtils::getRequestParameterValue($request$this->options['password_parameter']);
  84.         }
  85.         if (!\is_string($username) && (!\is_object($username) || !method_exists($username'__toString'))) {
  86.             throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.'$this->options['username_parameter'], \gettype($username)));
  87.         }
  88.         $username trim($username);
  89.         if (\strlen($username) > Security::MAX_USERNAME_LENGTH) {
  90.             throw new BadCredentialsException('Invalid username.');
  91.         }
  92.         $request->getSession()->set(Security::LAST_USERNAME$username);
  93.         $token $this->simpleAuthenticator->createToken($request$username$password$this->providerKey);
  94.         return $this->authenticationManager->authenticate($token);
  95.     }
  96. }