simplesamlphp /
xml-security
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\XMLSecurity\XML\ds; |
||
| 6 | |||
| 7 | use DOMElement; |
||
| 8 | use SimpleSAML\Assert\Assert; |
||
| 9 | use SimpleSAML\XML\ExtendableElementTrait; |
||
| 10 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
| 11 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 12 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 13 | use SimpleSAML\XMLSchema\Exception\TooManyElementsException; |
||
| 14 | use SimpleSAML\XMLSchema\Type\AnyURIValue; |
||
| 15 | use SimpleSAML\XMLSchema\XML\Constants\NS; |
||
| 16 | use SimpleSAML\XMLSecurity\Constants as C; |
||
| 17 | use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
||
| 18 | |||
| 19 | use function array_keys; |
||
| 20 | use function array_merge; |
||
| 21 | use function array_pop; |
||
| 22 | use function strval; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class representing a ds:SignatureMethod element. |
||
| 26 | * |
||
| 27 | * @package simplesamlphp/xml-security |
||
| 28 | */ |
||
| 29 | final class SignatureMethod extends AbstractDsElement implements SchemaValidatableElementInterface |
||
| 30 | { |
||
| 31 | use ExtendableElementTrait; |
||
| 32 | use SchemaValidatableElementTrait; |
||
| 33 | |||
| 34 | |||
| 35 | /** The namespace-attribute for the xs:any element */ |
||
| 36 | public const string XS_ANY_ELT_NAMESPACE = NS::OTHER; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Initialize a SignatureMethod element. |
||
| 41 | * |
||
| 42 | * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $Algorithm |
||
| 43 | * @param \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null $hmacOutputLength |
||
| 44 | * @param array<\SimpleSAML\XML\SerializableElementInterface> $children |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 47 | protected AnyURIValue $Algorithm, |
||
| 48 | protected ?HMACOutputLength $hmacOutputLength = null, |
||
| 49 | array $children = [], |
||
| 50 | ) { |
||
| 51 | Assert::oneOf( |
||
| 52 | $Algorithm->getValue(), |
||
| 53 | array_merge( |
||
| 54 | array_keys(C::$RSA_DIGESTS), |
||
| 55 | array_keys(C::$HMAC_DIGESTS), |
||
| 56 | ), |
||
| 57 | 'Invalid signature method: %s', |
||
| 58 | InvalidArgumentException::class, |
||
| 59 | ); |
||
| 60 | |||
| 61 | $this->setElements($children); |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Collect the value of the Algorithm-property |
||
| 67 | * |
||
| 68 | * @return \SimpleSAML\XMLSchema\Type\AnyURIValue |
||
| 69 | */ |
||
| 70 | public function getAlgorithm(): AnyURIValue |
||
| 71 | { |
||
| 72 | return $this->Algorithm; |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Collect the value of the hmacOutputLength-property |
||
| 78 | * |
||
| 79 | * @return \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null |
||
| 80 | */ |
||
| 81 | public function getHMACOutputLength(): ?HMACOutputLength |
||
| 82 | { |
||
| 83 | return $this->hmacOutputLength; |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Convert XML into a SignatureMethod |
||
| 89 | * |
||
| 90 | * @param \DOMElement $xml The XML element we should load |
||
| 91 | * |
||
| 92 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 93 | * If the qualified name of the supplied element is wrong |
||
| 94 | */ |
||
| 95 | public static function fromXML(DOMElement $xml): static |
||
| 96 | { |
||
| 97 | Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class); |
||
| 98 | Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class); |
||
| 99 | |||
| 100 | $Algorithm = self::getAttribute($xml, 'Algorithm', AnyURIValue::class); |
||
| 101 | |||
| 102 | $hmacOutputLength = HMACOutputLength::getChildrenOfClass($xml); |
||
| 103 | Assert::maxCount($hmacOutputLength, 1, TooManyElementsException::class); |
||
| 104 | |||
| 105 | return new static($Algorithm, array_pop($hmacOutputLength), self::getChildElementsFromXML($xml)); |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Convert this SignatureMethod element to XML. |
||
| 111 | * |
||
| 112 | * @param \DOMElement|null $parent The element we should append this SignatureMethod element to. |
||
| 113 | */ |
||
| 114 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 115 | { |
||
| 116 | $e = $this->instantiateParentElement($parent); |
||
| 117 | $e->setAttribute('Algorithm', strval($this->getAlgorithm())); |
||
| 118 | |||
| 119 | $this->getHMACOutputLength()?->toXML($e); |
||
| 120 | |||
| 121 | foreach ($this->getElements() as $elt) { |
||
| 122 | $elt->toXML($e); |
||
| 123 | } |
||
| 124 | |||
| 125 | return $e; |
||
| 126 | } |
||
| 127 | } |
||
| 128 |