vendor/roothirsch/core-bundle/Security/CoreAuthenticator.php line 68

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\CoreBundle\Security;
  3. use Roothirsch\CoreBundle\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  11. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Core\User\UserProviderInterface;
  15. use Symfony\Component\Security\Csrf\CsrfToken;
  16. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  17. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  18. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  19. class CoreAuthenticator extends AbstractFormLoginAuthenticator
  20. {
  21.     use TargetPathTrait;
  22.     private $entityManager;
  23.     private $urlGenerator;
  24.     private $csrfTokenManager;
  25.     private $passwordEncoder;
  26.     public function __construct(
  27.         EntityManagerInterface $entityManager,
  28.         UrlGeneratorInterface $urlGenerator,
  29.         CsrfTokenManagerInterface $csrfTokenManager,
  30.         UserPasswordEncoderInterface $passwordEncoder
  31.     ) {
  32.         $this->entityManager $entityManager;
  33.         $this->urlGenerator $urlGenerator;
  34.         $this->csrfTokenManager $csrfTokenManager;
  35.         $this->passwordEncoder $passwordEncoder;
  36.     }
  37.     public function supports(Request $request)
  38.     {
  39.         return 'app_login' === $request->attributes->get('_route')
  40.             && $request->isMethod('POST');
  41.     }
  42.     public function getCredentials(Request $request)
  43.     {
  44.         $credentials = [
  45.             'username' => $request->request->get('username'),
  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['username']
  52.         );
  53.         return $credentials;
  54.     }
  55.     public function getUser($credentialsUserProviderInterface $userProvider)
  56.     {
  57.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  58.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  59.             throw new InvalidCsrfTokenException();
  60.         }
  61.         $user $this->entityManager->getRepository(User::class)->findOneBy(['username' => $credentials['username']]);
  62.         if (!$user) {
  63.             // fail authentication with a custom error
  64.             throw new CustomUserMessageAuthenticationException('Username could not be found.');
  65.         }
  66.         return $user;
  67.     }
  68.     public function checkCredentials($credentialsUserInterface $user)
  69.     {
  70.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  71.     }
  72.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  73.     {
  74.         if ($targetPath $this->getTargetPath($request->getSession(), $providerKey)) {
  75.             return new RedirectResponse($targetPath);
  76.         }
  77.         return new RedirectResponse('/');
  78.     }
  79.     protected function getLoginUrl()
  80.     {
  81.         return $this->urlGenerator->generate('app_login');
  82.     }
  83. }