Skip to content

Commit a16cb03

Browse files
committed
Leverage new getChildElementsFromXML
1 parent aa7e9d7 commit a16cb03

File tree

10 files changed

+53
-121
lines changed

10 files changed

+53
-121
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"ext-spl": "*",
4444

4545
"simplesamlphp/assert": "^1.3",
46-
"simplesamlphp/xml-common": "^1.17"
46+
"simplesamlphp/xml-common": "^1.18"
4747
},
4848
"require-dev": {
4949
"simplesamlphp/simplesamlphp-test-framework": "^1.7"

src/XML/ds/DigestMethod.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,7 @@ public static function fromXML(DOMElement $xml): static
7373
Assert::same($xml->namespaceURI, DigestMethod::NS, InvalidDOMElementException::class);
7474

7575
$Algorithm = DigestMethod::getAttribute($xml, 'Algorithm');
76-
77-
$elements = [];
78-
foreach ($xml->childNodes as $elt) {
79-
if (!($elt instanceof DOMElement)) {
80-
continue;
81-
}
82-
83-
$elements[] = new Chunk($elt);
84-
}
76+
$elements = self::getChildElementsFromXML($xml);
8577

8678
return new static($Algorithm, $elements);
8779
}

src/XML/ds/DsObject.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,7 @@ public static function fromXML(DOMElement $xml): static
112112
$Id = DsObject::getOptionalAttribute($xml, 'Id', null);
113113
$MimeType = DsObject::getOptionalAttribute($xml, 'MimeType', null);
114114
$Encoding = DsObject::getOptionalAttribute($xml, 'Encoding', null);
115-
116-
$elements = [];
117-
foreach ($xml->childNodes as $elt) {
118-
if (!($elt instanceof DOMElement)) {
119-
// @TODO: support mixed content
120-
continue;
121-
} elseif ($elt->namespaceURI === self::NS) {
122-
$elements[] = match ($elt->localName) {
123-
'SignatureProperties' => SignatureProperties::fromXML($elt),
124-
'Manifest' => Manifest::fromXML($elt),
125-
default => new Chunk($elt),
126-
};
127-
}
128-
129-
$elements[] = new Chunk($elt);
130-
}
115+
$elements = self::getChildElementsFromXML($xml);
131116

132117
return new static($Id, $MimeType, $Encoding, $elements);
133118
}

src/XML/ds/KeyInfo.php

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Chunk;
1010
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11+
use SimpleSAML\XML\ExtendableElementTrait;
12+
use SimpleSAML\XML\SerializableElementInterface;
13+
use SimpleSAML\XML\XsNamespace as NS;
1114
use SimpleSAML\XMLSecurity\Constants as C;
1215
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
1316
use SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference;
@@ -21,11 +24,16 @@
2124
*/
2225
final class KeyInfo extends AbstractDsElement
2326
{
27+
use ExtendableElementTrait;
28+
29+
/** @var \SimpleSAML\XML\XsNamespace */
30+
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
31+
32+
2433
/**
2534
* Initialize a KeyInfo element.
2635
*
2736
* @param (
28-
* \SimpleSAML\XML\SerializableElementInterface|
2937
* \SimpleSAML\XMLSecurity\XML\ds\KeyName|
3038
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
3139
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
@@ -34,29 +42,26 @@ final class KeyInfo extends AbstractDsElement
3442
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
3543
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
3644
* )[] $info
45+
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
3746
* @param string|null $Id
3847
*/
3948
public function __construct(
4049
protected array $info,
50+
array $children = [],
4151
protected ?string $Id = null,
4252
) {
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.',
53+
$combi = array_merge($info, $children);
54+
55+
Assert::notEmpty($combi, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class);
56+
Assert::maxCount($combi, C::UNBOUNDED_LIMIT);
57+
Assert::allIsInstanceOf(
58+
$combi,
59+
SerializableElementInterface::class,
5760
InvalidArgumentException::class,
5861
);
5962
Assert::nullOrValidNCName($Id);
63+
64+
$this->setElements($children);
6065
}
6166

6267

@@ -87,7 +92,7 @@ public function getId(): ?string
8792
*/
8893
public function getInfo(): array
8994
{
90-
return $this->info;
95+
return array_merge($this->info, $this->getElements());;
9196
}
9297

9398

@@ -106,36 +111,27 @@ public static function fromXML(DOMElement $xml): static
106111
Assert::same($xml->namespaceURI, KeyInfo::NS, InvalidDOMElementException::class);
107112

108113
$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-
}
137114

138-
return new static($info, $Id);
115+
$keyName = KeyName::getChildrenOfClass($xml);
116+
$keyValue = KeyValue::getChildrenOfClass($xml);
117+
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
118+
$x509Data = X509Data::getChildrenOfClass($xml);
119+
//$pgpData = PGPData::getChildrenOfClass($xml);
120+
//$spkiData = SPKIData::getChildrenOfClass($xml);
121+
//$mgmtData = MgmtData::getChildrenOfClass($xml);
122+
123+
$info = array_merge(
124+
$keyName,
125+
$keyValue,
126+
$retrievalMethod,
127+
$x509Data,
128+
//$pgpdata,
129+
//$spkidata,
130+
//$mgmtdata,
131+
);
132+
133+
$children = self::getChildElementsFromXML($xml);
134+
return new static($info, $children, $Id);
139135
}
140136

141137

@@ -153,8 +149,8 @@ public function toXML(DOMElement $parent = null): DOMElement
153149
$e->setAttribute('Id', $this->getId());
154150
}
155151

156-
foreach ($this->getInfo() as $n) {
157-
$n->toXML($e);
152+
foreach ($this->getInfo() as $elt) {
153+
$elt->toXML($e);
158154
}
159155

160156
return $e;

src/XML/ds/KeyValue.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,7 @@ public static function fromXML(DOMElement $xml): static
8383
TooManyElementsException::class,
8484
);
8585

86-
$elements = [];
87-
foreach ($xml->childNodes as $element) {
88-
if (!($element instanceof DOMElement) || $element->namespaceURI === KeyValue::NS) {
89-
continue;
90-
}
91-
92-
$elements[] = new Chunk($element);
93-
}
86+
$elements = self::getChildElementsFromXML($xml);
9487
Assert::maxCount(
9588
$elements,
9689
1,

src/XML/ds/SignatureProperty.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,7 @@ public static function fromXML(DOMElement $xml): static
8181
$Target = self::getAttribute($xml, 'Target');
8282
$Id = self::getOptionalAttribute($xml, 'Id', null);
8383

84-
$children = [];
85-
foreach ($xml->childNodes as $child) {
86-
if (!($child instanceof DOMElement)) {
87-
continue;
88-
}
89-
90-
$children[] = new Chunk($child);
91-
}
92-
84+
$children = self::getChildElementsFromXML($xml);
9385
Assert::minCount(
9486
$children,
9587
1,

src/XML/xenc/AbstractEncryptionMethod.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,7 @@ public static function fromXML(DOMElement $xml): static
108108
$oaepParams = OAEPparams::getChildrenOfClass($xml);
109109
Assert::maxCount($oaepParams, 1, TooManyElementsException::class);
110110

111-
$children = [];
112-
foreach ($xml->childNodes as $node) {
113-
if (!$node instanceof DOMElement) {
114-
continue;
115-
} elseif ($node->namespaceURI === C::NS_XENC) {
116-
if ($node->localName === 'KeySize') {
117-
continue;
118-
} elseif ($node->localName === 'OAEPparams') {
119-
continue;
120-
}
121-
}
122-
123-
$children[] = Chunk::fromXML($node);
124-
}
111+
$children = self::getChildElementsFromXML($xml);
125112

126113
return new static($algorithm, array_pop($keySize), array_pop($oaepParams), $children);
127114
}

src/XML/xenc/AbstractEncryptionPropertyType.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,8 @@ public static function fromXML(DOMElement $xml): static
8888
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
8989
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
9090

91-
$children = [];
92-
foreach ($xml->childNodes as $child) {
93-
if (!($child instanceof DOMElement)) {
94-
continue;
95-
}
96-
97-
$children[] = new Chunk($child);
98-
}
99-
10091
return new static(
101-
$children,
92+
self::getChildElementsFromXML($xml),
10293
self::getOptionalAttribute($xml, 'Target', null),
10394
self::getOptionalAttribute($xml, 'Id', null),
10495
self::getAttributesNSFromXML($xml),

src/XML/xenc/AbstractReference.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,7 @@ public static function fromXML(DOMElement $xml): static
6666
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
6767

6868
$URI = self::getAttribute($xml, 'URI');
69-
70-
$elements = [];
71-
foreach ($xml->childNodes as $element) {
72-
if ($element instanceof DOMElement) {
73-
$elements[] = new Chunk($element);
74-
}
75-
}
69+
$elements = self::getChildElementsFromXML($xml);
7670

7771
return new static($URI, $elements);
7872
}

tests/XML/ds/KeyInfoTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public function testMarshalling(): void
9494
new X509SubjectName(self::$certData['name']),
9595
],
9696
),
97+
],
98+
[
9799
new Chunk(DOMDocumentFactory::fromString(
98100
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
99101
)->documentElement),

0 commit comments

Comments
 (0)