From 00c0eca6fbd89e783a774173173a5b070b6c8ce7 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Tue, 23 Jan 2024 23:23:04 +0100 Subject: [PATCH] Add wst classes --- src/XML/wst/AbstractAuthenticatorType.php | 25 +++- src/XML/wst/AbstractSignChallengeType.php | 118 ++++++++++++++++++ src/XML/wst/SignChallenge.php | 14 +++ .../WSSecurity/XML/wst/AuthenticatorTest.php | 28 ++--- .../WSSecurity/XML/wst/SignChallengeTest.php | 65 ++++++++++ tests/resources/xml/wst_SignChallenge.xml | 4 + 6 files changed, 234 insertions(+), 20 deletions(-) create mode 100644 src/XML/wst/AbstractSignChallengeType.php create mode 100644 src/XML/wst/SignChallenge.php create mode 100644 tests/WSSecurity/XML/wst/SignChallengeTest.php create mode 100644 tests/resources/xml/wst_SignChallenge.xml diff --git a/src/XML/wst/AbstractAuthenticatorType.php b/src/XML/wst/AbstractAuthenticatorType.php index cc8d1e6c..f3d745d8 100644 --- a/src/XML/wst/AbstractAuthenticatorType.php +++ b/src/XML/wst/AbstractAuthenticatorType.php @@ -14,11 +14,11 @@ use function array_pop; /** - * Class defining the AuthenticatorTokenType element + * Class defining the AuthenticatorType element * * @package tvdijen/ws-security */ -abstract class AbstractAuthenticatorTokenType extends AbstractWstElement +abstract class AbstractAuthenticatorType extends AbstractWstElement { use ExtendableElementTrait; @@ -27,19 +27,28 @@ abstract class AbstractAuthenticatorTokenType extends AbstractWstElement /** - * AbstractAuthenticatorTokenType constructor + * AbstractAuthenticatorType constructor * * @param \SimpleSAML\WSSecurity\XML\wst\CombinedHash|null $combinedHash * @param \SimpleSAML\XML\SerializableElementInterface[] $children */ final public function __construct( - protected ?CombinedHash $combinedHashh = null, + protected ?CombinedHash $combinedHash = null, array $children = [] ) { $this->setElements($children); } + /** + * @return \SimpleSAML\WSSecurity\XML\wst\CombinedHash|null + */ + public function getCombinedHash(): ?CombinedHash + { + return $this->combinedHash; + } + + /** * Test if an object, at the state it's in, would produce an empty XML-element * @@ -72,20 +81,22 @@ public static function fromXML(DOMElement $xml): static foreach ($xml->childNodes as $child) { if (!($child instanceof DOMElement)) { continue; + } elseif ($child->namespaceURI === static::NS) { + continue; } $children[] = new Chunk($child); } return new static( - array_pop($combined_hash), + array_pop($combinedHash), $children, ); } /** - * Add this AuthenticatorTokenType to an XML element. + * Add this AuthenticatorType to an XML element. * * @param \DOMElement $parent The element we should append this username token to. * @return \DOMElement @@ -94,6 +105,8 @@ public function toXML(DOMElement $parent = null): DOMElement { $e = parent::instantiateParentElement($parent); + $this->getCombinedHash()?->toXML($e); + foreach ($this->getElements() as $child) { if (!$child->isEmptyElement()) { $child->toXML($e); diff --git a/src/XML/wst/AbstractSignChallengeType.php b/src/XML/wst/AbstractSignChallengeType.php new file mode 100644 index 00000000..92dccc88 --- /dev/null +++ b/src/XML/wst/AbstractSignChallengeType.php @@ -0,0 +1,118 @@ +setElements($children); + } + + + /** + * @return \SimpleSAML\WSSecurity\XML\wst\Challenge|null + */ + public function getChallenge(): ?Challenge + { + return $this->Challenge; + } + + + /** + * Test if an object, at the state it's in, would produce an empty XML-element + * + * @return bool + */ + public function isEmptyElement(): bool + { + return empty($this->getChallenge()) + && empty($this->getElements()); + } + + + /** + * Create an instance of this object from its XML representation. + * + * @param \DOMElement $xml + * @return static + * + * @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::NS, InvalidDOMElementException::class); + + $challenge = Challenge::getChildrenOfClass($xml); + + $children = []; + foreach ($xml->childNodes as $child) { + if (!($child instanceof DOMElement)) { + continue; + } elseif ($child->namespaceURI === static::NS) { + continue; + } + + $children[] = new Chunk($child); + } + + return new static( + array_pop($challenge), + $children, + ); + } + + + /** + * Add this SignChallengeType to an XML element. + * + * @param \DOMElement $parent The element we should append this username token to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = parent::instantiateParentElement($parent); + + $this->getChallenge()?->toXML($e); + + foreach ($this->getElements() as $child) { + if (!$child->isEmptyElement()) { + $child->toXML($e); + } + } + + return $e; + } +} diff --git a/src/XML/wst/SignChallenge.php b/src/XML/wst/SignChallenge.php new file mode 100644 index 00000000..2edf3dfe --- /dev/null +++ b/src/XML/wst/SignChallenge.php @@ -0,0 +1,14 @@ +assertEquals( self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), - strval($authenticatorToken), + strval($authenticator), ); } /** - * Test creating an empty AuthenticatorToken object from scratch. + * Test creating an empty Authenticator object from scratch. */ public function testMarshallingEmpty(): void { - $authenticatorToken = new AuthenticatorToken(); + $authenticator = new Authenticator(); - $this->assertTrue($authenticatorToken->isEmptyElement()); + $this->assertTrue($authenticator->isEmptyElement()); } } diff --git a/tests/WSSecurity/XML/wst/SignChallengeTest.php b/tests/WSSecurity/XML/wst/SignChallengeTest.php new file mode 100644 index 00000000..f40605c0 --- /dev/null +++ b/tests/WSSecurity/XML/wst/SignChallengeTest.php @@ -0,0 +1,65 @@ +Some' + )->documentElement); + } + + + // test marshalling + + + /** + * Test creating a SignChallenge object from scratch. + */ + public function testMarshalling(): void + { + $challenge = new Challenge('accepted'); + $signChallenge = new SignChallenge($challenge, [self::$chunk]); + + $this->assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($signChallenge), + ); + } +} diff --git a/tests/resources/xml/wst_SignChallenge.xml b/tests/resources/xml/wst_SignChallenge.xml new file mode 100644 index 00000000..d63c68cd --- /dev/null +++ b/tests/resources/xml/wst_SignChallenge.xml @@ -0,0 +1,4 @@ + + accepted + Some +