Skip to content

Commit

Permalink
Add wst classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 27, 2024
1 parent d2c6270 commit 334f340
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 3 deletions.
82 changes: 82 additions & 0 deletions src/XML/wst/AbstractRequestedReferenceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\wst;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\TooManyElementsException;

use function array_pop;

/**
* Class defining the RequestedReferenceType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractRequestedReferenceType extends AbstractWstElement
{
/**
* AbstractRequestedReferenceType constructor
*
* @param \SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference $securityTokenReference
*/
final public function __construct(
protected SecurityTokenReference $securityTokenReference
) {
}


/**
* Collect the value of the securityTokenReference property.
*
* @return \SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference
*/
public function getSecurityTokenReference(): SecurityTokenReference
{
return $this->securityTokenReference;
}


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

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

return new static(array_pop($securityTokenReference));
}


/**
* Add this RequestedReferenceType 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->getSecurityTokenReference()->toXML($e);

return $e;
}
}
6 changes: 3 additions & 3 deletions src/XML/wst/AbstractRequestedSecurityTokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractRequestedSecurityTokenType extends AbstractWstElement
/**
* AbstractRequestedSecurityTokenType constructor
*
* @param \SimpleSAML\XML\SerializableElementInterface[]|null $child
* @param \SimpleSAML\XML\SerializableElementInterface $child
*/
final public function __construct(
SerializableElementInterface $child
Expand Down Expand Up @@ -64,8 +64,8 @@ public static function fromXML(DOMElement $xml): static
$children[] = new Chunk($child);
}

Assert::minCount($children, 1, MissingElementInterfaceException::class);
Assert::maxCount($children, 1, TooManyElementsInterfaceException::class);
Assert::minCount($children, 1, MissingElementEcxeption::class);
Assert::maxCount($children, 1, TooManyElementsException::class);

return new static(array_pop($children));
}
Expand Down
14 changes: 14 additions & 0 deletions src/XML/wst/RequestedAttachedReference.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 RequestedAttachedReference element
*
* @package tvdijen/ws-security
*/
final class RequestedAttachedReference extends AbstractRequestedReferenceType
{
}
14 changes: 14 additions & 0 deletions src/XML/wst/RequestedUnattachedReference.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 RequestedUnattachedReference element
*
* @package tvdijen/ws-security
*/
final class RequestedUnattachedReference extends AbstractRequestedReferenceType
{
}
75 changes: 75 additions & 0 deletions tests/WSSecurity/XML/wst/RequestedAttachedReferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\wst;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference;
use SimpleSAML\WSSecurity\XML\wst\RequestedAttachedReference;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\wst\RequestedAttachedReferenceTest
*
* @covers \SimpleSAML\WSSecurity\XML\wst\RequestedAttachedReference
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractRequestedReferenceType
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement
*
* @package tvdijen/ws-security
*/
final class RequestedAttachedReferenceTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-trust.xsd';

self::$testedClass = RequestedAttachedReference::class;

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


// test marshalling


/**
* Test creating a RequestedAttachedReference object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1');
$child = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">SomeChunk</ssp:Chunk>',
);

$securityTokenReference = new SecurityTokenReference(
'SomeID',
'SomeUsage',
[new Chunk($child->documentElement)],
[$attr1],
);

$requestedAttachedReference = new RequestedAttachedReference($securityTokenReference);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($requestedAttachedReference),
);
}
}
75 changes: 75 additions & 0 deletions tests/WSSecurity/XML/wst/RequestedUnattachedReferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\wst;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\wsse\SecurityTokenReference;
use SimpleSAML\WSSecurity\XML\wst\RequestedUnattachedReference;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

/**
* Class \SimpleSAML\WSSecurity\XML\wst\RequestedUnattachedReferenceTest
*
* @covers \SimpleSAML\WSSecurity\XML\wst\RequestedUnattachedReference
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractRequestedReferenceType
* @covers \SimpleSAML\WSSecurity\XML\wst\AbstractWstElement
*
* @package tvdijen/ws-security
*/
final class RequestedUnattachedReferenceTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


/**
*/
public static function setUpBeforeClass(): void
{
self::$schemaFile = dirname(__FILE__, 5) . '/resources/schemas/ws-trust.xsd';

self::$testedClass = RequestedUnattachedReference::class;

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


// test marshalling


/**
* Test creating a RequestedUnattachedReference object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1');
$child = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">SomeChunk</ssp:Chunk>',
);

$securityTokenReference = new SecurityTokenReference(
'SomeID',
'SomeUsage',
[new Chunk($child->documentElement)],
[$attr1],
);

$requestedUnattachedReference = new RequestedUnattachedReference($securityTokenReference);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($requestedUnattachedReference),
);
}
}
5 changes: 5 additions & 0 deletions tests/resources/xml/wst_RequestedAttachedReference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<wst:RequestedAttachedReference xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512/">
<wsse:SecurityTokenReference xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ssp="urn:x-simplesamlphp:namespace" wsse:Usage="SomeUsage" wsu:Id="SomeID" ssp:attr1="testval1">
<ssp:Chunk>SomeChunk</ssp:Chunk>
</wsse:SecurityTokenReference>
</wst:RequestedAttachedReference>
5 changes: 5 additions & 0 deletions tests/resources/xml/wst_RequestedUnattachedReference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<wst:RequestedUnattachedReference xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512/">
<wsse:SecurityTokenReference xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ssp="urn:x-simplesamlphp:namespace" wsse:Usage="SomeUsage" wsu:Id="SomeID" ssp:attr1="testval1">
<ssp:Chunk>SomeChunk</ssp:Chunk>
</wsse:SecurityTokenReference>
</wst:RequestedUnattachedReference>

0 comments on commit 334f340

Please sign in to comment.