Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Security;
  4.  
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  13. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Security\Core\User\UserProviderInterface;
  17. use Symfony\Component\Security\Csrf\CsrfToken;
  18. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  19. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  20. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  21. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  22.  
  23. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
  24. {
  25.     use TargetPathTrait;
  26.  
  27.     private $entityManager;
  28.     private $urlGenerator;
  29.     private $csrfTokenManager;
  30.     private $passwordEncoder;
  31.  
  32.     public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
  33.     {
  34.         $this->entityManager = $entityManager;
  35.         $this->urlGenerator = $urlGenerator;
  36.         $this->csrfTokenManager = $csrfTokenManager;
  37.         $this->passwordEncoder = $passwordEncoder;
  38.     }
  39.  
  40.     public function supports(Request $request)
  41.     {
  42.         return 'app_login' === $request->attributes->get('_route')
  43.             && $request->isMethod('POST');
  44.     }
  45.  
  46.     public function getCredentials(Request $request)
  47.     {
  48.         $credentials = [
  49.             'email' => $request->request->get('email'),
  50.             'password' => $request->request->get('password'),
  51.             'csrf_token' => $request->request->get('_csrf_token'),
  52.         ];
  53.         $request->getSession()->set(
  54.             Security::LAST_USERNAME,
  55.             $credentials['email']
  56.         );
  57.  
  58.         return $credentials;
  59.     }
  60.  
  61.     public function getUser($credentials, UserProviderInterface $userProvider)
  62.     {
  63.         $token = new CsrfToken('authenticate', $credentials['csrf_token']);
  64.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  65.             throw new InvalidCsrfTokenException();
  66.         }
  67.  
  68.         $user = $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
  69.  
  70.         if (!$user) {
  71.             // fail authentication with a custom error
  72.             throw new CustomUserMessageAuthenticationException('Email could not be found.');
  73.         }
  74.  
  75.         return $user;
  76.     }
  77.  
  78.     public function checkCredentials($credentials, UserInterface $user)
  79.     {
  80.         return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
  81.     }
  82.  
  83.     /**
  84.      * Used to upgrade (rehash) the user's password automatically over time.
  85.      */
  86.     public function getPassword($credentials): ?string
  87.     {
  88.         return $credentials['password'];
  89.     }
  90.  
  91.     public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
  92.     {
  93.         if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
  94.             return new RedirectResponse($targetPath);
  95.         }
  96.  
  97.         // For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
  98.         throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  99.     }
  100.  
  101.     protected function getLoginUrl()
  102.     {
  103.         return $this->urlGenerator->generate('app_login');
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement