src/Entity/RefreshToken.php line 30
<?php
namespace App\Entity;
use App\Entity\User;
use App\Repository\RefreshTokenRepository;
use App\Security\Model\BelongUser;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Blameable\Traits\BlameableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\Controller\Api\Auth\RefreshController;
use ApiPlatform\Metadata\Get;
use DateTime;
#[ORM\Entity(RefreshTokenRepository::class)]
#[ORM\Table(name: '`RefreshToken`')]
#[ApiResource(
operations: [
new Get(),
new Post(
uriTemplate: '/refresh_tokens/refresh',
controller: RefreshController::class,
normalizationContext: ['groups' => ['token_get', 'token_get_login']],
denormalizationContext: ['groups' => ['token_post_refresh']]
)
]
)]
class RefreshToken implements BelongUser{
use BlameableEntity;
use TimestampableEntity;
#[ORM\Column(type: 'string', length: 128)]
#[ORM\Id()]
#[Groups([
'token_get_login',
'user_get_me',
])]
private ?string $id;
#[ORM\Column(type: "integer")]
#[Groups([
'token_get_login',
'user_get_me',
])]
private ?int $createTime;
#[ORM\Column(type: "integer", nullable: true)]
#[Groups([
'token_get_login',
'user_get_me',
])]
private ?int $lifetime;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: "refreshTokens")]
#[ORM\JoinColumn(nullable: true)]
private ?User $user;
#[ORM\OneToOne(mappedBy: 'refreshToken', targetEntity: Token::class, cascade: [ 'remove' ])]
private ?Token $token;
public function __construct(?User $user = null, ?int $lifetime = null) {
$this->id = 'REFRESH_'.bin2hex(random_bytes(20));
$this->user = $user;
$this->createdAt = new DateTime();
$this->updatedAt = new DateTime();
$this->createTime = time();
$this->lifetime = $lifetime;
if ($user) {
$user->addRefreshToken($this);
}
}
public function getId(): string {
return $this->id;
}
public function getCreateTime(): int {
return $this->createTime;
}
public function getLifetime(): ?int {
return $this->lifetime;
}
public function getUser(): User {
return $this->user;
}
public function getToken(): ?Token {
return $this->token;
}
public function isValid(): bool {
return
$this->lifetime !== null ||
time() <= $this->createTime + $this->lifetime
;
}
}