src/Security/LoginFormAuthenticator.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  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\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Core\User\UserProviderInterface;
  16. use Symfony\Component\Security\Csrf\CsrfToken;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  19. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  20. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  21. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
  22. {
  23.     use TargetPathTrait;
  24.     public const LOGIN_ROUTE 'app_login';
  25.     private $entityManager;
  26.     private $urlGenerator;
  27.     private $csrfTokenManager;
  28.     private $passwordEncoder;
  29.     public function __construct(
  30.         EntityManagerInterface $entityManager,
  31.         UrlGeneratorInterface $urlGenerator,
  32.         CsrfTokenManagerInterface $csrfTokenManager,
  33.         UserPasswordHasherInterface $passwordEncoder
  34.     ) {
  35.         $this->entityManager $entityManager;
  36.         $this->urlGenerator $urlGenerator;
  37.         $this->csrfTokenManager $csrfTokenManager;
  38.         $this->passwordEncoder $passwordEncoder;
  39.     }
  40.     public function supports(Request $request)
  41.     {
  42.         return self::LOGIN_ROUTE === $request->attributes->get('_route')
  43.             && $request->isMethod('POST');
  44.     }
  45.     public function getCredentials(Request $request)
  46.     {
  47.         $credentials = [
  48.             'email' => $request->request->get('email'),
  49.             'password' => $request->request->get('password'),
  50.             'csrf_token' => $request->request->get('_csrf_token'),
  51.         ];
  52.         $request->getSession()->set(
  53.             Security::LAST_USERNAME,
  54.             $credentials['email']
  55.         );
  56.         return $credentials;
  57.     }
  58.     public function getUser($credentialsUserProviderInterface $userProvider)
  59.     {
  60.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  61.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  62.             throw new InvalidCsrfTokenException();
  63.         }
  64.         $user $this->entityManager->getRepository(User::class)->findOneBy(
  65.             [
  66.                 'email' => $credentials['email'],
  67.             ]
  68.     );
  69.         if (!$user) {
  70.             // fail authentication with a custom error
  71.             throw new CustomUserMessageAuthenticationException('L\'e-mail est introuvable.');
  72.         }
  73.         return $user;
  74.     }
  75.     public function checkCredentials($credentialsUserInterface $user)
  76.     {
  77.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  78.     }
  79.     /**
  80.      * Used to upgrade (rehash) the user's password automatically over time.
  81.      */
  82.     public function getPassword($credentials): ?string
  83.     {
  84.         return $credentials['password'];
  85.     }
  86.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $providerKey)
  87.     {
  88.         if ($targetPath $this->getTargetPath($request->getSession(), $providerKey)) {
  89.             return new RedirectResponse($targetPath);
  90.         }
  91.         return new RedirectResponse($this->urlGenerator->generate('dashboard'));
  92.     }
  93.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  94.     {
  95.         if ($request->hasSession()) {
  96.             $request->getSession()->set(Security::AUTHENTICATION_ERROR, new CustomUserMessageAuthenticationException('Les informations d\'identification sont invalides. '));
  97.         }
  98.         $url $this->getLoginUrl();
  99.         return new RedirectResponse($url);
  100.     }
  101.     protected function getLoginUrl()
  102.     {
  103.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  104.     }
  105. }