Skip to content

Commit

Permalink
Add securitypolicy classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 21, 2023
1 parent 893b08e commit 094647c
Show file tree
Hide file tree
Showing 24 changed files with 1,153 additions and 40 deletions.
2 changes: 1 addition & 1 deletion resources/schemas/ws-securitypolicy-1.2.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
<xs:anyAttribute namespace="##any" processContents="lax" />
</xs:complexType>

<xs:element name="RequiredParts" type="tns:SePartsType" >
<xs:element name="RequiredParts" type="tns:ReqPartsType" >
<xs:annotation>
<xs:documentation xml:lang="en">
4.3.2 RequiredParts Assertion
Expand Down
41 changes: 41 additions & 0 deletions src/Utils/XPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\Utils;

use DOMNode;
use DOMXPath;
use SimpleSAML\WSSecurity\Constants as C;

/**
* Compilation of utilities for XPath.
*
* @package tvdijen/wssecurity
*/
class XPath extends \SimpleSAML\XMLSecurity\Utils\XPath
{
/**
* Get a DOMXPath object that can be used to search for WS Security elements.
*
* @param \DOMNode $node The document to associate to the DOMXPath object.
*
* @return \DOMXPath A DOMXPath object ready to use in the given document, with several
* ws-related namespaces already registered.
*/
public static function getXPath(DOMNode $node): DOMXPath
{
$xp = parent::getXPath($node);

$xp->registerNamespace('addr', C::NS_ADDR);
$xp->registerNamespace('auth', C::NS_AUTH);
$xp->registerNamespace('fed', C::NS_FED);
$xp->registerNamespace('trust', C::NS_TRUST);
$xp->registerNamespace('policy', C::NS_POLICY);
$xp->registerNamespace('sp', C::NS_SEC_POLICY);
$xp->registerNamespace('wsse', C::NS_SEC_EXT);
$xp->registerNamespace('wsu', C::NS_SEC_UTIL);

return $xp;
}
}
30 changes: 5 additions & 25 deletions src/XML/sp/AbstractEmptyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

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

use function sprintf;

Expand All @@ -20,26 +16,16 @@
*/
abstract class AbstractEmptyType extends AbstractSpElement
{
use ExtendableAttributesTrait;

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


/**
* AbstractQNameAssertionType constructor.
*
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
* AbstractEmptyType constructor.
*/
final public function __construct(
array $namespacedAttributes = []
) {
$this->setAttributesNS($namespacedAttributes);
}


/**
* Initialize an QNameAssertionType.
* Initialize an EmptyType.
*
* Note: this method cannot be used when extending this class, if the constructor has a different signature.
*
Expand All @@ -55,12 +41,12 @@ public static function fromXML(DOMElement $xml): static
Assert::eq(
$xml->localName,
$qualifiedName,
sprintf('Unexpected name for Empty: %s. Expected: %s.', $xml->localName, $qualifiedName),
sprintf('Unexpected name for EmptyType: %s. Expected: %s.', $xml->localName, $qualifiedName),
InvalidDOMElementException::class
);


return new static(self::getAttributesNSFromXML($xml));
return new static();
}


Expand All @@ -72,12 +58,6 @@ public static function fromXML(DOMElement $xml): static
*/
public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);

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

return $e;
return $this->instantiateParentElement($parent);
}
}
128 changes: 128 additions & 0 deletions src/XML/sp/AbstractHeaderType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\sp;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\XsNamespace as NS;

use function sprintf;

/**
* Class representing WS security policy HeaderType.
*
* @package tvdijen/ws-security
*/
abstract class AbstractHeaderType extends AbstractSpElement
{
use ExtendableAttributesTrait;

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


/**
* AbstractHeaderType constructor.
*
* @param string $namespace
* @param string|null $name
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected string $namespace,
protected ?string $name = null,
array $namespacedAttributes = []
) {
Assert::nullOrValidURI($namespace);
Assert::validQName($name);

$this->setAttributesNS($namespacedAttributes);
}


/**
* Collect the value of the Name property.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}


/**
* Collect the value of the Namespace property.
*
* @return string
*/
public function getNamespace(): string
{
return $this->namespace;
}


/**
* Initialize an HeaderType.
*
* 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
*/
public static function fromXML(DOMElement $xml): static
{
$qualifiedName = static::getClassName(static::class);
Assert::eq(
$xml->localName,
$qualifiedName,
sprintf('Unexpected name for HeaderType: %s. Expected: %s.', $xml->localName, $qualifiedName),
InvalidDOMElementException::class
);

$namespacedAttributes = self::getAttributesNSFromXML($xml);
foreach ($namespacedAttributes as $i => $attr) {
if ($attr->getNamespaceURI() === null) {
if ($attr->getAttrName() === 'Name' || $attr->getAttrName() === 'Namespace') {
unset($namespacedAttributes[$i]);
}
}
}

return new static(
self::getAttribute($xml, 'Namespace'),
self::getOptionalAttribute($xml, 'Name', null),
$namespacedAttributes,
);
}


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

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

$e->setAttribute('Namespace', $this->getNamespace());

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

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\sp;

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

use function array_pop;
use function sprintf;

/**
* Class representing WS security policy AbstractReqPartsType.
*
* @package tvdijen/ws-security
*/
abstract class AbstractReqPartsType extends AbstractSpElement
{
use ExtendableElementTrait;
use ExtendableAttributesTrait;

/** 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::ANY;


/**
* AbstractReqPartsType constructor.
*
* @param \SimpleSAML\WSSecurity\XML\sp\Header[] $header
* @param \SimpleSAML\XML\Chunk[] $details
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
*/
final public function __construct(
protected array $header = [],
array $details = [],
array $namespacedAttributes = []
) {
$this->setElements($details);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Collect the Header
*
* @return \SimpleSAML\WSSecurity\XML\sp\Header[]
*/
public function getHeader(): array
{
return $this->header;
}


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


/**
* Initialize an ReqPartsType.
*
* 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
*/
public static function fromXML(DOMElement $xml): static
{
$qualifiedName = static::getClassName(static::class);
Assert::eq(
$xml->localName,
$qualifiedName,
sprintf('Unexpected name for Empty: %s. Expected: %s.', $xml->localName, $qualifiedName),
InvalidDOMElementException::class
);

$header = Header::getChildrenOfClass($xml);
Assert::maxcount($header, 1, TooManyElementsException::class);

$details = [];
foreach ($xml->childNodes as $detail) {
if (!($detail instanceof DOMElement)) {
continue;
} elseif ($detail->namespaceURI === static::NS) {
continue;
}

$details[] = new Chunk($detail);
}

return new static(
$header,
$details,
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);

foreach ($this->getHeader() as $header) {
$header->toXML($e);
}

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

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

return $e;
}
}
Loading

0 comments on commit 094647c

Please sign in to comment.