<?php
namespace App\Api\Security\EventListener;
use Jenssegers\Agent\Agent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Api\Security\Exception\UserNotMatchException;
use Symfony\Component\Security\Core\User\UserInterface;
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
use Gesdinet\JWTRefreshTokenBundle\Request\Extractor\ExtractorInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGeneratorInterface;
use Gesdinet\JWTRefreshTokenBundle\EventListener\AttachRefreshTokenOnSuccessListener as EventListenerAttachRefreshTokenOnSuccessListener;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
class AttachRefreshTokenOnSuccessListener extends EventListenerAttachRefreshTokenOnSuccessListener
{
private ?string $token = null;
public function __construct(
RefreshTokenManagerInterface $refreshTokenManager,
$ttl,
RequestStack $requestStack,
$tokenParameterName,
$singleUse,
RefreshTokenGeneratorInterface $refreshTokenGenerator,
ExtractorInterface $extractor,
array $cookieSettings,
bool $returnExpiration,
string $returnExpirationParameterName,
private JWTEncoderInterface $jWTEncoder
) {
parent::__construct(
$refreshTokenManager,
$ttl,
$requestStack,
$tokenParameterName,
$singleUse,
$refreshTokenGenerator,
$extractor,
$cookieSettings,
$returnExpiration,
$returnExpirationParameterName
);
}
public function attachRefreshToken(AuthenticationSuccessEvent $event): void
{
parent::attachRefreshToken($event);
$data = $event->getData();
if (!empty($data['token'])) {
$decodeToken = $this->jWTEncoder->decode($data['token']);
if (!empty($decodeToken['exp'])) {
$data['token_expiration'] = (new \DateTime())->setTimestamp($decodeToken['exp'])->format('Y-m-d\TH:i:sP');
}
}
if (!empty($data['refresh_token_expiration'])) {
$data['refresh_token_expiration'] = (new \DateTime())->setTimestamp($data['refresh_token_expiration'])->format('Y-m-d\TH:i:sP');
}
$event->setData($data);
}
}