Skip to content

Commit

Permalink
Leverage new getChildElementsFromXML
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 13, 2024
1 parent aa7e9d7 commit a16cb03
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 121 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"ext-spl": "*",

"simplesamlphp/assert": "^1.3",
"simplesamlphp/xml-common": "^1.17"
"simplesamlphp/xml-common": "^1.18"
},
"require-dev": {
"simplesamlphp/simplesamlphp-test-framework": "^1.7"
Expand Down
10 changes: 1 addition & 9 deletions src/XML/ds/DigestMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ public static function fromXML(DOMElement $xml): static
Assert::same($xml->namespaceURI, DigestMethod::NS, InvalidDOMElementException::class);

$Algorithm = DigestMethod::getAttribute($xml, 'Algorithm');

$elements = [];
foreach ($xml->childNodes as $elt) {
if (!($elt instanceof DOMElement)) {
continue;
}

$elements[] = new Chunk($elt);
}
$elements = self::getChildElementsFromXML($xml);

return new static($Algorithm, $elements);
}
Expand Down
17 changes: 1 addition & 16 deletions src/XML/ds/DsObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,7 @@ public static function fromXML(DOMElement $xml): static
$Id = DsObject::getOptionalAttribute($xml, 'Id', null);
$MimeType = DsObject::getOptionalAttribute($xml, 'MimeType', null);
$Encoding = DsObject::getOptionalAttribute($xml, 'Encoding', null);

$elements = [];
foreach ($xml->childNodes as $elt) {
if (!($elt instanceof DOMElement)) {
// @TODO: support mixed content
continue;
} elseif ($elt->namespaceURI === self::NS) {
$elements[] = match ($elt->localName) {
'SignatureProperties' => SignatureProperties::fromXML($elt),
'Manifest' => Manifest::fromXML($elt),
default => new Chunk($elt),
};
}

$elements[] = new Chunk($elt);
}
$elements = self::getChildElementsFromXML($xml);

return new static($Id, $MimeType, $Encoding, $elements);
}
Expand Down
90 changes: 43 additions & 47 deletions src/XML/ds/KeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\SerializableElementInterface;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XMLSecurity\Constants as C;
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
use SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference;
Expand All @@ -21,11 +24,16 @@
*/
final class KeyInfo extends AbstractDsElement
{
use ExtendableElementTrait;

/** @var \SimpleSAML\XML\XsNamespace */
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;


/**
* Initialize a KeyInfo element.
*
* @param (
* \SimpleSAML\XML\SerializableElementInterface|
* \SimpleSAML\XMLSecurity\XML\ds\KeyName|
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
Expand All @@ -34,29 +42,26 @@ final class KeyInfo extends AbstractDsElement
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
* \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey
* )[] $info
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
* @param string|null $Id
*/
public function __construct(
protected array $info,
array $children = [],
protected ?string $Id = null,
) {
Assert::notEmpty($info, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class);
Assert::maxCount($info, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOfAny(
$info,
[
Chunk::class,
KeyName::class,
KeyValue::class,
RetrievalMethod::class,
X509Data::class,
EncryptedData::class,
EncryptedKey::class,
],
'KeyInfo can only contain instances of KeyName, X509Data, EncryptedKey or Chunk.',
$combi = array_merge($info, $children);

Assert::notEmpty($combi, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class);
Assert::maxCount($combi, C::UNBOUNDED_LIMIT);
Assert::allIsInstanceOf(
$combi,
SerializableElementInterface::class,
InvalidArgumentException::class,
);
Assert::nullOrValidNCName($Id);

$this->setElements($children);
}


Expand Down Expand Up @@ -87,7 +92,7 @@ public function getId(): ?string
*/
public function getInfo(): array
{
return $this->info;
return array_merge($this->info, $this->getElements());;
}


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

$Id = self::getOptionalAttribute($xml, 'Id', null);
$info = [];

foreach ($xml->childNodes as $n) {
if (!($n instanceof DOMElement)) {
continue;
} elseif ($n->namespaceURI === C::NS_XDSIG) {
$info[] = match ($n->localName) {
'KeyName' => KeyName::fromXML($n),
'KeyValue' => KeyValue::fromXML($n),
'RetrievalMethod' => RetrievalMethod::fromXML($n),
'X509Data' => X509Data::fromXML($n),
default => new Chunk($n),
};
} elseif ($n->namespaceURI === C::NS_XDSIG11) {
$info[] = match ($n->localName) {
'KeyInfoReference' => KeyInfoReference::fromXML($n),
default => new Chunk($n),
};
} elseif ($n->namespaceURI === C::NS_XENC) {
$info[] = match ($n->localName) {
'EncryptedData' => EncryptedData::fromXML($n),
'EncryptedKey' => EncryptedKey::fromXML($n),
default => new Chunk($n),
};
} else {
$info[] = new Chunk($n);
}
}

return new static($info, $Id);
$keyName = KeyName::getChildrenOfClass($xml);
$keyValue = KeyValue::getChildrenOfClass($xml);
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
$x509Data = X509Data::getChildrenOfClass($xml);
//$pgpData = PGPData::getChildrenOfClass($xml);
//$spkiData = SPKIData::getChildrenOfClass($xml);
//$mgmtData = MgmtData::getChildrenOfClass($xml);

$info = array_merge(
$keyName,
$keyValue,
$retrievalMethod,
$x509Data,
//$pgpdata,
//$spkidata,
//$mgmtdata,
);

$children = self::getChildElementsFromXML($xml);
return new static($info, $children, $Id);
}


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

foreach ($this->getInfo() as $n) {
$n->toXML($e);
foreach ($this->getInfo() as $elt) {
$elt->toXML($e);
}

return $e;
Expand Down
9 changes: 1 addition & 8 deletions src/XML/ds/KeyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,7 @@ public static function fromXML(DOMElement $xml): static
TooManyElementsException::class,
);

$elements = [];
foreach ($xml->childNodes as $element) {
if (!($element instanceof DOMElement) || $element->namespaceURI === KeyValue::NS) {
continue;
}

$elements[] = new Chunk($element);
}
$elements = self::getChildElementsFromXML($xml);
Assert::maxCount(
$elements,
1,
Expand Down
10 changes: 1 addition & 9 deletions src/XML/ds/SignatureProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,7 @@ public static function fromXML(DOMElement $xml): static
$Target = self::getAttribute($xml, 'Target');
$Id = self::getOptionalAttribute($xml, 'Id', null);

$children = [];
foreach ($xml->childNodes as $child) {
if (!($child instanceof DOMElement)) {
continue;
}

$children[] = new Chunk($child);
}

$children = self::getChildElementsFromXML($xml);
Assert::minCount(
$children,
1,
Expand Down
15 changes: 1 addition & 14 deletions src/XML/xenc/AbstractEncryptionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,7 @@ public static function fromXML(DOMElement $xml): static
$oaepParams = OAEPparams::getChildrenOfClass($xml);
Assert::maxCount($oaepParams, 1, TooManyElementsException::class);

$children = [];
foreach ($xml->childNodes as $node) {
if (!$node instanceof DOMElement) {
continue;
} elseif ($node->namespaceURI === C::NS_XENC) {
if ($node->localName === 'KeySize') {
continue;
} elseif ($node->localName === 'OAEPparams') {
continue;
}
}

$children[] = Chunk::fromXML($node);
}
$children = self::getChildElementsFromXML($xml);

return new static($algorithm, array_pop($keySize), array_pop($oaepParams), $children);
}
Expand Down
11 changes: 1 addition & 10 deletions src/XML/xenc/AbstractEncryptionPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,8 @@ public static function fromXML(DOMElement $xml): static
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

$children = [];
foreach ($xml->childNodes as $child) {
if (!($child instanceof DOMElement)) {
continue;
}

$children[] = new Chunk($child);
}

return new static(
$children,
self::getChildElementsFromXML($xml),
self::getOptionalAttribute($xml, 'Target', null),
self::getOptionalAttribute($xml, 'Id', null),
self::getAttributesNSFromXML($xml),
Expand Down
8 changes: 1 addition & 7 deletions src/XML/xenc/AbstractReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,7 @@ public static function fromXML(DOMElement $xml): static
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

$URI = self::getAttribute($xml, 'URI');

$elements = [];
foreach ($xml->childNodes as $element) {
if ($element instanceof DOMElement) {
$elements[] = new Chunk($element);
}
}
$elements = self::getChildElementsFromXML($xml);

return new static($URI, $elements);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/XML/ds/KeyInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public function testMarshalling(): void
new X509SubjectName(self::$certData['name']),
],
),
],
[
new Chunk(DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
)->documentElement),
Expand Down

0 comments on commit a16cb03

Please sign in to comment.