Skip to content

Commit ef19985

Browse files
committed
Add xenc11:Parameters element
1 parent c1aeb1a commit ef19985

3 files changed

Lines changed: 180 additions & 0 deletions

File tree

src/XML/xenc11/Parameters.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\ExtendableAttributesTrait;
11+
use SimpleSAML\XML\ExtendableElementTrait;
12+
use SimpleSAML\XML\XsNamespace as NS;
13+
14+
/**
15+
* Class representing xenc11:Parameters
16+
*
17+
* @package simplesamlphp/xml-security
18+
*/
19+
final class Parameters extends AbstractXenc11Element
20+
{
21+
use ExtendableAttributesTrait;
22+
use ExtendableElementTrait;
23+
24+
/** The namespace-attribute for the xs:any element */
25+
public const XS_ANY_ELT_NAMESPACE = NS::ANY;
26+
27+
/** The namespace-attribute for the xs:anyAttribute element */
28+
public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
29+
30+
31+
/**
32+
* Initialize a Parameters element.
33+
*
34+
* @param \SimpleSAML\XML\Chunk[] $elements
35+
* @param \SimpleSAML\XML\Attribute[] $attributes
36+
*/
37+
public function __construct(array $elements = [], array $attributes = [])
38+
{
39+
$this->setElements($elements);
40+
$this->setAttributesNS($attributes);
41+
}
42+
43+
44+
45+
/**
46+
* Test if an object, at the state it's in, would produce an empty XML-element
47+
*
48+
* @return bool
49+
*/
50+
public function isEmptyElement(): bool
51+
{
52+
return empty($this->getAttributesNS())
53+
&& empty($this->getElements());
54+
}
55+
56+
57+
/**
58+
* Convert XML into a Parameters element
59+
*
60+
* @param \DOMElement $xml The XML element we should load
61+
* @return static
62+
*
63+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
64+
* if the qualified name of the supplied element is wrong
65+
*/
66+
public static function fromXML(DOMElement $xml): static
67+
{
68+
Assert::same($xml->localName, 'Parameters', InvalidDOMElementException::class);
69+
Assert::same($xml->namespaceURI, Parameters::NS, InvalidDOMElementException::class);
70+
71+
return new static(
72+
self::getChildElementsFromXML($xml),
73+
self::getAttributesNSFromXML($xml),
74+
);
75+
}
76+
77+
78+
/**
79+
* Convert this Parameters element to XML.
80+
*
81+
* @param \DOMElement|null $parent The element we should append this Parameters to.
82+
* @return \DOMElement
83+
*/
84+
public function toXML(?DOMElement $parent = null): DOMElement
85+
{
86+
$e = $this->instantiateParentElement($parent);
87+
88+
foreach ($this->getAttributesNS() as $attr) {
89+
$attr->toXML($e);
90+
}
91+
92+
foreach ($this->getElements() as $element) {
93+
/** @psalm-var \SimpleSAML\XML\SerializableElementInterface $element */
94+
$element->toXML($e);
95+
}
96+
97+
return $e;
98+
}
99+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\Parameters;
11+
use SimpleSAML\XML\Attribute as XMLAttribute;
12+
use SimpleSAML\XML\Chunk;
13+
use SimpleSAML\XML\DOMDocumentFactory;
14+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
15+
16+
use function dirname;
17+
use function strval;
18+
19+
/**
20+
* Class \SimpleSAML\XMLSecurity\XML\xenc11\ParametersTest
21+
*
22+
* @package simplesamlphp/xml-security
23+
*/
24+
#[CoversClass(Parameters::class)]
25+
#[CoversClass(AbstractXenc11Element::class)]
26+
final class ParametersTest extends TestCase
27+
{
28+
use SerializableElementTestTrait;
29+
30+
/**
31+
*/
32+
public static function setUpBeforeClass(): void
33+
{
34+
self::$testedClass = Parameters::class;
35+
36+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
37+
dirname(__FILE__, 3) . '/resources/xml/xenc11_Parameters.xml',
38+
);
39+
}
40+
41+
42+
// marshalling
43+
44+
45+
/**
46+
*/
47+
public function testMarshalling(): void
48+
{
49+
$chunk = new Chunk(DOMDocumentFactory::fromString(
50+
<<<XML
51+
<ssp:AuthenticationContextDeclaration xmlns:ssp="urn:x-simplesamlphp:namespace">
52+
<ssp:Identification nym="verinymity">
53+
<ssp:Extension>
54+
<ssp:NoVerification/>
55+
</ssp:Extension>
56+
</ssp:Identification>
57+
</ssp:AuthenticationContextDeclaration>
58+
XML
59+
,
60+
)->documentElement);
61+
62+
$parameters = new Parameters(
63+
[$chunk],
64+
[new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1')],
65+
);
66+
67+
$this->assertEquals(
68+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
69+
strval($parameters),
70+
);
71+
}
72+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<xenc11:Parameters xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" xmlns:ssp="urn:x-simplesamlphp:namespace" ssp:attr1="testval1">
2+
<ssp:AuthenticationContextDeclaration>
3+
<ssp:Identification nym="verinymity">
4+
<ssp:Extension>
5+
<ssp:NoVerification/>
6+
</ssp:Extension>
7+
</ssp:Identification>
8+
</ssp:AuthenticationContextDeclaration>
9+
</xenc11:Parameters>

0 commit comments

Comments
 (0)