PublicKeyToken.php 4.6 KB

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