Skip to content

Commit 2a42d1a

Browse files
committed
Add xenc11:OtherSource element
1 parent 056d0b6 commit 2a42d1a

4 files changed

Lines changed: 194 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\xenc11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XMLSecurity\XML\ds\DigestMethod;
13+
14+
use function array_pop;
15+
16+
/**
17+
* Class representing <xenc11:AlgorithmIdentifierType>.
18+
*
19+
* @package simplesamlphp/xml-security
20+
*/
21+
abstract class AbstractAlgorithmIdentifierType extends AbstractXenc11Element
22+
{
23+
/**
24+
* AlgorithmIdentifierType constructor.
25+
*
26+
* @param string $Algorithm
27+
* @param \SimpleSAML\XMLSecurity\XML\xenc11\Parameters|null $parameters
28+
*/
29+
final public function __construct(
30+
protected string $Algorithm,
31+
protected ?Parameters $parameters = null,
32+
) {
33+
Assert::validURI($Algorithm, SchemaViolationException::class);
34+
}
35+
36+
37+
/**
38+
* Get the value of the $Algorithm property.
39+
*
40+
* @return string
41+
*/
42+
public function getAlgorithm(): string
43+
{
44+
return $this->Algorithm;
45+
}
46+
47+
48+
/**
49+
* Get the value of the $parameters property.
50+
*
51+
* @return \SimpleSAML\XMLSecurity\XML\xenc11\Parameters
52+
*/
53+
public function getParameters(): Parameters
54+
{
55+
return $this->parameters;
56+
}
57+
58+
59+
/**
60+
* @inheritDoc
61+
*
62+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
63+
* If the qualified name of the supplied element is wrong
64+
*/
65+
public static function fromXML(DOMElement $xml): static
66+
{
67+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
68+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
69+
70+
$parameter = Parameters::getChildrenOfClass($xml);
71+
Assert::maxCount($parameter, 1, TooManyElementsException::class);
72+
73+
return new static(
74+
self::getOptionalAttribute($xml, 'Algorithm'),
75+
array_pop($parameter),
76+
);
77+
}
78+
79+
80+
/**
81+
* @inheritDoc
82+
*/
83+
public function toXML(?DOMElement $parent = null): DOMElement
84+
{
85+
$e = $this->instantiateParentElement($parent);
86+
$e->setAttribute('Algorithm', $this->getAlgorithm());
87+
88+
$this->getParameters()?->toXML($e);
89+
90+
return $e;
91+
}
92+
}

src/XML/xenc11/OtherSource.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\xenc11;
6+
7+
/**
8+
* A class implementing the xenc11:OtherSource element.
9+
*
10+
* @package simplesamlphp/xml-security
11+
*/
12+
final class OtherSource extends AbstractAlgorithmIdentifierType
13+
{
14+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\SAML2\XML\xenc11;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractXenc11Element;
10+
use SimpleSAML\XMLSecurity\XML\xenc11\AbstractAlgorithmIdentifierType;
11+
use SimpleSAML\XMLSecurity\XML\xenc11\OtherSource;
12+
use SimpleSAML\XMLSecurity\XML\xenc11\Parameters;
13+
use SimpleSAML\XML\Attribute as XMLAttribute;
14+
use SimpleSAML\XML\Chunk;
15+
use SimpleSAML\XML\DOMDocumentFactory;
16+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
17+
18+
use function dirname;
19+
use function strval;
20+
21+
/**
22+
* Class \SimpleSAML\XMLSecurity\XML\xenc11\OtherSourceTest
23+
*
24+
* @package simplesamlphp/xml-security
25+
*/
26+
#[CoversClass(OtherSource::class)]
27+
#[CoversClass(AbstractAlgorithmIdentifierType::class)]
28+
#[CoversClass(AbstractXenc11Element::class)]
29+
final class OtherSourceTest extends TestCase
30+
{
31+
use SerializableElementTestTrait;
32+
33+
/**
34+
*/
35+
public static function setUpBeforeClass(): void
36+
{
37+
self::$testedClass = OtherSource::class;
38+
39+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
40+
dirname(__FILE__, 3) . '/resources/xml/xenc11_OtherSource.xml',
41+
);
42+
}
43+
44+
45+
// marshalling
46+
47+
48+
/**
49+
*/
50+
public function testMarshalling(): void
51+
{
52+
$chunk = new Chunk(DOMDocumentFactory::fromString(
53+
<<<XML
54+
<ssp:AuthenticationContextDeclaration xmlns:ssp="urn:x-simplesamlphp:namespace">
55+
<ssp:Identification nym="verinymity">
56+
<ssp:Extension>
57+
<ssp:NoVerification/>
58+
</ssp:Extension>
59+
</ssp:Identification>
60+
</ssp:AuthenticationContextDeclaration>
61+
XML
62+
,
63+
)->documentElement);
64+
65+
$parameters = new Parameters(
66+
[$chunk],
67+
[new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1')],
68+
);
69+
70+
$otherSource = new OtherSource('urn:x-simplesamlphp:algorithm', $parameters);
71+
72+
$this->assertEquals(
73+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
74+
strval($otherSource),
75+
);
76+
}
77+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<xenc11:OtherSource xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" Algorithm="urn:x-simplesamlphp:algorithm">
2+
<xenc11:Parameters xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
3+
<ssp:AuthenticationContextDeclaration>
4+
<ssp:Identification nym="verinymity">
5+
<ssp:Extension>
6+
<ssp:NoVerification/>
7+
</ssp:Extension>
8+
</ssp:Identification>
9+
</ssp:AuthenticationContextDeclaration>
10+
</xenc11:Parameters>
11+
</xenc11:OtherSource>

0 commit comments

Comments
 (0)