Skip to content

Commit

Permalink
WIP: create fed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 9, 2024
1 parent 4774ffc commit cd6dba2
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/XML/fed/AbstractSecurityTokenType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\fed;

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

/**
* Class defining the SecurityTokenType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractSecurityTokenType 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::OTHER;


/**
* AbstractSecurityTokenType constructor
*
* @param \SimpleSAML\XML\SerializableElementInterface $child
* @param array $namespacedAttributes
*/
final public function __construct(
protected SerializableElementinterface $child,
array $namespacedAttributes = []
) {
$this->setElements([$child]);
$this->setAttributesNS($namespacedAttributes);
}


/**
* 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;
} elseif ($child->namespaceURI === static::NS) {
continue;
}

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

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

return new static(
array_pop($children),
self::getAttributesNSFromXML($xml),
);
}


/**
* Add this AbstractSecurityTokenType 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->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/SecurityToken.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 SecurityToken element
*
* @package tvdijen/ws-security
*/
final class SecurityToken extends AbstractSecurityTokenType
{
}
70 changes: 70 additions & 0 deletions tests/WSSecurity/XML/fed/SecurityTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\fed;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\WSSecurity\XML\fed\SecurityToken;
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;
use function strval;

/**
* Tests for fed:SecurityToken.
*
* @covers \SimpleSAML\WSSecurity\XML\fed\SecurityToken
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractSecurityTokenType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractFedElement
* @package tvdijen/ws-security
*/
final class SecurityTokenTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


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

self::$testedClass = SecurityToken::class;

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


// test marshalling


/**
* Test creating a SecurityToken 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>',
);

$securityToken = new SecurityToken(
new Chunk($child->documentElement),
[$attr1],
);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($securityToken),
);
}
}
3 changes: 3 additions & 0 deletions tests/resources/xml/fed_SecurityToken.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<fed:SecurityToken xmlns:fed="http://docs.oasis-open.org/wsfed/federation/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<ssp:Chunk>SomeChunk</ssp:Chunk>
</fed:SecurityToken>

0 comments on commit cd6dba2

Please sign in to comment.