Token.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Util;
  29. use App\Core\Entity;
  30. use DateTimeImmutable;
  31. use DateTimeInterface;
  32. use Functional as F;
  33. use League\OAuth2\Server\Entities\ClientEntityInterface;
  34. use League\OAuth2\Server\Entities\ScopeEntityInterface;
  35. use League\OAuth2\Server\Entities\TokenInterface;
  36. use Plugin\OAuth2\Repository;
  37. /**
  38. * A type of token, needs to be extended.
  39. *
  40. * Since there's no way to specify an abstract method that returns a
  41. * child of self, need to use method annotations
  42. *
  43. * @template T of self
  44. *
  45. * @method T setId(string $id)
  46. * @method T setExpiry(\DateTimeInterface $expiry)
  47. * @method T setUserId(?int $id)
  48. * @method T setClientId(string $id)
  49. * @method T setTokenScopes(string $scopes)
  50. *
  51. * From Entity:
  52. * @method bool hasTokenScopes()
  53. */
  54. abstract class Token extends Entity implements TokenInterface
  55. {
  56. abstract public function getId(): string;
  57. // abstract public function setId(string $id): child;
  58. abstract public function getExpiry(): DateTimeInterface;
  59. // abstract public function setExpiry(\DateTimeInterface $expiry): child;
  60. abstract public function getUserId(): ?int;
  61. // abstract public function setUserId(?int $id): child;
  62. abstract public function getClientId(): string;
  63. // abstract public function setClientId(string $id): child;
  64. abstract public function getTokenScopes(): string;
  65. // abstract public function setTokenScopes(string $scopes): child;
  66. public function getIdentifier(): string
  67. {
  68. return $this->getId();
  69. }
  70. public function setIdentifier($identifier)
  71. {
  72. $this->setId($identifier);
  73. }
  74. /**
  75. * Get the token's expiry date time.
  76. */
  77. public function getExpiryDateTime(): DateTimeImmutable
  78. {
  79. return DateTimeImmutable::createFromInterface($this->getExpiry());
  80. }
  81. /**
  82. * Set the date time when the token expires.
  83. */
  84. public function setExpiryDateTime(DateTimeImmutable $dateTime)
  85. {
  86. $this->setExpiry($dateTime);
  87. }
  88. /**
  89. * Set the identifier of the user associated with the token.
  90. *
  91. * @param null|int|string $identifier The identifier of the user
  92. */
  93. public function setUserIdentifier($identifier)
  94. {
  95. $this->setUserId($identifier);
  96. }
  97. /**
  98. * Get the token user's identifier.
  99. */
  100. public function getUserIdentifier(): int|string|null
  101. {
  102. return $this->getUserId();
  103. }
  104. /**
  105. * Get the client that the token was issued to.
  106. */
  107. public function getClient(): ClientEntityInterface
  108. {
  109. return (new Repository\Client)->getClientEntity($this->getClientId());
  110. }
  111. /**
  112. * Set the client that the token was issued to.
  113. */
  114. public function setClient(ClientEntityInterface $client)
  115. {
  116. $this->setClientId($client->getIdentifier());
  117. }
  118. /**
  119. * Associate a scope with the token.
  120. */
  121. public function addScope(ScopeEntityInterface $scope)
  122. {
  123. $scope = $this->hasTokenScopes() ? $this->getTokenScopes() . ' ' . $scope->getIdentifier() : $scope->getIdentifier();
  124. $this->setTokenScopes($scope);
  125. }
  126. /**
  127. * Return an array of scopes associated with the token.
  128. *
  129. * @return ScopeEntityInterface[]
  130. */
  131. public function getScopes(): array
  132. {
  133. return F\map(
  134. explode(' ', $this->getTokenScopes()),
  135. fn (string $scope) => (new Repository\Scope)->getScopeEntityByIdentifier($scope),
  136. );
  137. }
  138. public static function tokenSchema(string $table_name): array
  139. {
  140. return [
  141. 'name' => $table_name,
  142. 'fields' => [
  143. 'id' => ['type' => 'char', 'length' => 64, 'not null' => true, 'description' => 'identifier for this token'],
  144. 'expiry' => ['type' => 'datetime', 'not null' => true, 'description' => 'when this token expires'],
  145. 'user_id' => ['type' => 'int', 'foreign key' => true, 'description' => 'Actor foreign key'],
  146. 'client_id' => ['type' => 'char', 'length' => 64, 'not null' => true, 'foreign key' => true, 'description' => 'OAuth client foreign key'],
  147. 'token_scopes' => ['type' => 'text', 'not null' => true, 'description' => 'Space separated scopes'],
  148. 'revoked' => ['type' => 'bool', 'not null' => true, 'foreign key' => true, 'description' => 'Whether this token is revoked'],
  149. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  150. ],
  151. 'primary key' => ['id'],
  152. ];
  153. }
  154. }