DefaultToken.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  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 void setVersion(int $version)
  39. */
  40. class DefaultToken extends Entity implements INamedToken {
  41. public const VERSION = 1;
  42. /** @var string user UID */
  43. protected $uid;
  44. /** @var string login name used for generating the token */
  45. protected $loginName;
  46. /** @var string encrypted user password */
  47. protected $password;
  48. /** @var string token name (e.g. browser/OS) */
  49. protected $name;
  50. /** @var string */
  51. protected $token;
  52. /** @var int */
  53. protected $type;
  54. /** @var int */
  55. protected $remember;
  56. /** @var int */
  57. protected $lastActivity;
  58. /** @var int */
  59. protected $lastCheck;
  60. /** @var string */
  61. protected $scope;
  62. /** @var int */
  63. protected $expires;
  64. /** @var int */
  65. protected $version;
  66. public function __construct() {
  67. $this->addType('uid', 'string');
  68. $this->addType('loginName', 'string');
  69. $this->addType('password', 'string');
  70. $this->addType('name', 'string');
  71. $this->addType('token', 'string');
  72. $this->addType('type', 'int');
  73. $this->addType('remember', 'int');
  74. $this->addType('lastActivity', 'int');
  75. $this->addType('lastCheck', 'int');
  76. $this->addType('scope', 'string');
  77. $this->addType('expires', 'int');
  78. $this->addType('version', 'int');
  79. }
  80. public function getId(): int {
  81. return $this->id;
  82. }
  83. public function getUID(): string {
  84. return $this->uid;
  85. }
  86. /**
  87. * Get the login name used when generating the token
  88. *
  89. * @return string
  90. */
  91. public function getLoginName(): string {
  92. return parent::getLoginName();
  93. }
  94. /**
  95. * Get the (encrypted) login password
  96. *
  97. * @return string|null
  98. */
  99. public function getPassword() {
  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. * @param int $time
  123. */
  124. public function setLastCheck(int $time) {
  125. parent::setLastCheck($time);
  126. }
  127. public function getScope(): string {
  128. $scope = parent::getScope();
  129. if ($scope === null) {
  130. return '';
  131. }
  132. return $scope;
  133. }
  134. public function getScopeAsArray(): array {
  135. $scope = json_decode($this->getScope(), true);
  136. if (!$scope) {
  137. return [
  138. 'filesystem' => true
  139. ];
  140. }
  141. return $scope;
  142. }
  143. public function setScope($scope) {
  144. if (\is_array($scope)) {
  145. parent::setScope(json_encode($scope));
  146. } else {
  147. parent::setScope((string)$scope);
  148. }
  149. }
  150. public function getName(): string {
  151. return parent::getName();
  152. }
  153. public function setName(string $name): void {
  154. parent::setName($name);
  155. }
  156. public function getRemember(): int {
  157. return parent::getRemember();
  158. }
  159. public function setToken(string $token) {
  160. parent::setToken($token);
  161. }
  162. public function setPassword(string $password = null) {
  163. parent::setPassword($password);
  164. }
  165. public function setExpires($expires) {
  166. parent::setExpires($expires);
  167. }
  168. /**
  169. * @return int|null
  170. */
  171. public function getExpires() {
  172. return parent::getExpires();
  173. }
  174. }