| Conditions | 13 |
| Paths | 50 |
| Total Lines | 131 |
| 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 |
||
| 248 | public function process(array &$state): void |
||
| 249 | { |
||
| 250 | Assert::keyExists($state, 'Destination'); |
||
| 251 | Assert::keyExists($state['Destination'], 'entityid'); |
||
| 252 | Assert::keyExists($state['Destination'], 'metadata-set'); |
||
| 253 | Assert::keyExists($state['Source'], 'entityid'); |
||
| 254 | Assert::keyExists($state['Source'], 'metadata-set'); |
||
| 255 | |||
| 256 | $spEntityId = $state['Destination']['entityid']; |
||
| 257 | $idpEntityId = $state['Source']['entityid']; |
||
| 258 | |||
| 259 | $metadata = \SimpleSAML\Metadata\MetaDataStorageHandler::getMetadataHandler(); |
||
| 260 | |||
| 261 | /** |
||
| 262 | * If the consent module is active on a bridge $state['saml:sp:IdP'] |
||
| 263 | * will contain an entry id for the remote IdP. If not, then the |
||
| 264 | * consent module is active on a local IdP and nothing needs to be |
||
| 265 | * done. |
||
| 266 | */ |
||
| 267 | if (isset($state['saml:sp:IdP'])) { |
||
| 268 | $idpEntityId = $state['saml:sp:IdP']; |
||
| 269 | $idpmeta = $metadata->getMetaData($idpEntityId, 'saml20-idp-remote'); |
||
| 270 | $state['Source'] = $idpmeta; |
||
| 271 | } |
||
| 272 | |||
| 273 | $statsData = ['spEntityID' => $spEntityId]; |
||
| 274 | |||
| 275 | // Do not use consent if disabled |
||
| 276 | if ( |
||
| 277 | isset($state['Source']['consent.disable']) && |
||
| 278 | self::checkDisable($state['Source']['consent.disable'], $spEntityId) |
||
| 279 | ) { |
||
| 280 | Logger::debug('Consent: Consent disabled for entity ' . $spEntityId . ' with IdP ' . $idpEntityId); |
||
| 281 | Stats::log('consent:disabled', $statsData); |
||
| 282 | return; |
||
| 283 | } |
||
| 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 |