src/Entity/RefreshToken.php line 30

  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\User;
  4. use App\Repository\RefreshTokenRepository;
  5. use App\Security\Model\BelongUser;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Gedmo\Blameable\Traits\BlameableEntity;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Symfony\Component\Serializer\Annotation\Groups;
  10. use ApiPlatform\Metadata\ApiResource;
  11. use ApiPlatform\Metadata\Post;
  12. use App\Controller\Api\Auth\RefreshController;
  13. use ApiPlatform\Metadata\Get;
  14. use DateTime;
  15. #[ORM\Entity(RefreshTokenRepository::class)]
  16. #[ORM\Table(name'`RefreshToken`')]
  17. #[ApiResource(
  18.     operations: [
  19.         new Get(),
  20.         new Post(
  21.             uriTemplate'/refresh_tokens/refresh',
  22.             controllerRefreshController::class,
  23.             normalizationContext: ['groups' => ['token_get''token_get_login']],
  24.             denormalizationContext: ['groups' => ['token_post_refresh']]
  25.         )
  26.     ]
  27. )]
  28. class RefreshToken implements BelongUser{
  29.     use BlameableEntity;
  30.     use TimestampableEntity;
  31.     #[ORM\Column(type'string'length128)]
  32.     #[ORM\Id()]
  33.     #[Groups([
  34.         'token_get_login',
  35.         'user_get_me',
  36.     ])]
  37.     private ?string $id;
  38.     #[ORM\Column(type"integer")]
  39.     #[Groups([
  40.         'token_get_login',
  41.         'user_get_me',
  42.     ])]
  43.     private ?int $createTime;
  44.     #[ORM\Column(type"integer"nullabletrue)]
  45.     #[Groups([
  46.         'token_get_login',
  47.         'user_get_me',
  48.     ])]
  49.     private ?int $lifetime;
  50.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy"refreshTokens")]
  51.     #[ORM\JoinColumn(nullabletrue)]
  52.     private ?User $user;
  53.     #[ORM\OneToOne(mappedBy'refreshToken'targetEntityToken::class, cascade: [ 'remove' ])]
  54.     private ?Token $token;
  55.     public function __construct(?User $user null, ?int $lifetime null) {
  56.         $this->id         'REFRESH_'.bin2hex(random_bytes(20));
  57.         $this->user       $user;
  58.         $this->createdAt = new DateTime();
  59.         $this->updatedAt = new DateTime();
  60.         $this->createTime time();
  61.         $this->lifetime   $lifetime;
  62.         if ($user) {
  63.             $user->addRefreshToken($this);
  64.         }
  65.     }
  66.     public function getId(): string {
  67.         return $this->id;
  68.     }
  69.     public function getCreateTime(): int {
  70.         return $this->createTime;
  71.     }
  72.     public function getLifetime(): ?int {
  73.         return $this->lifetime;
  74.     }
  75.     public function getUser(): User {
  76.         return $this->user;
  77.     }
  78.     public function getToken(): ?Token {
  79.         return $this->token;
  80.     }
  81.     public function isValid(): bool {
  82.         return
  83.             $this->lifetime !== null ||
  84.             time() <= $this->createTime $this->lifetime
  85.         ;
  86.     }
  87. }