1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AuthCodeService.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.2.0 |
11
|
|
|
* @link http://www.sweelix.net |
12
|
|
|
* @package sweelix\oauth2\server\services\mySql |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\services\mySql; |
16
|
|
|
|
17
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateIndexException; |
18
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateKeyException; |
19
|
|
|
use sweelix\oauth2\server\interfaces\AuthCodeModelInterface; |
20
|
|
|
use sweelix\oauth2\server\interfaces\AuthCodeServiceInterface; |
21
|
|
|
use yii\db\Exception as DatabaseException; |
22
|
|
|
use Yii; |
23
|
|
|
use yii\db\Query; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This is the auth code service for mySql |
27
|
|
|
* database structure |
28
|
|
|
* * oauth2:authCodes:<aid> : hash (AuthCode) |
29
|
|
|
* |
30
|
|
|
* @author Philippe Gaultier <[email protected]> |
31
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
32
|
|
|
* @license http://www.sweelix.net/license license |
33
|
|
|
* @version 1.2.0 |
34
|
|
|
* @link http://www.sweelix.net |
35
|
|
|
* @package sweelix\oauth2\server\services\mySql |
36
|
|
|
* @since 1.0.0 |
37
|
|
|
*/ |
38
|
|
|
class AuthCodeService extends BaseService implements AuthCodeServiceInterface |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var string sql authorizationCodes table |
42
|
|
|
*/ |
43
|
|
|
public $authorizationCodesTable = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string sql scope authorizationCode table |
47
|
|
|
*/ |
48
|
|
|
public $scopeAuthorizationCodeTable = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Save Auth Code |
52
|
|
|
* @param AuthCodeModelInterface $authCode |
53
|
|
|
* @param null|array $attributes attributes to save |
54
|
|
|
* @return bool |
55
|
|
|
* @throws DatabaseException |
56
|
|
|
* @throws DuplicateIndexException |
57
|
|
|
* @throws DuplicateKeyException |
58
|
|
|
* @since 1.0.0 |
59
|
|
|
*/ |
60
|
|
|
protected function insert(AuthCodeModelInterface $authCode, $attributes) |
61
|
|
|
{ |
62
|
|
|
$result = false; |
63
|
|
|
if (!$authCode->beforeSave(true)) { |
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
$authCodeKey = $authCode->getKey(); |
67
|
|
|
$entity = (new Query()) |
68
|
|
|
->select('*') |
69
|
|
|
->from($this->authorizationCodesTable) |
70
|
|
|
->where('id = :id', [':id' => $authCodeKey]) |
71
|
|
|
->one($this->db); |
72
|
|
|
if ($entity !== false) { |
73
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $authCodeKey . '"'); |
74
|
|
|
} |
75
|
|
|
$values = $authCode->getDirtyAttributes($attributes); |
|
|
|
|
76
|
|
|
$authCodeParameters = []; |
77
|
|
|
$this->setAttributesDefinitions($authCode->attributesDefinition()); |
78
|
|
|
foreach ($values as $key => $value) { |
79
|
|
|
if (($value !== null) && ($key !== 'scopes')) { |
80
|
|
|
$authCodeParameters[$key] = $this->convertToDatabase($key, $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
$authCodeParameters['dateCreated'] = date('Y-m-d H:i:s'); |
84
|
|
|
$authCodeParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
85
|
|
|
try { |
86
|
|
|
$this->db->createCommand() |
87
|
|
|
->insert($this->authorizationCodesTable, $authCodeParameters) |
88
|
|
|
->execute(); |
89
|
|
|
if (!empty($values['scopes'])) { |
90
|
|
|
$values['scopes'] = array_unique($values['scopes']); |
91
|
|
|
foreach ($values['scopes'] as $scope) { |
92
|
|
|
$scopeAuthCodeParams = [ |
93
|
|
|
'scopeId' => $scope, |
94
|
|
|
'authorizationCodeId' => $authCodeKey |
95
|
|
|
]; |
96
|
|
|
$this->db->createCommand() |
97
|
|
|
->insert($this->scopeAuthorizationCodeTable, $scopeAuthCodeParams) |
98
|
|
|
->execute(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} catch (DatabaseException $e) { |
102
|
|
|
// @codeCoverageIgnoreStart |
103
|
|
|
// we have a MYSQL exception, we should not discard |
104
|
|
|
Yii::debug('Error while inserting entity', __METHOD__); |
105
|
|
|
throw $e; |
106
|
|
|
// @codeCoverageIgnoreEnd |
107
|
|
|
} |
108
|
|
|
$changedAttributes = array_fill_keys(array_keys($values), null); |
109
|
|
|
$authCode->setOldAttributes($values); |
110
|
|
|
$authCode->afterSave(true, $changedAttributes); |
111
|
|
|
$result = true; |
112
|
|
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Update Auth Code |
117
|
|
|
* @param AuthCodeModelInterface $authCode |
118
|
|
|
* @param null|array $attributes attributes to save |
119
|
|
|
* @return bool |
120
|
|
|
* @throws DatabaseException |
121
|
|
|
* @throws DuplicateIndexException |
122
|
|
|
* @throws DuplicateKeyException |
123
|
|
|
*/ |
124
|
|
|
protected function update(AuthCodeModelInterface $authCode, $attributes) |
125
|
|
|
{ |
126
|
|
|
if (!$authCode->beforeSave(false)) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$values = $authCode->getDirtyAttributes($attributes); |
|
|
|
|
131
|
|
|
$modelKey = $authCode->key(); |
132
|
|
|
if (isset($values[$modelKey]) === true) { |
133
|
|
|
$entity = (new Query()) |
134
|
|
|
->select('*') |
135
|
|
|
->from($this->authorizationCodesTable) |
136
|
|
|
->where('id = :id', [':id' => $values[$modelKey]]) |
137
|
|
|
->one($this->db); |
138
|
|
|
if ($entity !== false) { |
139
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $values[$modelKey] . '"'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
$authCodeKey = isset($values[$modelKey]) ? $values[$modelKey] : $authCode->getKey(); |
143
|
|
|
|
144
|
|
|
$authCodeParameters = []; |
145
|
|
|
$this->setAttributesDefinitions($authCode->attributesDefinition()); |
146
|
|
|
foreach ($values as $key => $value) { |
147
|
|
|
if ($key !== 'scopes') { |
148
|
|
|
$authCodeParameters[$key] = ($value !== null) ? $this->convertToDatabase($key, $value) : null; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
$authCodeParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
152
|
|
|
try { |
153
|
|
|
if (array_key_exists($modelKey, $values) === true) { |
154
|
|
|
$oldAuthCodeKey = $authCode->getOldKey(); |
155
|
|
|
$this->db->createCommand() |
156
|
|
|
->update($this->authorizationCodesTable, $authCodeParameters, 'id = :id', [':id' => $oldAuthCodeKey]) |
157
|
|
|
->execute(); |
158
|
|
|
} else { |
159
|
|
|
$this->db->createCommand() |
160
|
|
|
->update($this->authorizationCodesTable, $authCodeParameters, 'id = :id', [':id' => $authCodeKey]) |
161
|
|
|
->execute(); |
162
|
|
|
} |
163
|
|
|
if (isset($values['scopes'])) { |
164
|
|
|
$values['scopes'] = array_unique($values['scopes']); |
165
|
|
|
$scopeAuthCodes = (new Query()) |
166
|
|
|
->select('*') |
167
|
|
|
->from($this->scopeAuthorizationCodeTable) |
168
|
|
|
->where('authorizationCodeId = :authorizationCodeId', [':authorizationCodeId' => $authCodeKey]) |
169
|
|
|
->all($this->db); |
170
|
|
|
foreach ($scopeAuthCodes as $scopeAuthCode) { |
171
|
|
|
if (($index = array_search($scopeAuthCode['scopeId'], $values['scopes'])) === false) { |
172
|
|
|
$this->db->createCommand() |
173
|
|
|
->delete($this->scopeAuthorizationCodeTable, |
174
|
|
|
'authorizationCodeId = :authorizationCodeId AND scopeId = :scopeId', |
175
|
|
|
[':authorizationCodeId' => $authCodeKey, ':scopeId' => $scopeAuthCode['scopeId']]) |
176
|
|
|
->execute(); |
177
|
|
|
} else { |
178
|
|
|
unset($values['scopes'][$index]); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
foreach ($values['scopes'] as $scope) { |
182
|
|
|
$scopeAuthCodeParams = [ |
183
|
|
|
'scopeId' => $scope, |
184
|
|
|
'authorizationCodeId' => $authCodeKey |
185
|
|
|
]; |
186
|
|
|
$this->db->createCommand() |
187
|
|
|
->insert($this->scopeAuthorizationCodeTable, $scopeAuthCodeParams) |
188
|
|
|
->execute(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} catch (DatabaseException $e) { |
192
|
|
|
// @codeCoverageIgnoreStart |
193
|
|
|
// we have a MYSQL exception, we should not discard |
194
|
|
|
Yii::debug('Error while updating entity', __METHOD__); |
195
|
|
|
throw $e; |
196
|
|
|
// @codeCoverageIgnoreEnd |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$changedAttributes = []; |
200
|
|
|
foreach ($values as $name => $value) { |
201
|
|
|
$oldAttributes = $authCode->getOldAttributes(); |
202
|
|
|
$changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null; |
203
|
|
|
$authCode->setOldAttribute($name, $value); |
204
|
|
|
} |
205
|
|
|
$authCode->afterSave(false, $changedAttributes); |
206
|
|
|
return true; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @inheritdoc |
211
|
|
|
*/ |
212
|
|
|
public function save(AuthCodeModelInterface $authCode, $attributes) |
213
|
|
|
{ |
214
|
|
|
if ($authCode->getIsNewRecord()) { |
215
|
|
|
$result = $this->insert($authCode, $attributes); |
216
|
|
|
} else { |
217
|
|
|
$result = $this->update($authCode, $attributes); |
218
|
|
|
} |
219
|
|
|
return $result; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @inheritdoc |
224
|
|
|
*/ |
225
|
|
|
public function findOne($key) |
226
|
|
|
{ |
227
|
|
|
$record = null; |
228
|
|
|
$authCodeData = (new Query()) |
229
|
|
|
->select('*') |
230
|
|
|
->from($this->authorizationCodesTable) |
231
|
|
|
->where('id = :id', [':id' => $key]) |
232
|
|
|
->one($this->db); |
233
|
|
|
|
234
|
|
|
if ($authCodeData !== false) { |
235
|
|
|
$authCodeData['scopes'] = []; |
236
|
|
|
$tmpScopes = (new Query()) |
237
|
|
|
->select('scopeId') |
238
|
|
|
->from($this->scopeAuthorizationCodeTable) |
239
|
|
|
->all($this->db); |
240
|
|
|
foreach ($tmpScopes as $scope) { |
241
|
|
|
$authCodeData['scopes'][] = $scope['scopeId']; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$record = Yii::createObject('sweelix\oauth2\server\interfaces\AuthCodeModelInterface'); |
245
|
|
|
/** @var AuthCodeModelInterface $record */ |
246
|
|
|
$properties = $record->attributesDefinition(); |
247
|
|
|
$this->setAttributesDefinitions($properties); |
248
|
|
|
$attributes = []; |
249
|
|
|
foreach ($authCodeData as $key => $value) { |
250
|
|
|
if (isset($properties[$key]) === true) { |
251
|
|
|
$authCodeData[$key] = $this->convertToModel($key, $value); |
252
|
|
|
$record->setAttribute($key, $authCodeData[$key]); |
253
|
|
|
$attributes[$key] = $authCodeData[$key]; |
254
|
|
|
// @codeCoverageIgnoreStart |
255
|
|
|
} elseif ($record->canSetProperty($key)) { |
256
|
|
|
// TODO: find a way to test attribute population |
257
|
|
|
$record->{$key} = $value; |
258
|
|
|
} |
259
|
|
|
// @codeCoverageIgnoreEnd |
260
|
|
|
} |
261
|
|
|
if (empty($attributes) === false) { |
262
|
|
|
$record->setOldAttributes($attributes); |
263
|
|
|
} |
264
|
|
|
$record->afterFind(); |
265
|
|
|
} |
266
|
|
|
return $record; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @inheritdoc |
271
|
|
|
*/ |
272
|
|
|
public function delete(AuthCodeModelInterface $authCode) |
273
|
|
|
{ |
274
|
|
|
$result = false; |
275
|
|
|
if ($authCode->beforeDelete()) { |
276
|
|
|
//TODO: check results to return correct information |
277
|
|
|
$this->db->createCommand() |
278
|
|
|
->delete($this->authorizationCodesTable, 'id = :id', [':id' => $authCode->getKey()]) |
279
|
|
|
->execute(); |
280
|
|
|
$authCode->setIsNewRecord(true); |
281
|
|
|
$authCode->afterDelete(); |
282
|
|
|
$result = true; |
283
|
|
|
} |
284
|
|
|
return $result; |
285
|
|
|
} |
286
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.