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 a4b2bfd commit 3b8075b
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/XML/wst/AbstractRequestSecurityTokenCollectionType.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\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;

/**
* A RequestSecurityTokenCollectionType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractRequestSecurityTokenCollectionType extends AbstractWstElement
{
/**
* @param \SimpleSAML\WSSecurity\XML\wst\RequestSecurityToken[] $requestSecurityToken
*/
final public function __construct(
protected array $requestSecurityToken
) {
Assert::minCount($requestSecurityToken, 2, MissingElementException::class);
Assert::allIsInstanceOf(
$requestSecurityToken,
RequestSecurityToken::class,
SchemaViolationException::class,
);
}


/**
* Get the requestSecurityToken property.
*
* @return \SimpleSAML\WSSecurity\XMLwst\RequestSecurityToken[]
*/
public function getRequestSecurityToken(): array
{
return $this->requestSecurityToken;
}


/**
* Convert XML into a class instance
*
* @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
{
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);

return new static(
RequestSecurityToken::getChildrenOfClass($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->getRequestSecurityToken() as $r) {
$r->toXML($e);
}

return $e;
}
}
14 changes: 14 additions & 0 deletions src/XML/wst/RequestSecurityTokenCollection.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 RequestSecurityTokenCollection element
*
* @package tvdijen/ws-security
*/
final class RequestSecurityTokenCollection extends AbstractRequestSecurityTokenCollectionType
{
}
76 changes: 76 additions & 0 deletions tests/WSSecurity/XML/wst/RequestSecurityTokenCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\wst;

use PHPUnit\Framework\TestCase;
use SimpleSAML\SOAP\Constants as SOAP;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\wsa\MessageID;
use SimpleSAML\WSSecurity\XML\wst\RequestSecurityToken;
use SimpleSAML\WSSecurity\XML\wst\RequestSecurityTokenCollection;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;

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


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

self::$testedClass = RequestSecurityTokenCollection::class;

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


// test marshalling


/**
* Test creating a RequestSecurityTokenCollection object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute(SOAP::NS_SOAP_ENV_11, 'soapenv', 'mustUnderstand', '1');
$attr2 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'testval1');
$attr3 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr2', 'testval2');
$msgId1 = new MessageID('uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de', [$attr1]);
$msgId2 = new MessageID('uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7df', [$attr1]);

$requestSecurityToken1 = new RequestSecurityToken(C::NAMESPACE, [$msgId1], [$attr2]);
$requestSecurityToken2 = new RequestSecurityToken(C::NAMESPACE, [$msgId2], [$attr3]);

$requestSecurityTokenCollection = new RequestSecurityTokenCollection([
$requestSecurityToken1,
$requestSecurityToken2,
]);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($requestSecurityTokenCollection),
);
}
}
8 changes: 8 additions & 0 deletions tests/resources/xml/wst_RequestSecurityTokenCollection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<wst:RequestSecurityTokenCollection xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512/">
<wst:RequestSecurityToken xmlns:ssp="urn:x-simplesamlphp:namespace" Context="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<wsa:MessageID xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" soapenv:mustUnderstand="1">uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7de</wsa:MessageID>
</wst:RequestSecurityToken>
<wst:RequestSecurityToken xmlns:ssp="urn:x-simplesamlphp:namespace" Context="urn:x-simplesamlphp:namespace" ssp:attr2="testval2">
<wsa:MessageID xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" soapenv:mustUnderstand="1">uuid:d0ccf3cd-2dce-4c1a-a5d6-be8912ecd7df</wsa:MessageID>
</wst:RequestSecurityToken>
</wst:RequestSecurityTokenCollection>

0 comments on commit 3b8075b

Please sign in to comment.