Skip to content

Commit fe6e748

Browse files
committed
Add dsig11:FieldID-element
1 parent e261471 commit fe6e748

File tree

4 files changed

+273
-0
lines changed

4 files changed

+273
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\XML\ExtendableElementTrait;
9+
use SimpleSAML\XML\XsNamespace as NS;
10+
11+
/**
12+
* Abstract class representing a dsig11:FieldIDType
13+
*
14+
* @package simplesaml/xml-security
15+
*/
16+
abstract class AbstractFieldIDType extends AbstractDsig11Element
17+
{
18+
use ExtendableElementTrait;
19+
20+
/** @var \SimpleSAML\XML\XsNamespace */
21+
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
22+
23+
24+
/**
25+
* Initialize a FieldIDType element.
26+
*
27+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\Prime $prime
28+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\TnB $tnb
29+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\PnB $pnb
30+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\GnB $gnb
31+
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children
32+
*/
33+
public function __construct(
34+
protected Prime $prime,
35+
protected TnB $tnb,
36+
protected PnB $pnb,
37+
protected GnB $gnb,
38+
array $children,
39+
) {
40+
$this->setElements($children);
41+
}
42+
43+
44+
/**
45+
* Collect the value of the prime-property
46+
*
47+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\Prime
48+
*/
49+
public function getPrime(): Prime
50+
{
51+
return $this->prime;
52+
}
53+
54+
55+
/**
56+
* Collect the value of the tnb-property
57+
*
58+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\TnB
59+
*/
60+
public function getTnB(): TnB
61+
{
62+
return $this->tnb;
63+
}
64+
65+
66+
/**
67+
* Collect the value of the pnb-property
68+
*
69+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\PnB
70+
*/
71+
public function getPnB(): PnB
72+
{
73+
return $this->pnb;
74+
}
75+
76+
77+
/**
78+
* Collect the value of the gnb-property
79+
*
80+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\GnB
81+
*/
82+
public function getGnB(): GnB
83+
{
84+
return $this->gnb;
85+
}
86+
87+
88+
/**
89+
* Convert this FieldIDType element to XML.
90+
*
91+
* @param \DOMElement|null $parent The element we should append this FieldIDType element to.
92+
* @return \DOMElement
93+
*/
94+
public function toXML(?DOMElement $parent = null): DOMElement
95+
{
96+
$e = $this->instantiateParentElement($parent);
97+
98+
$this->getPrime()->toXML($e);
99+
$this->getTnB()->toXML($e);
100+
$this->getPnB()->toXML($e);
101+
$this->getGnB()->toXML($e);
102+
103+
foreach ($this->getElements() as $elt) {
104+
$elt->toXML($e);
105+
}
106+
107+
return $e;
108+
}
109+
}

src/XML/dsig11/FieldID.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\MissingElementException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XML\SchemaValidatableElementInterface;
13+
use SimpleSAML\XML\SchemaValidatableElementTrait;
14+
15+
/**
16+
* Class representing a dsig11:FieldID element.
17+
*
18+
* @package simplesaml/xml-security
19+
*/
20+
final class FieldID extends AbstractFieldIDType implements SchemaValidatableElementInterface
21+
{
22+
use SchemaValidatableElementTrait;
23+
24+
/**
25+
* Convert XML into a FieldID element
26+
*
27+
* @param \DOMElement $xml The XML element we should load
28+
* @return static
29+
*
30+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
31+
* If the qualified name of the supplied element is wrong
32+
*/
33+
public static function fromXML(DOMElement $xml): static
34+
{
35+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
36+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
37+
38+
$prime = Prime::getChildrenOfClass($xml);
39+
Assert::minCount($prime, 1, MissingElementException::class);
40+
Assert::maxCount($prime, 1, TooManyElementsException::class);
41+
42+
$tnb = TnB::getChildrenOfClass($xml);
43+
Assert::minCount($tnb, 1, MissingElementException::class);
44+
Assert::maxCount($tnb, 1, TooManyElementsException::class);
45+
46+
$pnb = PnB::getChildrenOfClass($xml);
47+
Assert::minCount($pnb, 1, MissingElementException::class);
48+
Assert::maxCount($pnb, 1, TooManyElementsException::class);
49+
50+
$gnb = GnB::getChildrenOfClass($xml);
51+
Assert::minCount($gnb, 1, MissingElementException::class);
52+
Assert::maxCount($gnb, 1, TooManyElementsException::class);
53+
54+
return new static(
55+
array_pop($prime),
56+
array_pop($tnb),
57+
array_pop($pnb),
58+
array_pop($gnb),
59+
self::getChildElementsFromXML($xml),
60+
);
61+
}
62+
}

tests/XML/dsig11/FieldIDTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\dsig11;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\Chunk;
10+
use SimpleSAML\XML\DOMDocumentFactory;
11+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractFieldIDType;
14+
use SimpleSAML\XMLSecurity\XML\dsig11\FieldID;
15+
use SimpleSAML\XMLSecurity\XML\dsig11\GnB;
16+
use SimpleSAML\XMLSecurity\XML\dsig11\K;
17+
use SimpleSAML\XMLSecurity\XML\dsig11\K1;
18+
use SimpleSAML\XMLSecurity\XML\dsig11\K2;
19+
use SimpleSAML\XMLSecurity\XML\dsig11\K3;
20+
use SimpleSAML\XMLSecurity\XML\dsig11\M;
21+
use SimpleSAML\XMLSecurity\XML\dsig11\P;
22+
use SimpleSAML\XMLSecurity\XML\dsig11\PnB;
23+
use SimpleSAML\XMLSecurity\XML\dsig11\Prime;
24+
use SimpleSAML\XMLSecurity\XML\dsig11\TnB;
25+
26+
use function dirname;
27+
use function strval;
28+
29+
/**
30+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\FieldIDTest
31+
*
32+
* @package simplesamlphp/xml-security
33+
*/
34+
#[CoversClass(AbstractDsig11Element::class)]
35+
#[CoversClass(AbstractFieldIDType::class)]
36+
#[CoversClass(FieldID::class)]
37+
final class FieldIDTest extends TestCase
38+
{
39+
use SerializableElementTestTrait;
40+
41+
42+
/**
43+
*/
44+
public static function setUpBeforeClass(): void
45+
{
46+
self::$testedClass = FieldID::class;
47+
48+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
49+
dirname(__FILE__, 3) . '/resources/xml/dsig11_FieldID.xml',
50+
);
51+
}
52+
53+
54+
/**
55+
*/
56+
public function testMarshalling(): void
57+
{
58+
$p = new P('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
59+
$prime = new Prime($p);
60+
61+
$m = new M(1024);
62+
$k = new K(64);
63+
$tnb = new TnB($m, $k);
64+
65+
$k1 = new K1(128);
66+
$k2 = new K2(256);
67+
$k3 = new K3(512);
68+
$pnb = new PnB($m, $k1, $k2, $k3);
69+
70+
$gnb = new GnB($m);
71+
72+
$chunk = new Chunk(DOMDocumentFactory::fromString(
73+
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
74+
)->documentElement);
75+
76+
$fieldId = new FieldID($prime, $tnb, $pnb, $gnb, [$chunk]);
77+
78+
$this->assertEquals(
79+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
80+
strval($fieldId),
81+
);
82+
}
83+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<dsig11:FieldID xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">
2+
<dsig11:Prime>
3+
<dsig11:P>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:P>
4+
</dsig11:Prime>
5+
<dsig11:TnB>
6+
<dsig11:M>1024</dsig11:M>
7+
<dsig11:K>64</dsig11:K>
8+
</dsig11:TnB>
9+
<dsig11:PnB>
10+
<dsig11:M>1024</dsig11:M>
11+
<dsig11:K1>128</dsig11:K1>
12+
<dsig11:K2>256</dsig11:K2>
13+
<dsig11:K3>512</dsig11:K3>
14+
</dsig11:PnB>
15+
<dsig11:GnB>
16+
<dsig11:M>1024</dsig11:M>
17+
</dsig11:GnB>
18+
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>
19+
</dsig11:FieldID>

0 commit comments

Comments
 (0)