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