vendor/contao/core-bundle/src/Security/Authentication/ContaoLoginAuthenticationListener.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\Security\Authentication;
  11. use Psr\Log\LoggerInterface;
  12. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  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\TokenInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  19. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  20. use Symfony\Component\Security\Core\Security;
  21. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  23. use Symfony\Component\Security\Http\Firewall\AbstractAuthenticationListener;
  24. use Symfony\Component\Security\Http\HttpUtils;
  25. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  26. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  27. class ContaoLoginAuthenticationListener extends AbstractAuthenticationListener
  28. {
  29.     /**
  30.      * @var TokenStorageInterface
  31.      */
  32.     private $tokenStorage;
  33.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerSessionAuthenticationStrategyInterface $sessionStrategyHttpUtils $httpUtilsstring $providerKeyAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandler, array $optionsLoggerInterface $logger nullEventDispatcherInterface $dispatcher null)
  34.     {
  35.         parent::__construct($tokenStorage$authenticationManager$sessionStrategy$httpUtils$providerKey$successHandler$failureHandler$options$logger$dispatcher);
  36.         $this->tokenStorage $tokenStorage;
  37.     }
  38.     protected function requiresAuthentication(Request $request): bool
  39.     {
  40.         return $request->isMethod('POST')
  41.             && $request->request->has('FORM_SUBMIT')
  42.             && === strncmp($request->request->get('FORM_SUBMIT'), 'tl_login'8);
  43.     }
  44.     protected function attemptAuthentication(Request $request): ?TokenInterface
  45.     {
  46.         $currentToken $this->tokenStorage->getToken();
  47.         if ($currentToken instanceof TwoFactorTokenInterface) {
  48.             $authCode $request->request->get('verify');
  49.             return $this->authenticationManager->authenticate($currentToken->createWithCredentials($authCode));
  50.         }
  51.         $username $request->request->get('username');
  52.         $password $request->request->get('password');
  53.         if (!\is_string($username)) {
  54.             throw new BadRequestHttpException(sprintf('The key "username" must be a string, "%s" given.', \gettype($username)));
  55.         }
  56.         $username trim($username);
  57.         if (\strlen($username) > Security::MAX_USERNAME_LENGTH) {
  58.             throw new BadCredentialsException('Invalid username.');
  59.         }
  60.         $request->getSession()->set(Security::LAST_USERNAME$username);
  61.         return $this->authenticationManager->authenticate(new UsernamePasswordToken($username$password$this->providerKey));
  62.     }
  63. }