Skip to content

Commit

Permalink
Add ws-addr 200408 elements
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 10, 2024
1 parent 8ef8539 commit db626cf
Show file tree
Hide file tree
Showing 53 changed files with 2,461 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Constants extends \SimpleSAML\SAML2\Constants
/**
* The namespace for WS-Addressing protocol.
*/
public const NS_ADDR = 'http://www.w3.org/2005/08/addressing';
public const NS_ADDR_200408 = 'http://schemas.xmlsoap.org/ws/2004/08/addressing';
public const NS_ADDR_200508 = 'http://www.w3.org/2005/08/addressing';

/**
* The namespace for WS-Authorization protocol.
Expand Down
3 changes: 2 additions & 1 deletion src/Utils/XPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static function getXPath(DOMNode $node): DOMXPath
{
$xp = parent::getXPath($node);

$xp->registerNamespace('addr', C::NS_ADDR);
$xp->registerNamespace('wsa', C::NS_ADDR_200508);
$xp->registerNamespace('wsa', C::NS_ADDR_200408);
$xp->registerNamespace('auth', C::NS_AUTH);
$xp->registerNamespace('fed', C::NS_FED);
$xp->registerNamespace('trust', C::NS_TRUST);
Expand Down
80 changes: 80 additions & 0 deletions src/XML/wsa_200408/AbstractAttributedQNameType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsa_200408;

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

/**
* Class representing WS-addressing AttributedQNameType.
*
* You can extend the class without extending the constructor. Then you can use the methods available and the class
* will generate an element with the same name as the extending class
* (e.g. \SimpleSAML\WSSecurity\wsa\ProblemHeaderQName).
*
* @package simplesamlphp/ws-security
*/
abstract class AbstractAttributedQNameType extends AbstractWsaElement
{
use ExtendableAttributesTrait;
use QNameElementTrait;

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


/**
* AbstractAttributedQNameType constructor.
*
* @param string $value The QName.
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(string $value, array $namespacedAttributes = [])
{
$this->setContent($value);
$this->setAttributesNS($namespacedAttributes);
}


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

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

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsa_200408;

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

/**
* Class representing WS-addressing AttributedURIType.
*
* You can extend the class without extending the constructor. Then you can use the methods available and the
* class will generate an element with the same name as the extending class (e.g. \SimpleSAML\WSSecurity\wsa\Address).
*
* @package simplesamlphp/ws-security
*/
abstract class AbstractAttributedURIType extends AbstractWsaElement
{
use ExtendableAttributesTrait;
use URIElementTrait;

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


/**
* AbstractAttributedURIType constructor.
*
* @param string $value The localized string.
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(string $value, array $namespacedAttributes = [])
{
$this->setContent($value);
$this->setAttributesNS($namespacedAttributes);
}


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

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

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wsa_200408;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\WSSecurity\Constants as C;
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\ExtendableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function array_pop;
use function sprintf;

/**
* Class representing WS-addressing EndpointReferenceType.
*
* You can extend the class without extending the constructor. Then you can use the methods available
* and the class will generate an element with the same name as the extending class
* (e.g. \SimpleSAML\WSSecurity\wsa\EndpointReference).
*
* @package simplesamlphp/ws-security
*/
abstract class AbstractEndpointReferenceType extends AbstractWsaElement
{
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;


/**
* EndpointReferenceType constructor.
*
* @param \SimpleSAML\WSSecurity\XML\wsa_200408\Address $address
* @param \SimpleSAML\WSSecurity\XML\wsa_200408\ReferenceProperties|null $referenceProperties
* @param \SimpleSAML\WSSecurity\XML\wsa_200408\ReferenceParameters|null $referenceParameters
* @param \SimpleSAML\WSSecurity\XML\wsa_200408\PortType|null $portType
* @param \SimpleSAML\WSSecurity\XML\wsa_200408\ServiceName|null $serviceName
* @param \SimpleSAML\XML\Chunk[] $children
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
*
* @throws \SimpleSAML\Assert\AssertionFailedException
*/
final public function __construct(
protected Address $address,
protected ?ReferenceProperties $referenceProperties = null,
protected ?ReferenceParameters $referenceParameters = null,
protected ?PortType $portType = null,
protected ?ServiceName $serviceName = null,
array $children = [],
array $namespacedAttributes = [],
) {
$this->setElements($children);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Collect the value of the address property.
*
* @return \SimpleSAML\WSSecurity\XML\wsa_200408\Address
*/
public function getAddress(): Address
{
return $this->address;
}


/**
* Collect the value of the referenceProperties property.
*
* @return \SimpleSAML\WSSecurity\XML\wsa_200408\ReferenceProperties|null
*/
public function getReferenceProperties(): ?ReferenceProperties
{
return $this->referenceProperties;
}


/**
* Collect the value of the referenceParameters property.
*
* @return \SimpleSAML\WSSecurity\XML\wsa_200408\ReferenceParameters|null
*/
public function getReferenceParameters(): ?ReferenceParameters
{
return $this->referenceParameters;
}


/**
* Collect the value of the portType property.
*
* @return \SimpleSAML\WSSecurity\XML\wsa_200408\PortType|null
*/
public function getPortType(): ?PortType
{
return $this->portType;
}


/**
* Collect the value of the serviceName property.
*
* @return \SimpleSAML\WSSecurity\XML\wsa_200408\ServiceName|null
*/
public function getServiceName(): ?ServiceName
{
return $this->serviceName;
}


/**
* Initialize an EndpointReferenceType.
*
* Note: this method cannot be used when extending this class, if the constructor has a different signature.
*
* @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
* @throws \SimpleSAML\XML\Exception\MissingAttributeException
* if the supplied element is missing any of the mandatory attributes
*/
public static function fromXML(DOMElement $xml): static
{
$qualifiedName = static::getClassName(static::class);
Assert::eq(
$xml->localName,
$qualifiedName,
sprintf('Unexpected name for endpoint reference: %s. Expected: %s.', $xml->localName, $qualifiedName),
InvalidDOMElementException::class,
);

$address = Address::getChildrenOfClass($xml);
Assert::minCount($address, 1, MissingElementException::class);
Assert::maxCount($address, 1, TooManyElementsException::class);

$referenceProperties = ReferenceProperties::getChildrenOfClass($xml);
Assert::maxCount($referenceProperties, 1, TooManyElementsException::class);

$referenceParameters = ReferenceParameters::getChildrenOfClass($xml);
Assert::maxCount($referenceParameters, 1, TooManyElementsException::class);

$portType = PortType::getChildrenOfClass($xml);
Assert::maxCount($portType, 1, TooManyElementsException::class);

$serviceName = ServiceName::getChildrenOfClass($xml);
Assert::maxCount($serviceName, 1, TooManyElementsException::class);

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

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

return new static(
array_pop($address),
array_pop($referenceProperties),
array_pop($referenceParameters),
array_pop($portType),
array_pop($serviceName),
$children,
self::getAttributesNSFromXML($xml),
);
}


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

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

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

$this->getReferenceProperties()?->toXML($e);
$this->getReferenceParameters()?->toXML($e);
$this->getPortType()?->toXML($e);
$this->getServiceName()?->toXML($e);

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

return $e;
}
}
Loading

0 comments on commit db626cf

Please sign in to comment.