src/Security/JsonLoginEmailAuthenticator.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security;
  3. use App\Authentication\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  16. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  17. class JsonLoginEmailAuthenticator extends AbstractAuthenticator
  18. {
  19.     private EntityManagerInterface $em;
  20.     private UserPasswordHasherInterface $encoder;
  21.     private AuthenticationSuccessHandler $jwtSuccessHandler;
  22.     public function __construct(
  23.         EntityManagerInterface $em,
  24.         UserPasswordHasherInterface $encoder,
  25.         AuthenticationSuccessHandler $jwtSuccessHandler
  26.     ) {
  27.         $this->em $em;
  28.         $this->encoder $encoder;
  29.         $this->jwtSuccessHandler $jwtSuccessHandler;
  30.     }
  31.     /**
  32.      * Called on every request to decide if this authenticator should be
  33.      * used for the request. Returning false will cause this authenticator
  34.      * to be skipped.
  35.      */
  36.     public function supports(Request $request): bool
  37.     {
  38.         $isLoginRoute $request->attributes->get('_route') === 'app_authentication_authentication_login' && $request->isMethod('POST');
  39.         if (!$isLoginRoute) {
  40.             return false;
  41.         }
  42.         $isJsonRequest $request->headers->has('Content-Type') && $request->headers->get('Content-Type') === 'application/json';
  43.         if (!$isJsonRequest) {
  44.             return false;
  45.         }
  46.         $body json_decode($request->getContent(), true);
  47.         if ($body === null) {
  48.             return false;
  49.         }
  50.         return array_key_exists('email'$body) && array_key_exists('password'$body);
  51.     }
  52.     public function authenticate(Request $request): Passport
  53.     {
  54.         $body json_decode($request->getContent(), true);
  55.         $email $body['email'];
  56.         $password $body['password'];
  57.         if (!$email || !$password) {
  58.             throw new UserNotFoundException();
  59.         }
  60.         $user $this->em->getRepository(User::class)->findOneBy([
  61.             'email' => $email
  62.         ]);
  63.         if ($user === null) {
  64.             throw new UserNotFoundException();
  65.         }
  66.         return new Passport(new UserBadge($email), new PasswordCredentials($password));
  67.     }
  68.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  69.     {
  70.         $user $token->getUser();
  71.         return $this->jwtSuccessHandler->handleAuthenticationSuccess($user);
  72.     }
  73.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  74.     {
  75.         $data = [
  76.             'code' => 'authentication.failure',
  77.             'message' => [$exception->getMessageKey(), $exception->getMessageData()],
  78.         ];
  79.         return new JsonResponse($dataResponse::HTTP_FORBIDDEN);
  80.     }
  81. }