PublicKeyToken.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Authentication\Token;
  8. use OCP\AppFramework\Db\Entity;
  9. use OCP\Authentication\Token\IToken;
  10. /**
  11. * @method void setId(int $id)
  12. * @method void setUid(string $uid);
  13. * @method void setLoginName(string $loginname)
  14. * @method string getToken()
  15. * @method void setType(int $type)
  16. * @method int getType()
  17. * @method void setRemember(int $remember)
  18. * @method void setLastActivity(int $lastactivity)
  19. * @method int getLastActivity()
  20. * @method string getPrivateKey()
  21. * @method void setPrivateKey(string $key)
  22. * @method string getPublicKey()
  23. * @method void setPublicKey(string $key)
  24. * @method void setVersion(int $version)
  25. * @method bool getPasswordInvalid()
  26. * @method string getPasswordHash()
  27. * @method setPasswordHash(string $hash)
  28. */
  29. class PublicKeyToken extends Entity implements INamedToken, IWipeableToken {
  30. public const VERSION = 2;
  31. /** @var string user UID */
  32. protected $uid;
  33. /** @var string login name used for generating the token */
  34. protected $loginName;
  35. /** @var string encrypted user password */
  36. protected $password;
  37. /** @var string hashed user password */
  38. protected $passwordHash;
  39. /** @var string token name (e.g. browser/OS) */
  40. protected $name;
  41. /** @var string */
  42. protected $token;
  43. /** @var int */
  44. protected $type;
  45. /** @var int */
  46. protected $remember;
  47. /** @var int */
  48. protected $lastActivity;
  49. /** @var int */
  50. protected $lastCheck;
  51. /** @var string */
  52. protected $scope;
  53. /** @var int */
  54. protected $expires;
  55. /** @var string */
  56. protected $privateKey;
  57. /** @var string */
  58. protected $publicKey;
  59. /** @var int */
  60. protected $version;
  61. /** @var bool */
  62. protected $passwordInvalid;
  63. public function __construct() {
  64. $this->addType('uid', 'string');
  65. $this->addType('loginName', 'string');
  66. $this->addType('password', 'string');
  67. $this->addType('passwordHash', 'string');
  68. $this->addType('name', 'string');
  69. $this->addType('token', 'string');
  70. $this->addType('type', 'int');
  71. $this->addType('remember', 'int');
  72. $this->addType('lastActivity', 'int');
  73. $this->addType('lastCheck', 'int');
  74. $this->addType('scope', 'string');
  75. $this->addType('expires', 'int');
  76. $this->addType('publicKey', 'string');
  77. $this->addType('privateKey', 'string');
  78. $this->addType('version', 'int');
  79. $this->addType('passwordInvalid', 'bool');
  80. }
  81. public function getId(): int {
  82. return $this->id;
  83. }
  84. public function getUID(): string {
  85. return $this->uid;
  86. }
  87. /**
  88. * Get the login name used when generating the token
  89. *
  90. * @return string
  91. */
  92. public function getLoginName(): string {
  93. return parent::getLoginName();
  94. }
  95. /**
  96. * Get the (encrypted) login password
  97. */
  98. public function getPassword(): ?string {
  99. return parent::getPassword();
  100. }
  101. public function jsonSerialize(): array {
  102. return [
  103. 'id' => $this->id,
  104. 'name' => $this->name,
  105. 'lastActivity' => $this->lastActivity,
  106. 'type' => $this->type,
  107. 'scope' => $this->getScopeAsArray()
  108. ];
  109. }
  110. /**
  111. * Get the timestamp of the last password check
  112. *
  113. * @return int
  114. */
  115. public function getLastCheck(): int {
  116. return parent::getLastCheck();
  117. }
  118. /**
  119. * Get the timestamp of the last password check
  120. */
  121. public function setLastCheck(int $time): void {
  122. parent::setLastCheck($time);
  123. }
  124. public function getScope(): string {
  125. $scope = parent::getScope();
  126. if ($scope === null) {
  127. return '';
  128. }
  129. return $scope;
  130. }
  131. public function getScopeAsArray(): array {
  132. $scope = json_decode($this->getScope(), true);
  133. if (!$scope) {
  134. return [
  135. IToken::SCOPE_FILESYSTEM => true
  136. ];
  137. }
  138. return $scope;
  139. }
  140. public function setScope(array|string|null $scope): void {
  141. if (is_array($scope)) {
  142. parent::setScope(json_encode($scope));
  143. } else {
  144. parent::setScope((string)$scope);
  145. }
  146. }
  147. public function getName(): string {
  148. return parent::getName();
  149. }
  150. public function setName(string $name): void {
  151. parent::setName($name);
  152. }
  153. public function getRemember(): int {
  154. return parent::getRemember();
  155. }
  156. public function setToken(string $token): void {
  157. parent::setToken($token);
  158. }
  159. public function setPassword(?string $password = null): void {
  160. parent::setPassword($password);
  161. }
  162. public function setExpires($expires): void {
  163. parent::setExpires($expires);
  164. }
  165. /**
  166. * @return int|null
  167. */
  168. public function getExpires() {
  169. return parent::getExpires();
  170. }
  171. public function setPasswordInvalid(bool $invalid) {
  172. parent::setPasswordInvalid($invalid);
  173. }
  174. public function wipe(): void {
  175. parent::setType(IToken::WIPE_TOKEN);
  176. }
  177. }