src/Security/LoginFormAuthenticator.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Lettruser;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  11. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Core\User\UserProviderInterface;
  15. use Symfony\Component\Security\Csrf\CsrfToken;
  16. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  17. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  18. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  19. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  20. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
  21. {
  22.     use TargetPathTrait;
  23.     public const LOGIN_ROUTE 'app_login';
  24.     private $entityManager;
  25.     private $urlGenerator;
  26.     private $csrfTokenManager;
  27.     private $passwordEncoder;
  28.     public function __construct(EntityManagerInterface $entityManagerUrlGeneratorInterface $urlGeneratorCsrfTokenManagerInterface $csrfTokenManagerUserPasswordEncoderInterface $passwordEncoder)
  29.     {
  30.         $this->entityManager $entityManager;
  31.         $this->urlGenerator $urlGenerator;
  32.         $this->csrfTokenManager $csrfTokenManager;
  33.         $this->passwordEncoder $passwordEncoder;
  34.     }
  35.     public function supports(Request $request)
  36.     {
  37.         return self::LOGIN_ROUTE === $request->attributes->get('_route')
  38.             && $request->isMethod('POST');
  39.     }
  40.     public function getCredentials(Request $request)
  41.     {
  42.         $credentials = [
  43.             'username' => $request->request->get('username'),
  44.             'password' => $request->request->get('password'),
  45.             'csrf_token' => $request->request->get('_csrf_token'),
  46.         ];
  47.         $request->getSession()->set(
  48.             Security::LAST_USERNAME,
  49.             $credentials['username']
  50.         );
  51.         return $credentials;
  52.     }
  53.     public function getUser($credentialsUserProviderInterface $userProvider)
  54.     {
  55.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  56.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  57.             throw new InvalidCsrfTokenException();
  58.         }
  59.         $user $this->entityManager->getRepository(Lettruser::class)->findOneBy(['username' => $credentials['username']]);
  60.         if (!$user) {
  61.             // fail authentication with a custom error
  62.             throw new CustomUserMessageAuthenticationException('Username could not be found.');
  63.         }
  64.         return $user;
  65.     }
  66.     public function checkCredentials($credentialsUserInterface $user)
  67.     {
  68.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  69.     }
  70.     /**
  71.      * Used to upgrade (rehash) the user's password automatically over time.
  72.      */
  73.     public function getPassword($credentials): ?string
  74.     {
  75.         return $credentials['password'];
  76.     }
  77.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $providerKey)
  78.     {
  79.         if ($targetPath $this->getTargetPath($request->getSession(), $providerKey)) {
  80.             return new RedirectResponse($targetPath);
  81.         }
  82.         return new RedirectResponse($this->urlGenerator->generate('lettre'));
  83.     }
  84.     protected function getLoginUrl()
  85.     {
  86.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  87.     }
  88. }