src/Entity/RefreshToken.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\RefreshTokenRepository;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. /**
  8.  * Jeton de renouvellement de session de l'app (#3841) : échangé contre un nouveau JWT par POST /api/token/refresh.
  9.  *
  10.  * 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
  11.  * (email, category)). Une famille = une connexion sur un appareil : chaque usage révoque le jeton (« rotated ») et en
  12.  * émet un nouveau dans la même famille ; présenter un jeton déjà tourné révoque toute la famille (« reuse_detected »).
  13.  *
  14.  * @ORM\Entity(repositoryClass=RefreshTokenRepository::class)
  15.  * @ORM\Table(name="refresh_token",
  16.  *     uniqueConstraints={@ORM\UniqueConstraint(name="uniq_refresh_token_hash", columns={"token_hash"})},
  17.  *     indexes={
  18.  *         @ORM\Index(name="idx_refresh_token_user", columns={"user_id"}),
  19.  *         @ORM\Index(name="idx_refresh_token_family", columns={"family"}),
  20.  *         @ORM\Index(name="idx_refresh_token_expires", columns={"expires_at"})
  21.  *     }
  22.  * )
  23.  */
  24. class RefreshToken
  25. {
  26.     public const REASON_ROTATED = 'rotated';
  27.     public const REASON_LOGOUT = 'logout';
  28.     public const REASON_PASSWORD_CHANGED = 'password_changed';
  29.     public const REASON_ACCOUNT_DELETED = 'account_deleted';
  30.     public const REASON_REUSE_DETECTED = 'reuse_detected';
  31.     public const DEVICES = ['ios', 'android'];
  32.     /**
  33.      * @ORM\Id
  34.      * @ORM\GeneratedValue
  35.      * @ORM\Column(type="integer")
  36.      */
  37.     private ?int $id = null;
  38.     /**
  39.      * Compte auquel la session appartient
  40.      *
  41.      * @ORM\ManyToOne(targetEntity=User::class)
  42.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  43.      */
  44.     private User $user;
  45.     /**
  46.      * SHA-256 (hex) du jeton ; le jeton lui-même n'est jamais stocké
  47.      *
  48.      * @ORM\Column(type="string", length=64, options={"fixed": true})
  49.      */
  50.     private string $tokenHash;
  51.     /**
  52.      * Une connexion sur un appareil ; révoquée en bloc si un jeton tourné est présenté de nouveau
  53.      *
  54.      * @ORM\Column(type="string", length=32, options={"fixed": true})
  55.      */
  56.     private string $family;
  57.     /**
  58.      * ios | android (User-Agent posé par l'app), sinon null
  59.      *
  60.      * @ORM\Column(type="string", length=20, nullable=true)
  61.      */
  62.     private ?string $device;
  63.     /**
  64.      * Fin de validité ; chaque rotation repart pour la durée complète (JWT_REFRESH_TTL)
  65.      *
  66.      * @ORM\Column(type="datetime")
  67.      */
  68.     private \DateTimeInterface $expiresAt;
  69.     /**
  70.      * Date de révocation (rotation, déconnexion, mot de passe, suppression, réutilisation), null si encore valable
  71.      *
  72.      * @ORM\Column(type="datetime", nullable=true)
  73.      */
  74.     private ?\DateTimeInterface $revokedAt = null;
  75.     /**
  76.      * Raison de la révocation : une des constantes REASON_*
  77.      *
  78.      * @ORM\Column(type="string", length=32, nullable=true)
  79.      */
  80.     private ?string $revokedReason = null;
  81.     /**
  82.      * @ORM\Column(type="datetime")
  83.      * @Gedmo\Timestampable(on="create")
  84.      */
  85.     private ?\DateTimeInterface $createdAt = null;
  86.     /**
  87.      * @ORM\Column(type="datetime", nullable=true)
  88.      * @Gedmo\Timestampable
  89.      */
  90.     private ?\DateTimeInterface $updatedAt = null;
  91.     public function __construct(User $user, string $tokenHash, string $family, \DateTimeInterface $expiresAt, ?string $device = null)
  92.     {
  93.         $this->user = $user;
  94.         $this->tokenHash = $tokenHash;
  95.         $this->family = $family;
  96.         $this->expiresAt = $expiresAt;
  97.         $this->device = $device;
  98.     }
  99.     public function getId(): ?int
  100.     {
  101.         return $this->id;
  102.     }
  103.     public function getUser(): User
  104.     {
  105.         return $this->user;
  106.     }
  107.     public function getTokenHash(): string
  108.     {
  109.         return $this->tokenHash;
  110.     }
  111.     public function getFamily(): string
  112.     {
  113.         return $this->family;
  114.     }
  115.     public function getDevice(): ?string
  116.     {
  117.         return $this->device;
  118.     }
  119.     public function getExpiresAt(): \DateTimeInterface
  120.     {
  121.         return $this->expiresAt;
  122.     }
  123.     public function isExpired(\DateTimeInterface $now): bool
  124.     {
  125.         return $this->expiresAt <= $now;
  126.     }
  127.     public function getRevokedAt(): ?\DateTimeInterface
  128.     {
  129.         return $this->revokedAt;
  130.     }
  131.     public function getRevokedReason(): ?string
  132.     {
  133.         return $this->revokedReason;
  134.     }
  135.     public function isRevoked(): bool
  136.     {
  137.         return null !== $this->revokedAt;
  138.     }
  139.     public function revoke(string $reason, \DateTimeInterface $at): self
  140.     {
  141.         $this->revokedAt = $at;
  142.         $this->revokedReason = $reason;
  143.         return $this;
  144.     }
  145.     public function getCreatedAt(): ?\DateTimeInterface
  146.     {
  147.         return $this->createdAt;
  148.     }
  149.     public function getUpdatedAt(): ?\DateTimeInterface
  150.     {
  151.         return $this->updatedAt;
  152.     }
  153. }