<?php
declare(strict_types=1);
namespace App\Entity;
use App\Repository\RefreshTokenRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Jeton de renouvellement de session de l'app (#3841) : échangé contre un nouveau JWT par POST /api/token/refresh.
*
* Seul le SHA-256 du jeton est stocké, jamais le jeton lui-même. Lié par user_id (un e-mail n'est unique que par
* (email, category)). Une famille = une connexion sur un appareil : chaque usage révoque le jeton (« rotated ») et en
* émet un nouveau dans la même famille ; présenter un jeton déjà tourné révoque toute la famille (« reuse_detected »).
*
* @ORM\Entity(repositoryClass=RefreshTokenRepository::class)
* @ORM\Table(name="refresh_token",
* uniqueConstraints={@ORM\UniqueConstraint(name="uniq_refresh_token_hash", columns={"token_hash"})},
* indexes={
* @ORM\Index(name="idx_refresh_token_user", columns={"user_id"}),
* @ORM\Index(name="idx_refresh_token_family", columns={"family"}),
* @ORM\Index(name="idx_refresh_token_expires", columns={"expires_at"})
* }
* )
*/
class RefreshToken
{
public const REASON_ROTATED = 'rotated';
public const REASON_LOGOUT = 'logout';
public const REASON_PASSWORD_CHANGED = 'password_changed';
public const REASON_ACCOUNT_DELETED = 'account_deleted';
public const REASON_REUSE_DETECTED = 'reuse_detected';
public const DEVICES = ['ios', 'android'];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* Compte auquel la session appartient
*
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private User $user;
/**
* SHA-256 (hex) du jeton ; le jeton lui-même n'est jamais stocké
*
* @ORM\Column(type="string", length=64, options={"fixed": true})
*/
private string $tokenHash;
/**
* Une connexion sur un appareil ; révoquée en bloc si un jeton tourné est présenté de nouveau
*
* @ORM\Column(type="string", length=32, options={"fixed": true})
*/
private string $family;
/**
* ios | android (User-Agent posé par l'app), sinon null
*
* @ORM\Column(type="string", length=20, nullable=true)
*/
private ?string $device;
/**
* Fin de validité ; chaque rotation repart pour la durée complète (JWT_REFRESH_TTL)
*
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $expiresAt;
/**
* Date de révocation (rotation, déconnexion, mot de passe, suppression, réutilisation), null si encore valable
*
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $revokedAt = null;
/**
* Raison de la révocation : une des constantes REASON_*
*
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $revokedReason = null;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private ?\DateTimeInterface $createdAt = null;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable
*/
private ?\DateTimeInterface $updatedAt = null;
public function __construct(User $user, string $tokenHash, string $family, \DateTimeInterface $expiresAt, ?string $device = null)
{
$this->user = $user;
$this->tokenHash = $tokenHash;
$this->family = $family;
$this->expiresAt = $expiresAt;
$this->device = $device;
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): User
{
return $this->user;
}
public function getTokenHash(): string
{
return $this->tokenHash;
}
public function getFamily(): string
{
return $this->family;
}
public function getDevice(): ?string
{
return $this->device;
}
public function getExpiresAt(): \DateTimeInterface
{
return $this->expiresAt;
}
public function isExpired(\DateTimeInterface $now): bool
{
return $this->expiresAt <= $now;
}
public function getRevokedAt(): ?\DateTimeInterface
{
return $this->revokedAt;
}
public function getRevokedReason(): ?string
{
return $this->revokedReason;
}
public function isRevoked(): bool
{
return null !== $this->revokedAt;
}
public function revoke(string $reason, \DateTimeInterface $at): self
{
$this->revokedAt = $at;
$this->revokedReason = $reason;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
}