vendor/symfony/security-http/Firewall/UsernamePasswordFormAuthenticationListener.php line 38

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\Authentication\Token\UsernamePasswordToken;
  18. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  19. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  20. use Symfony\Component\Security\Core\Security;
  21. use Symfony\Component\Security\Csrf\CsrfToken;
  22. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  24. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  25. use Symfony\Component\Security\Http\HttpUtils;
  26. use Symfony\Component\Security\Http\ParameterBagUtils;
  27. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  28. /**
  29.  * UsernamePasswordFormAuthenticationListener is the default implementation of
  30.  * an authentication via a simple form composed of a username and a password.
  31.  *
  32.  * @author Fabien Potencier <fabien@symfony.com>
  33.  */
  34. class UsernamePasswordFormAuthenticationListener extends AbstractAuthenticationListener {
  35.     private $csrfTokenManager;
  36.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerSessionAuthenticationStrategyInterface $sessionStrategyHttpUtils $httpUtilsstring $providerKeyAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandler, array $options = [], LoggerInterface $logger nullEventDispatcherInterface $dispatcher nullCsrfTokenManagerInterface $csrfTokenManager null) {
  37.         parent::__construct($tokenStorage$authenticationManager$sessionStrategy$httpUtils$providerKey$successHandler$failureHandlerarray_merge([
  38.             'username_parameter' => '_username',
  39.             'password_parameter' => '_password',
  40.             'csrf_parameter' => '_csrf_token',
  41.             'csrf_token_id' => 'authenticate',
  42.             'post_only' => true,
  43.                         ], $options), $logger$dispatcher);
  44.         $this->csrfTokenManager $csrfTokenManager;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     protected function requiresAuthentication(Request $request) {
  50.         if ($this->options['post_only'] && !$request->isMethod('POST')) {
  51.             return false;
  52.         }
  53.         return parent::requiresAuthentication($request);
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     protected function attemptAuthentication(Request $request) {
  59.         if (null !== $this->csrfTokenManager) {
  60.             $csrfToken ParameterBagUtils::getRequestParameterValue($request$this->options['csrf_parameter']);
  61.             if (false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['csrf_token_id'], $csrfToken))) {
  62.                 throw new InvalidCsrfTokenException('Invalid CSRF token.');
  63.             }
  64.         }
  65.         if ($this->options['post_only']) {
  66.             $username ParameterBagUtils::getParameterBagValue($request->request$this->options['username_parameter']);
  67.             $password ParameterBagUtils::getParameterBagValue($request->request$this->options['password_parameter']);
  68.         } else {
  69.             $username ParameterBagUtils::getRequestParameterValue($request$this->options['username_parameter']);
  70.             $password ParameterBagUtils::getRequestParameterValue($request$this->options['password_parameter']);
  71.         }
  72.         if (!\is_string($username) && (!\is_object($username) || !method_exists($username'__toString'))) {
  73.             throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.'$this->options['username_parameter'], \gettype($username)));
  74.         }
  75.         $username trim($username);
  76.         if (\strlen($username) > Security::MAX_USERNAME_LENGTH) {
  77.             throw new BadCredentialsException('Invalid username.');
  78.         }
  79.         $request->getSession()->set(Security::LAST_USERNAME$username);
  80.         return $this->authenticationManager->authenticate(new UsernamePasswordToken($username$password$this->providerKey));
  81.     }
  82. }