6
6
7
7
use DOMElement ;
8
8
use SimpleSAML \Assert \Assert ;
9
- use SimpleSAML \XML \ElementInterface ;
9
+ use SimpleSAML \XML \Chunk ;
10
10
use SimpleSAML \XML \Exception \InvalidDOMElementException ;
11
11
use SimpleSAML \XML \Exception \SchemaViolationException ;
12
12
use SimpleSAML \XML \Exception \TooManyElementsException ;
13
13
use SimpleSAML \XML \ExtendableElementTrait ;
14
14
use SimpleSAML \XML \SchemaValidatableElementInterface ;
15
15
use SimpleSAML \XML \SchemaValidatableElementTrait ;
16
+ use SimpleSAML \XML \SerializableElementInterface ;
16
17
use SimpleSAML \XML \XsNamespace as NS ;
18
+ use SimpleSAML \XMLSecurity \Constants as C ;
19
+ use SimpleSAML \XMLSecurity \XML \dsig11 \ECKeyValue ;
20
+
21
+ use function array_merge ;
22
+ use function array_pop ;
17
23
18
24
/**
19
25
* Class representing a ds:KeyValue element.
22
28
*/
23
29
final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface
24
30
{
25
- use ExtendableElementTrait;
31
+ // We use our own getter instead of the trait's one, so we prevent their use by marking them private
32
+ use ExtendableElementTrait {
33
+ getElements as private ;
34
+ setElements as private ;
35
+ }
26
36
use SchemaValidatableElementTrait;
27
37
28
38
@@ -33,33 +43,38 @@ final class KeyValue extends AbstractDsElement implements SchemaValidatableEleme
33
43
/**
34
44
* Initialize an KeyValue.
35
45
*
36
- * @param \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null $RSAKeyValue
37
- * @param \SimpleSAML\XML\SerializableElementInterface|null $element
46
+ * @param \SimpleSAML\XML\SerializableElementInterface $keyValue
38
47
*/
39
48
final public function __construct (
40
- protected ?RSAKeyValue $ RSAKeyValue ,
41
- ?ElementInterface $ element = null ,
49
+ protected RSAKeyValue |DSAKeyValue |ECKeyValue |SerializableElementInterface $ keyValue ,
42
50
) {
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 ]);
51
+ if (
52
+ !($ keyValue instanceof RSAKeyValue
53
+ || $ keyValue instanceof DSAKeyValue
54
+ || $ keyValue instanceof ECKeyValue)
55
+ ) {
56
+ Assert::true (
57
+ (($ keyValue instanceof Chunk) ? $ keyValue ->getNamespaceURI () : $ keyValue ::getNameSpaceURI ())
58
+ !== C::NS_XDSIG ,
59
+ 'A <ds:KeyValue> requires either a RSAKeyValue, DSAKeyValue, ECKeyValue '
60
+ . 'or an element in namespace ##other ' ,
61
+ SchemaViolationException::class,
62
+ );
51
63
}
52
64
}
53
65
54
66
55
67
/**
56
68
* Collect the value of the RSAKeyValue-property
57
69
*
58
- * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null
70
+ * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|
71
+ * \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue|
72
+ * \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue|
73
+ * \SimpeSAML\XML\SerializableElementInterface
59
74
*/
60
- public function getRSAKeyValue (): ? RSAKeyValue
75
+ public function getKeyValue (): RSAKeyValue | DSAKeyValue | ECKeyValue | SerializableElementInterface
61
76
{
62
- return $ this ->RSAKeyValue ;
77
+ return $ this ->keyValue ;
63
78
}
64
79
65
80
@@ -77,23 +92,20 @@ public static function fromXML(DOMElement $xml): static
77
92
Assert::same ($ xml ->localName , 'KeyValue ' , InvalidDOMElementException::class);
78
93
Assert::same ($ xml ->namespaceURI , KeyValue::NS , InvalidDOMElementException::class);
79
94
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,
95
+ $ keyValue = array_merge (
96
+ RSAKeyValue::getChildrenOfClass ($ xml ),
97
+ DSAKeyValue::getChildrenOfClass ($ xml ),
98
+ self ::getChildElementsFromXML ($ xml ),
86
99
);
87
100
88
- $ elements = self ::getChildElementsFromXML ($ xml );
89
- Assert::maxCount (
90
- $ elements ,
101
+ Assert::count (
102
+ $ keyValue ,
91
103
1 ,
92
- 'A <ds:KeyValue> can contain exactly one element in namespace ##other ' ,
104
+ 'A <ds:KeyValue> must contain exactly one child element ' ,
93
105
TooManyElementsException::class,
94
106
);
95
107
96
- return new static (array_pop ($ RSAKeyValue ), array_pop ( $ elements ));
108
+ return new static (array_pop ($ keyValue ));
97
109
}
98
110
99
111
@@ -107,13 +119,7 @@ public function toXML(?DOMElement $parent = null): DOMElement
107
119
{
108
120
$ e = $ this ->instantiateParentElement ($ parent );
109
121
110
- $ this ->getRSAKeyValue ()?->toXML($ e );
111
-
112
- foreach ($ this ->elements as $ elt ) {
113
- if (!$ elt ->isEmptyElement ()) {
114
- $ elt ->toXML ($ e );
115
- }
116
- }
122
+ $ this ->getKeyValue ()->toXML ($ e );
117
123
118
124
return $ e ;
119
125
}
0 commit comments