| Total Complexity | 57 |
| Total Lines | 483 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AuthnResponse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuthnResponse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class AuthnResponse |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var \SimpleSAML\XML\Validator|null This variable contains an XML validator for this message. |
||
| 32 | */ |
||
| 33 | private ?Validator $validator = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool Whether this response was validated by some external means (e.g. SSL). |
||
| 37 | */ |
||
| 38 | private bool $messageValidated = false; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | public const SHIB_PROTOCOL_NS = 'urn:oasis:names:tc:SAML:1.0:protocol'; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | public const SHIB_ASSERT_NS = 'urn:oasis:names:tc:SAML:1.0:assertion'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \DOMDocument|null The DOMDocument which represents this message. |
||
| 48 | */ |
||
| 49 | private ?DOMDocument $dom = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string|null The relaystate which is associated with this response. |
||
| 53 | */ |
||
| 54 | private ?string $relayState = null; |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Set whether this message was validated externally. |
||
| 59 | * |
||
| 60 | * @param bool $messageValidated TRUE if the message is already validated, FALSE if not. |
||
| 61 | */ |
||
| 62 | public function setMessageValidated(bool $messageValidated): void |
||
| 63 | { |
||
| 64 | $this->messageValidated = $messageValidated; |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $xml |
||
| 70 | * @throws \Exception |
||
| 71 | */ |
||
| 72 | public function setXML(string $xml): void |
||
| 73 | { |
||
| 74 | try { |
||
| 75 | $this->dom = DOMDocumentFactory::fromString(str_replace("\r", "", $xml)); |
||
| 76 | } catch (Exception $e) { |
||
| 77 | throw new Exception('Unable to parse AuthnResponse XML.'); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string|null $relayState |
||
| 84 | */ |
||
| 85 | public function setRelayState(?string $relayState): void |
||
| 86 | { |
||
| 87 | $this->relayState = $relayState; |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string|null |
||
| 93 | */ |
||
| 94 | public function getRelayState(): ?string |
||
| 95 | { |
||
| 96 | return $this->relayState; |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * @throws \SimpleSAML\Error\Exception |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function validate(): bool |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Checks if the given node is validated by the signature on this response. |
||
| 154 | * |
||
| 155 | * @param \DOMElement|\SimpleXMLElement $node Node to be validated. |
||
| 156 | * @return bool TRUE if the node is validated or FALSE if not. |
||
| 157 | */ |
||
| 158 | private function isNodeValidated($node): bool |
||
| 159 | { |
||
| 160 | if ($this->messageValidated) { |
||
| 161 | // This message was validated externally |
||
| 162 | return true; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($this->validator === null) { |
||
| 166 | return false; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Convert the node to a DOM node if it is an element from SimpleXML |
||
| 170 | if ($node instanceof SimpleXMLElement) { |
||
| 171 | $node = dom_import_simplexml($node); |
||
| 172 | } |
||
| 173 | |||
| 174 | Assert::isInstanceOf($node, DOMNode::class); |
||
| 175 | |||
| 176 | return $this->validator->isNodeValidated($node); |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * This function runs an xPath query on this authentication response. |
||
| 182 | * |
||
| 183 | * @param string $query The query which should be run. |
||
| 184 | * @param \DOMNode $node The node which this query is relative to. If this node is NULL (the default) |
||
| 185 | * then the query will be relative to the root of the response. |
||
| 186 | * @return \DOMNodeList |
||
| 187 | */ |
||
| 188 | private function doXPathQuery(string $query, DOMNode $node = null): DOMNodeList |
||
| 189 | { |
||
| 190 | Assert::isInstanceOf($node, DOMNode::class); |
||
| 191 | |||
| 192 | if ($node === null) { |
||
| 193 | $node = $this->dom->documentElement; |
||
| 194 | } |
||
| 195 | |||
| 196 | Assert::isInstanceOf($node, DOMNode::class); |
||
| 197 | |||
| 198 | $xPath = new DOMXpath($this->dom); |
||
| 199 | $xPath->registerNamespace('shibp', self::SHIB_PROTOCOL_NS); |
||
| 200 | $xPath->registerNamespace('shib', self::SHIB_ASSERT_NS); |
||
| 201 | |||
| 202 | return $xPath->query($query, $node); |
||
| 203 | } |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * Retrieve the session index of this response. |
||
| 208 | * |
||
| 209 | * @return string|null The session index of this response. |
||
| 210 | */ |
||
| 211 | public function getSessionIndex(): ?string |
||
| 212 | { |
||
| 213 | $query = '/shibp:Response/shib:Assertion/shib:AuthnStatement'; |
||
| 214 | $nodelist = $this->doXPathQuery($query); |
||
| 215 | |||
| 216 | if ($node = $nodelist->item(0)) { |
||
| 217 | return $node->getAttribute('SessionIndex'); |
||
| 218 | } |
||
| 219 | |||
| 220 | return null; |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @throws \Exception |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getAttributes(): array |
||
| 301 | } |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * @throws \Exception |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getIssuer(): string |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function getNameID(): array |
||
| 325 | { |
||
| 326 | $nameID = []; |
||
| 327 | |||
| 328 | $query = '/shibp:Response/shib:Assertion/shib:AuthenticationStatement/shib:Subject/shib:NameIdentifier'; |
||
| 329 | $nodelist = $this->doXPathQuery($query); |
||
| 330 | |||
| 331 | if ($node = $nodelist->item(0)) { |
||
| 332 | $nameID["Value"] = $node->nodeValue; |
||
| 333 | $nameID["Format"] = $node->getAttribute('Format'); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $nameID; |
||
| 337 | } |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * Build a authentication response. |
||
| 342 | * |
||
| 343 | * @param \SimpleSAML\Configuration $idp Metadata for the IdP the response is sent from. |
||
| 344 | * @param \SimpleSAML\Configuration $sp Metadata for the SP the response is sent to. |
||
| 345 | * @param string $shire The endpoint on the SP the response is sent to. |
||
| 346 | * @param array|null $attributes The attributes which should be included in the response. |
||
| 347 | * @return string The response. |
||
| 348 | */ |
||
| 349 | public function generate(Configuration $idp, Configuration $sp, string $shire, ?array $attributes): string |
||
| 350 | { |
||
| 351 | if ($sp->hasValue('scopedattributes')) { |
||
| 352 | $scopedAttributes = $sp->getArray('scopedattributes'); |
||
| 353 | } elseif ($idp->hasValue('scopedattributes')) { |
||
| 354 | $scopedAttributes = $idp->getArray('scopedattributes'); |
||
| 355 | } else { |
||
| 356 | $scopedAttributes = []; |
||
| 357 | } |
||
| 358 | |||
| 359 | $randomUtils = new Random(); |
||
| 360 | $timeUtils = new Utils\Time(); |
||
| 361 | |||
| 362 | $id = $randomUtils->generateID(); |
||
| 363 | $issueInstant = $timeUtils->generateTimestamp(); |
||
| 364 | |||
| 365 | // 30 seconds timeskew back in time to allow differing clocks |
||
| 366 | $notBefore = $timeUtils->generateTimestamp(time() - 30); |
||
| 367 | |||
| 368 | $assertionExpire = $timeUtils->generateTimestamp(time() + 300); // 5 minutes |
||
| 369 | $assertionid = $randomUtils->generateID(); |
||
| 370 | |||
| 371 | $spEntityId = $sp->getString('entityid'); |
||
| 372 | |||
| 373 | $audience = $sp->getOptionalString('audience', $spEntityId); |
||
| 374 | $base64 = $sp->getOptionalBoolean('base64attributes', false); |
||
| 375 | |||
| 376 | $namequalifier = $sp->getOptionalString('NameQualifier', $spEntityId); |
||
| 377 | $nameid = $randomUtils->generateID(); |
||
| 378 | $subjectNode = |
||
| 379 | '<Subject>' . |
||
| 380 | '<NameIdentifier' . |
||
| 381 | ' Format="urn:mace:shibboleth:1.0:nameIdentifier"' . |
||
| 382 | ' NameQualifier="' . htmlspecialchars($namequalifier) . '"' . |
||
| 383 | '>' . |
||
| 384 | htmlspecialchars($nameid) . |
||
| 385 | '</NameIdentifier>' . |
||
| 386 | '<SubjectConfirmation>' . |
||
| 387 | '<ConfirmationMethod>' . |
||
| 388 | 'urn:oasis:names:tc:SAML:1.0:cm:bearer' . |
||
| 389 | '</ConfirmationMethod>' . |
||
| 390 | '</SubjectConfirmation>' . |
||
| 391 | '</Subject>'; |
||
| 392 | |||
| 393 | $encodedattributes = ''; |
||
| 394 | |||
| 395 | if (is_array($attributes)) { |
||
| 396 | $encodedattributes .= '<AttributeStatement>'; |
||
| 397 | $encodedattributes .= $subjectNode; |
||
| 398 | |||
| 399 | foreach ($attributes as $name => $value) { |
||
| 400 | $encodedattributes .= $this->encAttribute($name, $value, $base64, $scopedAttributes); |
||
| 401 | } |
||
| 402 | |||
| 403 | $encodedattributes .= '</AttributeStatement>'; |
||
| 404 | } |
||
| 405 | |||
| 406 | /* |
||
| 407 | * The SAML 1.1 response message |
||
| 408 | */ |
||
| 409 | $response = '<Response xmlns="urn:oasis:names:tc:SAML:1.0:protocol" |
||
| 410 | xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" |
||
| 411 | xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol" xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
||
| 412 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" IssueInstant="' . $issueInstant . '" |
||
| 413 | MajorVersion="1" MinorVersion="1" |
||
| 414 | Recipient="' . htmlspecialchars($shire) . '" ResponseID="' . $id . '"> |
||
| 415 | <Status> |
||
| 416 | <StatusCode Value="samlp:Success" /> |
||
| 417 | </Status> |
||
| 418 | <Assertion xmlns="urn:oasis:names:tc:SAML:1.0:assertion" |
||
| 419 | AssertionID="' . $assertionid . '" IssueInstant="' . $issueInstant . '" |
||
| 420 | Issuer="' . htmlspecialchars($idp->getString('entityid')) . '" MajorVersion="1" MinorVersion="1"> |
||
| 421 | <Conditions NotBefore="' . $notBefore . '" NotOnOrAfter="' . $assertionExpire . '"> |
||
| 422 | <AudienceRestrictionCondition> |
||
| 423 | <Audience>' . htmlspecialchars($audience) . '</Audience> |
||
| 424 | </AudienceRestrictionCondition> |
||
| 425 | </Conditions> |
||
| 426 | <AuthenticationStatement AuthenticationInstant="' . $issueInstant . '" |
||
| 427 | AuthenticationMethod="urn:oasis:names:tc:SAML:1.0:am:unspecified">' . |
||
| 428 | $subjectNode . ' |
||
| 429 | </AuthenticationStatement> |
||
| 430 | ' . $encodedattributes . ' |
||
| 431 | </Assertion> |
||
| 432 | </Response>'; |
||
| 433 | |||
| 434 | return $response; |
||
| 435 | } |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * Format a shib13 attribute. |
||
| 440 | * |
||
| 441 | * @param string $name Name of the attribute. |
||
| 442 | * @param array $values Values of the attribute (as an array of strings). |
||
| 443 | * @param bool $base64 Whether the attriubte values should be base64-encoded. |
||
| 444 | * @param array $scopedAttributes Array of attributes names which are scoped. |
||
| 445 | * @return string The attribute encoded as an XML-string. |
||
| 446 | */ |
||
| 447 | private function encAttribute(string $name, array $values, bool $base64, array $scopedAttributes): string |
||
| 448 | { |
||
| 449 | if (in_array($name, $scopedAttributes, true)) { |
||
| 450 | $scoped = true; |
||
| 451 | } else { |
||
| 452 | $scoped = false; |
||
| 453 | } |
||
| 454 | |||
| 455 | $attr = '<Attribute AttributeName="' . htmlspecialchars($name) . |
||
| 456 | '" AttributeNamespace="urn:mace:shibboleth:1.0:attributeNamespace:uri">'; |
||
| 457 | foreach ($values as $value) { |
||
| 458 | $scopePart = ''; |
||
| 459 | if ($scoped) { |
||
| 460 | $tmp = explode('@', $value, 2); |
||
| 461 | if (count($tmp) === 2) { |
||
| 462 | $value = $tmp[0]; |
||
| 463 | $scopePart = ' Scope="' . htmlspecialchars($tmp[1]) . '"'; |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | if ($base64) { |
||
| 468 | $value = base64_encode($value); |
||
| 469 | } |
||
| 470 | |||
| 471 | $attr .= '<AttributeValue' . $scopePart . '>' . htmlspecialchars($value) . '</AttributeValue>'; |
||
| 472 | } |
||
| 473 | $attr .= '</Attribute>'; |
||
| 474 | |||
| 475 | return $attr; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Check if we are currently between the given date & time conditions. |
||
| 480 | * |
||
| 481 | * Note that this function allows a 10-minute leap from the initial time as marked by $start. |
||
| 482 | * |
||
| 483 | * @param string|null $start A SAML2 timestamp marking the start of the period to check. Defaults to null, in which |
||
| 484 | * case there's no limitations in the past. |
||
| 485 | * @param string|null $end A SAML2 timestamp marking the end of the period to check. Defaults to null, in which |
||
| 486 | * case there's no limitations in the future. |
||
| 487 | * |
||
| 488 | * @return bool True if the current time belongs to the period specified by $start and $end. False otherwise. |
||
| 489 | * |
||
| 490 | * @see \SAML2\Utils::xsDateTimeToTimestamp. |
||
| 491 | * |
||
| 492 | */ |
||
| 493 | protected static function checkDateConditions(string $start = null, string $end = null): bool |
||
| 511 | } |
||
| 512 | } |
||
| 513 |