|
| 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\XMLSecurity\XML\ds\Signature; |
| 22 | + |
| 23 | +use function array_pop; |
| 24 | +use function version_compare; |
| 25 | + |
| 26 | +/** |
| 27 | + * Class for handling SAML2 NameIDMappingRequest. |
| 28 | + * |
| 29 | + * @package simplesamlphp/saml2 |
| 30 | + */ |
| 31 | +final class NameIDMappingRequest extends AbstractNameIDMappingRequest implements |
| 32 | + SchemaValidatableElementInterface |
| 33 | +{ |
| 34 | + use SchemaValidatableElementTrait; |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * Convert XML into a NameIDMappingRequest |
| 39 | + * |
| 40 | + * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
| 41 | + * if the qualified name of the supplied element is wrong |
| 42 | + */ |
| 43 | + public static function fromXML(DOMElement $xml): static |
| 44 | + { |
| 45 | + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); |
| 46 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 47 | + |
| 48 | + $version = self::getAttribute($xml, 'Version', SAMLStringValue::class); |
| 49 | + Assert::true(version_compare('2.0', strval($version), '<='), RequestVersionTooLowException::class); |
| 50 | + Assert::true(version_compare('2.0', strval($version), '>='), RequestVersionTooHighException::class); |
| 51 | + |
| 52 | + $issuer = Issuer::getChildrenOfClass($xml); |
| 53 | + Assert::maxCount($issuer, 1, 'Only one <saml:Issuer> element is allowed.', TooManyElementsException::class); |
| 54 | + |
| 55 | + $extensions = Extensions::getChildrenOfClass($xml); |
| 56 | + Assert::maxCount( |
| 57 | + $extensions, |
| 58 | + 1, |
| 59 | + 'Only one <samlp:Extensions> element is allowed.', |
| 60 | + TooManyElementsException::class, |
| 61 | + ); |
| 62 | + |
| 63 | + $signature = Signature::getChildrenOfClass($xml); |
| 64 | + Assert::maxCount( |
| 65 | + $signature, |
| 66 | + 1, |
| 67 | + 'Only one <ds:Signature> element is allowed.', |
| 68 | + TooManyElementsException::class, |
| 69 | + ); |
| 70 | + |
| 71 | + $nameIdPolicy = NameIDPolicy::getChildrenOfClass($xml); |
| 72 | + Assert::minCount($nameIdPolicy, 1, MissingElementException::class); |
| 73 | + Assert::maxCount($nameIdPolicy, 1, TooManyElementsException::class); |
| 74 | + |
| 75 | + $request = new static( |
| 76 | + self::getIdentifierFromXML($xml), |
| 77 | + array_pop($nameIdPolicy), |
| 78 | + self::getAttribute($xml, 'ID', IDValue::class), |
| 79 | + array_pop($issuer), |
| 80 | + self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class), |
| 81 | + self::getOptionalAttribute($xml, 'Destination', SAMLAnyURIValue::class, null), |
| 82 | + self::getOptionalAttribute($xml, 'Consent', SAMLAnyURIValue::class, null), |
| 83 | + array_pop($extensions), |
| 84 | + ); |
| 85 | + |
| 86 | + if (!empty($signature)) { |
| 87 | + $request->setSignature($signature[0]); |
| 88 | + $request->messageContainedSignatureUponConstruction = true; |
| 89 | + $request->setXML($xml); |
| 90 | + } |
| 91 | + |
| 92 | + return $request; |
| 93 | + } |
| 94 | +} |
0 commit comments