From 2a42d1aa21df203f58c209cabf2711fae41c816d Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Thu, 5 Dec 2024 22:43:41 +0100 Subject: [PATCH] Add xenc11:OtherSource element --- .../AbstractAlgorithmIdentifierType.php | 92 +++++++++++++++++++ src/XML/xenc11/OtherSource.php | 14 +++ tests/XML/xenc11/OtherSourceTest.php | 77 ++++++++++++++++ tests/resources/xml/xenc11_OtherSource.xml | 11 +++ 4 files changed, 194 insertions(+) create mode 100644 src/XML/xenc11/AbstractAlgorithmIdentifierType.php create mode 100644 src/XML/xenc11/OtherSource.php create mode 100644 tests/XML/xenc11/OtherSourceTest.php create mode 100644 tests/resources/xml/xenc11_OtherSource.xml diff --git a/src/XML/xenc11/AbstractAlgorithmIdentifierType.php b/src/XML/xenc11/AbstractAlgorithmIdentifierType.php new file mode 100644 index 00000000..43957f1f --- /dev/null +++ b/src/XML/xenc11/AbstractAlgorithmIdentifierType.php @@ -0,0 +1,92 @@ +. + * + * @package simplesamlphp/xml-security + */ +abstract class AbstractAlgorithmIdentifierType extends AbstractXenc11Element +{ + /** + * AlgorithmIdentifierType constructor. + * + * @param string $Algorithm + * @param \SimpleSAML\XMLSecurity\XML\xenc11\Parameters|null $parameters + */ + final public function __construct( + protected string $Algorithm, + protected ?Parameters $parameters = null, + ) { + Assert::validURI($Algorithm, SchemaViolationException::class); + } + + + /** + * Get the value of the $Algorithm property. + * + * @return string + */ + public function getAlgorithm(): string + { + return $this->Algorithm; + } + + + /** + * Get the value of the $parameters property. + * + * @return \SimpleSAML\XMLSecurity\XML\xenc11\Parameters + */ + public function getParameters(): Parameters + { + return $this->parameters; + } + + + /** + * @inheritDoc + * + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException + * If the qualified name of the supplied element is wrong + */ + public static function fromXML(DOMElement $xml): static + { + Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class); + Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class); + + $parameter = Parameters::getChildrenOfClass($xml); + Assert::maxCount($parameter, 1, TooManyElementsException::class); + + return new static( + self::getOptionalAttribute($xml, 'Algorithm'), + array_pop($parameter), + ); + } + + + /** + * @inheritDoc + */ + public function toXML(?DOMElement $parent = null): DOMElement + { + $e = $this->instantiateParentElement($parent); + $e->setAttribute('Algorithm', $this->getAlgorithm()); + + $this->getParameters()?->toXML($e); + + return $e; + } +} diff --git a/src/XML/xenc11/OtherSource.php b/src/XML/xenc11/OtherSource.php new file mode 100644 index 00000000..51e93a50 --- /dev/null +++ b/src/XML/xenc11/OtherSource.php @@ -0,0 +1,14 @@ + + + + + + + +XML + , + )->documentElement); + + $parameters = new Parameters( + [$chunk], + [new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1')], + ); + + $otherSource = new OtherSource('urn:x-simplesamlphp:algorithm', $parameters); + + $this->assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($otherSource), + ); + } +} diff --git a/tests/resources/xml/xenc11_OtherSource.xml b/tests/resources/xml/xenc11_OtherSource.xml new file mode 100644 index 00000000..434949ad --- /dev/null +++ b/tests/resources/xml/xenc11_OtherSource.xml @@ -0,0 +1,11 @@ + + + + + + + + + + +