<?php declare(strict_types=1);
namespace App\Security;
use App\Authentication\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
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;
class JsonLoginUsernameAuthenticator extends AbstractAuthenticator
{
private EntityManagerInterface $em;
private UserPasswordHasherInterface $encoder;
private AuthenticationSuccessHandler $jwtSuccessHandler;
public function __construct(
EntityManagerInterface $em,
UserPasswordHasherInterface $encoder,
AuthenticationSuccessHandler $jwtSuccessHandler
) {
$this->em = $em;
$this->encoder = $encoder;
$this->jwtSuccessHandler = $jwtSuccessHandler;
}
/**
* Called on every request to decide if this authenticator should be
* used for the request. Returning false will cause this authenticator
* to be skipped.
*/
public function supports(Request $request): bool
{
$isLoginRoute = $request->attributes->get('_route') === 'app_authentication_authentication_login' && $request->isMethod('POST');
if (!$isLoginRoute) {
return false;
}
$isJsonRequest = $request->headers->has('Content-Type') && $request->headers->get('Content-Type') === 'application/json';
if (!$isJsonRequest) {
return false;
}
$body = json_decode($request->getContent(), true);
if ($body === null) {
return false;
}
return array_key_exists('username', $body) && array_key_exists('password', $body);
}
public function authenticate(Request $request): Passport
{
$body = json_decode($request->getContent(), true);
$username = $body['username'];
$password = $body['password'];
if (!$username || !$password) {
throw new UserNotFoundException();
}
$user = $this->em->getRepository(User::class)->findOneBy([
'username' => $username,
]);
if ($user === null) {
throw new UserNotFoundException();
}
return new Passport(new UserBadge($username), new PasswordCredentials($password));
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
$user = $token->getUser();
return $this->jwtSuccessHandler->handleAuthenticationSuccess($user);
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
$data = [
'code' => 'authentication.failure',
'message' => [$exception->getMessageKey(), $exception->getMessageData()],
];
return new JsonResponse($data, Response::HTTP_FORBIDDEN);
}
}