<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class RefAuthAuthenticator extends AbstractLoginFormAuthenticator
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private UrlGeneratorInterface $urlGenerator;
private Security $security;
public function __construct(UrlGeneratorInterface $urlGenerator, Security $security)
{
$this->urlGenerator = $urlGenerator;
$this->security = $security;
}
public function authenticate(Request $request): Passport
{
// if($request->request->get('reponsecaptcha')) {
// // Connecter à Google avec les clés
// $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
// $recaptcha_secret = '6Lf_McYqAAAAAKfaSj8d8sItDN7tEUz0W_ClFinZ';
// $recaptcha_response = $request->request->get('reponsecaptcha');
// // Décoder les informations récupérées
// $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
// $recaptcha = json_decode($recaptcha);
// // dd($recaptcha->score);
// // Effectuer une action en fonction du score obtenu.
// if ($recaptcha->score >= 0.5) {
$email = $request->request->get('email', '');
$request->getSession()->set(Security::LAST_USERNAME, $email);
// dd(new Passport(
// new UserBadge($email),
// new PasswordCredentials($request->request->get('password', '')),
// [
// new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
// ]
// ));
return new Passport(
new UserBadge($email),
new PasswordCredentials($request->request->get('password', '')),
[
new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),
]
);
// } else {
// // Vous n'avez pas vérifier si l'utilisateur est un humain ou un robot. Affichez le message d'erreur.
// throw new UserNotFoundException();
// }
// }
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
return new RedirectResponse($targetPath);
}
// For example:
$user = $this->security->getUser();
if( $user->isAdmin() ) {
return new RedirectResponse($this->urlGenerator->generate('admin_index'));
}
return new RedirectResponse($this->urlGenerator->generate('index'));
}
protected function getLoginUrl(Request $request): string
{
if (in_array($request->get('_route'), [self::LOGIN_ROUTE, 'index'])) {
return $this->urlGenerator->generate($request->get('_route'));
}
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}