src/Security/RefAuthAuthenticator.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  10. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  11. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  14. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  15. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  16. class RefAuthAuthenticator extends AbstractLoginFormAuthenticator
  17. {
  18.     use TargetPathTrait;
  19.     public const LOGIN_ROUTE 'app_login';
  20.     private UrlGeneratorInterface $urlGenerator;
  21.     private Security $security;
  22.     public function __construct(UrlGeneratorInterface $urlGeneratorSecurity $security)
  23.     {
  24.         $this->urlGenerator $urlGenerator;
  25.         $this->security $security;
  26.     }
  27.     public function authenticate(Request $request): Passport
  28.     {
  29.         // if($request->request->get('reponsecaptcha')) {
  30.         //     // Connecter à Google avec les clés
  31.         //     $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
  32.         //     $recaptcha_secret = '6Lf_McYqAAAAAKfaSj8d8sItDN7tEUz0W_ClFinZ';
  33.         //     $recaptcha_response = $request->request->get('reponsecaptcha');
  34.         //     // Décoder les informations récupérées
  35.         //     $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
  36.         //     $recaptcha = json_decode($recaptcha);
  37.         //     // dd($recaptcha->score);
  38.         //     // Effectuer une action en fonction du score obtenu.
  39.         //     if ($recaptcha->score >= 0.5) {
  40.                 $email $request->request->get('email''');
  41.                 $request->getSession()->set(Security::LAST_USERNAME$email);
  42.                 // dd(new Passport(
  43.                 //     new UserBadge($email),
  44.                 //     new PasswordCredentials($request->request->get('password', '')),
  45.                 //     [
  46.                 //         new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
  47.                 //     ]
  48.                 // ));
  49.                 return new Passport(
  50.                     new UserBadge($email),
  51.                     new PasswordCredentials($request->request->get('password''')),
  52.                     [
  53.                         new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  54.                     ]
  55.                 );
  56.             // } else {
  57.             //     // Vous n'avez pas vérifier si l'utilisateur est un humain ou un robot. Affichez le message d'erreur.
  58.             //     throw new UserNotFoundException();
  59.             // }
  60.         // }
  61.     }
  62.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  63.     {
  64.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  65.             return new RedirectResponse($targetPath);
  66.         }
  67.         // For example:
  68.         $user $this->security->getUser();
  69.         if( $user->isAdmin() ) {
  70.             return new RedirectResponse($this->urlGenerator->generate('admin_index'));
  71.         }
  72.         return new RedirectResponse($this->urlGenerator->generate('index'));
  73.     }
  74.     protected function getLoginUrl(Request $request): string
  75.     {
  76.         if (in_array($request->get('_route'), [self::LOGIN_ROUTE'index'])) {
  77.             return $this->urlGenerator->generate($request->get('_route'));
  78.         }
  79.         
  80.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  81.     }
  82. }