Skip to content

Commit

Permalink
WIP: add fed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 11, 2024
1 parent f3c39be commit 76f9d7f
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 0 deletions.
213 changes: 213 additions & 0 deletions src/XML/fed/AbstractPseudonymType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

use DateTimeImmutable;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\WSSecurity\XML\wsu\Expires;
use SimpleSAML\WSSecurity\Constants as C;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;

/**
* A PseudonymType
*
* @package tvdijen/ws-security
*/
abstract class AbstractPseudonymType extends AbstractFedElement
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;

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

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


/**
* PseudonymType constructor.
*
* @param \SimpleSAML\WSSecurity\XML\fed\PseudonymBasis $pseudonymBasis
* @param \SimpleSAML\WSSecurity\XML\fed\RelativeTo $relativeTo
* @param \SimpleSAML\WSSecurity\XML\wsu\Expires|null $expires
* @param \SimpleSAML\WSSecurity\XML\fed\SecurityToken[] $securityToken
* @param \SimpleSAML\WSSecurity\XML\fed\ProofToken[] $proofToken
* @param \SimpleSAML\XML\SerializableElementInterface[] $childeren
* @param \SimpleSAML\XML\Attribute[] $namespacedAttributes
*/
final public function __construct(
protected PseudonymBasis $pseudonymBasis,
protected RelativeTo $relativeTo,
protected ?Expires $expires = null,
protected array $securityToken = [],
protected array $proofToken = [],
array $children = [],
array $namespacedAttributes = [],
) {
Assert::minCount($securityToken, 1, MissingElementException::class);
Assert::allIsInstanceOf(
$securityToken,
SecurityToken::class,
SchemaViolationException::class,
);
Assert::minCount($proofToken, 1, MissingElementException::class);
Assert::allIsInstanceOf(
$proofToken,
ProofToken::class,
SchemaViolationException::class,
);

$this->setElements($children);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Collect the value of the pseudonymBasis-property
*
* @return \SimpleSAML\WSSecurity\XML\fed\PseudonymBasis
*/
public function getPseudonymBasis(): PseudonymBasis
{
return $this->pseudonymBasis;
}


/**
* Collect the value of the relativeTo-property
*
* @return \SimpleSAML\WSSecurity\XML\fed\RelativeTo
*/
public function getRelativeTo(): RelativeTo
{
return $this->relativeTo;
}


/**
* Collect the value of the expires-property
*
* @return \SimpleSAML\WSSecurity\XML\wsu\Expires|null
*/
public function getExpires(): ?Expires
{
return $this->expires;
}


/**
* Collect the value of the securityToken-property
*
* @return \SimpleSAML\WSSecurity\XML\fed\SecurityToken[]
*/
public function getSecurityToken(): array
{
return $this->securityToken;
}


/**
* Collect the value of the proofToken-property
*
* @return \SimpleSAML\WSSecurity\XML\fed\ProofToken[]
*/
public function getProofToken(): array
{
return $this->proofToken;
}


/**
* 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 = $pseudonymBasis = $relativeTo = $expires = $securityToken = $proofToken = [];
foreach ($xml->childNodes as $child) {
if (!($child instanceof DOMElement)) {
continue;
} elseif ($child->namespaceURI === static::NS) {
if ($child->localName === 'PseudonymBasis') {
$pseudonymBasis[] = PseudonymBasis::fromXML($child);
} elseif ($child->localName === 'RelativeTo') {
$relativeTo[] = RelativeTo::fromXML($child);
} elseif ($child->localName === 'SecurityToken') {
$securityToken[] = SecurityToken::fromXML($child);
} elseif ($child->localName === 'ProofToken') {
$proofToken[] = ProofToken::fromXML($child);
} else {
$children[] = new Chunk($child);
}
continue;
} elseif ($child->namespaceURI === C::NS_SEC_UTIL) {
$expires[] = Expires::fromXML($child);
continue;
}

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

return new static(
array_pop($pseudonymBasis),
array_pop($relativeTo),
array_pop($expires),
$securityToken,
$proofToken,
$children,
self::getAttributesNSFromXML($xml),
);
}


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

$this->getPseudonymBasis()->toXML($e);
$this->getRelativeTo()->toXML($e);
$this->getExpires()?->toXML($e);

foreach ($this->getSecurityToken() as $st) {
$st->toXML($e);
}

foreach ($this->getProofToken() as $pt) {
$pt->toXML($e);
}

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

/** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
foreach ($this->getElements() as $child) {
if (!$child->isEmptyElement()) {
$child->toXML($e);
}
}

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

/**
* A PseudonymType element
*
* @package tvdijen/ws-security
*/
final class Pseudonym extends AbstractPseudonymType
{
}
16 changes: 16 additions & 0 deletions tests/resources/xml/fed_Pseudonym.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<fed:Pseudonym xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<fed:PseudonymBasis ssp:attr2="testval2">
<ssp:Chunk>Basis</ssp:Chunk>
</fed:PseudonymBasis>
<fed:RelativeTo ssp:attr3="testval3">
<ssp:Chunk>Relative</ssp:Chunk>
</fed:RelativeTo>
<wsu:Expires>2001-10-13T09:00:00Z</wsu:Expires>
<fed:SecurityToken ssp:attr4="testval4">
<ssp:Chunk>Security</ssp:Chunk>
</fed:SecurityToken>
<fed:ProofToken ssp:attr5="testval5">
<ssp:Chunk>Proof</ssp:Chunk>
</fed:ProofToken>
<ssp:Chunk>Some</ssp:Chunk>
</fed:Pseudonym>

0 comments on commit 76f9d7f

Please sign in to comment.