Skip to content

Commit

Permalink
Create wst classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 22, 2024
1 parent eaabd84 commit 7f864a3
Show file tree
Hide file tree
Showing 14 changed files with 531 additions and 6 deletions.
10 changes: 5 additions & 5 deletions resources/schemas/ws-trust.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ No other rights are granted by implication, estoppel or otherwise.
xmlns:wsa='http://schemas.xmlsoap.org/ws/2004/08/addressing'
xmlns:wst='http://schemas.xmlsoap.org/ws/2005/02/trust'
targetNamespace='http://schemas.xmlsoap.org/ws/2005/02/trust'
elementFormDefault='qualified'>
elementFormDefault='qualified' >

<xs:import namespace='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
schemaLocation='oasis-200401-wss-wssecurity-secext-1.0.xsd' />
schemaLocation='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' />
<xs:import namespace='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'
schemaLocation='oasis-200401-wss-wssecurity-utility-1.0.xsd' />
schemaLocation='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' />
<xs:import namespace='http://schemas.xmlsoap.org/ws/2004/09/policy'
schemaLocation='ws-policy.xsd' />
schemaLocation='http://schemas.xmlsoap.org/ws/2004/09/policy/ws-policy.xsd' />
<xs:import namespace='http://schemas.xmlsoap.org/ws/2004/08/addressing'
schemaLocation='ws-addr.xsd' />
schemaLocation='http://schemas.xmlsoap.org/ws/2004/08/addressing/' />

<!-- WS-Trust Section 5.1 -->
<xs:element name='RequestSecurityToken' type='wst:RequestSecurityTokenType' />
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Constants extends \SimpleSAML\SAML2\Constants
/**
* The namespace for WS-Trust protocol.
*/
public const NS_TRUST = 'http://docs.oasis-open.org/ws-sx/ws-trust/200802';
public const NS_TRUST = 'http://schemas.xmlsoap.org/ws/2005/02/trust';

/**
* The namespace for WS-Security extensions.
Expand Down
92 changes: 92 additions & 0 deletions src/XML/wst/AbstractParticipantType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XML\XsNamespace as NS;
use SimpleSAML\XML\SerializableElementInterface;

/**
* Class defining the ParticipantType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractParticipantType extends AbstractWstElement
{
use ExtendableElementTrait;

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


/**
* AbstractParticipantType constructor
*
* @param \SimpleSAML\XML\SerializableElementInterface $child
*/
final public function __construct(
SerializableElementInterface $child
) {
$this->setElements([$child]);
}


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

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

Assert::minCount($children, 1, MissingElementException::class);
Assert::maxCount($children, 1, TooManyElementsException::class);

return new static(
array_pop($children),
);
}


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

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

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

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

/**
* Class defining the ParticipantsType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractParticipantsType extends AbstractWstElement
{
use ExtendableElementTrait;

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


/**
* AbstractParticipantsType constructor
*
* @param \SimpleSAML\WSSecurity\XML\wst\Primary|null $primary
* @param \SimpleSAML\WSSecurity\XML\wst\Participant[] $participant
* @param \SimpleSAML\XML\SerializableElementInterface[] $children
*/
final public function __construct(
protected ?Primary $primary = null,
protected array $participant = [],
array $children = []
) {
$this->setElements($children);
}


/**
* @return \SimpleSAML\WSSecurity\XML\wst\Primary|null
*/
public function getPrimary(): ?Primary
{
return $this->primary;
}


/**
* @return \SimpleSAML\WSSecurity\XML\wst\Participant[]
*/
public function getParticipant(): array
{
return $this->participant;
}


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


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

$primary = Primary::getChildrenOfClass($xml);
Assert::maxCount($primary, 1, TooManyElementsException::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(
array_pop($primary),
Participant::getChildrenOfClass($xml),
$children,
);
}


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

$this->getPrimary()?->toXML($e);

foreach ($this->getParticipant() as $p) {
$p->toXML($e);
}

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

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

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 AbstractWstElement extends AbstractElement
{
/** @var string */
public const NS = C::NS_TRUST;

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

/**
* A Participant element
*
* @package tvdijen/ws-security
*/
final class Participant extends AbstractParticipantType
{
}
14 changes: 14 additions & 0 deletions src/XML/wst/Participants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

/**
* A Participants element
*
* @package tvdijen/ws-security
*/
final class Participants extends AbstractParticipantsType
{
}
14 changes: 14 additions & 0 deletions src/XML/wst/Primary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

/**
* A Primary element
*
* @package tvdijen/ws-security
*/
final class Primary extends AbstractParticipantType
{
}
65 changes: 65 additions & 0 deletions tests/WSSecurity/XML/wst/ParticipantTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\wst;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\wst\Participant;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\wst\ParticipantTest
*
* @covers \SimpleSAML\WSSecurity\XML\wst\Participant
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractParticipantType
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement
*
* @package tvdijen/ws-security
*/
final class ParticipantTest extends TestCase
{
use SerializableElementTestTrait;

/** @var \SimpleSAML\XML\Chunk $chunk */
protected static Chunk $chunk;


/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = Participant::class;

self::$xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/wst_Participant.xml',
);

self::$chunk = new Chunk(DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>'
)->documentElement);
}


// test marshalling


/**
* Test creating a Participant object from scratch.
*/
public function testMarshalling(): void
{
$participant = new Participant(self::$chunk);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($participant),
);
}
}
Loading

0 comments on commit 7f864a3

Please sign in to comment.