This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * ScopeService.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\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\ScopeModelInterface; |
||
20 | use sweelix\oauth2\server\models\Scope; |
||
21 | use sweelix\oauth2\server\interfaces\ScopeServiceInterface; |
||
22 | use yii\db\Exception as DatabaseException; |
||
23 | use Yii; |
||
24 | |||
25 | /** |
||
26 | * This is the scope service for redis |
||
27 | * database structure |
||
28 | * * oauth2:scopes:<sid> : hash (Scope) |
||
29 | * * oauth2:scopes:keys : set scopeIds |
||
30 | * * oauth2:scopes:defaultkeys : set default scopeIds |
||
31 | * |
||
32 | * @author Philippe Gaultier <[email protected]> |
||
33 | * @copyright 2010-2017 Philippe Gaultier |
||
34 | * @license http://www.sweelix.net/license license |
||
35 | * @version 1.2.0 |
||
36 | * @link http://www.sweelix.net |
||
37 | * @package sweelix\oauth2\server\services\redis |
||
38 | * @since 1.0.0 |
||
39 | */ |
||
40 | class ScopeService extends BaseService implements ScopeServiceInterface |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * @param string $sid scope ID |
||
45 | * @return string scope Key |
||
46 | * @since 1.0.0 |
||
47 | */ |
||
48 | 18 | protected function getScopeKey($sid) |
|
49 | { |
||
50 | 18 | return $this->namespace . ':' . $sid; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return string key of all scopes list |
||
55 | * @since 1.0.0 |
||
56 | */ |
||
57 | 18 | protected function getScopeListKey() |
|
58 | { |
||
59 | 18 | return $this->namespace . ':keys'; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return string key of default scopes list |
||
64 | * @since 1.0.0 |
||
65 | */ |
||
66 | 25 | protected function getScopeDefaultListKey() |
|
67 | { |
||
68 | 25 | return $this->namespace . ':defaultkeys'; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 18 | public function save(ScopeModelInterface $scope, $attributes) |
|
75 | { |
||
76 | 18 | if ($scope->getIsNewRecord()) { |
|
77 | 18 | $result = $this->insert($scope, $attributes); |
|
78 | 18 | } else { |
|
79 | 1 | $result = $this->update($scope, $attributes); |
|
80 | } |
||
81 | 18 | return $result; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Save Scope |
||
86 | * @param ScopeModelInterface $scope |
||
87 | * @param null|array $attributes attributes to save |
||
88 | * @return bool |
||
89 | * @throws DatabaseException |
||
90 | * @throws DuplicateIndexException |
||
91 | * @throws DuplicateKeyException |
||
92 | * @since 1.0.0 |
||
93 | */ |
||
94 | 18 | protected function insert(ScopeModelInterface $scope, $attributes) |
|
95 | { |
||
96 | 18 | $result = false; |
|
97 | 18 | if (!$scope->beforeSave(true)) { |
|
98 | return $result; |
||
99 | } |
||
100 | 18 | $scopeKey = $this->getScopeKey($scope->getKey()); |
|
101 | 18 | $scopeListKey = $this->getScopeListKey(); |
|
102 | 18 | $scopeDefaultListKey = $this->getScopeDefaultListKey(); |
|
103 | //check if record exists |
||
104 | 18 | $entityStatus = (int)$this->db->executeCommand('EXISTS', [$scopeKey]); |
|
105 | 18 | if ($entityStatus === 1) { |
|
106 | 1 | throw new DuplicateKeyException('Duplicate key "'.$scopeKey.'"'); |
|
107 | } |
||
108 | |||
109 | 18 | $values = $scope->getDirtyAttributes($attributes); |
|
0 ignored issues
–
show
|
|||
110 | 18 | $redisParameters = [$scopeKey]; |
|
111 | 18 | $this->setAttributesDefinitions($scope->attributesDefinition()); |
|
112 | 18 | foreach ($values as $key => $value) |
|
113 | { |
||
114 | 18 | if ($value !== null) { |
|
115 | 18 | $redisParameters[] = $key; |
|
116 | 18 | $redisParameters[] = $this->convertToDatabase($key, $value); |
|
117 | 18 | } |
|
118 | 18 | } |
|
119 | //TODO: use EXEC/MULTI to avoid errors |
||
120 | 18 | $transaction = $this->db->executeCommand('MULTI'); |
|
121 | 18 | if ($transaction === true) { |
|
122 | try { |
||
123 | 18 | $this->db->executeCommand('HMSET', $redisParameters); |
|
124 | 18 | $this->db->executeCommand('SADD', [$scopeListKey, $scope->getKey()]); |
|
125 | 18 | if ($scope->isDefault === true) { |
|
0 ignored issues
–
show
Accessing
isDefault on the interface sweelix\oauth2\server\in...ces\ScopeModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
![]() |
|||
126 | 17 | $this->db->executeCommand('SADD', [$scopeDefaultListKey, $scope->getKey()]); |
|
127 | 17 | } |
|
128 | 18 | $this->db->executeCommand('EXEC'); |
|
129 | 18 | } catch (DatabaseException $e) { |
|
130 | // @codeCoverageIgnoreStart |
||
131 | // we have a REDIS exception, we should not discard |
||
132 | Yii::debug('Error while inserting entity', __METHOD__); |
||
133 | throw $e; |
||
134 | // @codeCoverageIgnoreEnd |
||
135 | } |
||
136 | 18 | } |
|
137 | 18 | $changedAttributes = array_fill_keys(array_keys($values), null); |
|
138 | 18 | $scope->setOldAttributes($values); |
|
139 | 18 | $scope->afterSave(true, $changedAttributes); |
|
140 | 18 | $result = true; |
|
141 | 18 | return $result; |
|
142 | } |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Update ScopeModelInterface |
||
147 | * @param Scope $scope |
||
148 | * @param null|array $attributes attributes to save |
||
149 | * @return bool |
||
150 | * @throws DatabaseException |
||
151 | * @throws DuplicateIndexException |
||
152 | * @throws DuplicateKeyException |
||
153 | */ |
||
154 | 1 | protected function update(ScopeModelInterface $scope, $attributes) |
|
155 | { |
||
156 | 1 | if (!$scope->beforeSave(false)) { |
|
157 | return false; |
||
158 | } |
||
159 | |||
160 | 1 | $values = $scope->getDirtyAttributes($attributes); |
|
0 ignored issues
–
show
It seems like
$attributes defined by parameter $attributes on line 154 can also be of type array ; however, sweelix\oauth2\server\in...e::getDirtyAttributes() does only seem to accept array<integer,string>|null , maybe add an additional type check?
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. ![]() |
|||
161 | 1 | $modelKey = $scope->key(); |
|
162 | 1 | $scopeId = isset($values[$modelKey]) ? $values[$modelKey] : $scope->getKey(); |
|
163 | 1 | $scopeKey = $this->getScopeKey($scopeId); |
|
164 | 1 | $scopeListKey = $this->getScopeListKey(); |
|
165 | 1 | $scopeDefaultListKey = $this->getScopeDefaultListKey(); |
|
166 | |||
167 | |||
168 | 1 | if (isset($values[$modelKey]) === true) { |
|
169 | 1 | $newScopeKey = $this->getScopeKey($values[$modelKey]); |
|
170 | 1 | $entityStatus = (int)$this->db->executeCommand('EXISTS', [$newScopeKey]); |
|
171 | 1 | if ($entityStatus === 1) { |
|
172 | 1 | throw new DuplicateKeyException('Duplicate key "'.$newScopeKey.'"'); |
|
173 | } |
||
174 | 1 | } |
|
175 | |||
176 | 1 | $this->db->executeCommand('MULTI'); |
|
177 | try { |
||
178 | 1 | $reAddKeyInList = false; |
|
179 | 1 | if (array_key_exists($modelKey, $values) === true) { |
|
180 | 1 | $oldId = $scope->getOldKey(); |
|
181 | 1 | $oldScopeKey = $this->getScopeKey($oldId); |
|
182 | |||
183 | 1 | $this->db->executeCommand('RENAMENX', [$oldScopeKey, $scopeKey]); |
|
184 | 1 | $this->db->executeCommand('SREM', [$scopeListKey, $oldScopeKey]); |
|
185 | 1 | $this->db->executeCommand('SREM', [$scopeDefaultListKey, $oldScopeKey]); |
|
186 | 1 | $reAddKeyInList = true; |
|
187 | 1 | } |
|
188 | |||
189 | 1 | $redisUpdateParameters = [$scopeKey]; |
|
190 | 1 | $redisDeleteParameters = [$scopeKey]; |
|
191 | 1 | $this->setAttributesDefinitions($scope->attributesDefinition()); |
|
192 | 1 | foreach ($values as $key => $value) |
|
193 | { |
||
194 | 1 | if ($value === null) { |
|
195 | 1 | $redisDeleteParameters[] = $key; |
|
196 | 1 | } else { |
|
197 | 1 | $redisUpdateParameters[] = $key; |
|
198 | 1 | $redisUpdateParameters[] = $this->convertToDatabase($key, $value); |
|
199 | } |
||
200 | 1 | } |
|
201 | 1 | if (count($redisDeleteParameters) > 1) { |
|
202 | 1 | $this->db->executeCommand('HDEL', $redisDeleteParameters); |
|
203 | 1 | } |
|
204 | 1 | if (count($redisUpdateParameters) > 1) { |
|
205 | 1 | $this->db->executeCommand('HMSET', $redisUpdateParameters); |
|
206 | 1 | } |
|
207 | |||
208 | 1 | if ($reAddKeyInList === true) { |
|
209 | 1 | $this->db->executeCommand('SADD', [$scopeListKey, $scopeId]); |
|
210 | 1 | } |
|
211 | 1 | if ($scope->isDefault === true) { |
|
0 ignored issues
–
show
Accessing
isDefault on the interface sweelix\oauth2\server\in...ces\ScopeModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
![]() |
|||
212 | 1 | $this->db->executeCommand('SADD', [$scopeDefaultListKey, $scopeId]); |
|
213 | 1 | } else { |
|
214 | 1 | $this->db->executeCommand('SREM', [$scopeDefaultListKey, $scopeId]); |
|
215 | } |
||
216 | |||
217 | 1 | $this->db->executeCommand('EXEC'); |
|
218 | 1 | } catch (DatabaseException $e) { |
|
219 | // @codeCoverageIgnoreStart |
||
220 | // we have a REDIS exception, we should not discard |
||
221 | Yii::debug('Error while updating entity', __METHOD__); |
||
222 | throw $e; |
||
223 | // @codeCoverageIgnoreEnd |
||
224 | } |
||
225 | |||
226 | 1 | $changedAttributes = []; |
|
227 | 1 | foreach ($values as $name => $value) { |
|
228 | 1 | $oldAttributes = $scope->getOldAttributes(); |
|
229 | 1 | $changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null; |
|
230 | 1 | $scope->setOldAttribute($name, $value); |
|
231 | 1 | } |
|
232 | 1 | $scope->afterSave(false, $changedAttributes); |
|
233 | 1 | return true; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * @inheritdoc |
||
238 | */ |
||
239 | 2 | public function findOne($key) |
|
240 | { |
||
241 | 2 | $record = null; |
|
242 | 2 | $scopeKey = $this->getScopeKey($key); |
|
243 | 2 | $scopeExists = (bool)$this->db->executeCommand('EXISTS', [$scopeKey]); |
|
244 | 2 | if ($scopeExists === true) { |
|
245 | 2 | $scopeData = $this->db->executeCommand('HGETALL', [$scopeKey]); |
|
246 | 2 | $record = Yii::createObject('sweelix\oauth2\server\interfaces\ScopeModelInterface'); |
|
247 | /** @var ScopeModelInterface $record */ |
||
248 | 2 | $properties = $record->attributesDefinition(); |
|
249 | 2 | $this->setAttributesDefinitions($properties); |
|
250 | 2 | $attributes = []; |
|
251 | 2 | for ($i = 0; $i < count($scopeData); $i += 2) { |
|
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||
252 | 2 | if (isset($properties[$scopeData[$i]]) === true) { |
|
253 | 2 | $scopeData[$i + 1] = $this->convertToModel($scopeData[$i], $scopeData[($i + 1)]); |
|
254 | 2 | $record->setAttribute($scopeData[$i], $scopeData[$i + 1]); |
|
255 | 2 | $attributes[$scopeData[$i]] = $scopeData[$i + 1]; |
|
256 | // @codeCoverageIgnoreStart |
||
257 | } elseif ($record->canSetProperty($scopeData[$i])) { |
||
258 | // TODO: find a way to test attribute population |
||
259 | $record->{$scopeData[$i]} = $scopeData[$i + 1]; |
||
260 | } |
||
261 | // @codeCoverageIgnoreEnd |
||
262 | 2 | } |
|
263 | 2 | if (empty($attributes) === false) { |
|
264 | 2 | $record->setOldAttributes($attributes); |
|
265 | 2 | } |
|
266 | 2 | $record->afterFind(); |
|
267 | 2 | } |
|
268 | 2 | return $record; |
|
269 | } |
||
270 | |||
271 | /** |
||
272 | * @inheritdoc |
||
273 | */ |
||
274 | 17 | public function findAvailableScopeIds() |
|
275 | { |
||
276 | 17 | $scopeListKey = $this->getScopeListKey(); |
|
277 | 17 | return $this->db->executeCommand('SMEMBERS', [$scopeListKey]); |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * @inheritdoc |
||
282 | */ |
||
283 | 11 | public function findDefaultScopeIds($clientId = null) |
|
284 | { |
||
285 | //TODO: add default scopes for clients |
||
286 | 11 | $scopeDefaultListKey = $this->getScopeDefaultListKey(); |
|
287 | 11 | return $this->db->executeCommand('SMEMBERS', [$scopeDefaultListKey]); |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * @inheritdoc |
||
292 | */ |
||
293 | 1 | public function delete(ScopeModelInterface $scope) |
|
294 | { |
||
295 | 1 | $result = false; |
|
296 | 1 | if ($scope->beforeDelete()) { |
|
297 | 1 | $this->db->executeCommand('MULTI'); |
|
298 | 1 | $id = $scope->getOldKey(); |
|
299 | 1 | $scopeKey = $this->getScopeKey($id); |
|
300 | 1 | $scopeListKey = $this->getScopeListKey(); |
|
301 | 1 | $scopeDefaultListKey = $this->getScopeDefaultListKey(); |
|
302 | |||
303 | |||
304 | 1 | $this->db->executeCommand('DEL', [$scopeKey]); |
|
305 | 1 | $this->db->executeCommand('SREM', [$scopeListKey, $id]); |
|
306 | 1 | $this->db->executeCommand('SREM', [$scopeDefaultListKey, $id]); |
|
307 | //TODO: check results to return correct information |
||
308 | 1 | $queryResult = $this->db->executeCommand('EXEC'); |
|
0 ignored issues
–
show
$queryResult is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
309 | 1 | $scope->setIsNewRecord(true); |
|
310 | 1 | $scope->afterDelete(); |
|
311 | 1 | $result = true; |
|
312 | 1 | } |
|
313 | 1 | return $result; |
|
314 | } |
||
315 | |||
316 | } |
||
317 |
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.