Conditions | 13 |
Paths | 50 |
Total Lines | 130 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
249 | public function process(array &$state): void |
||
250 | { |
||
251 | Assert::keyExists($state, 'Destination'); |
||
252 | Assert::keyExists($state['Destination'], 'entityid'); |
||
253 | Assert::keyExists($state['Destination'], 'metadata-set'); |
||
254 | Assert::keyExists($state['Source'], 'entityid'); |
||
255 | Assert::keyExists($state['Source'], 'metadata-set'); |
||
256 | |||
257 | $spEntityId = $state['Destination']['entityid']; |
||
258 | $idpEntityId = $state['Source']['entityid']; |
||
259 | |||
260 | $metadata = \SimpleSAML\Metadata\MetaDataStorageHandler::getMetadataHandler(); |
||
261 | |||
262 | /** |
||
263 | * If the consent module is active on a bridge $state['saml:sp:IdP'] |
||
264 | * will contain an entry id for the remote IdP. If not, then the |
||
265 | * consent module is active on a local IdP and nothing needs to be |
||
266 | * done. |
||
267 | */ |
||
268 | if (isset($state['saml:sp:IdP'])) { |
||
269 | $idpEntityId = $state['saml:sp:IdP']; |
||
270 | $idpmeta = $metadata->getMetaData($idpEntityId, 'saml20-idp-remote'); |
||
271 | $state['Source'] = $idpmeta; |
||
272 | } |
||
273 | |||
274 | $statsData = ['spEntityID' => $spEntityId]; |
||
275 | |||
276 | // Do not use consent if disabled |
||
277 | if ( |
||
278 | isset($state['Source']['consent.disable']) && |
||
279 | self::checkDisable($state['Source']['consent.disable'], $spEntityId) |
||
280 | ) { |
||
281 | Logger::debug('Consent: Consent disabled for entity ' . $spEntityId . ' with IdP ' . $idpEntityId); |
||
282 | Stats::log('consent:disabled', $statsData); |
||
283 | return; |
||
284 | } |
||
285 | if ( |
||
286 | isset($state['Destination']['consent.disable']) && |
||
287 | self::checkDisable($state['Destination']['consent.disable'], $idpEntityId) |
||
288 | ) { |
||
289 | Logger::debug('Consent: Consent disabled for entity ' . $spEntityId . ' with IdP ' . $idpEntityId); |
||
290 | Stats::log('consent:disabled', $statsData); |
||
291 | return; |
||
292 | } |
||
293 | |||
294 | if ($this->store !== null) { |
||
295 | $attributes = $state['Attributes']; |
||
296 | Assert::keyExists( |
||
297 | $attributes, |
||
298 | $this->identifyingAttribute, |
||
299 | "Consent: Missing '" . $this->identifyingAttribute . "' in user's attributes.", |
||
300 | ); |
||
301 | |||
302 | $source = $state['Source']['metadata-set'] . '|' . $idpEntityId; |
||
303 | $destination = $state['Destination']['metadata-set'] . '|' . $spEntityId; |
||
304 | |||
305 | Assert::keyExists( |
||
306 | $attributes, |
||
307 | $this->identifyingAttribute, |
||
308 | sprintf("Consent: No attribute '%s' was found in the user's attributes.", $this->identifyingAttribute), |
||
309 | ); |
||
310 | |||
311 | $userId = $attributes[$this->identifyingAttribute][0]; |
||
312 | Assert::stringNotEmpty($userId); |
||
313 | |||
314 | // Remove attributes that do not require consent |
||
315 | foreach ($attributes as $attrkey => $attrval) { |
||
316 | if (in_array($attrkey, $this->noconsentattributes, true)) { |
||
317 | unset($attributes[$attrkey]); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | Logger::debug('Consent: userid: ' . $userId); |
||
322 | Logger::debug('Consent: source: ' . $source); |
||
323 | Logger::debug('Consent: destination: ' . $destination); |
||
324 | |||
325 | $hashedUserId = self::getHashedUserID($userId, $source); |
||
326 | $targetedId = self::getTargetedID($userId, $source, $destination); |
||
327 | $attributeSet = self::getAttributeHash($attributes, $this->includeValues); |
||
328 | |||
329 | Logger::debug( |
||
330 | 'Consent: hasConsent() [' . $hashedUserId . '|' . $targetedId . '|' . $attributeSet . ']', |
||
331 | ); |
||
332 | |||
333 | try { |
||
334 | if ($this->store->hasConsent($hashedUserId, $targetedId, $attributeSet)) { |
||
335 | // Consent already given |
||
336 | Logger::stats('consent found'); |
||
337 | Stats::log('consent:found', $statsData); |
||
338 | return; |
||
339 | } |
||
340 | |||
341 | Logger::stats('consent notfound'); |
||
342 | Stats::log('consent:notfound', $statsData); |
||
343 | |||
344 | $state['consent:store'] = $this->store; |
||
345 | $state['consent:store.userId'] = $hashedUserId; |
||
346 | $state['consent:store.destination'] = $targetedId; |
||
347 | $state['consent:store.attributeSet'] = $attributeSet; |
||
348 | } catch (\Exception $e) { |
||
349 | Logger::error('Consent: Error reading from storage: ' . $e->getMessage()); |
||
350 | Logger::stats('Consent failed'); |
||
351 | Stats::log('consent:failed', $statsData); |
||
352 | } |
||
353 | } else { |
||
354 | Logger::stats('consent nostorage'); |
||
355 | Stats::log('consent:nostorage', $statsData); |
||
356 | } |
||
357 | |||
358 | $state['consent:focus'] = $this->focus; |
||
359 | $state['consent:checked'] = $this->checked; |
||
360 | $state['consent:hiddenAttributes'] = $this->hiddenAttributes; |
||
361 | $state['consent:noconsentattributes'] = $this->noconsentattributes; |
||
362 | $state['consent:showNoConsentAboutService'] = $this->showNoConsentAboutService; |
||
363 | |||
364 | // user interaction necessary. Throw exception on isPassive request |
||
365 | if (isset($state['isPassive']) && $state['isPassive'] === true) { |
||
366 | Stats::log('consent:nopassive', $statsData); |
||
367 | throw new Module\saml\Error\NoPassive( |
||
368 | Constants::STATUS_REQUESTER, |
||
369 | 'Unable to give consent on passive request.', |
||
370 | ); |
||
371 | } |
||
372 | |||
373 | // Save state and redirect |
||
374 | $id = Auth\State::saveState($state, 'consent:request'); |
||
375 | $url = Module::getModuleURL('consent/getconsent'); |
||
376 | |||
377 | $httpUtils = new Utils\HTTP(); |
||
378 | $httpUtils->redirectTrustedURL($url, ['StateId' => $id]); |
||
379 | } |
||
440 |