88use SimpleSAML \Assert \Assert ;
99use SimpleSAML \XML \Exception \InvalidDOMElementException ;
1010use SimpleSAML \XML \Exception \SchemaViolationException ;
11+ use SimpleSAML \XML \Exception \TooManyElementsException ;
12+ use SimpleSAML \XML \ExtendableElementTrait ;
13+ use SimpleSAML \XML \SerializableElementInterface ;
14+ use SimpleSAML \XML \XsNamespace as NS ;
1115use SimpleSAML \XMLSecurity \Constants as C ;
1216use SimpleSAML \XMLSecurity \Exception \InvalidArgumentException ;
1317
18+ use function array_keys ;
19+ use function array_merge ;
20+ use function array_pop ;
21+
1422/**
1523 * Class representing a ds:SignatureMethod element.
1624 *
1725 * @package simplesamlphp/xml-security
1826 */
1927final class SignatureMethod extends AbstractDsElement
2028{
29+ use ExtendableElementTrait;
30+
31+ /** The namespace-attribute for the xs:any element */
32+ public const XS_ANY_ELT_NAMESPACE = NS ::OTHER ;
33+
34+
2135 /**
2236 * Initialize a SignatureMethod element.
2337 *
2438 * @param string $Algorithm
39+ * @param \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null $hmacOutputLength
40+ * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
2541 */
2642 public function __construct (
2743 protected string $ Algorithm ,
44+ protected ?HMACOutputLength $ hmacOutputLength = null ,
45+ array $ children = [],
2846 ) {
2947 Assert::validURI ($ Algorithm , SchemaViolationException::class);
3048 Assert::oneOf (
@@ -36,6 +54,8 @@ public function __construct(
3654 'Invalid signature method: %s ' ,
3755 InvalidArgumentException::class,
3856 );
57+
58+ $ this ->setElements ($ children );
3959 }
4060
4161
@@ -50,6 +70,17 @@ public function getAlgorithm(): string
5070 }
5171
5272
73+ /**
74+ * Collect the value of the hmacOutputLength-property
75+ *
76+ * @return \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null
77+ */
78+ public function getHMACOutputLength (): ?HMACOutputLength
79+ {
80+ return $ this ->hmacOutputLength ;
81+ }
82+
83+
5384 /**
5485 * Convert XML into a SignatureMethod
5586 *
@@ -66,7 +97,10 @@ public static function fromXML(DOMElement $xml): static
6697
6798 $ Algorithm = SignatureMethod::getAttribute ($ xml , 'Algorithm ' );
6899
69- return new static ($ Algorithm );
100+ $ hmacOutputLength = HMACOutputLength::getChildrenOfClass ($ xml );
101+ Assert::maxCount ($ hmacOutputLength , 1 , TooManyElementsException::class);
102+
103+ return new static ($ Algorithm , array_pop ($ hmacOutputLength ), self ::getChildElementsFromXML ($ xml ));
70104 }
71105
72106
@@ -81,6 +115,12 @@ public function toXML(DOMElement $parent = null): DOMElement
81115 $ e = $ this ->instantiateParentElement ($ parent );
82116 $ e ->setAttribute ('Algorithm ' , $ this ->getAlgorithm ());
83117
118+ $ this ->getHMACOutputLength ()?->toXML($ e );
119+
120+ foreach ($ this ->getElements () as $ elt ) {
121+ $ elt ->toXML ($ e );
122+ }
123+
84124 return $ e ;
85125 }
86126}
0 commit comments