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 7ef68bf commit 4774ffc
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 1 deletion.
107 changes: 107 additions & 0 deletions src/XML/fed/AbstractProofTokenType.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 ProofTokenType element
*
* @package tvdijen/ws-security
*/
abstract class AbstractProofTokenType 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;


/**
* AbstractProofTokenType 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 AbstractProofTokenType 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/ProofToken.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 ProofToken element
*
* @package tvdijen/ws-security
*/
final class ProofToken extends AbstractProofTokenType
{
}
70 changes: 70 additions & 0 deletions tests/WSSecurity/XML/fed/ProofTokenTest.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\ProofToken;
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:ProofToken.
*
* @covers \SimpleSAML\WSSecurity\XML\fed\ProofToken
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractProofTokenType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractFedElement
* @package tvdijen/ws-security
*/
final class ProofTokenTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


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

self::$testedClass = ProofToken::class;

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


// test marshalling


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

$proofToken = new ProofToken(
new Chunk($child->documentElement),
[$attr1],
);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($proofToken),
);
}
}
2 changes: 1 addition & 1 deletion tests/WSSecurity/XML/fed/RequestPseudonymTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Tests for fed:RequestPseudonym.
*
* @covers \SimpleSAML\WSSecurity\XML\fed\RequestPseudonym
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractRequestPseudonym
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractRequestPseudonymType
* @covers \SimpleSAML\WSSecurity\XML\fed\AbstractFedElement
* @package tvdijen/ws-security
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/resources/xml/fed_ProofToken.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<fed:ProofToken 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:ProofToken>

0 comments on commit 4774ffc

Please sign in to comment.