src/Security/Account/AccountAuthenticator.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\Security\Account;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Messenger\MessageBusInterface;
  8. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  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\PasswordAuthenticatedUserInterface;
  15. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface as NellappUserInterface;
  16. use Nellapp\Bundle\SDKBundle\Auth\Messenger\Message\RefreshUserMessage;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Security\Core\User\UserProviderInterface;
  19. use Symfony\Component\Security\Csrf\CsrfToken;
  20. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  21. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  22. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  23. class AccountAuthenticator extends AbstractFormLoginAuthenticator
  24. {
  25.     use TargetPathTrait;
  26.     public const LOGIN_ROUTE 'app_login';
  27.     public function __construct(
  28.         private EntityManagerInterface      $em,
  29.         private UrlGeneratorInterface       $urlGenerator,
  30.         private CsrfTokenManagerInterface   $csrfTokenManager,
  31.         private UserPasswordHasherInterface $passwordHasher,
  32.         private MessageBusInterface         $bus,
  33.     )
  34.     {
  35.     }
  36.     public function supports(Request $request): bool
  37.     {
  38.         return
  39.             self::LOGIN_ROUTE === $request->attributes->get('_route')
  40.             && $request->isMethod('POST');
  41.     }
  42.     public function getCredentials(Request $request): array
  43.     {
  44.         $credentials = [
  45.             'email' => $request->request->get('email'),
  46.             'password' => $request->request->get('password'),
  47.             'csrf_token' => $request->request->get('_csrf_token'),
  48.         ];
  49.         $request->getSession()->set(
  50.             Security::LAST_USERNAME,
  51.             $credentials['email'],
  52.         );
  53.         return $credentials;
  54.     }
  55.     public function getUser($credentialsUserProviderInterface $userProvider): UserInterface
  56.     {
  57.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  58.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  59.             throw new InvalidCsrfTokenException();
  60.         }
  61.         $user $userProvider->loadUserByIdentifier($credentials['email']);
  62.         if (!$user) {
  63.             // fail authentication with a custom error
  64.             throw new CustomUserMessageAuthenticationException('Email could not be found.');
  65.         }
  66.         return $user;
  67.     }
  68.     public function checkCredentials($credentialsUserInterface $user): bool
  69.     {
  70.         if (!$user instanceof PasswordAuthenticatedUserInterface) {
  71.             return false;
  72.         }
  73.         return $this->passwordHasher->isPasswordValid($user$credentials['password']);
  74.     }
  75.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey): Response
  76.     {
  77.         $user $token->getUser();
  78.         if ($user instanceof NellappUserInterface) {
  79.             $this->bus->dispatch(new RefreshUserMessage($user->getId()));
  80.         }
  81.         if ($request->request->get('_target_path') !== null) {
  82.             return new RedirectResponse($request->request->get('_target_path'));
  83.         }
  84.         if ($targetPath $this->getTargetPath($request->getSession(), $providerKey)) {
  85.             return new RedirectResponse($targetPath);
  86.         }
  87.         return new RedirectResponse('/');
  88.     }
  89.     protected function getLoginUrl(): string
  90.     {
  91.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  92.     }
  93. }