|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\SAML2\XML\samlp; |
| 6 | + |
| 7 | +use DOMElement; |
| 8 | +use SimpleSAML\SAML2\Assert\Assert; |
| 9 | +use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooHighException; |
| 10 | +use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooLowException; |
| 11 | +use SimpleSAML\SAML2\Type\SAMLAnyURIValue; |
| 12 | +use SimpleSAML\SAML2\Type\SAMLDateTimeValue; |
| 13 | +use SimpleSAML\SAML2\Type\SAMLStringValue; |
| 14 | +use SimpleSAML\SAML2\XML\saml\Issuer; |
| 15 | +use SimpleSAML\XML\SchemaValidatableElementInterface; |
| 16 | +use SimpleSAML\XML\SchemaValidatableElementTrait; |
| 17 | +use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
| 18 | +use SimpleSAML\XMLSchema\Exception\MissingElementException; |
| 19 | +use SimpleSAML\XMLSchema\Exception\TooManyElementsException; |
| 20 | +use SimpleSAML\XMLSchema\Type\IDValue; |
| 21 | +use SimpleSAML\XMLSchema\Type\NCNameValue; |
| 22 | +use SimpleSAML\XMLSecurity\XML\ds\Signature; |
| 23 | + |
| 24 | +use function array_pop; |
| 25 | +use function strval; |
| 26 | + |
| 27 | +/** |
| 28 | + * Class for SAML 2 ManageNameIDResponse messages. |
| 29 | + * |
| 30 | + * @package simplesamlphp/saml2 |
| 31 | + */ |
| 32 | +class ManageNameIDResponse extends AbstractStatusResponse implements SchemaValidatableElementInterface |
| 33 | +{ |
| 34 | + use SchemaValidatableElementTrait; |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * Constructor for SAML 2 response messages. |
| 39 | + * |
| 40 | + * @param \SimpleSAML\XMLSchema\Type\IDValue $id |
| 41 | + * @param \SimpleSAML\SAML2\XML\samlp\Status $status |
| 42 | + * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue $issueInstant |
| 43 | + * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer |
| 44 | + * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $inResponseTo |
| 45 | + * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $destination |
| 46 | + * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $consent |
| 47 | + * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions |
| 48 | + */ |
| 49 | + final public function __construct( |
| 50 | + IDValue $id, |
| 51 | + Status $status, |
| 52 | + SAMLDateTimeValue $issueInstant, |
| 53 | + ?Issuer $issuer = null, |
| 54 | + ?NCNameValue $inResponseTo = null, |
| 55 | + ?SAMLAnyURIValue $destination = null, |
| 56 | + ?SAMLAnyURIValue $consent = null, |
| 57 | + ?Extensions $extensions = null, |
| 58 | + ) { |
| 59 | + parent::__construct( |
| 60 | + $id, |
| 61 | + $status, |
| 62 | + $issueInstant, |
| 63 | + $issuer, |
| 64 | + $inResponseTo, |
| 65 | + $destination, |
| 66 | + $consent, |
| 67 | + $extensions, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Convert XML into a ManageNameIDResponse element. |
| 74 | + * |
| 75 | + * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
| 76 | + * if the qualified name of the supplied element is wrong |
| 77 | + * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException |
| 78 | + * if the supplied element is missing one of the mandatory attributes |
| 79 | + * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException |
| 80 | + * if one of the mandatory child-elements is missing |
| 81 | + */ |
| 82 | + public static function fromXML(DOMElement $xml): static |
| 83 | + { |
| 84 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 85 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 86 | + |
| 87 | + $version = self::getAttribute($xml, 'Version', SAMLStringValue::class); |
| 88 | + Assert::true(version_compare('2.0', strval($version), '<='), RequestVersionTooLowException::class); |
| 89 | + Assert::true(version_compare('2.0', strval($version), '>='), RequestVersionTooHighException::class); |
| 90 | + |
| 91 | + $signature = Signature::getChildrenOfClass($xml); |
| 92 | + Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class); |
| 93 | + |
| 94 | + $issuer = Issuer::getChildrenOfClass($xml); |
| 95 | + Assert::countBetween($issuer, 0, 1); |
| 96 | + |
| 97 | + $status = Status::getChildrenOfClass($xml); |
| 98 | + Assert::minCount($status, 1, MissingElementException::class); |
| 99 | + Assert::maxCount($status, 1, TooManyElementsException::class); |
| 100 | + |
| 101 | + $extensions = Extensions::getChildrenOfClass($xml); |
| 102 | + Assert::maxCount( |
| 103 | + $extensions, |
| 104 | + 1, |
| 105 | + 'Only one saml:Extensions element is allowed.', |
| 106 | + TooManyElementsException::class, |
| 107 | + ); |
| 108 | + |
| 109 | + $response = new static( |
| 110 | + self::getAttribute($xml, 'ID', IDValue::class), |
| 111 | + array_pop($status), |
| 112 | + self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class), |
| 113 | + empty($issuer) ? null : array_pop($issuer), |
| 114 | + self::getOptionalAttribute($xml, 'InResponseTo', NCNameValue::class, null), |
| 115 | + self::getOptionalAttribute($xml, 'Destination', SAMLAnyURIValue::class, null), |
| 116 | + self::getOptionalAttribute($xml, 'Consent', SAMLAnyURIValue::class, null), |
| 117 | + empty($extensions) ? null : array_pop($extensions), |
| 118 | + ); |
| 119 | + |
| 120 | + if (!empty($signature)) { |
| 121 | + $response->setSignature($signature[0]); |
| 122 | + $response->messageContainedSignatureUponConstruction = true; |
| 123 | + $response->setXML($xml); |
| 124 | + } |
| 125 | + |
| 126 | + return $response; |
| 127 | + } |
| 128 | +} |
0 commit comments