|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright 2018, Roeland Jago Douma <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license AGPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OC\Authentication\Token; |
|
25
|
|
|
|
|
26
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException; |
|
27
|
|
|
use OC\Authentication\Exceptions\PasswordlessTokenException; |
|
28
|
|
|
|
|
29
|
|
|
class Manager implements IProvider { |
|
30
|
|
|
|
|
31
|
|
|
/** @var DefaultTokenProvider */ |
|
32
|
|
|
private $defaultTokenProvider; |
|
33
|
|
|
|
|
34
|
|
|
/** @var PublicKeyTokenProvider */ |
|
35
|
|
|
private $publicKeyTokenProvider; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(DefaultTokenProvider $defaultTokenProvider, PublicKeyTokenProvider $publicKeyTokenProvider) { |
|
38
|
|
|
$this->defaultTokenProvider = $defaultTokenProvider; |
|
39
|
|
|
$this->publicKeyTokenProvider = $publicKeyTokenProvider; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create and persist a new token |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $token |
|
46
|
|
|
* @param string $uid |
|
47
|
|
|
* @param string $loginName |
|
48
|
|
|
* @param string|null $password |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @param int $type token type |
|
51
|
|
|
* @param int $remember whether the session token should be used for remember-me |
|
52
|
|
|
* @return IToken |
|
53
|
|
|
*/ |
|
54
|
|
|
public function generateToken(string $token, |
|
55
|
|
|
string $uid, |
|
56
|
|
|
string $loginName, |
|
57
|
|
|
$password, |
|
58
|
|
|
string $name, |
|
59
|
|
|
int $type = IToken::TEMPORARY_TOKEN, |
|
60
|
|
|
int $remember = IToken::DO_NOT_REMEMBER): IToken { |
|
61
|
|
|
return $this->publicKeyTokenProvider->generateToken( |
|
62
|
|
|
$token, |
|
63
|
|
|
$uid, |
|
64
|
|
|
$loginName, |
|
65
|
|
|
$password, |
|
66
|
|
|
$name, |
|
67
|
|
|
$type, |
|
68
|
|
|
$remember |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Save the updated token |
|
74
|
|
|
* |
|
75
|
|
|
* @param IToken $token |
|
76
|
|
|
* @throws InvalidTokenException |
|
77
|
|
|
*/ |
|
78
|
|
|
public function updateToken(IToken $token) { |
|
79
|
|
|
$provider = $this->getProvider($token); |
|
80
|
|
|
$provider->updateToken($token); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Update token activity timestamp |
|
85
|
|
|
* |
|
86
|
|
|
* @throws InvalidTokenException |
|
87
|
|
|
* @param IToken $token |
|
88
|
|
|
*/ |
|
89
|
|
|
public function updateTokenActivity(IToken $token) { |
|
90
|
|
|
$provider = $this->getProvider($token); |
|
91
|
|
|
$provider->updateTokenActivity($token); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $uid |
|
96
|
|
|
* @return IToken[] |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getTokenByUser(string $uid): array { |
|
99
|
|
|
$old = $this->defaultTokenProvider->getTokenByUser($uid); |
|
100
|
|
|
$new = $this->publicKeyTokenProvider->getTokenByUser($uid); |
|
101
|
|
|
|
|
102
|
|
|
return array_merge($old, $new); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get a token by token |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $tokenId |
|
109
|
|
|
* @throws InvalidTokenException |
|
110
|
|
|
* @return IToken |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getToken(string $tokenId): IToken { |
|
113
|
|
|
try { |
|
114
|
|
|
return $this->publicKeyTokenProvider->getToken($tokenId); |
|
115
|
|
|
} catch (InvalidTokenException $e) { |
|
116
|
|
|
// No worries we try to convert it to a PublicKey Token |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
//Convert! |
|
120
|
|
|
$token = $this->defaultTokenProvider->getToken($tokenId); |
|
121
|
|
|
|
|
122
|
|
|
try { |
|
123
|
|
|
$password = $this->defaultTokenProvider->getPassword($token, $tokenId); |
|
124
|
|
|
} catch (PasswordlessTokenException $e) { |
|
125
|
|
|
$password = null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $this->publicKeyTokenProvider->convertToken($token, $tokenId, $password); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get a token by token id |
|
133
|
|
|
* |
|
134
|
|
|
* @param int $tokenId |
|
135
|
|
|
* @throws InvalidTokenException |
|
136
|
|
|
* @return IToken |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getTokenById(int $tokenId): IToken { |
|
139
|
|
|
try { |
|
140
|
|
|
return $this->publicKeyTokenProvider->getTokenById($tokenId); |
|
141
|
|
|
} catch (InvalidTokenException $e) { |
|
142
|
|
|
return $this->defaultTokenProvider->getTokenById($tokenId); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $oldSessionId |
|
148
|
|
|
* @param string $sessionId |
|
149
|
|
|
* @throws InvalidTokenException |
|
150
|
|
|
*/ |
|
151
|
|
|
public function renewSessionToken(string $oldSessionId, string $sessionId) { |
|
152
|
|
|
try { |
|
153
|
|
|
$this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId); |
|
154
|
|
|
} catch (InvalidTokenException $e) { |
|
155
|
|
|
$this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param IToken $savedToken |
|
161
|
|
|
* @param string $tokenId session token |
|
162
|
|
|
* @throws InvalidTokenException |
|
163
|
|
|
* @throws PasswordlessTokenException |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getPassword(IToken $savedToken, string $tokenId): string { |
|
167
|
|
|
$provider = $this->getProvider($savedToken); |
|
168
|
|
|
return $provider->getPassword($savedToken, $tokenId); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function setPassword(IToken $token, string $tokenId, string $password) { |
|
172
|
|
|
$provider = $this->getProvider($token); |
|
173
|
|
|
$provider->setPassword($token, $tokenId, $password); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function invalidateToken(string $token) { |
|
177
|
|
|
$this->defaultTokenProvider->invalidateToken($token); |
|
178
|
|
|
$this->publicKeyTokenProvider->invalidateToken($token); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function invalidateTokenById(string $uid, int $id) { |
|
182
|
|
|
$this->defaultTokenProvider->invalidateTokenById($uid, $id); |
|
183
|
|
|
$this->publicKeyTokenProvider->invalidateTokenById($uid, $id); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function invalidateOldTokens() { |
|
187
|
|
|
$this->defaultTokenProvider->invalidateOldTokens(); |
|
188
|
|
|
$this->publicKeyTokenProvider->invalidateOldTokens(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param IToken $token |
|
193
|
|
|
* @param string $oldTokenId |
|
194
|
|
|
* @param string $newTokenId |
|
195
|
|
|
* @return IToken |
|
196
|
|
|
* @throws InvalidTokenException |
|
197
|
|
|
*/ |
|
198
|
|
|
public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { |
|
199
|
|
|
if ($token instanceof DefaultToken) { |
|
200
|
|
|
try { |
|
201
|
|
|
$password = $this->defaultTokenProvider->getPassword($token, $oldTokenId); |
|
202
|
|
|
} catch (PasswordlessTokenException $e) { |
|
203
|
|
|
$password = null; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return $this->publicKeyTokenProvider->convertToken($token, $newTokenId, $password); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if ($token instanceof PublicKeyToken) { |
|
210
|
|
|
return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
throw new InvalidTokenException(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param IToken $token |
|
218
|
|
|
* @return IProvider |
|
219
|
|
|
* @throws InvalidTokenException |
|
220
|
|
|
*/ |
|
221
|
|
|
private function getProvider(IToken $token): IProvider { |
|
222
|
|
|
if ($token instanceof DefaultToken) { |
|
223
|
|
|
return $this->defaultTokenProvider; |
|
224
|
|
|
} |
|
225
|
|
|
if ($token instanceof PublicKeyToken) { |
|
226
|
|
|
return $this->publicKeyTokenProvider; |
|
227
|
|
|
} |
|
228
|
|
|
throw new InvalidTokenException(); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.