DefaultToken.php 4.3 KB

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