<?php
namespace App\EventListener;
use App\Util\EsUtil;
use App\Service\EsCache;
use App\Entity\Container;
use App\Handler\AccessTokenHandler;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTEncodedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
class JWTCreatedListener
{
private $requestStack;
private $esCache;
private $accessTokenHandler;
private $_em;
public function __construct(RequestStack $requestStack, EsCache $esCache, AccessTokenHandler $accessTokenHandler, EntityManagerInterface $_em)
{
$this->requestStack = $requestStack;
$this->esCache = $esCache;
$this->accessTokenHandler = $accessTokenHandler;
$this->_em = $_em;
}
/**
* @param JWTCreatedEvent $event
*
* @return void
*/
public function onJWTCreated(JWTCreatedEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
$payload = $event->getData();
if (PHP_SAPI === 'cli' && isset($GLOBALS['container'])) {
$redirectContainerId = $GLOBALS['container']->getId();
$payload['cid'] = $GLOBALS['container']->getClient()->getId();
$payload['ip'] = 'EXPERTSHARE';
} else {
$redirectContainerId = $request->attributes->get('redirectContainerId', false);
$payload['ip'] = $request->getClientIp();
$client = $this->esCache->getClient();
if ($client) {
$payload['cid'] = $client->getId();
}
}
if ($redirectContainerId) {
$payload['cntid'] = (int)$redirectContainerId;
} else {
$container = $this->esCache->getContainer();
if ($container) {
$payload['cntid'] = (int)$container->getId();
}
}
$event->setData($payload);
}
/**
* @param JWTEncodedEvent $event
*/
public function onJwtEncoded(JWTEncodedEvent $event)
{
$prevRefreshToken = null;
if (PHP_SAPI === 'cli' && isset($GLOBALS['container'])) {
$redirectContainerId = $GLOBALS['container']->getId();
} else {
$request = $this->requestStack->getCurrentRequest();
$redirectContainerId = $request->attributes->get('redirectContainerId', false);
$prevRefreshToken = $request->headers->get('ES-REFRESH-TOKEN', null);
}
$token = $event->getJWTString();
$container = null;
if ($redirectContainerId) {
$container = $this->_em->getRepository(Container::class)->find($redirectContainerId);
}
$this->accessTokenHandler->create($token, null, $container, $prevRefreshToken);
}
/**
* @param JWTDecodedEvent $event
*
* @return void
*/
public function onJWTDecoded(JWTDecodedEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
$payload = $event->getPayload();
$container = $this->esCache->getContainer();
$token = substr($request->headers->get('Authorization'), 7);
if (!$this->accessTokenHandler->isValid($token) || $container->getId() !== $payload['cntid']) {
$event->markAsInvalid();
}
// $tokenDetails = EsUtil::decodeJWTPayloadOnly($token);
// $iat = new \DateTime();
// $iat->setTimestamp(($tokenDetails['iat'] + 60 * 60));
// $exp = new \DateTime();
// $exp->setTimestamp($tokenDetails['exp']);
// $ct = new \DateTime();
// if ($iat < $ct) {
// //if (!isset($payload['ip']) || ($payload['ip'] !== $request->getClientIp() && $payload['ip'] !== 'EXPERTSHARE') || $container->getId() !== $payload['cntid']) {
// if (!$this->accessTokenHandler->isValid($token) || $container->getId() !== $payload['cntid']) {
// $event->markAsInvalid();
// }
// } else {
// if ($exp < $ct || $container->getId() !== $payload['cntid']) {
// $event->markAsInvalid();
// }
// }
}
}