1
|
|
|
<?php |
2
|
|
|
/** @noinspection ALL */ |
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license GNU AGPL version 3 or any later version |
10
|
|
|
* |
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
14
|
|
|
* License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OC\Authentication\Token; |
27
|
|
|
|
28
|
|
|
use OCP\AppFramework\Db\Entity; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @method void setId(int $id) |
32
|
|
|
* @method void setUid(string $uid); |
33
|
|
|
* @method void setLoginName(string $loginname) |
34
|
|
|
* @method void setName(string $name) |
35
|
|
|
* @method string getToken() |
36
|
|
|
* @method void setType(int $type) |
37
|
|
|
* @method int getType() |
38
|
|
|
* @method void setRemember(int $remember) |
39
|
|
|
* @method void setLastActivity(int $lastactivity) |
40
|
|
|
* @method int getLastActivity() |
41
|
|
|
* @method string getPrivateKey() |
42
|
|
|
* @method void setPrivateKey(string $key) |
43
|
|
|
* @method string getPublicKey() |
44
|
|
|
* @method void setPublicKey(string $key) |
45
|
|
|
* @method void setVersion(int $version) |
46
|
|
|
* @method bool getPasswordInvalid() |
47
|
|
|
*/ |
48
|
|
|
class PublicKeyToken extends Entity implements IToken { |
49
|
|
|
|
50
|
|
|
const VERSION = 2; |
51
|
|
|
|
52
|
|
|
/** @var string user UID */ |
53
|
|
|
protected $uid; |
54
|
|
|
|
55
|
|
|
/** @var string login name used for generating the token */ |
56
|
|
|
protected $loginName; |
57
|
|
|
|
58
|
|
|
/** @var string encrypted user password */ |
59
|
|
|
protected $password; |
60
|
|
|
|
61
|
|
|
/** @var string token name (e.g. browser/OS) */ |
62
|
|
|
protected $name; |
63
|
|
|
|
64
|
|
|
/** @var string */ |
65
|
|
|
protected $token; |
66
|
|
|
|
67
|
|
|
/** @var int */ |
68
|
|
|
protected $type; |
69
|
|
|
|
70
|
|
|
/** @var int */ |
71
|
|
|
protected $remember; |
72
|
|
|
|
73
|
|
|
/** @var int */ |
74
|
|
|
protected $lastActivity; |
75
|
|
|
|
76
|
|
|
/** @var int */ |
77
|
|
|
protected $lastCheck; |
78
|
|
|
|
79
|
|
|
/** @var string */ |
80
|
|
|
protected $scope; |
81
|
|
|
|
82
|
|
|
/** @var int */ |
83
|
|
|
protected $expires; |
84
|
|
|
|
85
|
|
|
/** @var string */ |
86
|
|
|
protected $privateKey; |
87
|
|
|
|
88
|
|
|
/** @var string */ |
89
|
|
|
protected $publicKey; |
90
|
|
|
|
91
|
|
|
/** @var int */ |
92
|
|
|
protected $version; |
93
|
|
|
|
94
|
|
|
/** @var bool */ |
95
|
|
|
protected $passwordInvalid; |
96
|
|
|
|
97
|
|
|
public function __construct() { |
98
|
|
|
$this->addType('uid', 'string'); |
99
|
|
|
$this->addType('loginName', 'string'); |
100
|
|
|
$this->addType('password', 'string'); |
101
|
|
|
$this->addType('name', 'string'); |
102
|
|
|
$this->addType('token', 'string'); |
103
|
|
|
$this->addType('type', 'int'); |
104
|
|
|
$this->addType('remember', 'int'); |
105
|
|
|
$this->addType('lastActivity', 'int'); |
106
|
|
|
$this->addType('lastCheck', 'int'); |
107
|
|
|
$this->addType('scope', 'string'); |
108
|
|
|
$this->addType('expires', 'int'); |
109
|
|
|
$this->addType('publicKey', 'string'); |
110
|
|
|
$this->addType('privateKey', 'string'); |
111
|
|
|
$this->addType('version', 'int'); |
112
|
|
|
$this->addType('passwordInvalid', 'bool'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getId(): int { |
116
|
|
|
return $this->id; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getUID(): string { |
120
|
|
|
return $this->uid; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the login name used when generating the token |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getLoginName(): string { |
129
|
|
|
return parent::getLoginName(); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the (encrypted) login password |
134
|
|
|
* |
135
|
|
|
* @return string|null |
136
|
|
|
*/ |
137
|
|
|
public function getPassword() { |
138
|
|
|
return parent::getPassword(); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
View Code Duplication |
public function jsonSerialize() { |
142
|
|
|
return [ |
143
|
|
|
'id' => $this->id, |
144
|
|
|
'name' => $this->name, |
145
|
|
|
'lastActivity' => $this->lastActivity, |
146
|
|
|
'type' => $this->type, |
147
|
|
|
'scope' => $this->getScopeAsArray() |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the timestamp of the last password check |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
public function getLastCheck(): int { |
157
|
|
|
return parent::getLastCheck(); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the timestamp of the last password check |
162
|
|
|
* |
163
|
|
|
* @param int $time |
164
|
|
|
*/ |
165
|
|
|
public function setLastCheck(int $time) { |
166
|
|
|
parent::setLastCheck($time); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getScope(): string { |
170
|
|
|
$scope = parent::getScope(); |
|
|
|
|
171
|
|
|
if ($scope === null) { |
172
|
|
|
return ''; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $scope; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
View Code Duplication |
public function getScopeAsArray(): array { |
179
|
|
|
$scope = json_decode($this->getScope(), true); |
180
|
|
|
if (!$scope) { |
181
|
|
|
return [ |
182
|
|
|
'filesystem'=> true |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
return $scope; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function setScope($scope) { |
189
|
|
|
if (is_array($scope)) { |
190
|
|
|
parent::setScope(json_encode($scope)); |
|
|
|
|
191
|
|
|
} else { |
192
|
|
|
parent::setScope((string)$scope); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getName(): string { |
197
|
|
|
return parent::getName(); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function getRemember(): int { |
201
|
|
|
return parent::getRemember(); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function setToken(string $token) { |
205
|
|
|
parent::setToken($token); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function setPassword(string $password = null) { |
209
|
|
|
parent::setPassword($password); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function setExpires($expires) { |
213
|
|
|
parent::setExpires($expires); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return int|null |
218
|
|
|
*/ |
219
|
|
|
public function getExpires() { |
220
|
|
|
return parent::getExpires(); |
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function setPasswordInvalid(bool $invalid) { |
224
|
|
|
parent::setPasswordInvalid($invalid); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: