src/Security/JwtAuthenticator.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Kernel\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Security\Core\User\UserProviderInterface;
  12. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Firebase\JWT\JWT;
  15. class JwtAuthenticator extends AbstractGuardAuthenticator
  16. {
  17.     private $em;
  18.     private $params;
  19.     public function __construct(EntityManagerInterface $em, ContainerBagInterface $params)
  20.     {
  21.         $this->em = $em;
  22.         $this->params = $params;
  23.     }
  24.     public function start(Request $request, AuthenticationException $authException = null)
  25.     {
  26.         $data = [ 
  27.             'message' => 'Authentication Required'
  28.         ];
  29.         return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
  30.     }
  31.     public function supports(Request $request)
  32.     {
  33.         return $request->headers->has('Authorization');
  34.     }
  35.     public function getCredentials(Request $request)
  36.     {
  37.          return $request->headers->get('Authorization');
  38.     }
  39.     public function getUser($credentials, UserProviderInterface $userProvider)
  40.     {
  41.         return $this->em->getRepository(User::class)
  42.                     ->findOneBy([
  43.                             'uuid' => 'admin',
  44.                     ]);
  45.         
  46.         $secret = $this->params->get('jwt_secret');
  47.         
  48.         try {
  49.             $credentials = str_replace('Bearer ', '', $credentials);
  50.             $jwt = (array) JWT::decode(
  51.                               $credentials, 
  52.                               $secret,
  53.                               ['HS512']
  54.                             );
  55.             return $this->em->getRepository(User::class)
  56.                     ->findOneBy([
  57.                             'uuid' => $jwt['user'],
  58.                     ]);
  59.         }catch (\Exception $exception) {
  60.                 throw new AuthenticationException($exception->getMessage());
  61.         }
  62.     }
  63.     public function checkCredentials($credentials, UserInterface $user)
  64.     {
  65.         return true;
  66.         $secret = $this->params->get('jwt_secret');
  67.         
  68.         $credentials = str_replace('Bearer ', '', $credentials);
  69.         $jwt = (array) JWT::decode($credentials, $secret, ['HS512']);
  70.         return $user->getUuid() === $jwt['user'];
  71.     }
  72.     public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
  73.     {
  74.         return new JsonResponse([
  75.                 'message' => $exception->getMessage()
  76.         ], Response::HTTP_UNAUTHORIZED);
  77.     }
  78.     public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
  79.     {
  80.          return;
  81.     }
  82.     public function supportsRememberMe()
  83.     {
  84.         return false;
  85.     }
  86. }