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\Exception\InvalidDOMElementException; |
||
10 | use SimpleSAML\XML\Exception\SchemaViolationException; |
||
11 | use SimpleSAML\XML\Exception\TooManyElementsException; |
||
12 | use SimpleSAML\XML\ExtendableElementTrait; |
||
13 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
14 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
15 | use SimpleSAML\XML\XsNamespace as 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 | |||
23 | /** |
||
24 | * Class representing a ds:SignatureMethod element. |
||
25 | * |
||
26 | * @package simplesamlphp/xml-security |
||
27 | */ |
||
28 | final class SignatureMethod extends AbstractDsElement implements SchemaValidatableElementInterface |
||
29 | { |
||
30 | use ExtendableElementTrait; |
||
31 | use SchemaValidatableElementTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
32 | |||
33 | /** The namespace-attribute for the xs:any element */ |
||
34 | public const XS_ANY_ELT_NAMESPACE = NS::OTHER; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Initialize a SignatureMethod element. |
||
39 | * |
||
40 | * @param string $Algorithm |
||
41 | * @param \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null $hmacOutputLength |
||
42 | * @param array<\SimpleSAML\XML\SerializableElementInterface> $children |
||
43 | */ |
||
44 | public function __construct( |
||
45 | protected string $Algorithm, |
||
46 | protected ?HMACOutputLength $hmacOutputLength = null, |
||
47 | array $children = [], |
||
48 | ) { |
||
49 | Assert::validURI($Algorithm, SchemaViolationException::class); |
||
50 | Assert::oneOf( |
||
51 | $Algorithm, |
||
52 | array_merge( |
||
53 | array_keys(C::$RSA_DIGESTS), |
||
54 | array_keys(C::$HMAC_DIGESTS), |
||
55 | ), |
||
56 | 'Invalid signature method: %s', |
||
57 | InvalidArgumentException::class, |
||
58 | ); |
||
59 | |||
60 | $this->setElements($children); |
||
61 | } |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Collect the value of the Algorithm-property |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getAlgorithm(): string |
||
70 | { |
||
71 | return $this->Algorithm; |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Collect the value of the hmacOutputLength-property |
||
77 | * |
||
78 | * @return \SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength|null |
||
79 | */ |
||
80 | public function getHMACOutputLength(): ?HMACOutputLength |
||
81 | { |
||
82 | return $this->hmacOutputLength; |
||
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Convert XML into a SignatureMethod |
||
88 | * |
||
89 | * @param \DOMElement $xml The XML element we should load |
||
90 | * @return static |
||
91 | * |
||
92 | * @throws \SimpleSAML\XML\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 = SignatureMethod::getAttribute($xml, 'Algorithm'); |
||
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 | * @return \DOMElement |
||
114 | */ |
||
115 | public function toXML(?DOMElement $parent = null): DOMElement |
||
116 | { |
||
117 | $e = $this->instantiateParentElement($parent); |
||
118 | $e->setAttribute('Algorithm', $this->getAlgorithm()); |
||
119 | |||
120 | $this->getHMACOutputLength()?->toXML($e); |
||
121 | |||
122 | foreach ($this->getElements() as $elt) { |
||
123 | $elt->toXML($e); |
||
124 | } |
||
125 | |||
126 | return $e; |
||
127 | } |
||
128 | } |
||
129 |