6
6
7
7
use DOMElement ;
8
8
use SimpleSAML \Assert \Assert ;
9
- use SimpleSAML \XML \Chunk ;
10
9
use SimpleSAML \XML \Exception \InvalidDOMElementException ;
10
+ use SimpleSAML \XML \ExtendableElementTrait ;
11
+ use SimpleSAML \XML \SerializableElementInterface ;
12
+ use SimpleSAML \XML \XsNamespace as NS ;
11
13
use SimpleSAML \XMLSecurity \Constants as C ;
12
14
use SimpleSAML \XMLSecurity \Exception \InvalidArgumentException ;
13
- use SimpleSAML \XMLSecurity \XML \dsig11 \KeyInfoReference ;
14
- use SimpleSAML \XMLSecurity \XML \xenc \EncryptedData ;
15
- use SimpleSAML \XMLSecurity \XML \xenc \EncryptedKey ;
16
15
17
16
/**
18
17
* Class representing a ds:KeyInfo element.
21
20
*/
22
21
final class KeyInfo extends AbstractDsElement
23
22
{
23
+ use ExtendableElementTrait;
24
+
25
+ /** @var \SimpleSAML\XML\XsNamespace */
26
+ public const XS_ANY_ELT_NAMESPACE = NS ::OTHER ;
27
+
28
+
24
29
/**
25
30
* Initialize a KeyInfo element.
26
31
*
27
32
* @param (
28
- * \SimpleSAML\XML\SerializableElementInterface|
29
33
* \SimpleSAML\XMLSecurity\XML\ds\KeyName|
30
34
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
31
35
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
@@ -34,29 +38,26 @@ final class KeyInfo extends AbstractDsElement
34
38
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
35
39
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
36
40
* )[] $info
41
+ * @param \SimpleSAML\XML\SerializableElementInterface[] $children
37
42
* @param string|null $Id
38
43
*/
39
44
public function __construct (
40
45
protected array $ info ,
46
+ array $ children = [],
41
47
protected ?string $ Id = null ,
42
48
) {
43
- Assert::notEmpty ($ info , 'ds:KeyInfo cannot be empty ' , InvalidArgumentException::class);
44
- Assert::maxCount ($ info , C::UNBOUNDED_LIMIT );
45
- Assert::allIsInstanceOfAny (
46
- $ info ,
47
- [
48
- Chunk::class,
49
- KeyName::class,
50
- KeyValue::class,
51
- RetrievalMethod::class,
52
- X509Data::class,
53
- EncryptedData::class,
54
- EncryptedKey::class,
55
- ],
56
- 'KeyInfo can only contain instances of KeyName, X509Data, EncryptedKey or Chunk. ' ,
49
+ $ combi = array_merge ($ info , $ children );
50
+
51
+ Assert::notEmpty ($ combi , 'ds:KeyInfo cannot be empty ' , InvalidArgumentException::class);
52
+ Assert::maxCount ($ combi , C::UNBOUNDED_LIMIT );
53
+ Assert::allIsInstanceOf (
54
+ $ combi ,
55
+ SerializableElementInterface::class,
57
56
InvalidArgumentException::class,
58
57
);
59
58
Assert::nullOrValidNCName ($ Id );
59
+
60
+ $ this ->setElements ($ children );
60
61
}
61
62
62
63
@@ -74,20 +75,11 @@ public function getId(): ?string
74
75
/**
75
76
* Collect the value of the info-property
76
77
*
77
- * @return (
78
- * \SimpleSAML\XML\SerializableElementInterface|
79
- * \SimpleSAML\XMLSecurity\XML\ds\KeyName|
80
- * \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
81
- * \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
82
- * \SimpleSAML\XMLSecurity\XML\ds\X509Data|
83
- * \SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference|
84
- * \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
85
- * \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
86
- * )[]
78
+ * @return list<\SimpleSAML\XML\SerializableElementInterface>
87
79
*/
88
80
public function getInfo (): array
89
81
{
90
- return $ this ->info ;
82
+ return array_merge ( $ this ->info , $ this -> getElements ()) ;
91
83
}
92
84
93
85
@@ -106,36 +98,27 @@ public static function fromXML(DOMElement $xml): static
106
98
Assert::same ($ xml ->namespaceURI , KeyInfo::NS , InvalidDOMElementException::class);
107
99
108
100
$ Id = self ::getOptionalAttribute ($ xml , 'Id ' , null );
109
- $ info = [];
110
-
111
- foreach ($ xml ->childNodes as $ n ) {
112
- if (!($ n instanceof DOMElement)) {
113
- continue ;
114
- } elseif ($ n ->namespaceURI === C::NS_XDSIG ) {
115
- $ info [] = match ($ n ->localName ) {
116
- 'KeyName ' => KeyName::fromXML ($ n ),
117
- 'KeyValue ' => KeyValue::fromXML ($ n ),
118
- 'RetrievalMethod ' => RetrievalMethod::fromXML ($ n ),
119
- 'X509Data ' => X509Data::fromXML ($ n ),
120
- default => new Chunk ($ n ),
121
- };
122
- } elseif ($ n ->namespaceURI === C::NS_XDSIG11 ) {
123
- $ info [] = match ($ n ->localName ) {
124
- 'KeyInfoReference ' => KeyInfoReference::fromXML ($ n ),
125
- default => new Chunk ($ n ),
126
- };
127
- } elseif ($ n ->namespaceURI === C::NS_XENC ) {
128
- $ info [] = match ($ n ->localName ) {
129
- 'EncryptedData ' => EncryptedData::fromXML ($ n ),
130
- 'EncryptedKey ' => EncryptedKey::fromXML ($ n ),
131
- default => new Chunk ($ n ),
132
- };
133
- } else {
134
- $ info [] = new Chunk ($ n );
135
- }
136
- }
137
101
138
- return new static ($ info , $ Id );
102
+ $ keyName = KeyName::getChildrenOfClass ($ xml );
103
+ $ keyValue = KeyValue::getChildrenOfClass ($ xml );
104
+ $ retrievalMethod = RetrievalMethod::getChildrenOfClass ($ xml );
105
+ $ x509Data = X509Data::getChildrenOfClass ($ xml );
106
+ //$pgpData = PGPData::getChildrenOfClass($xml);
107
+ //$spkiData = SPKIData::getChildrenOfClass($xml);
108
+ //$mgmtData = MgmtData::getChildrenOfClass($xml);
109
+
110
+ $ info = array_merge (
111
+ $ keyName ,
112
+ $ keyValue ,
113
+ $ retrievalMethod ,
114
+ $ x509Data ,
115
+ //$pgpdata,
116
+ //$spkidata,
117
+ //$mgmtdata,
118
+ );
119
+
120
+ $ children = self ::getChildElementsFromXML ($ xml );
121
+ return new static ($ info , $ children , $ Id );
139
122
}
140
123
141
124
@@ -153,8 +136,8 @@ public function toXML(DOMElement $parent = null): DOMElement
153
136
$ e ->setAttribute ('Id ' , $ this ->getId ());
154
137
}
155
138
156
- foreach ($ this ->getInfo () as $ n ) {
157
- $ n ->toXML ($ e );
139
+ foreach ($ this ->getInfo () as $ elt ) {
140
+ $ elt ->toXML ($ e );
158
141
}
159
142
160
143
return $ e ;
0 commit comments