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