From c4a1e67e92f906fc341dfe9b375591f8d8c00261 Mon Sep 17 00:00:00 2001 From: Tim van Dijen Date: Sat, 27 Jan 2024 14:07:14 +0100 Subject: [PATCH] Add wst classes --- src/XML/wst/AbstractBinarySecretType.php | 116 ++++++++++++++++++ .../AbstractBinarySecretTypeOpenEnumType.php | 60 --------- ...ractRequestSecurityTokenCollectionType.php | 82 +++++++++++++ ...estSecurityTokenResponseCollectionType.php | 99 +++++++++++++++ src/XML/wst/BinarySecret.php | 14 +++ src/XML/wst/IssuedTokens.php | 14 +++ src/XML/wst/Issuer.php | 22 ++++ .../wst/RequestSecurityTokenCollection.php | 14 +++ ...RequestSecurityTokenResponseCollection.php | 14 +++ tests/WSSecurity/XML/wst/BinarySecretTest.php | 68 ++++++++++ tests/WSSecurity/XML/wst/IssuedTokensTest.php | 74 +++++++++++ tests/WSSecurity/XML/wst/IssuerTest.php | 97 +++++++++++++++ .../RequestSecurityTokenCollectionTest.php | 76 ++++++++++++ ...estSecurityTokenResponseCollectionTest.php | 74 +++++++++++ tests/resources/xml/IssuedTokens.php | 5 + tests/resources/xml/wst_BinarySecret.xml | 1 + tests/resources/xml/wst_IssuedTokens.xml | 5 + tests/resources/xml/wst_Issuer.xml | 14 +++ .../wst_RequestSecurityTokenCollection.xml | 8 ++ ...RequestSecurityTokenResponseCollection.xml | 5 + 20 files changed, 802 insertions(+), 60 deletions(-) create mode 100644 src/XML/wst/AbstractBinarySecretType.php delete mode 100644 src/XML/wst/AbstractBinarySecretTypeOpenEnumType.php create mode 100644 src/XML/wst/AbstractRequestSecurityTokenCollectionType.php create mode 100644 src/XML/wst/AbstractRequestSecurityTokenResponseCollectionType.php create mode 100644 src/XML/wst/BinarySecret.php create mode 100644 src/XML/wst/IssuedTokens.php create mode 100644 src/XML/wst/Issuer.php create mode 100644 src/XML/wst/RequestSecurityTokenCollection.php create mode 100644 src/XML/wst/RequestSecurityTokenResponseCollection.php create mode 100644 tests/WSSecurity/XML/wst/BinarySecretTest.php create mode 100644 tests/WSSecurity/XML/wst/IssuedTokensTest.php create mode 100644 tests/WSSecurity/XML/wst/IssuerTest.php create mode 100644 tests/WSSecurity/XML/wst/RequestSecurityTokenCollectionTest.php create mode 100644 tests/WSSecurity/XML/wst/RequestSecurityTokenResponseCollectionTest.php create mode 100644 tests/resources/xml/IssuedTokens.php create mode 100644 tests/resources/xml/wst_BinarySecret.xml create mode 100644 tests/resources/xml/wst_IssuedTokens.xml create mode 100644 tests/resources/xml/wst_Issuer.xml create mode 100644 tests/resources/xml/wst_RequestSecurityTokenCollection.xml create mode 100644 tests/resources/xml/wst_RequestSecurityTokenResponseCollection.xml diff --git a/src/XML/wst/AbstractBinarySecretType.php b/src/XML/wst/AbstractBinarySecretType.php new file mode 100644 index 00000000..f5c9c8c0 --- /dev/null +++ b/src/XML/wst/AbstractBinarySecretType.php @@ -0,0 +1,116 @@ +value : $v; + }, + $Type, + ); + Assert::allValidURI($Type, SchemaViolationException::class); + $this->Type = $Type; + } + + $this->setContent($content); + $this->setAttributesNS($namespacedAttributes); + } + + + /** + * Get the Type property. + * + * @return string[]|null + */ + public function getType(): ?array + { + return $this->Type; + } + + + /** + * Convert XML into a class instance + * + * @param \DOMElement $xml The XML element we should load + * @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); + + return new static( + $xml->textContent, + explode(' ', self::getAttribute($xml, 'Type')), + self::getAttributesNSFromXML($xml), + ); + } + + + /** + * Convert this element to XML. + * + * @param \DOMElement|null $parent The element we should append this element to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = $this->instantiateParentElement($parent); + $e->textContent = $this->getContent(); + + if ($this->getType() !== null) { + $e->setAttribute('Type', implode(' ', $this->getType())); + } + + foreach ($this->getAttributesNS() as $attr) { + $attr->toXML($e); + } + + return $e; + } +} diff --git a/src/XML/wst/AbstractBinarySecretTypeOpenEnumType.php b/src/XML/wst/AbstractBinarySecretTypeOpenEnumType.php deleted file mode 100644 index 0a3ca4d3..00000000 --- a/src/XML/wst/AbstractBinarySecretTypeOpenEnumType.php +++ /dev/null @@ -1,60 +0,0 @@ -value : $v; - }, - $values, - ); - Assert::allValidURI($values, SchemaViolationException::class); - - $this->setContent(implode(' ', $values)); - } - - - /** - * Convert XML into a class instance - * - * @param \DOMElement $xml The XML element we should load - * @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); - - return new static(explode(' ', $xml->textContent)); - } -} diff --git a/src/XML/wst/AbstractRequestSecurityTokenCollectionType.php b/src/XML/wst/AbstractRequestSecurityTokenCollectionType.php new file mode 100644 index 00000000..816244c0 --- /dev/null +++ b/src/XML/wst/AbstractRequestSecurityTokenCollectionType.php @@ -0,0 +1,82 @@ +requestSecurityToken; + } + + + /** + * Convert XML into a class instance + * + * @param \DOMElement $xml The XML element we should load + * @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); + + return new static( + RequestSecurityToken::getChildrenOfClass($xml), + ); + } + + + /** + * Convert this element to XML. + * + * @param \DOMElement|null $parent The element we should append this element to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = $this->instantiateParentElement($parent); + + foreach ($this->getRequestSecurityToken() as $r) { + $r->toXML($e); + } + + return $e; + } +} diff --git a/src/XML/wst/AbstractRequestSecurityTokenResponseCollectionType.php b/src/XML/wst/AbstractRequestSecurityTokenResponseCollectionType.php new file mode 100644 index 00000000..c06c3cb9 --- /dev/null +++ b/src/XML/wst/AbstractRequestSecurityTokenResponseCollectionType.php @@ -0,0 +1,99 @@ +setAttributesNS($namespacedAttributes); + } + + + /** + * Get the requestSecurityTokenResponse property. + * + * @return \SimpleSAML\WSSecurity\XML\wst\RequestSecurityTokenResponse[] + */ + public function getRequestSecurityTokenResponse(): array + { + return $this->requestSecurityTokenResponse; + } + + + /** + * Convert XML into a class instance + * + * @param \DOMElement $xml The XML element we should load + * @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); + + return new static( + RequestSecurityTokenResponse::getChildrenOfClass($xml), + self::getAttributesNSFromXML($xml), + ); + } + + + /** + * Convert this element to XML. + * + * @param \DOMElement|null $parent The element we should append this element to. + * @return \DOMElement + */ + public function toXML(DOMElement $parent = null): DOMElement + { + $e = $this->instantiateParentElement($parent); + + foreach ($this->getRequestSecurityTokenResponse() as $r) { + $r->toXML($e); + } + + foreach ($this->getAttributesNS() as $attr) { + $attr->toXML($e); + } + + return $e; + } +} diff --git a/src/XML/wst/BinarySecret.php b/src/XML/wst/BinarySecret.php new file mode 100644 index 00000000..40a8f2ed --- /dev/null +++ b/src/XML/wst/BinarySecret.php @@ -0,0 +1,14 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($binarySecret), + ); + } +} diff --git a/tests/WSSecurity/XML/wst/IssuedTokensTest.php b/tests/WSSecurity/XML/wst/IssuedTokensTest.php new file mode 100644 index 00000000..b64ea5d6 --- /dev/null +++ b/tests/WSSecurity/XML/wst/IssuedTokensTest.php @@ -0,0 +1,74 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($issuedTokens), + ); + } +} diff --git a/tests/WSSecurity/XML/wst/IssuerTest.php b/tests/WSSecurity/XML/wst/IssuerTest.php new file mode 100644 index 00000000..56a9e2a9 --- /dev/null +++ b/tests/WSSecurity/XML/wst/IssuerTest.php @@ -0,0 +1,97 @@ +Pears' + )->documentElement; + + self::$metadataContent = DOMDocumentFactory::fromString( + 'Apples' + )->documentElement; + + self::$customContent = DOMDocumentFactory::fromString( + 'SomeChunk' + )->documentElement; + } + + + // test marshalling + + + /** + * Test creating an Issuer object from scratch. + */ + public function testMarshalling(): void + { + $attr1 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'test1', 'value1'); + $attr2 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'test2', 'value2'); + $attr3 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'test3', 'value3'); + $attr4 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'test4', 'value4'); + + $referenceParameters = new ReferenceParameters([new Chunk(self::$referenceParametersContent)], [$attr4]); + $metadata = new Metadata([new Chunk(self::$metadataContent)], [$attr3]); + $chunk = new Chunk(self::$customContent); + + $issuer = new Issuer( + new Address('https://login.microsoftonline.com/login.srf', [$attr2]), + [$referenceParameters], + [$metadata], + [$chunk], + [$attr1], + ); + + $this->assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($issuer) + ); + } +} diff --git a/tests/WSSecurity/XML/wst/RequestSecurityTokenCollectionTest.php b/tests/WSSecurity/XML/wst/RequestSecurityTokenCollectionTest.php new file mode 100644 index 00000000..944a9809 --- /dev/null +++ b/tests/WSSecurity/XML/wst/RequestSecurityTokenCollectionTest.php @@ -0,0 +1,76 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($requestSecurityTokenCollection), + ); + } +} diff --git a/tests/WSSecurity/XML/wst/RequestSecurityTokenResponseCollectionTest.php b/tests/WSSecurity/XML/wst/RequestSecurityTokenResponseCollectionTest.php new file mode 100644 index 00000000..575f5693 --- /dev/null +++ b/tests/WSSecurity/XML/wst/RequestSecurityTokenResponseCollectionTest.php @@ -0,0 +1,74 @@ +assertEquals( + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), + strval($requestSecurityTokenResponseCollection), + ); + } +} diff --git a/tests/resources/xml/IssuedTokens.php b/tests/resources/xml/IssuedTokens.php new file mode 100644 index 00000000..0010356c --- /dev/null +++ b/tests/resources/xml/IssuedTokens.php @@ -0,0 +1,5 @@ + + + uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de + + diff --git a/tests/resources/xml/wst_BinarySecret.xml b/tests/resources/xml/wst_BinarySecret.xml new file mode 100644 index 00000000..a2acf4d7 --- /dev/null +++ b/tests/resources/xml/wst_BinarySecret.xml @@ -0,0 +1 @@ +/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI= diff --git a/tests/resources/xml/wst_IssuedTokens.xml b/tests/resources/xml/wst_IssuedTokens.xml new file mode 100644 index 00000000..0010356c --- /dev/null +++ b/tests/resources/xml/wst_IssuedTokens.xml @@ -0,0 +1,5 @@ + + + uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de + + diff --git a/tests/resources/xml/wst_Issuer.xml b/tests/resources/xml/wst_Issuer.xml new file mode 100644 index 00000000..dc57ede4 --- /dev/null +++ b/tests/resources/xml/wst_Issuer.xml @@ -0,0 +1,14 @@ + + https://login.microsoftonline.com/login.srf + + + Pears + + + + + Apples + + + SomeChunk + diff --git a/tests/resources/xml/wst_RequestSecurityTokenCollection.xml b/tests/resources/xml/wst_RequestSecurityTokenCollection.xml new file mode 100644 index 00000000..fd884825 --- /dev/null +++ b/tests/resources/xml/wst_RequestSecurityTokenCollection.xml @@ -0,0 +1,8 @@ + + + uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de + + + uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7df + + diff --git a/tests/resources/xml/wst_RequestSecurityTokenResponseCollection.xml b/tests/resources/xml/wst_RequestSecurityTokenResponseCollection.xml new file mode 100644 index 00000000..ce482f44 --- /dev/null +++ b/tests/resources/xml/wst_RequestSecurityTokenResponseCollection.xml @@ -0,0 +1,5 @@ + + + uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de + +