src/Security/AppAuthenticator.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Form\LoginFormType;
  4. use ReCaptcha\ReCaptcha;
  5. use Symfony\Component\Form\FormFactoryInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  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\Security;
  13. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  18. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  19. class AppAuthenticator extends AbstractLoginFormAuthenticator
  20. {
  21. use TargetPathTrait;
  22. public const LOGIN_ROUTE = 'app_login';
  23. private ReCaptcha $reCaptcha;
  24. public function __construct(private UrlGeneratorInterface $urlGenerator,
  25. private FormFactoryInterface $formFactory,
  26. string $googleRecaptchaSiteSecret)
  27. {
  28. $this->reCaptcha = new ReCaptcha($googleRecaptchaSiteSecret);
  29. }
  30. public function authenticate(Request $request)
  31. {
  32. //form generation should be in the same way (createdNamed in this case) as in LoginController
  33. $loginForm = $this->formFactory->createNamed('login', LoginFormType::class);
  34. if (!$loginForm->has('captcha')) {
  35. throw new CustomUserMessageAuthenticationException('Ha ocurrido un error1. Por favor intenta de nuevo.');
  36. }
  37. $loginArray = $request->request->get('login_form');
  38. $loginForm->submit($loginArray);
  39. if (!$loginForm->isSubmitted()) {
  40. throw new CustomUserMessageAuthenticationException('Ha ocurrido un error2. Por favor intenta de nuevo.');
  41. }
  42. if (!$loginForm->get('captcha')->isValid()) {
  43. throw new CustomUserMessageAuthenticationException('Error validando si es un robot. Por favor intenta de nuevo');
  44. }
  45. $username = $loginForm->get('_username')->getData();
  46. $password = $loginForm->get('_password')->getData();
  47. $token = $loginArray['_csrf_token']??'';
  48. $request->getSession()->set(Security::LAST_USERNAME, $username);
  49. return new Passport(
  50. new UserBadge($username),
  51. new PasswordCredentials($password),
  52. [new CsrfTokenBadge('authenticate', $token)]
  53. );
  54. }
  55. public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
  56. {
  57. if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
  58. return new RedirectResponse($targetPath);
  59. }
  60. return new RedirectResponse($this->urlGenerator->generate('dashboard'));
  61. }
  62. protected function getLoginUrl(Request $request): string
  63. {
  64. return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  65. }
  66. }