1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RefreshTokenService.php |
4
|
|
|
* |
5
|
|
|
* PHP version 5.6+ |
6
|
|
|
* |
7
|
|
|
* @author Philippe Gaultier <[email protected]> |
8
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
9
|
|
|
* @license http://www.sweelix.net/license license |
10
|
|
|
* @version 1.1.0 |
11
|
|
|
* @link http://www.sweelix.net |
12
|
|
|
* @package sweelix\oauth2\server\services\redis |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\services\redis; |
16
|
|
|
|
17
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateIndexException; |
18
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateKeyException; |
19
|
|
|
use sweelix\oauth2\server\interfaces\RefreshTokenModelInterface; |
20
|
|
|
use sweelix\oauth2\server\interfaces\RefreshTokenServiceInterface; |
21
|
|
|
use yii\db\Exception as DatabaseException; |
22
|
|
|
use Yii; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* This is the refresh token service for redis |
26
|
|
|
* database structure |
27
|
|
|
* * oauth2:refreshTokens:<rid> : hash (RefreshToken) |
28
|
|
|
* * oauth2:users:<uid>:refreshTokens : set (RefreshTokens for user) |
29
|
|
|
* * oauth2:clients:<cid>:refreshTokens : set (RefreshTokens for client) |
30
|
|
|
* |
31
|
|
|
* @author Philippe Gaultier <[email protected]> |
32
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
33
|
|
|
* @license http://www.sweelix.net/license license |
34
|
|
|
* @version 1.1.0 |
35
|
|
|
* @link http://www.sweelix.net |
36
|
|
|
* @package sweelix\oauth2\server\services\redis |
37
|
|
|
* @since 1.0.0 |
38
|
|
|
*/ |
39
|
|
|
class RefreshTokenService extends BaseService implements RefreshTokenServiceInterface |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string user namespace (collection for refreshtokens) |
44
|
|
|
*/ |
45
|
|
|
public $userNamespace = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string client namespace (collection for refreshtokens) |
49
|
|
|
*/ |
50
|
|
|
public $clientNamespace = ''; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $rid refresh token ID |
54
|
|
|
* @return string refresh token Key |
55
|
|
|
* @since 1.0.0 |
56
|
|
|
*/ |
57
|
9 |
|
protected function getRefreshTokenKey($rid) |
58
|
|
|
{ |
59
|
9 |
|
return $this->namespace . ':' . $rid; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $uid user ID |
64
|
|
|
* @return string user refresh tokens collection Key |
65
|
|
|
* @since XXX |
66
|
|
|
*/ |
67
|
10 |
|
protected function getUserRefreshTokensKey($uid) |
68
|
|
|
{ |
69
|
10 |
|
return $this->userNamespace . ':' . $uid . ':refreshTokens'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $cid client ID |
74
|
|
|
* @return string client refresh tokens collection Key |
75
|
|
|
* @since XXX |
76
|
|
|
*/ |
77
|
11 |
|
protected function getClientRefreshTokensKey($cid) |
78
|
|
|
{ |
79
|
11 |
|
return $this->clientNamespace . ':' . $cid . ':refreshTokens'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
9 |
|
public function save(RefreshTokenModelInterface $refreshToken, $attributes) |
86
|
|
|
{ |
87
|
9 |
|
if ($refreshToken->getIsNewRecord()) { |
88
|
9 |
|
$result = $this->insert($refreshToken, $attributes); |
89
|
9 |
|
} else { |
90
|
1 |
|
$result = $this->update($refreshToken, $attributes); |
91
|
|
|
} |
92
|
9 |
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Save Refresh Token |
97
|
|
|
* @param RefreshTokenModelInterface $refreshToken |
98
|
|
|
* @param null|array $attributes attributes to save |
99
|
|
|
* @return bool |
100
|
|
|
* @throws DatabaseException |
101
|
|
|
* @throws DuplicateIndexException |
102
|
|
|
* @throws DuplicateKeyException |
103
|
|
|
* @since 1.0.0 |
104
|
|
|
*/ |
105
|
9 |
|
protected function insert(RefreshTokenModelInterface $refreshToken, $attributes) |
106
|
|
|
{ |
107
|
9 |
|
$result = false; |
108
|
9 |
|
if (!$refreshToken->beforeSave(true)) { |
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
9 |
|
$refreshTokenId = $refreshToken->getKey(); |
112
|
9 |
|
$refreshTokenKey = $this->getRefreshTokenKey($refreshTokenId); |
113
|
9 |
|
if (empty($refreshToken->userId) === false) { |
|
|
|
|
114
|
9 |
|
$userRefreshTokensKey = $this->getUserRefreshTokensKey($refreshToken->userId); |
|
|
|
|
115
|
9 |
|
} else { |
116
|
|
|
$userRefreshTokensKey = null; |
117
|
|
|
} |
118
|
9 |
|
$clientRefreshTokensKey = $this->getClientRefreshTokensKey($refreshToken->clientId); |
|
|
|
|
119
|
|
|
|
120
|
|
|
//check if record exists |
121
|
9 |
|
$entityStatus = (int)$this->db->executeCommand('EXISTS', [$refreshTokenKey]); |
122
|
9 |
|
if ($entityStatus === 1) { |
123
|
1 |
|
throw new DuplicateKeyException('Duplicate key "'.$refreshTokenKey.'"'); |
124
|
|
|
} |
125
|
|
|
|
126
|
9 |
|
$values = $refreshToken->getDirtyAttributes($attributes); |
|
|
|
|
127
|
9 |
|
$redisParameters = [$refreshTokenKey]; |
128
|
9 |
|
$this->setAttributesDefinitions($refreshToken->attributesDefinition()); |
129
|
9 |
|
$expire = null; |
130
|
9 |
|
foreach ($values as $key => $value) |
131
|
|
|
{ |
132
|
9 |
|
if (($key === 'expiry') && ($value > 0)) { |
133
|
9 |
|
$expire = $value; |
134
|
9 |
|
} |
135
|
9 |
|
if ($value !== null) { |
136
|
9 |
|
$redisParameters[] = $key; |
137
|
9 |
|
$redisParameters[] = $this->convertToDatabase($key, $value); |
138
|
9 |
|
} |
139
|
9 |
|
} |
140
|
|
|
//TODO: use EXEC/MULTI to avoid errors |
141
|
9 |
|
$transaction = $this->db->executeCommand('MULTI'); |
142
|
9 |
|
if ($transaction === true) { |
143
|
|
|
try { |
144
|
9 |
|
$this->db->executeCommand('HMSET', $redisParameters); |
145
|
9 |
|
if ($expire !== null) { |
146
|
9 |
|
$this->db->executeCommand('EXPIREAT', [$refreshTokenKey, $expire]); |
147
|
9 |
|
} |
148
|
9 |
|
if ($userRefreshTokensKey !== null) { |
149
|
9 |
|
$this->db->executeCommand('SADD', [$userRefreshTokensKey, $refreshTokenId]); |
150
|
9 |
|
} |
151
|
9 |
|
$this->db->executeCommand('SADD', [$clientRefreshTokensKey, $refreshTokenId]); |
152
|
9 |
|
$this->db->executeCommand('EXEC'); |
153
|
9 |
|
} catch (DatabaseException $e) { |
154
|
|
|
// @codeCoverageIgnoreStart |
155
|
|
|
// we have a REDIS exception, we should not discard |
156
|
|
|
Yii::trace('Error while inserting entity', __METHOD__); |
157
|
|
|
throw $e; |
158
|
|
|
// @codeCoverageIgnoreEnd |
159
|
|
|
} |
160
|
9 |
|
} |
161
|
9 |
|
$changedAttributes = array_fill_keys(array_keys($values), null); |
162
|
9 |
|
$refreshToken->setOldAttributes($values); |
163
|
9 |
|
$refreshToken->afterSave(true, $changedAttributes); |
164
|
9 |
|
$result = true; |
165
|
9 |
|
return $result; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Update Refresh Token |
171
|
|
|
* @param RefreshTokenModelInterface $refreshToken |
172
|
|
|
* @param null|array $attributes attributes to save |
173
|
|
|
* @return bool |
174
|
|
|
* @throws DatabaseException |
175
|
|
|
* @throws DuplicateIndexException |
176
|
|
|
* @throws DuplicateKeyException |
177
|
|
|
*/ |
178
|
1 |
|
protected function update(RefreshTokenModelInterface $refreshToken, $attributes) |
179
|
|
|
{ |
180
|
1 |
|
if (!$refreshToken->beforeSave(false)) { |
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
$values = $refreshToken->getDirtyAttributes($attributes); |
|
|
|
|
185
|
1 |
|
$modelKey = $refreshToken->key(); |
186
|
1 |
|
$refreshTokenId = isset($values[$modelKey]) ? $values[$modelKey] : $refreshToken->getKey(); |
187
|
1 |
|
$refreshTokenKey = $this->getRefreshTokenKey($refreshTokenId); |
188
|
|
|
|
189
|
1 |
|
if (empty($refreshToken->userId) === false) { |
|
|
|
|
190
|
1 |
|
$userRefreshTokensKey = $this->getUserRefreshTokensKey($refreshToken->userId); |
|
|
|
|
191
|
1 |
|
} else { |
192
|
|
|
$userRefreshTokensKey = null; |
193
|
|
|
} |
194
|
1 |
|
$clientRefreshTokensKey = $this->getClientRefreshTokensKey($refreshToken->clientId); |
|
|
|
|
195
|
|
|
|
196
|
1 |
|
if (isset($values[$modelKey]) === true) { |
197
|
1 |
|
$newRefreshTokenKey = $this->getRefreshTokenKey($values[$modelKey]); |
198
|
1 |
|
$entityStatus = (int)$this->db->executeCommand('EXISTS', [$newRefreshTokenKey]); |
199
|
1 |
|
if ($entityStatus === 1) { |
200
|
1 |
|
throw new DuplicateKeyException('Duplicate key "'.$newRefreshTokenKey.'"'); |
201
|
|
|
} |
202
|
1 |
|
} |
203
|
|
|
|
204
|
1 |
|
$this->db->executeCommand('MULTI'); |
205
|
|
|
try { |
206
|
1 |
|
if (array_key_exists($modelKey, $values) === true) { |
207
|
1 |
|
$oldId = $refreshToken->getOldKey(); |
208
|
1 |
|
$oldRefreshTokenKey = $this->getRefreshTokenKey($oldId); |
209
|
|
|
|
210
|
1 |
|
$this->db->executeCommand('RENAMENX', [$oldRefreshTokenKey, $refreshTokenKey]); |
211
|
1 |
|
if ($userRefreshTokensKey !== null) { |
212
|
1 |
|
$this->db->executeCommand('SREM', [$userRefreshTokensKey, $oldRefreshTokenKey]); |
213
|
1 |
|
$this->db->executeCommand('SADD', [$userRefreshTokensKey, $refreshTokenKey]); |
214
|
1 |
|
} |
215
|
1 |
|
$this->db->executeCommand('SREM', [$clientRefreshTokensKey, $oldRefreshTokenKey]); |
216
|
1 |
|
$this->db->executeCommand('SADD', [$clientRefreshTokensKey, $refreshTokenKey]); |
217
|
1 |
|
} |
218
|
|
|
|
219
|
1 |
|
$redisUpdateParameters = [$refreshTokenKey]; |
220
|
1 |
|
$redisDeleteParameters = [$refreshTokenKey]; |
221
|
1 |
|
$this->setAttributesDefinitions($refreshToken->attributesDefinition()); |
222
|
1 |
|
$expire = null; |
223
|
1 |
|
foreach ($values as $key => $value) |
224
|
|
|
{ |
225
|
1 |
|
if ($value === null) { |
226
|
1 |
|
if ($key === 'expiry') { |
227
|
1 |
|
$expire = false; |
228
|
1 |
|
} |
229
|
1 |
|
$redisDeleteParameters[] = $key; |
230
|
1 |
|
} else { |
231
|
1 |
|
if (($key === 'expiry') && ($value > 0)) { |
232
|
|
|
$expire = $value; |
233
|
|
|
} |
234
|
1 |
|
$redisUpdateParameters[] = $key; |
235
|
1 |
|
$redisUpdateParameters[] = $this->convertToDatabase($key, $value); |
236
|
|
|
} |
237
|
1 |
|
} |
238
|
1 |
|
if (count($redisDeleteParameters) > 1) { |
239
|
1 |
|
$this->db->executeCommand('HDEL', $redisDeleteParameters); |
240
|
1 |
|
} |
241
|
1 |
|
if (count($redisUpdateParameters) > 1) { |
242
|
1 |
|
$this->db->executeCommand('HMSET', $redisUpdateParameters); |
243
|
1 |
|
} |
244
|
1 |
|
if ($expire === false) { |
245
|
1 |
|
$this->db->executeCommand('PERSIST', [$refreshTokenKey]); |
246
|
1 |
|
} elseif ($expire > 0) { |
247
|
|
|
$this->db->executeCommand('EXPIREAT', [$refreshTokenKey, $expire]); |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
$this->db->executeCommand('EXEC'); |
251
|
1 |
|
} catch (DatabaseException $e) { |
252
|
|
|
// @codeCoverageIgnoreStart |
253
|
|
|
// we have a REDIS exception, we should not discard |
254
|
|
|
Yii::trace('Error while updating entity', __METHOD__); |
255
|
|
|
throw $e; |
256
|
|
|
// @codeCoverageIgnoreEnd |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
$changedAttributes = []; |
260
|
1 |
|
foreach ($values as $name => $value) { |
261
|
1 |
|
$oldAttributes = $refreshToken->getOldAttributes(); |
262
|
1 |
|
$changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null; |
263
|
1 |
|
$refreshToken->setOldAttribute($name, $value); |
264
|
1 |
|
} |
265
|
1 |
|
$refreshToken->afterSave(false, $changedAttributes); |
266
|
1 |
|
return true; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @inheritdoc |
271
|
|
|
*/ |
272
|
9 |
|
public function findOne($key) |
273
|
|
|
{ |
274
|
9 |
|
$record = null; |
275
|
9 |
|
$refreshTokenKey = $this->getRefreshTokenKey($key); |
276
|
9 |
|
$refreshTokenExists = (bool)$this->db->executeCommand('EXISTS', [$refreshTokenKey]); |
277
|
9 |
|
if ($refreshTokenExists === true) { |
278
|
9 |
|
$refreshTokenData = $this->db->executeCommand('HGETALL', [$refreshTokenKey]); |
279
|
9 |
|
$record = Yii::createObject('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface'); |
280
|
|
|
/** @var RefreshTokenModelInterface $record */ |
281
|
9 |
|
$properties = $record->attributesDefinition(); |
282
|
9 |
|
$this->setAttributesDefinitions($properties); |
283
|
9 |
|
$attributes = []; |
284
|
9 |
|
for ($i = 0; $i < count($refreshTokenData); $i += 2) { |
|
|
|
|
285
|
9 |
|
if (isset($properties[$refreshTokenData[$i]]) === true) { |
286
|
9 |
|
$refreshTokenData[$i + 1] = $this->convertToModel($refreshTokenData[$i], $refreshTokenData[($i + 1)]); |
287
|
9 |
|
$record->setAttribute($refreshTokenData[$i], $refreshTokenData[$i + 1]); |
288
|
9 |
|
$attributes[$refreshTokenData[$i]] = $refreshTokenData[$i + 1]; |
289
|
|
|
// @codeCoverageIgnoreStart |
290
|
|
|
} elseif ($record->canSetProperty($refreshTokenData[$i])) { |
291
|
|
|
// TODO: find a way to test attribute population |
292
|
|
|
$record->{$refreshTokenData[$i]} = $refreshTokenData[$i + 1]; |
293
|
|
|
} |
294
|
|
|
// @codeCoverageIgnoreEnd |
295
|
9 |
|
} |
296
|
9 |
|
if (empty($attributes) === false) { |
297
|
9 |
|
$record->setOldAttributes($attributes); |
298
|
9 |
|
} |
299
|
9 |
|
$record->afterFind(); |
300
|
9 |
|
} |
301
|
9 |
|
return $record; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @inheritdoc |
306
|
|
|
*/ |
307
|
3 |
|
public function findAllByUserId($userId) |
308
|
|
|
{ |
309
|
3 |
|
$userRefreshTokensKey = $this->getUserRefreshTokensKey($userId); |
310
|
3 |
|
$userRefreshTokens = $this->db->executeCommand('SMEMBERS', [$userRefreshTokensKey]); |
311
|
3 |
|
$refreshTokens = []; |
312
|
3 |
|
if ((is_array($userRefreshTokens) === true) && (count($userRefreshTokens) > 0)) { |
313
|
2 |
|
foreach($userRefreshTokens as $userRefreshTokenId) { |
314
|
2 |
|
$refreshTokens[] = $this->findOne($userRefreshTokenId); |
315
|
2 |
|
} |
316
|
2 |
|
} |
317
|
3 |
|
return $refreshTokens; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @inheritdoc |
322
|
|
|
*/ |
323
|
1 |
|
public function deleteAllByUserId($userId) |
324
|
|
|
{ |
325
|
1 |
|
$userRefreshTokensKey = $this->getUserRefreshTokensKey($userId); |
326
|
1 |
|
$userRefreshTokens = $this->db->executeCommand('SMEMBERS', [$userRefreshTokensKey]); |
327
|
1 |
|
$userRefreshTokenKeys = [$userRefreshTokensKey]; |
328
|
1 |
|
foreach ($userRefreshTokens as $userRefreshToken) { |
329
|
1 |
|
$userRefreshTokenKeys[] = $this->getRefreshTokenKey($userRefreshToken); |
330
|
1 |
|
} |
331
|
1 |
|
$this->db->executeCommand('DEL', $userRefreshTokenKeys); |
332
|
1 |
|
return true; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @inheritdoc |
337
|
|
|
*/ |
338
|
3 |
|
public function findAllByClientId($clientId) |
339
|
|
|
{ |
340
|
3 |
|
$clientRefreshTokensKey = $this->getClientRefreshTokensKey($clientId); |
341
|
3 |
|
$clientRefreshTokens = $this->db->executeCommand('SMEMBERS', [$clientRefreshTokensKey]); |
342
|
3 |
|
$refreshTokens = []; |
343
|
3 |
|
if ((is_array($clientRefreshTokens) === true) && (count($clientRefreshTokens) > 0)) { |
344
|
2 |
|
foreach($clientRefreshTokens as $clientRefreshTokenId) { |
345
|
2 |
|
$refreshTokens[] = $this->findOne($clientRefreshTokenId); |
346
|
2 |
|
} |
347
|
2 |
|
} |
348
|
3 |
|
return $refreshTokens; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @inheritdoc |
353
|
|
|
*/ |
354
|
2 |
|
public function deleteAllByClientId($clientId) |
355
|
|
|
{ |
356
|
2 |
|
$clientRefreshTokensKey = $this->getClientRefreshTokensKey($clientId); |
357
|
2 |
|
$clientRefreshTokens = $this->db->executeCommand('SMEMBERS', [$clientRefreshTokensKey]); |
358
|
2 |
|
$clientRefreshTokenKeys = [$clientRefreshTokensKey]; |
359
|
2 |
|
foreach ($clientRefreshTokens as $clientRefreshToken) { |
360
|
1 |
|
$clientRefreshTokenKeys[] = $this->getRefreshTokenKey($clientRefreshToken); |
361
|
2 |
|
} |
362
|
2 |
|
$this->db->executeCommand('DEL', $clientRefreshTokenKeys); |
363
|
2 |
|
return true; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @inheritdoc |
369
|
|
|
*/ |
370
|
2 |
|
public function delete(RefreshTokenModelInterface $refreshToken) |
371
|
|
|
{ |
372
|
2 |
|
$result = false; |
373
|
2 |
|
if ($refreshToken->beforeDelete()) { |
374
|
2 |
|
if (empty($refreshToken->userId) === false) { |
|
|
|
|
375
|
2 |
|
$userRefreshTokensKey = $this->getUserRefreshTokensKey($refreshToken->userId); |
|
|
|
|
376
|
2 |
|
} else { |
377
|
|
|
$userRefreshTokensKey = null; |
378
|
|
|
} |
379
|
2 |
|
$clientRefreshTokensKey = $this->getClientRefreshTokensKey($refreshToken->userId); |
|
|
|
|
380
|
|
|
|
381
|
2 |
|
$this->db->executeCommand('MULTI'); |
382
|
2 |
|
$id = $refreshToken->getOldKey(); |
383
|
2 |
|
$refreshTokenKey = $this->getRefreshTokenKey($id); |
384
|
|
|
|
385
|
2 |
|
$this->db->executeCommand('DEL', [$refreshTokenKey]); |
386
|
2 |
|
if ($userRefreshTokensKey !== null) { |
387
|
2 |
|
$this->db->executeCommand('SREM', [$userRefreshTokensKey, $id]); |
388
|
2 |
|
} |
389
|
2 |
|
$this->db->executeCommand('SREM', [$clientRefreshTokensKey, $id]); |
390
|
|
|
//TODO: check results to return correct information |
391
|
2 |
|
$queryResult = $this->db->executeCommand('EXEC'); |
|
|
|
|
392
|
2 |
|
$refreshToken->setIsNewRecord(true); |
393
|
2 |
|
$refreshToken->afterDelete(); |
394
|
2 |
|
$result = true; |
395
|
2 |
|
} |
396
|
2 |
|
return $result; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
} |
400
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: