Skip to content

Commit

Permalink
Add xenc11:OtherSource element
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 5, 2024
1 parent 056d0b6 commit 2a42d1a
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/XML/xenc11/AbstractAlgorithmIdentifierType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\Exception\TooManyElementsException;
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod;

use function array_pop;

/**
* Class representing <xenc11:AlgorithmIdentifierType>.
*
* @package simplesamlphp/xml-security
*/
abstract class AbstractAlgorithmIdentifierType extends AbstractXenc11Element
{
/**
* AlgorithmIdentifierType constructor.
*
* @param string $Algorithm
* @param \SimpleSAML\XMLSecurity\XML\xenc11\Parameters|null $parameters
*/
final public function __construct(
protected string $Algorithm,
protected ?Parameters $parameters = null,
) {
Assert::validURI($Algorithm, SchemaViolationException::class);
}


/**
* Get the value of the $Algorithm property.
*
* @return string
*/
public function getAlgorithm(): string
{
return $this->Algorithm;
}


/**
* Get the value of the $parameters property.
*
* @return \SimpleSAML\XMLSecurity\XML\xenc11\Parameters
*/
public function getParameters(): Parameters
{
return $this->parameters;
}


/**
* @inheritDoc
*
* @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::getNamespaceURI(), InvalidDOMElementException::class);

$parameter = Parameters::getChildrenOfClass($xml);
Assert::maxCount($parameter, 1, TooManyElementsException::class);

return new static(
self::getOptionalAttribute($xml, 'Algorithm'),
array_pop($parameter),
);
}


/**
* @inheritDoc
*/
public function toXML(?DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$e->setAttribute('Algorithm', $this->getAlgorithm());

$this->getParameters()?->toXML($e);

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

declare(strict_types=1);

namespace SimpleSAML\XMLSecurity\XML\xenc11;

/**
* A class implementing the xenc11:OtherSource element.
*
* @package simplesamlphp/xml-security
*/
final class OtherSource extends AbstractAlgorithmIdentifierType
{
}
77 changes: 77 additions & 0 deletions tests/XML/xenc11/OtherSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\SAML2\XML\xenc11;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractAlgorithmIdentifierType;
use SimpleSAML\XMLSecurity\XML\xenc11\OtherSource;
use SimpleSAML\XMLSecurity\XML\xenc11\Parameters;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;

use function dirname;
use function strval;

/**
* Class \SimpleSAML\XMLSecurity\XML\xenc11\OtherSourceTest
*
* @package simplesamlphp/xml-security
*/
#[CoversClass(OtherSource::class)]
#[CoversClass(AbstractAlgorithmIdentifierType::class)]
#[CoversClass(AbstractXenc11Element::class)]
final class OtherSourceTest extends TestCase
{
use SerializableElementTestTrait;

/**
*/
public static function setUpBeforeClass(): void
{
self::$testedClass = OtherSource::class;

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


// marshalling


/**
*/
public function testMarshalling(): void
{
$chunk = new Chunk(DOMDocumentFactory::fromString(
<<<XML
<ssp:AuthenticationContextDeclaration xmlns:ssp="urn:x-simplesamlphp:namespace">
<ssp:Identification nym="verinymity">
<ssp:Extension>
<ssp:NoVerification/>
</ssp:Extension>
</ssp:Identification>
</ssp:AuthenticationContextDeclaration>
XML
,
)->documentElement);

$parameters = new Parameters(
[$chunk],
[new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1')],
);

$otherSource = new OtherSource('urn:x-simplesamlphp:algorithm', $parameters);

$this->assertEquals(
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
strval($otherSource),
);
}
}
11 changes: 11 additions & 0 deletions tests/resources/xml/xenc11_OtherSource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<xenc11:OtherSource xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" Algorithm="urn:x-simplesamlphp:algorithm">
<xenc11:Parameters xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
<ssp:AuthenticationContextDeclaration>
<ssp:Identification nym="verinymity">
<ssp:Extension>
<ssp:NoVerification/>
</ssp:Extension>
</ssp:Identification>
</ssp:AuthenticationContextDeclaration>
</xenc11:Parameters>
</xenc11:OtherSource>

0 comments on commit 2a42d1a

Please sign in to comment.