src/EventListener/RefreshTokenValidator.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Security\User;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. class RefreshTokenValidator
  12. {
  13.     /** @var UrlGeneratorInterface */
  14.     protected $urlGenerator;
  15.     /** @var TokenStorageInterface */
  16.     protected $token;
  17.     /** @var FlashBagInterface */
  18.     protected $flashBag;
  19.     /** @var Security */
  20.     protected $security;
  21.     /** @var SessionInterface */
  22.     protected $session;
  23.     /**
  24.      * DbLoader constructor.
  25.      *
  26.      * @param UrlGeneratorInterface $urlGenerator
  27.      * @param TokenStorageInterface $token
  28.      */
  29.     public function __construct(TokenStorageInterface $tokenUrlGeneratorInterface $urlGeneratorFlashBagInterface $flashBagSecurity $securitySessionInterface $session)
  30.     {
  31.         $this->token        $token;
  32.         $this->urlGenerator $urlGenerator;
  33.         $this->flashBag     $flashBag;
  34.         $this->security $security;
  35.         $this->session $session;
  36.     }
  37.     /**
  38.      * @param GetResponseEvent $event
  39.      */
  40.     public function onKernelRequest(GetResponseEvent $event)
  41.     {
  42.         $token $this->token->getToken();
  43.         if (!empty($token) && $event->getRequest()->get('_route') != 'app_login')
  44.         {
  45.             /** @var User $user */
  46.             $user $token->getUser();
  47.             if ($user instanceof User && $user->getToken()->isRefreshFailed())
  48.             {
  49.                 /* Add prefix to make this messages translatable */
  50.                 $errMsg sprintf('login.%s''Invalid refresh token');
  51.                 $this->flashBag->add('danger'$errMsg);
  52.                 $this->security->getToken()->setAuthenticated(false);
  53.                 /* No session, no flashbag. */
  54. //            $this->session->invalidate();
  55.                 $event->setResponse(new RedirectResponse($this->urlGenerator->generate('app_login')));
  56.             }
  57.         }
  58.     }
  59. }