vendor/nellapp/sdk-bundle/src/Auth/Security/OAuth/HandleExpireTokenSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the nellapp-core package.
  4.  *
  5.  * (c) Benjamin Georgeault
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nellapp\Bundle\SDKBundle\Auth\Security\OAuth;
  11. use Nellapp\Bundle\SDKBundle\Auth\Controller\Security\LogoutAction;
  12. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpKernel\Event\RequestEvent;
  16. use Symfony\Component\HttpKernel\HttpKernelInterface;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. /**
  21.  * For now, User must be logged out if there token expired.
  22.  * TODO LATER Auto refresh token.
  23.  *
  24.  * @author Benjamin Georgeault
  25.  */
  26. class HandleExpireTokenSubscriber implements EventSubscriberInterface
  27. {
  28.     public function __construct(
  29.         private Security $security,
  30.         private UrlGeneratorInterface $urlGenerator,
  31.     ) {}
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             KernelEvents::REQUEST => 'onKernelRequest',
  36.         ];
  37.     }
  38.     public function onKernelRequest(RequestEvent $event)
  39.     {
  40.         if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
  41.             return;
  42.         }
  43.         $user $this->security->getUser();
  44.         if (!($user instanceof UserInterface) || (null === $token $user->getToken()) || !$token->isExpired()) {
  45.             return;
  46.         }
  47.         $event->setResponse(new RedirectResponse($this->urlGenerator->generate(LogoutAction::ROUTE_NAME)));
  48.     }
  49. }