OAuth2Client.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * ActivityPub implementation for GNU social
  21. *
  22. * @package GNUsocial
  23. * @category OAuth2
  24. *
  25. * @author Diogo Peralta Cordeiro <@diogo.site>
  26. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace Plugin\IndieAuth\Entity;
  30. use App\Core\Entity;
  31. use DateTimeInterface;
  32. /**
  33. * OAuth application registration record
  34. *
  35. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class OAuth2Client extends Entity
  39. {
  40. // {{{ Autocode
  41. // @codeCoverageIgnoreStart
  42. private string $identifier;
  43. private ?string $secret;
  44. private string $redirect_uris = '';
  45. private string $grants = '';
  46. private string $scopes = '';
  47. private bool $active = true;
  48. private bool $allow_plain_text_pkce = false;
  49. private ?string $client_name = null;
  50. private ?string $website = null;
  51. private DateTimeInterface $created;
  52. private DateTimeInterface $modified;
  53. public function __toString(): string
  54. {
  55. return $this->getIdentifier();
  56. }
  57. public function getIdentifier(): string
  58. {
  59. return $this->identifier;
  60. }
  61. public function getSecret(): ?string
  62. {
  63. return $this->secret;
  64. }
  65. public function setSecret(?string $secret): self
  66. {
  67. $this->secret = $secret;
  68. return $this;
  69. }
  70. public function getRedirectUris(): array
  71. {
  72. return explode(' ', $this->redirect_uris);
  73. }
  74. public function setRedirectUris(string ...$redirect_uris): self
  75. {
  76. $this->redirect_uris = implode(' ', $redirect_uris);
  77. return $this;
  78. }
  79. public function getGrants(): array
  80. {
  81. return explode(' ', $this->grants);
  82. }
  83. public function setGrants(string ...$grants): self
  84. {
  85. $this->grants = implode(' ', $grants);
  86. return $this;
  87. }
  88. public function getScopes(): array
  89. {
  90. return explode(' ', $this->scopes);
  91. }
  92. public function setScopes(string ...$scopes): self
  93. {
  94. $this->scopes = implode(' ', $scopes);
  95. return $this;
  96. }
  97. public function isActive(): bool
  98. {
  99. return $this->active;
  100. }
  101. public function setActive(bool $active): self
  102. {
  103. $this->active = $active;
  104. return $this;
  105. }
  106. public function isConfidential(): bool
  107. {
  108. return !empty($this->secret);
  109. }
  110. public function isPlainTextPkceAllowed(): bool
  111. {
  112. return $this->allow_plain_text_pkce;
  113. }
  114. public function setAllowPlainTextPkce(bool $allow_plain_text_pkce): self
  115. {
  116. $this->allow_plain_text_pkce = $allow_plain_text_pkce;
  117. return $this;
  118. }
  119. public function setIdentifier(string $identifier): self
  120. {
  121. $this->identifier = $identifier;
  122. return $this;
  123. }
  124. public function getClientName(): string
  125. {
  126. return $this->client_name;
  127. }
  128. public function setClientName(string $client_name): self
  129. {
  130. $this->client_name = $client_name;
  131. return $this;
  132. }
  133. public function getWebsite(): ?string
  134. {
  135. return $this->website;
  136. }
  137. public function setWebsite(?string $website): self
  138. {
  139. $this->website = $website;
  140. return $this;
  141. }
  142. public function setCreated(DateTimeInterface $created): self
  143. {
  144. $this->created = $created;
  145. return $this;
  146. }
  147. public function getCreated(): DateTimeInterface
  148. {
  149. return $this->created;
  150. }
  151. public function setModified(DateTimeInterface $modified): self
  152. {
  153. $this->modified = $modified;
  154. return $this;
  155. }
  156. public function getModified(): DateTimeInterface
  157. {
  158. return $this->modified;
  159. }
  160. // @codeCoverageIgnoreEnd
  161. // }}} Autocode
  162. /**
  163. * Return table definition for Schema setup and Entity usage.
  164. *
  165. * @return array array of column definitions
  166. */
  167. public static function schemaDef(): array
  168. {
  169. return [
  170. 'name' => 'oauth2_client',
  171. 'fields' => [
  172. 'identifier' => ['type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'foreign key to oauth2_client->identifier'],
  173. 'secret' => ['type' => 'varchar', 'length' => 128, 'not null' => false, 'description' => 'foreign key to oauth2_client->identifier'],
  174. 'client_name' => ['type' => 'varchar', 'length' => 191, 'not null' => false, 'description' => 'name of the application'],
  175. 'redirect_uris' => ['type' => 'text', 'not null' => false, 'description' => 'application homepage - used for source link'],
  176. 'grants' => ['type' => 'text', 'not null' => true, 'default' => '', 'description' => 'application homepage - used for source link'],
  177. 'scopes' => ['type' => 'text', 'not null' => true, 'default' => '', 'description' => 'application homepage - used for source link'],
  178. 'active' => ['type' => 'bool', 'not null' => true, 'description' => 'was this note generated by a local actor'],
  179. 'allow_plain_text_pkce' => ['type' => 'bool', 'not null' => true, 'default' => false, 'description' => 'was this note generated by a local actor'],
  180. 'website' => ['type' => 'text', 'not null' => false, 'description' => 'application homepage - used for source link'],
  181. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  182. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  183. ],
  184. 'primary key' => ['identifier'],
  185. ];
  186. }
  187. }