src/EventSubscriber/Api/AuthenticatorSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Api;
  3. use App\Constant\EmailConfiguration;
  4. use App\Constant\ErrorCode;
  5. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Lexik\Bundle\JWTAuthenticationBundle\Events as LexikEvents;
  10. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
  11. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  12. use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationSuccessResponse;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use App\AppEvents;
  16. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Mime\Address;
  19. use Symfony\Component\Mime\Email;
  20. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  21. use Symfony\Component\Security\Core\Exception\DisabledException;
  22. use Symfony\Component\Mailer\MailerInterface;
  23. class AuthenticatorSubscriber implements EventSubscriberInterface
  24. {
  25.     private $request;
  26.     private $em;
  27.     private $parameterBag;
  28.     public function __construct(
  29.         RequestStack $requestStack,
  30.         EntityManagerInterface $em,
  31.         ParameterBagInterface $parameterBag,
  32.         MailerInterface $mailer,
  33.         EventDispatcherInterface $dispatcher)
  34.     {
  35.         $this->request $requestStack->getCurrentRequest();
  36.         $this->em $em;
  37.         $this->parameterBag $parameterBag;
  38.     }
  39.     public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
  40.     {
  41.         $now = new \DateTime();
  42.         $ttl $this->parameterBag->get('lexik_jwt_authentication.token_ttl');
  43.         $event->setData(array_merge($event->getData(), [
  44.             'life_time' => $ttl,
  45.             'expire' => $now->getTimestamp() + $ttl
  46.         ]));
  47.     }
  48.     public function onAuthentificationExpired(JWTExpiredEvent $event)
  49.     {
  50.         $exception $event->getException();
  51.         $reponse $event->getResponse();
  52.         switch (get_class($exception)) {
  53.             case BadCredentialsException::class:
  54.                 /// Badcredential
  55.                 $reponse->setStatusCode(ErrorCode::JWT_BAD_CREDENTIAL);
  56.                 $reponse->setMessage([
  57.                     'en' => 'Username/Email  or Password invalid!',
  58.                     'fr' => 'Nom utilisateur/Email ou Mot de passe invalide!'
  59.                 ]);
  60.                 break;
  61.             case DisabledException::class:
  62.                 /// Compte desactiver pour licence ou autre cas
  63.                 $reponse->setStatusCode(ErrorCode::DISABLED_ACCOUNT);
  64.                 $reponse->setMessage([
  65.                     'en' => 'User account is disabled.',
  66.                     'fr' => 'Compte utilisateur dĂ©sactivĂ©.'
  67.                 ]);
  68.                 break;
  69.         }
  70.         $event->setResponse($reponse);
  71.     }
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             LexikEvents::JWT_EXPIRED => 'onAuthentificationExpired',
  76.             LexikEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
  77.         ];
  78.     }
  79. }