vendor/appaydin/pd-api/Listener/JWTExceptionListener.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the pd-admin pd-api package.
  4.  *
  5.  * @package     pd-api
  6.  * @license     LICENSE
  7.  * @author      Ramazan APAYDIN <apaydin541@gmail.com>
  8.  * @link        https://github.com/appaydin/pd-api
  9.  */
  10. namespace Pd\ApiBundle\Listener;
  11. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  12. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
  13. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
  14. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTNotFoundEvent;
  15. use Lexik\Bundle\JWTAuthenticationBundle\Events;
  16. use Pd\ApiBundle\Services\AcceptContentType;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Serializer\SerializerInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. class JWTExceptionListener implements EventSubscriberInterface
  22. {
  23.     public function __construct(
  24.         private SerializerInterface $serializer,
  25.         private TranslatorInterface $translator,
  26.         private AcceptContentType $accept)
  27.     {
  28.     }
  29.     public function onJWTAuthFailure(AuthenticationFailureEvent $event): void
  30.     {
  31.         $event->setResponse($this->handleException($event->getResponse(), (new \ReflectionClass($event->getException()))->getShortName()));
  32.     }
  33.     public function onJWTInvalid(JWTInvalidEvent $event): void
  34.     {
  35.         $event->setResponse($this->handleException($event->getResponse(), 'InvalidTokenException'));
  36.     }
  37.     public function onJWTExpired(JWTExpiredEvent $event): void
  38.     {
  39.         $event->setResponse($this->handleException($event->getResponse(), 'ExpiredTokenException'));
  40.     }
  41.     public function onJWTNotFound(JWTNotFoundEvent $event): void
  42.     {
  43.         $event->setResponse($this->handleException($event->getResponse(), 'MissingTokenException'));
  44.     }
  45.     private function handleException(Response $responsestring $type): Response
  46.     {
  47.         // Create Exception Response
  48.         $message json_decode($response->getContent(), true)['message'];
  49.         $response->setContent($this->serializer->serialize([
  50.             'message' => $this->translator->trans($message, [], 'exception'),
  51.             'code' => $response->getStatusCode(),
  52.             'type' => $type,
  53.         ], $this->accept->getAcceptType()));
  54.         // Set Accept Content Type
  55.         $response->headers->set('Content-Type''application/'.$this->accept->getAcceptType());
  56.         return $response;
  57.     }
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             Events::AUTHENTICATION_FAILURE => [['onJWTAuthFailure']],
  62.             Events::JWT_INVALID => [['onJWTInvalid']],
  63.             Events::JWT_EXPIRED => [['onJWTExpired']],
  64.             Events::JWT_NOT_FOUND => [['onJWTNotFound']],
  65.         ];
  66.     }
  67. }