src/Security/JWTAuthenticator.php line 25

  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: JMR
  5.  * Date: 25/10/2024
  6.  * Time: 09:12
  7.  */
  8. // src/Security/JWTAuthenticator.php
  9. namespace App\Security;
  10. use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Response// Import the correct Response class
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Security\Core\User\UserProviderInterface;
  19. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  20. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  21. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  22. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  23. class JWTAuthenticator extends AbstractAuthenticator
  24. {
  25.     private $jwtEncoder;
  26.     public function __construct(JWTEncoderInterface $jwtEncoder)
  27.     {
  28.         $this->jwtEncoder $jwtEncoder;
  29.     }
  30.     public function supports(Request $request): ?bool
  31.     {
  32.         // Check if the request contains the Authorization header
  33.         return $request->headers->has('x-authorization');
  34.     }
  35.     public function authenticate(Request $request): Passport
  36.     {
  37.         $authHeader $request->headers->get('x-authorization');
  38.         $token null;
  39.         if ($authHeader && str_starts_with($authHeader'Bearer ')) {
  40.             $token substr($authHeader7);
  41.         }
  42.         /*if (!$token) {
  43.             throw new CustomUserMessageAuthenticationException('Token not provided');
  44.         }*/
  45. //var_dump($token);
  46.         $decodedData $this->jwtEncoder->decode($token);
  47. //var_dump($decodedData);
  48.         /*if (!$decodedData) {
  49.             throw new CustomUserMessageAuthenticationException('Invalid token');
  50.         }*/
  51.         // Here you could fetch the user using the decoded data, e.g., decodedData['username']
  52.         return new SelfValidatingPassport(
  53.             new UserBadge($decodedData['username'])
  54.         );
  55.     }
  56.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  57.     {
  58.         // No action needed on success, just return null to continue to the controller
  59.         return null;
  60.     }
  61.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  62.     {
  63.         return new JsonResponse(['error' => $exception->getMessageKey()], 401);
  64.     }
  65. }