PublicKeyToken.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Authentication\Token;
  27. use OCP\AppFramework\Db\Entity;
  28. /**
  29. * @method void setId(int $id)
  30. * @method void setUid(string $uid);
  31. * @method void setLoginName(string $loginname)
  32. * @method string getToken()
  33. * @method void setType(int $type)
  34. * @method int getType()
  35. * @method void setRemember(int $remember)
  36. * @method void setLastActivity(int $lastactivity)
  37. * @method int getLastActivity()
  38. * @method string getPrivateKey()
  39. * @method void setPrivateKey(string $key)
  40. * @method string getPublicKey()
  41. * @method void setPublicKey(string $key)
  42. * @method void setVersion(int $version)
  43. * @method bool getPasswordInvalid()
  44. * @method string getPasswordHash()
  45. * @method setPasswordHash(string $hash)
  46. */
  47. class PublicKeyToken extends Entity implements INamedToken, IWipeableToken {
  48. public const VERSION = 2;
  49. /** @var string user UID */
  50. protected $uid;
  51. /** @var string login name used for generating the token */
  52. protected $loginName;
  53. /** @var string encrypted user password */
  54. protected $password;
  55. /** @var string hashed user password */
  56. protected $passwordHash;
  57. /** @var string token name (e.g. browser/OS) */
  58. protected $name;
  59. /** @var string */
  60. protected $token;
  61. /** @var int */
  62. protected $type;
  63. /** @var int */
  64. protected $remember;
  65. /** @var int */
  66. protected $lastActivity;
  67. /** @var int */
  68. protected $lastCheck;
  69. /** @var string */
  70. protected $scope;
  71. /** @var int */
  72. protected $expires;
  73. /** @var string */
  74. protected $privateKey;
  75. /** @var string */
  76. protected $publicKey;
  77. /** @var int */
  78. protected $version;
  79. /** @var bool */
  80. protected $passwordInvalid;
  81. public function __construct() {
  82. $this->addType('uid', 'string');
  83. $this->addType('loginName', 'string');
  84. $this->addType('password', 'string');
  85. $this->addType('passwordHash', 'string');
  86. $this->addType('name', 'string');
  87. $this->addType('token', 'string');
  88. $this->addType('type', 'int');
  89. $this->addType('remember', 'int');
  90. $this->addType('lastActivity', 'int');
  91. $this->addType('lastCheck', 'int');
  92. $this->addType('scope', 'string');
  93. $this->addType('expires', 'int');
  94. $this->addType('publicKey', 'string');
  95. $this->addType('privateKey', 'string');
  96. $this->addType('version', 'int');
  97. $this->addType('passwordInvalid', 'bool');
  98. }
  99. public function getId(): int {
  100. return $this->id;
  101. }
  102. public function getUID(): string {
  103. return $this->uid;
  104. }
  105. /**
  106. * Get the login name used when generating the token
  107. *
  108. * @return string
  109. */
  110. public function getLoginName(): string {
  111. return parent::getLoginName();
  112. }
  113. /**
  114. * Get the (encrypted) login password
  115. */
  116. public function getPassword(): ?string {
  117. return parent::getPassword();
  118. }
  119. public function jsonSerialize(): array {
  120. return [
  121. 'id' => $this->id,
  122. 'name' => $this->name,
  123. 'lastActivity' => $this->lastActivity,
  124. 'type' => $this->type,
  125. 'scope' => $this->getScopeAsArray()
  126. ];
  127. }
  128. /**
  129. * Get the timestamp of the last password check
  130. *
  131. * @return int
  132. */
  133. public function getLastCheck(): int {
  134. return parent::getLastCheck();
  135. }
  136. /**
  137. * Get the timestamp of the last password check
  138. */
  139. public function setLastCheck(int $time): void {
  140. parent::setLastCheck($time);
  141. }
  142. public function getScope(): string {
  143. $scope = parent::getScope();
  144. if ($scope === null) {
  145. return '';
  146. }
  147. return $scope;
  148. }
  149. public function getScopeAsArray(): array {
  150. $scope = json_decode($this->getScope(), true);
  151. if (!$scope) {
  152. return [
  153. 'filesystem' => true
  154. ];
  155. }
  156. return $scope;
  157. }
  158. public function setScope(array|string|null $scope): void {
  159. if (is_array($scope)) {
  160. parent::setScope(json_encode($scope));
  161. } else {
  162. parent::setScope((string)$scope);
  163. }
  164. }
  165. public function getName(): string {
  166. return parent::getName();
  167. }
  168. public function setName(string $name): void {
  169. parent::setName($name);
  170. }
  171. public function getRemember(): int {
  172. return parent::getRemember();
  173. }
  174. public function setToken(string $token): void {
  175. parent::setToken($token);
  176. }
  177. public function setPassword(string $password = null): void {
  178. parent::setPassword($password);
  179. }
  180. public function setExpires($expires): void {
  181. parent::setExpires($expires);
  182. }
  183. /**
  184. * @return int|null
  185. */
  186. public function getExpires() {
  187. return parent::getExpires();
  188. }
  189. public function setPasswordInvalid(bool $invalid) {
  190. parent::setPasswordInvalid($invalid);
  191. }
  192. public function wipe(): void {
  193. parent::setType(IToken::WIPE_TOKEN);
  194. }
  195. }