Skip to content

Commit

Permalink
Add mex classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 28, 2024
1 parent f3dcfdb commit 4dc2ec5
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 1 deletion.
155 changes: 155 additions & 0 deletions src/XML/mex/MetadataSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\mex;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\SerializableElementInterface;
use SimpleSAML\XML\XsNamespace as NS;

use function array_pop;

/**
* Class defining the MetadataSection element
*
* @package tvdijen/ws-security
*/
final class MetadataSection extends AbstractMexElement
{
use ExtendableAttributesTrait;

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


/**
* MetadataSection constructor
*
* @param (\SimpleSAML\XML\SerializableElementInterface|
* \SimpleSAML\WSSecurity\XML\mex\MetadataReference|
* \SimpleSAML\WSSecurity\XML\mex\Location) $child
* @param string $Dialect
* @param string|null $Identifier
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected SerializableElementInterface|MetadataReference|Location $child,
protected string $Dialect,
protected ?string $Identifier = null,
array $namespacedAttributes = []
) {
Assert::validURI($Dialect);
Assert::nullOrValidURI($Identifier);

$this->setAttributesNS($namespacedAttributes);
}


/**
* Get the child property.
*
* @return (\SimpleSAML\XML\SerializableElementInterface|
* \SimpleSAML\WSSecurity\XML\mex\MetadataReference|
* \SimpleSAML\WSSecurity\XML\mex\Location)
*/
public function getChild(): SerializableElementInterface|MetadataReference|Location
{
return $this->child;
}


/**
* Get the Dialect property.
*
* @return string
*/
public function getDialect(): string
{
return $this->Dialect;
}


/**
* Get the Identifier property.
*
* @return string|null
*/
public function getIdentifier(): ?string
{
return $this->Identifier;
}


/**
* 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;
} elseif ($child->namespaceURI === static::NS) {
if ($child->localName === 'MetadataReference') {
$children[] = MetadataReference::fromXML($child);
} elseif ($child->localName === 'Location') {
$children[] = Location::fromXML($child);
}
continue;
}

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

Assert::minCount($children, 1, MissingElementException::class);
Assert::maxCount($children, 1, TooManyElementsException::class);

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


/**
* Add this MetadataSection to an XML element.
*
* @param \DOMElement $parent The element we should append this MetadataSection to.
* @return \DOMElement
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = parent::instantiateParentElement($parent);
$e->setAttribute('Dialect', $this->getDialect());

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

$this->getChild()->toXML($e);

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

return $e;
}
}
2 changes: 1 addition & 1 deletion src/XML/wst/AbstractBinaryExchangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class AbstractBinaryExchangeType extends AbstractWstElement
use ExtendableAttributesTrait;
use StringElementTrait;

/** @var string|\SimpleSAML\XML\XsNamespace */
/** The namespace-attribute for the xs:anyAttribute element */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;


Expand Down
68 changes: 68 additions & 0 deletions tests/WSSecurity/XML/mex/MetadataSectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\mex;

use DOMElement;
use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\mex\MetadataSection;
use SimpleSAML\WSSecurity\XML\mex\Location;
use SimpleSAML\XML\Attribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Tests for mex:MetadataSection.
*
* @covers \SimpleSAML\WSSecurity\XML\mex\MetadataSection
* @covers \SimpleSAML\WSSecurity\XML\wsa\AbstractMexElement
* @package tvdijen/ws-security
*/
final class MetadataSectionTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = MetadataSection::class;

self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/MetadataExchange.xsd';

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


// test marshalling


/**
* Test creating an MetadataSection object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new Attribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1');

$metadataSection = new MetadataSection(
new Location('urn:x-simplesamlphp:namespace'),
'urn:x-simplesamlphp:namespace',
'urn:x-simplesamlphp:namespace',
[$attr1],
);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($metadataSection)
);
}
}
3 changes: 3 additions & 0 deletions tests/resources/xml/mex_MetadataSection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<mex:MetadataSection xmlns:mex="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:ssp="urn:x-simplesamlphp:namespace" Dialect="urn:x-simplesamlphp:namespace" Identifier="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<mex:Location>urn:x-simplesamlphp:namespace</mex:Location>
</mex:MetadataSection>

0 comments on commit 4dc2ec5

Please sign in to comment.