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 b3c94bf commit 466c61a
Show file tree
Hide file tree
Showing 24 changed files with 1,122 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Constants extends \SimpleSAML\SAML2\Constants
*/
public const NS_SEC_UTIL = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';

/**
* The namespace for the Metadata Exchange protocol.
*/
public const NS_METADATA_EXCHANGE = 'http://schemas.xmlsoap.org/ws/2004/09/mex';

/**
* The schema-defined wsa fault codes
*/
Expand Down
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
22 changes: 22 additions & 0 deletions src/XML/wsx/AbstractWsxElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsx;

use SimpleSAML\WSSecurity\Constants as C;
use SimpleSAML\XML\AbstractElement;

/**
* Abstract class to be implemented by all the classes in this namespace
*
* @package tvdijen/ws-security
*/
abstract class AbstractWsxElement extends AbstractElement
{
/** @var string */
public const NS = C::NS_METADATA_EXCHANGE;

/** @var string */
public const NS_PREFIX = 'wsx';
}
26 changes: 26 additions & 0 deletions src/XML/wsx/Dialect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsx;

use SimpleSAML\XML\URIElementTrait;

/**
* An Dialect element
*
* @package tvdijen/ws-security
*/
final class Dialect extends AbstractWsxElement
{
use URIElementTrait;


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}
}
128 changes: 128 additions & 0 deletions src/XML/wsx/GetMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsx;

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

use function array_pop;

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

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


/**
* GetMetadata constructor
*
* @param \SimpleSAML\WSSecurity\XML\wsx\Dialect|null $dialect
* @param \SimpleSAML\WSSecurity\XML\wsx\Identifier|null $identifier
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected ?Dialect $dialect = null,
protected ?Identifier $identifier = null,
array $namespacedAttributes = []
) {
$this->setAttributesNS($namespacedAttributes);
}


/**
* Get the dialect property.
*
* @return \SimpleSAML\WSSecurity\XML\wsx\Dialect|null
*/
public function getDialect(): ?Dialect
{
return $this->dialect;
}


/**
* Get the identifier property.
*
* @return \SimpleSAML\WSSecurity\XML\wsx\Identifier|null
*/
public function getIdentifier(): ?Identifier
{
return $this->identifier;
}


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

$dialect = Dialect::getChildrenOfClass($xml);
Assert::maxCount($dialect, 1, TooManyElementsException::class);

$identifier = Identifier::getChildrenOfClass($xml);
Assert::maxCount($identifier, 1, TooManyElementsException::class);

return new static(
array_pop($dialect),
array_pop($identifier),
self::getAttributesNSFromXML($xml),
);
}


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

$this->getDialect()?->toXML($e);
$this->getIdentifier()?->toXML($e);

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

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsx;

use SimpleSAML\XML\URIElementTrait;

/**
* An Identifier element
*
* @package tvdijen/ws-security
*/
final class Identifier extends AbstractWsxElement
{
use URIElementTrait;


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}
}
26 changes: 26 additions & 0 deletions src/XML/wsx/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsx;

use SimpleSAML\XML\URIElementTrait;

/**
* An Location element
*
* @package tvdijen/ws-security
*/
final class Location extends AbstractWsxElement
{
use URIElementTrait;


/**
* @param string $content
*/
public function __construct(string $content)
{
$this->setContent($content);
}
}
132 changes: 132 additions & 0 deletions src/XML/wsx/Metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsx;

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

/**
* Class defining the Metadata element
*
* @package tvdijen/ws-security
*/
final class Metadata extends AbstractWsxElement
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;

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

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


/**
* Metadata constructor
*
* @param array<\SimpleSAML\WSSecurity\XML\wsx\MetadataSection> $metadataSection
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children
* @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected array $metadataSection = [],
array $children = [],
array $namespacedAttributes = []
) {
$this->setElements($children);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Get the child property.
*
* @return array<\SimpleSAML\WSSecurity\XML\wsx\MetadataSection>
*/
public function getMetadataSection(): array
{
return $this->metadataSection;
}


/**
* 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->getMetadataSection())
&& 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;
} elseif ($child->namespaceURI === static::NS) {
continue;
}

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

return new static(
MetadataSection::getChildrenOfClass($xml),
$children,
self::getAttributesNSFromXML($xml),
);
}


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

foreach ($this->getMetadataSection() as $metadataSection) {
$metadataSection->toXML($e);
}

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

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

return $e;
}
}
Loading

0 comments on commit 466c61a

Please sign in to comment.