-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |