1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
5
|
|
|
* |
6
|
|
|
* @author Christoph Wurst <[email protected]> |
7
|
|
|
* @author Robin Appelman <[email protected]> |
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
|
|
|
|
25
|
|
|
namespace OC\Authentication\Token; |
26
|
|
|
|
27
|
|
|
use OCP\AppFramework\Db\Entity; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @method void setId(int $id) |
31
|
|
|
* @method void setUid(string $uid); |
32
|
|
|
* @method void setLoginName(string $loginname) |
33
|
|
|
* @method string getToken() |
34
|
|
|
* @method void setType(int $type) |
35
|
|
|
* @method int getType() |
36
|
|
|
* @method void setRemember(int $remember) |
37
|
|
|
* @method void setLastActivity(int $lastactivity) |
38
|
|
|
* @method int getLastActivity() |
39
|
|
|
* @method void setVersion(int $version) |
40
|
|
|
*/ |
41
|
|
|
class DefaultToken extends Entity implements INamedToken { |
42
|
|
|
|
43
|
|
|
const VERSION = 1; |
44
|
|
|
|
45
|
|
|
/** @var string user UID */ |
46
|
|
|
protected $uid; |
47
|
|
|
|
48
|
|
|
/** @var string login name used for generating the token */ |
49
|
|
|
protected $loginName; |
50
|
|
|
|
51
|
|
|
/** @var string encrypted user password */ |
52
|
|
|
protected $password; |
53
|
|
|
|
54
|
|
|
/** @var string token name (e.g. browser/OS) */ |
55
|
|
|
protected $name; |
56
|
|
|
|
57
|
|
|
/** @var string */ |
58
|
|
|
protected $token; |
59
|
|
|
|
60
|
|
|
/** @var int */ |
61
|
|
|
protected $type; |
62
|
|
|
|
63
|
|
|
/** @var int */ |
64
|
|
|
protected $remember; |
65
|
|
|
|
66
|
|
|
/** @var int */ |
67
|
|
|
protected $lastActivity; |
68
|
|
|
|
69
|
|
|
/** @var int */ |
70
|
|
|
protected $lastCheck; |
71
|
|
|
|
72
|
|
|
/** @var string */ |
73
|
|
|
protected $scope; |
74
|
|
|
|
75
|
|
|
/** @var int */ |
76
|
|
|
protected $expires; |
77
|
|
|
|
78
|
|
|
/** @var int */ |
79
|
|
|
protected $version; |
80
|
|
|
|
81
|
|
|
public function __construct() { |
82
|
|
|
$this->addType('uid', 'string'); |
83
|
|
|
$this->addType('loginName', 'string'); |
84
|
|
|
$this->addType('password', 'string'); |
85
|
|
|
$this->addType('name', 'string'); |
86
|
|
|
$this->addType('token', 'string'); |
87
|
|
|
$this->addType('type', 'int'); |
88
|
|
|
$this->addType('remember', 'int'); |
89
|
|
|
$this->addType('lastActivity', 'int'); |
90
|
|
|
$this->addType('lastCheck', 'int'); |
91
|
|
|
$this->addType('scope', 'string'); |
92
|
|
|
$this->addType('expires', 'int'); |
93
|
|
|
$this->addType('version', 'int'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getId(): int { |
97
|
|
|
return $this->id; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getUID(): string { |
101
|
|
|
return $this->uid; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the login name used when generating the token |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getLoginName(): string { |
110
|
|
|
return parent::getLoginName(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the (encrypted) login password |
115
|
|
|
* |
116
|
|
|
* @return string|null |
117
|
|
|
*/ |
118
|
|
|
public function getPassword() { |
119
|
|
|
return parent::getPassword(); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function jsonSerialize() { |
123
|
|
|
return [ |
124
|
|
|
'id' => $this->id, |
125
|
|
|
'name' => $this->name, |
126
|
|
|
'lastActivity' => $this->lastActivity, |
127
|
|
|
'type' => $this->type, |
128
|
|
|
'scope' => $this->getScopeAsArray() |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the timestamp of the last password check |
134
|
|
|
* |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
|
|
public function getLastCheck(): int { |
138
|
|
|
return parent::getLastCheck(); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the timestamp of the last password check |
143
|
|
|
* |
144
|
|
|
* @param int $time |
145
|
|
|
*/ |
146
|
|
|
public function setLastCheck(int $time) { |
147
|
|
|
parent::setLastCheck($time); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getScope(): string { |
151
|
|
|
$scope = parent::getScope(); |
|
|
|
|
152
|
|
|
if ($scope === null) { |
|
|
|
|
153
|
|
|
return ''; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $scope; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getScopeAsArray(): array { |
160
|
|
|
$scope = json_decode($this->getScope(), true); |
161
|
|
|
if (!$scope) { |
162
|
|
|
return [ |
163
|
|
|
'filesystem'=> true |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
return $scope; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function setScope($scope) { |
170
|
|
|
if (\is_array($scope)) { |
171
|
|
|
parent::setScope(json_encode($scope)); |
|
|
|
|
172
|
|
|
} else { |
173
|
|
|
parent::setScope((string)$scope); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getName(): string { |
178
|
|
|
return parent::getName(); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function setName(string $name): void { |
182
|
|
|
parent::setName($name); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function getRemember(): int { |
186
|
|
|
return parent::getRemember(); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function setToken(string $token) { |
190
|
|
|
parent::setToken($token); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function setPassword(string $password = null) { |
194
|
|
|
parent::setPassword($password); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setExpires($expires) { |
198
|
|
|
parent::setExpires($expires); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return int|null |
203
|
|
|
*/ |
204
|
|
|
public function getExpires() { |
205
|
|
|
return parent::getExpires(); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|