-
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
24 changed files
with
1,153 additions
and
40 deletions.
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
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.