Skip to content

Commit

Permalink
Add wst classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 25, 2024
1 parent 2e1687b commit 8948709
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/XML/wst/AbstractClaimsType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function array_pop;

/**
* Class defining the ClaimsType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractClaimsType extends AbstractWstElement
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = NS::ANY;

/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


/**
* AbstractClaimsType constructor
*
* @param string|null $dialect
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
*/
final public function __construct(
protected ?string $dialect = null,
array $children = [],
array $namespacedAttributes = []
) {
Assert::nullOrValidURI($dialect, SchemaViolationException::class);

$this->setElements($children);
$this->setAttributesNS($namespacedAttributes);
}


/**
* @return string|null
*/
public function getDialect(): ?string
{
return $this->dialect;
}


/**
* 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->getDialect())
&& empty($this->getElements())
&& empty($this->getAttributesNS());
}


/**
* 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);

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

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

return new static(
self::getOptionalAttribute($xml, 'Dialect', null),
$children,
self::getAttributesNSFromXML($xml),
);
}


/**
* Add this ClaimsType to an XML element.
*
* @param \DOMElement $parent The element we should append this element to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);

if ($this->getDialect() !== null) {
$e->setAttribute('Dialect', $this->getDialect());
}

foreach ($this->getElements() as $child) {
if (!$child->isEmptyElement()) {
$child->toXML($e);
}
}

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

return $e;
}
}
14 changes: 14 additions & 0 deletions src/XML/wst/Claims.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

/**
* A Claims element
*
* @package tvdijen/ws-security
*/
final class Claims extends AbstractClaimsType
{
}
78 changes: 78 additions & 0 deletions tests/WSSecurity/XML/wst/ClaimsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\wst;

use PHPUnit\Framework\TestCase;
use SimpleSAML\SOAP\Constants as SOAP;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\wsa\MessageID;
use SimpleSAML\WSSecurity\XML\wst\Claims;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\wst\ClaimsTest
*
* @covers \SimpleSAML\WSSecurity\XML\wst\Claims
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractClaimsType
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement
*
* @package tvdijen/ws-security
*/
final class ClaimsTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-trust.xsd';

self::$testedClass = Claims::class;

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/wst_Claims.xml',
);
}


// test marshalling


/**
* Test creating a Claims object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'testval1');
$attr2 = new XMLAttribute(SOAP::NS_SOAP_ENV_11, 'soapenv', 'mustUnderstand', '1');
$msgId = new MessageID('uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de', [$attr2]);

$claims = new Claims(C::NAMESPACE, [$msgId], [$attr1]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($claims),
);
}


/**
* Test creating an empty Claims object from scratch.
*/
public function testMarshallingEmpty(): void
{
$claims = new Claims();

$this->assertTrue($claims->isEmptyElement());
}
}
3 changes: 3 additions & 0 deletions tests/resources/xml/wst_Claims.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<wst:Claims xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512/" xmlns:ssp="urn:x-simplesamlphp:namespace" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" Dialect="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<wsa:MessageID soapenv:mustUnderstand="1">uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de</wsa:MessageID>
</wst:Claims>

0 comments on commit 8948709

Please sign in to comment.