src/Security/Voter/Resource/TokenRefreshVoter.php line 12
<?php
namespace App\Security\Voter\Resource;
use Symfony\Component\Security\Core\Authorization\Voter\Voter,
Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use App\DTO\TokenRefreshDTO,
App\Lib\Actions;
final class TokenRefreshVoter extends Voter
{
const ACTIVITY_CHECK = '10 mins';
const MESSAGE = 'Expired. No activity in the last ' . self::ACTIVITY_CHECK;
private JWTTokenManagerInterface $jwtManager;
public function __construct(JWTTokenManagerInterface $jwtManager)
{
$this->jwtManager = $jwtManager;
}
protected function supports($attribute, $subject): bool
{
if (! $subject instanceof TokenRefreshDTO || Actions::VIEW !== $attribute) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$currentToken = $this->jwtManager->decode($token);
$issuedAt = new \DateTime("@{$currentToken['iat']}");
$issuedAt->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$checkFrom = new \DateTimeImmutable('-' . self::ACTIVITY_CHECK);
if ($checkFrom < $issuedAt) {
$checkFrom = $issuedAt;
}
/** @var Admin */
$admin = $token->getUser();
if ($admin->getLastActivityAt() >= $checkFrom) {
return true;
}
return false;
}
}