|
|
|
@ -3,19 +3,18 @@
|
|
|
|
|
namespace App\Security;
|
|
|
|
|
|
|
|
|
|
use App\Entity\User;
|
|
|
|
|
use App\Form\SecuritySignInForm;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
|
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
|
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
|
|
|
|
|
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
|
|
|
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken;
|
|
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
|
|
|
|
|
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
|
|
|
|
|
use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
|
|
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait;
|
|
|
|
@ -25,19 +24,19 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements P
|
|
|
|
|
|
|
|
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
|
private UrlGeneratorInterface $urlGenerator;
|
|
|
|
|
private CsrfTokenManagerInterface $csrfTokenManager;
|
|
|
|
|
private UserPasswordEncoderInterface $passwordEncoder;
|
|
|
|
|
private FormFactoryInterface $formFactory;
|
|
|
|
|
|
|
|
|
|
public function __construct (
|
|
|
|
|
EntityManagerInterface $entityManager,
|
|
|
|
|
UrlGeneratorInterface $urlGenerator,
|
|
|
|
|
CsrfTokenManagerInterface $csrfTokenManager,
|
|
|
|
|
UserPasswordEncoderInterface $passwordEncoder
|
|
|
|
|
UserPasswordEncoderInterface $passwordEncoder,
|
|
|
|
|
FormFactoryInterface $formFactory
|
|
|
|
|
) {
|
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
|
$this->csrfTokenManager = $csrfTokenManager;
|
|
|
|
|
$this->passwordEncoder = $passwordEncoder;
|
|
|
|
|
$this->formFactory = $formFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function supports (Request $request) {
|
|
|
|
@ -45,11 +44,17 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements P
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCredentials (Request $request) {
|
|
|
|
|
$credentials = [
|
|
|
|
|
'email' => $request->request->get('email'),
|
|
|
|
|
'password' => $request->request->get('password'),
|
|
|
|
|
'csrf_token' => $request->request->get('_csrf_token'),
|
|
|
|
|
];
|
|
|
|
|
if (!$request->isMethod('POST') || $request->attributes->get('_route') != 'app_security_sign_in') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$form = $this->formFactory->create(SecuritySignInForm::class);
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
if (!$form->isSubmitted() || !$form->isValid()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$credentials = $form->getData();
|
|
|
|
|
$request->getSession()->set(
|
|
|
|
|
Security::LAST_USERNAME,
|
|
|
|
|
$credentials['email']
|
|
|
|
@ -59,15 +64,8 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements P
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUser ($credentials, UserProviderInterface $userProvider) {
|
|
|
|
|
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
|
|
|
|
|
if (!$this->csrfTokenManager->isTokenValid($token)) {
|
|
|
|
|
throw new InvalidCsrfTokenException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
|
|
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
|
// fail authentication with a custom error
|
|
|
|
|
throw new CustomUserMessageAuthenticationException('Email could not be found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|