66
77use DOMElement ;
88use SimpleSAML \Assert \Assert ;
9- use SimpleSAML \XML \ElementInterface ;
109use SimpleSAML \XML \Exception \InvalidDOMElementException ;
1110use SimpleSAML \XML \Exception \SchemaViolationException ;
1211use SimpleSAML \XML \Exception \TooManyElementsException ;
1312use SimpleSAML \XML \ExtendableElementTrait ;
1413use SimpleSAML \XML \SchemaValidatableElementInterface ;
1514use SimpleSAML \XML \SchemaValidatableElementTrait ;
15+ use SimpleSAML \XML \SerializableElementInterface ;
1616use SimpleSAML \XML \XsNamespace as NS ;
17+ use SimpleSAML \XMLSecurity \Constants as C ;
18+
19+ use function array_merge ;
20+ use function array_pop ;
1721
1822/**
1923 * Class representing a ds:KeyValue element.
2226 */
2327final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface
2428{
25- use ExtendableElementTrait;
29+ use ExtendableElementTrait {
30+ // We use our own getter instead of the trait's one
31+ getElements as private ;
32+ setElements as private ;
33+ }
2634 use SchemaValidatableElementTrait;
2735
2836
@@ -33,33 +41,41 @@ final class KeyValue extends AbstractDsElement implements SchemaValidatableEleme
3341 /**
3442 * Initialize an KeyValue.
3543 *
36- * @param \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null $RSAKeyValue
37- * @param \SimpleSAML\XML\SerializableElementInterface|null $element
44+ * @param \SimpleSAML\XML\SerializableElementInterface $keyValue
3845 */
3946 final public function __construct (
40- protected ?RSAKeyValue $ RSAKeyValue ,
41- ?ElementInterface $ element = null ,
47+ protected RSAKeyValue |DSAKeyValue |ECKeyValue |SerializableElementInterface $ keyValue ,
4248 ) {
43- Assert::false (
44- is_null ($ RSAKeyValue ) && is_null ($ element ),
45- 'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other ' ,
46- SchemaViolationException::class,
47- );
48-
49- if ($ element !== null ) {
50- $ this ->setElements ([$ element ]);
49+ if (!(
50+ $ keyValue instanceof RSAKeyValue
51+ || $ keyValue instanceof DSAKeyValue
52+ || $ keyValue instanceof ECKeyValue
53+ )) {
54+ Assert::true (
55+ (
56+ ($ keyValue instanceof Chunk)
57+ ? $ keyValue ->getNamespaceURI ()
58+ : $ keyValue ::getNameSpaceURI ()
59+ ) !== C::NS_XDSIG ,
60+ 'A <ds:KeyValue> requires either a RSAKeyValue, DSAKeyValue, ECKeyValue '
61+ . 'or an element in namespace ##other ' ,
62+ SchemaViolationException::class,
63+ );
5164 }
5265 }
5366
5467
5568 /**
5669 * Collect the value of the RSAKeyValue-property
5770 *
58- * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null
71+ * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|
72+ * \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue|
73+ * \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue|
74+ * \SimpeSAML\XML\SerializableElementInterface
5975 */
60- public function getRSAKeyValue (): ? RSAKeyValue
76+ public function getKeyValue (): RSAKeyValue | DSAKeyValue | ECKeyValue | SerializableElementInterface
6177 {
62- return $ this ->RSAKeyValue ;
78+ return $ this ->keyValue ;
6379 }
6480
6581
@@ -77,23 +93,20 @@ public static function fromXML(DOMElement $xml): static
7793 Assert::same ($ xml ->localName , 'KeyValue ' , InvalidDOMElementException::class);
7894 Assert::same ($ xml ->namespaceURI , KeyValue::NS , InvalidDOMElementException::class);
7995
80- $ RSAKeyValue = RSAKeyValue::getChildrenOfClass ($ xml );
81- Assert::maxCount (
82- $ RSAKeyValue ,
83- 1 ,
84- 'A <ds:KeyValue> can contain exactly one <ds:RSAKeyValue> ' ,
85- TooManyElementsException::class,
96+ $ keyValue = array_merge (
97+ RSAKeyValue::getChildrenOfClass ($ xml ),
98+ DSAKeyValue::getChildrenOfClass ($ xml ),
99+ self ::getChildElementsFromXML ($ xml ),
86100 );
87101
88- $ elements = self ::getChildElementsFromXML ($ xml );
89- Assert::maxCount (
90- $ elements ,
102+ Assert::count (
103+ $ keyValue ,
91104 1 ,
92- 'A <ds:KeyValue> can contain exactly one element in namespace ##other ' ,
105+ 'A <ds:KeyValue> must contain exactly one child element ' ,
93106 TooManyElementsException::class,
94107 );
95108
96- return new static (array_pop ($ RSAKeyValue ), array_pop ( $ elements ));
109+ return new static (array_pop ($ keyValue ));
97110 }
98111
99112
@@ -107,13 +120,7 @@ public function toXML(?DOMElement $parent = null): DOMElement
107120 {
108121 $ e = $ this ->instantiateParentElement ($ parent );
109122
110- $ this ->getRSAKeyValue ()?->toXML($ e );
111-
112- foreach ($ this ->elements as $ elt ) {
113- if (!$ elt ->isEmptyElement ()) {
114- $ elt ->toXML ($ e );
115- }
116- }
123+ $ this ->getKeyValue ()->toXML ($ e );
117124
118125 return $ e ;
119126 }
0 commit comments