PublicKeyToken.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. * @return string|null
  117. */
  118. public function getPassword() {
  119. return parent::getPassword();
  120. }
  121. public function jsonSerialize(): array {
  122. return [
  123. 'id' => $this->id,
  124. 'name' => $this->name,
  125. 'lastActivity' => $this->lastActivity,
  126. 'type' => $this->type,
  127. 'scope' => $this->getScopeAsArray()
  128. ];
  129. }
  130. /**
  131. * Get the timestamp of the last password check
  132. *
  133. * @return int
  134. */
  135. public function getLastCheck(): int {
  136. return parent::getLastCheck();
  137. }
  138. /**
  139. * Get the timestamp of the last password check
  140. *
  141. * @param int $time
  142. */
  143. public function setLastCheck(int $time) {
  144. parent::setLastCheck($time);
  145. }
  146. public function getScope(): string {
  147. $scope = parent::getScope();
  148. if ($scope === null) {
  149. return '';
  150. }
  151. return $scope;
  152. }
  153. public function getScopeAsArray(): array {
  154. $scope = json_decode($this->getScope(), true);
  155. if (!$scope) {
  156. return [
  157. 'filesystem' => true
  158. ];
  159. }
  160. return $scope;
  161. }
  162. public function setScope($scope) {
  163. if (is_array($scope)) {
  164. parent::setScope(json_encode($scope));
  165. } else {
  166. parent::setScope((string)$scope);
  167. }
  168. }
  169. public function getName(): string {
  170. return parent::getName();
  171. }
  172. public function setName(string $name): void {
  173. parent::setName($name);
  174. }
  175. public function getRemember(): int {
  176. return parent::getRemember();
  177. }
  178. public function setToken(string $token) {
  179. parent::setToken($token);
  180. }
  181. public function setPassword(string $password = null) {
  182. parent::setPassword($password);
  183. }
  184. public function setExpires($expires) {
  185. parent::setExpires($expires);
  186. }
  187. /**
  188. * @return int|null
  189. */
  190. public function getExpires() {
  191. return parent::getExpires();
  192. }
  193. public function setPasswordInvalid(bool $invalid) {
  194. parent::setPasswordInvalid($invalid);
  195. }
  196. public function wipe(): void {
  197. parent::setType(IToken::WIPE_TOKEN);
  198. }
  199. }