You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
5.1 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. *
  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. */
  45. class PublicKeyToken extends Entity implements INamedToken, IWipeableToken {
  46. public const VERSION = 2;
  47. /** @var string user UID */
  48. protected $uid;
  49. /** @var string login name used for generating the token */
  50. protected $loginName;
  51. /** @var string encrypted user password */
  52. protected $password;
  53. /** @var string token name (e.g. browser/OS) */
  54. protected $name;
  55. /** @var string */
  56. protected $token;
  57. /** @var int */
  58. protected $type;
  59. /** @var int */
  60. protected $remember;
  61. /** @var int */
  62. protected $lastActivity;
  63. /** @var int */
  64. protected $lastCheck;
  65. /** @var string */
  66. protected $scope;
  67. /** @var int */
  68. protected $expires;
  69. /** @var string */
  70. protected $privateKey;
  71. /** @var string */
  72. protected $publicKey;
  73. /** @var int */
  74. protected $version;
  75. /** @var bool */
  76. protected $passwordInvalid;
  77. public function __construct() {
  78. $this->addType('uid', 'string');
  79. $this->addType('loginName', 'string');
  80. $this->addType('password', 'string');
  81. $this->addType('name', 'string');
  82. $this->addType('token', 'string');
  83. $this->addType('type', 'int');
  84. $this->addType('remember', 'int');
  85. $this->addType('lastActivity', 'int');
  86. $this->addType('lastCheck', 'int');
  87. $this->addType('scope', 'string');
  88. $this->addType('expires', 'int');
  89. $this->addType('publicKey', 'string');
  90. $this->addType('privateKey', 'string');
  91. $this->addType('version', 'int');
  92. $this->addType('passwordInvalid', 'bool');
  93. }
  94. public function getId(): int {
  95. return $this->id;
  96. }
  97. public function getUID(): string {
  98. return $this->uid;
  99. }
  100. /**
  101. * Get the login name used when generating the token
  102. *
  103. * @return string
  104. */
  105. public function getLoginName(): string {
  106. return parent::getLoginName();
  107. }
  108. /**
  109. * Get the (encrypted) login password
  110. *
  111. * @return string|null
  112. */
  113. public function getPassword() {
  114. return parent::getPassword();
  115. }
  116. public function jsonSerialize() {
  117. return [
  118. 'id' => $this->id,
  119. 'name' => $this->name,
  120. 'lastActivity' => $this->lastActivity,
  121. 'type' => $this->type,
  122. 'scope' => $this->getScopeAsArray()
  123. ];
  124. }
  125. /**
  126. * Get the timestamp of the last password check
  127. *
  128. * @return int
  129. */
  130. public function getLastCheck(): int {
  131. return parent::getLastCheck();
  132. }
  133. /**
  134. * Get the timestamp of the last password check
  135. *
  136. * @param int $time
  137. */
  138. public function setLastCheck(int $time) {
  139. parent::setLastCheck($time);
  140. }
  141. public function getScope(): string {
  142. $scope = parent::getScope();
  143. if ($scope === null) {
  144. return '';
  145. }
  146. return $scope;
  147. }
  148. public function getScopeAsArray(): array {
  149. $scope = json_decode($this->getScope(), true);
  150. if (!$scope) {
  151. return [
  152. 'filesystem'=> true
  153. ];
  154. }
  155. return $scope;
  156. }
  157. public function setScope($scope) {
  158. if (is_array($scope)) {
  159. parent::setScope(json_encode($scope));
  160. } else {
  161. parent::setScope((string)$scope);
  162. }
  163. }
  164. public function getName(): string {
  165. return parent::getName();
  166. }
  167. public function setName(string $name): void {
  168. parent::setName($name);
  169. }
  170. public function getRemember(): int {
  171. return parent::getRemember();
  172. }
  173. public function setToken(string $token) {
  174. parent::setToken($token);
  175. }
  176. public function setPassword(string $password = null) {
  177. parent::setPassword($password);
  178. }
  179. public function setExpires($expires) {
  180. parent::setExpires($expires);
  181. }
  182. /**
  183. * @return int|null
  184. */
  185. public function getExpires() {
  186. return parent::getExpires();
  187. }
  188. public function setPasswordInvalid(bool $invalid) {
  189. parent::setPasswordInvalid($invalid);
  190. }
  191. public function wipe(): void {
  192. parent::setType(IToken::WIPE_TOKEN);
  193. }
  194. }