src/Api/Security/EventListener/AttachRefreshTokenOnSuccessListener.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Api\Security\EventListener;
  3. use Jenssegers\Agent\Agent;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Security\Http\HttpUtils;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use App\Api\Security\Exception\UserNotMatchException;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
  12. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  13. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
  14. use Gesdinet\JWTRefreshTokenBundle\Request\Extractor\ExtractorInterface;
  15. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  16. use Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGeneratorInterface;
  17. use Gesdinet\JWTRefreshTokenBundle\EventListener\AttachRefreshTokenOnSuccessListener as EventListenerAttachRefreshTokenOnSuccessListener;
  18. use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
  19. class AttachRefreshTokenOnSuccessListener extends EventListenerAttachRefreshTokenOnSuccessListener
  20. {
  21.     private ?string $token null;
  22.     public function __construct(
  23.         RefreshTokenManagerInterface $refreshTokenManager,
  24.         $ttl,
  25.         RequestStack $requestStack,
  26.         $tokenParameterName,
  27.         $singleUse,
  28.         RefreshTokenGeneratorInterface $refreshTokenGenerator,
  29.         ExtractorInterface $extractor,
  30.         array $cookieSettings,
  31.         bool $returnExpiration,
  32.         string $returnExpirationParameterName,
  33.         private JWTEncoderInterface $jWTEncoder
  34.     ) {
  35.         parent::__construct(
  36.             $refreshTokenManager,
  37.             $ttl,
  38.             $requestStack,
  39.             $tokenParameterName,
  40.             $singleUse,
  41.             $refreshTokenGenerator,
  42.             $extractor,
  43.             $cookieSettings,
  44.             $returnExpiration,
  45.             $returnExpirationParameterName
  46.         );
  47.     }
  48.     public function attachRefreshToken(AuthenticationSuccessEvent $event): void
  49.     {
  50.        parent::attachRefreshToken($event);
  51.         $data $event->getData();
  52.         if (!empty($data['token'])) {
  53.            $decodeToken $this->jWTEncoder->decode($data['token']);
  54.             if (!empty($decodeToken['exp'])) {
  55.                 $data['token_expiration'] = (new \DateTime())->setTimestamp($decodeToken['exp'])->format('Y-m-d\TH:i:sP');
  56.             }
  57.         }
  58.         if (!empty($data['refresh_token_expiration'])) {
  59.             $data['refresh_token_expiration'] = (new \DateTime())->setTimestamp($data['refresh_token_expiration'])->format('Y-m-d\TH:i:sP');
  60.         }
  61.         
  62.         $event->setData($data);
  63.     }
  64. }