Skip to content

Commit

Permalink
Add element auth:AdditionalContext
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Sep 15, 2024
1 parent fe73a1f commit 1eec9db
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
109 changes: 109 additions & 0 deletions src/XML/auth/AbstractAdditionalContextType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\auth;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\WSSecurity\XML\auth\ContextItem;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\ExtendableAttributesTrait;
use SimpleSAML\XML\ExtendableElementTrait;
use SimpleSAML\XML\XsNamespace as NS;

/**
* Class defining the AdditionalContextType element
*
* @package simplesamlphp/ws-security
*/
abstract class AbstractAdditionalContextType extends AbstractAuthElement
{
use ExtendableAttributesTrait;
use ExtendableElementTrait;

/** The namespace-attribute for the xs:anyAttribute */
public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;

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


/**
* AbstractAdditionalContextType constructor
*
* @param \SimpleSAML\WSSecurity\XML\auth\ContextItem[] $contextItem
* @param list<\SimpleSAML\XML\SerializableElementInterface> $children
* @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
*/
final public function __construct(
protected array $contextItem = [],
array $children = [],
array $namespacedAttributes = [],
) {
$this->setElements($children);
$this->setAttributesNS($namespacedAttributes);
}


/**
* Get the value of the $contextItem property.
*
* @return \SimpleSAML\WSSecurity\XML\auth\ContextItem[]
*/
public function getContextItem(): array
{
return $this->contextItem;
}


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

$contextItem = ContextItem::getChildrenOfClass($xml);
$children = self::getChildElementsFromXML($xml);

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


/**
* Add this AdditionalContext 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 = $this->instantiateParentElement($parent);

foreach ($this->getAttributesNS() as $attr) {
$attr->toXML($e);
}

foreach ($this->getContextItem() as $ctx) {
$ctx->toXML($e);
}

foreach ($this->getElements() as $elt) {
$elt->toXML($e);
}

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

declare(strict_types=1);

namespace SimpleSAML\WSSecurity\XML\auth;

/**
* Class representing WS-authorization AdditionalContext.
*
* @package simplesamlphp/ws-security
*/
final class AdditionalContext extends AbstractAdditionalContextType
{
}
88 changes: 88 additions & 0 deletions tests/WSSecurity/XML/auth/AdditionalContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\WSSecurity\XML\auth;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Test\WSSecurity\Constants as C;
use SimpleSAML\WSSecurity\XML\auth\AbstractAdditionalContextType;
use SimpleSAML\WSSecurity\XML\auth\AbstractAuthElement;
use SimpleSAML\WSSecurity\XML\auth\AdditionalContext;
use SimpleSAML\WSSecurity\XML\auth\ContextItem;
use SimpleSAML\WSSecurity\XML\auth\Value;
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 auth:AdditionalContext.
*
* @package simplesamlphp/ws-security
*/
#[Group('auth')]
#[CoversClass(AdditionalContext::class)]
#[CoversClass(AbstractAdditionalContextType::class)]
#[CoversClass(AbstractAuthElement::class)]
final class AdditionalContextTest extends TestCase
{
use SchemaValidationTestTrait;
use SerializableElementTestTrait;


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

self::$testedClass = AdditionalContext::class;

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


// test marshalling


/**
* Test creating a ContextItem object from scratch.
*/
public function testMarshalling(): void
{
$attr1 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr1', 'value1');
$attr2 = new XMLAttribute(C::NAMESPACE, 'ssp', 'attr2', 'value2');

$contextItem = new ContextItem(
C::NAMESPACE,
'urn:x-simplesamlphp:scope',
new Value('someValue'),
null,
[$attr1],
);

$child = DOMDocumentFactory::fromString(
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">Some</ssp:Chunk>',
);

$additionalContext = new AdditionalContext(
[$contextItem],
[new Chunk($child->documentElement)],
[$attr2],
);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($additionalContext),
);
}
}
6 changes: 6 additions & 0 deletions tests/resources/xml/auth_AdditionalContext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<auth:AdditionalContext xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr2="value2">
<auth:ContextItem Name="urn:x-simplesamlphp:namespace" Scope="urn:x-simplesamlphp:scope" ssp:attr1="value1">
<auth:Value>someValue</auth:Value>
</auth:ContextItem>
<ssp:Chunk>Some</ssp:Chunk>
</auth:AdditionalContext>

0 comments on commit 1eec9db

Please sign in to comment.