RefreshToken.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * OAuth2 Client
  21. *
  22. * @package GNUsocial
  23. *
  24. * @author Hugo Sales <hugo@hsal.es>
  25. * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. namespace Plugin\OAuth2\Entity;
  29. use App\Core\Entity;
  30. use DateTimeImmutable;
  31. use DateTimeInterface;
  32. use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
  33. use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
  34. use Plugin\OAuth2\Repository;
  35. class RefreshToken extends Entity implements RefreshTokenEntityInterface
  36. {
  37. // {{{ Autocode
  38. // @codeCoverageIgnoreStart
  39. private string $id;
  40. private DateTimeInterface $expiry;
  41. private ?string $access_token_id = null;
  42. private bool $revoked;
  43. private DateTimeInterface $created;
  44. public function setId(string $id): self
  45. {
  46. $this->id = mb_substr($id, 0, 64);
  47. return $this;
  48. }
  49. public function getId(): string
  50. {
  51. return $this->id;
  52. }
  53. public function setExpiry(DateTimeInterface $expiry): self
  54. {
  55. $this->expiry = $expiry;
  56. return $this;
  57. }
  58. public function getExpiry(): DateTimeInterface
  59. {
  60. return $this->expiry;
  61. }
  62. public function setAccessTokenId(?string $access_token_id): self
  63. {
  64. $this->access_token_id = \is_null($access_token_id) ? null : mb_substr($access_token_id, 0, 64);
  65. return $this;
  66. }
  67. public function getAccessTokenId(): ?string
  68. {
  69. return $this->access_token_id;
  70. }
  71. public function setRevoked(bool $revoked): self
  72. {
  73. $this->revoked = $revoked;
  74. return $this;
  75. }
  76. public function getRevoked(): bool
  77. {
  78. return $this->revoked;
  79. }
  80. public function setCreated(DateTimeInterface $created): self
  81. {
  82. $this->created = $created;
  83. return $this;
  84. }
  85. public function getCreated(): DateTimeInterface
  86. {
  87. return $this->created;
  88. }
  89. // @codeCoverageIgnoreEnd
  90. // }}} Autocode
  91. /**
  92. * Get the access token that the refresh token was originally associated with.
  93. */
  94. public function getAccessToken(): AccessTokenEntityInterface
  95. {
  96. return (new Repository\AccessToken())->getAccessTokenEntity($this->getAccessTokenId());
  97. }
  98. /**
  99. * Set the access token that the refresh token was associated with.
  100. */
  101. public function setAccessToken(AccessTokenEntityInterface $accessToken)
  102. {
  103. $this->setAccessTokenId($accessToken->getIdentifier());
  104. }
  105. public function setIdentifier($identifier): self
  106. {
  107. return $this->setId($identifier);
  108. }
  109. public function getIdentifier(): string
  110. {
  111. return $this->getId();
  112. }
  113. /**
  114. * Set the date time when the token expires.
  115. */
  116. public function setExpiryDateTime(DateTimeImmutable $dateTime)
  117. {
  118. $this->setExpiry($dateTime);
  119. }
  120. public function getExpiryDateTime(): DateTimeImmutable
  121. {
  122. return DateTimeImmutable::createFromInterface($this->getExpiry());
  123. }
  124. public static function schemaDef(): array
  125. {
  126. return [
  127. 'name' => 'oauth2_refresh_token',
  128. 'fields' => [
  129. 'id' => ['type' => 'char', 'length' => 64, 'not null' => true, 'description' => 'identifier for this token'],
  130. 'expiry' => ['type' => 'datetime', 'not null' => true, 'description' => 'when this token expires'],
  131. 'access_token_id' => ['type' => 'char', 'length' => 64, 'foreign key' => true, 'description' => 'Actor foreign key'],
  132. 'revoked' => ['type' => 'bool', 'not null' => true, 'foreign key' => true, 'description' => 'Whether this token is revoked'],
  133. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  134. ],
  135. 'primary key' => ['id'],
  136. ];
  137. }
  138. }