Skip to content

Commit 05c4bd0

Browse files
committed
Refactor FieldIDType
1 parent 77461fa commit 05c4bd0

File tree

9 files changed

+49
-182
lines changed

9 files changed

+49
-182
lines changed

src/XML/dsig11/AbstractECKeyValueType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
protected PublicKey $publicKey,
2828
protected ?string $id = null,
2929
protected ?ECParameters $ecParameters = null,
30-
protected ?NamedCurve $ecParamOrNamedCurve = null,
30+
protected ?NamedCurve $namedCurve = null,
3131
) {
3232
Assert::validNCName($id, SchemaViolationException::class);
3333
Assert::oneOf(

src/XML/dsig11/AbstractFieldIDType.php

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace SimpleSAML\XMLSecurity\XML\dsig11;
66

77
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\SchemaViolationException;
810
use SimpleSAML\XML\ExtendableElementTrait;
11+
use SimpleSAML\XML\SerializableElementInterface;
912
use SimpleSAML\XML\XsNamespace as NS;
1013

1114
/**
@@ -15,7 +18,11 @@
1518
*/
1619
abstract class AbstractFieldIDType extends AbstractDsig11Element
1720
{
18-
use ExtendableElementTrait;
21+
// We use our own getter instead of the trait's one, so we prevent their use by marking them private
22+
use ExtendableElementTrait {
23+
getElements as private;
24+
setElements as private;
25+
}
1926

2027
/** @var \SimpleSAML\XML\XsNamespace */
2128
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
@@ -24,64 +31,39 @@ abstract class AbstractFieldIDType extends AbstractDsig11Element
2431
/**
2532
* Initialize a FieldIDType element.
2633
*
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
34+
* @param \SimpleSAML\XML\SerializableElementInterface $fieldId
3235
*/
3336
public function __construct(
34-
protected Prime $prime,
35-
protected TnB $tnb,
36-
protected PnB $pnb,
37-
protected GnB $gnb,
38-
array $children,
37+
protected Prime|TnB|PnB|GnB|SerializableElementInterface $fieldId,
3938
) {
40-
$this->setElements($children);
39+
if (
40+
!($fieldId instanceof Prime
41+
|| $fieldId instanceof TnB
42+
|| $fieldId instanceof PnB
43+
|| $fieldId instanceof GnB)
44+
) {
45+
Assert::true(
46+
(($fieldId instanceof Chunk) ? $fieldId->getNamespaceURI() : $fieldId::getNameSpaceURI())
47+
!== C::NS_XDSIG11,
48+
'A <dsig11:FieldIDType> requires either a Prime, TnB, PnB, GnB or an element in namespace ##other',
49+
SchemaViolationException::class,
50+
);
51+
}
4152
}
4253

4354

4455
/**
45-
* Collect the value of the prime-property
56+
* Collect the value of the fieldId-property
4657
*
4758
* @return \SimpleSAML\XMLSecurity\XML\dsig11\Prime
59+
* \SimpleSAML\XMLSecurity\XML\dsig11\TnB
60+
* \SimpleSAML\XMLSecurity\XML\dsig11\PnB
61+
* \SimpleSAML\XMLSecurity\XML\dsig11\GnB
62+
* \SimpleSAML\XML\SerializableElementInterface
4863
*/
49-
public function getPrime(): Prime
64+
public function getFieldId(): Prime|TnB|PnB|GnB|SerializableElementInterface
5065
{
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;
66+
return $this->fieldId;
8567
}
8668

8769

@@ -95,14 +77,7 @@ public function toXML(?DOMElement $parent = null): DOMElement
9577
{
9678
$e = $this->instantiateParentElement($parent);
9779

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-
}
80+
$this->getFieldId()->toXML($e);
10681

10782
return $e;
10883
}

src/XML/dsig11/FieldID.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10-
use SimpleSAML\XML\Exception\MissingElementException;
1110
use SimpleSAML\XML\Exception\TooManyElementsException;
1211
use SimpleSAML\XML\SchemaValidatableElementInterface;
1312
use SimpleSAML\XML\SchemaValidatableElementTrait;
@@ -35,28 +34,23 @@ public static function fromXML(DOMElement $xml): static
3534
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
3635
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
3736

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);
37+
$fieldId = array_merge(
38+
Prime::getChildrenOfClass($xml),
39+
TnB::getChildrenOfClass($xml),
40+
PnB::getChildrenOfClass($xml),
41+
GnB::getChildrenOfClass($xml),
42+
self::getChildElementsFromXML($xml),
43+
);
4944

50-
$gnb = GnB::getChildrenOfClass($xml);
51-
Assert::minCount($gnb, 1, MissingElementException::class);
52-
Assert::maxCount($gnb, 1, TooManyElementsException::class);
45+
Assert::count(
46+
$fieldId,
47+
1,
48+
'A <dsig11:FieldID> must contain exactly one child element',
49+
TooManyElementsException::class,
50+
);
5351

5452
return new static(
55-
array_pop($prime),
56-
array_pop($tnb),
57-
array_pop($pnb),
58-
array_pop($gnb),
59-
self::getChildElementsFromXML($xml),
53+
array_pop($fieldId),
6054
);
6155
}
6256
}

tests/XML/dsig11/ECKeyValueTest.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\TestCase;
9-
use SimpleSAML\XML\Chunk;
109
use SimpleSAML\XML\DOMDocumentFactory;
1110
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
1211
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
@@ -21,19 +20,14 @@
2120
use SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue;
2221
use SimpleSAML\XMLSecurity\XML\dsig11\ECParameters;
2322
use SimpleSAML\XMLSecurity\XML\dsig11\FieldID;
24-
use SimpleSAML\XMLSecurity\XML\dsig11\GnB;
25-
use SimpleSAML\XMLSecurity\XML\dsig11\K;
2623
use SimpleSAML\XMLSecurity\XML\dsig11\K1;
2724
use SimpleSAML\XMLSecurity\XML\dsig11\K2;
2825
use SimpleSAML\XMLSecurity\XML\dsig11\K3;
2926
use SimpleSAML\XMLSecurity\XML\dsig11\M;
3027
use SimpleSAML\XMLSecurity\XML\dsig11\Order;
31-
use SimpleSAML\XMLSecurity\XML\dsig11\P;
3228
use SimpleSAML\XMLSecurity\XML\dsig11\PnB;
33-
use SimpleSAML\XMLSecurity\XML\dsig11\Prime;
3429
use SimpleSAML\XMLSecurity\XML\dsig11\PublicKey;
3530
use SimpleSAML\XMLSecurity\XML\dsig11\Seed;
36-
use SimpleSAML\XMLSecurity\XML\dsig11\TnB;
3731
use SimpleSAML\XMLSecurity\XML\dsig11\ValidationData;
3832

3933
use function dirname;
@@ -70,25 +64,13 @@ public static function setUpBeforeClass(): void
7064
public function testMarshalling(): void
7165
{
7266
// Build FieldID
73-
$p = new P('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
74-
$prime = new Prime($p);
75-
7667
$m = new M(1024);
77-
$k = new K(64);
78-
$tnb = new TnB($m, $k);
79-
8068
$k1 = new K1(128);
8169
$k2 = new K2(256);
8270
$k3 = new K3(512);
8371
$pnb = new PnB($m, $k1, $k2, $k3);
8472

85-
$gnb = new GnB($m);
86-
87-
$chunk = new Chunk(DOMDocumentFactory::fromString(
88-
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
89-
)->documentElement);
90-
91-
$fieldId = new FieldID($prime, $tnb, $pnb, $gnb, [$chunk]);
73+
$fieldId = new FieldID($pnb);
9274

9375
// Build Curve
9476
$a = new A('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');

tests/XML/dsig11/ECParametersTest.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\TestCase;
9-
use SimpleSAML\XML\Chunk;
109
use SimpleSAML\XML\DOMDocumentFactory;
1110
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1211
use SimpleSAML\XMLSecurity\Constants as C;
@@ -19,16 +18,9 @@
1918
use SimpleSAML\XMLSecurity\XML\dsig11\Curve;
2019
use SimpleSAML\XMLSecurity\XML\dsig11\ECParameters;
2120
use SimpleSAML\XMLSecurity\XML\dsig11\FieldID;
22-
use SimpleSAML\XMLSecurity\XML\dsig11\GnB;
2321
use SimpleSAML\XMLSecurity\XML\dsig11\K;
24-
use SimpleSAML\XMLSecurity\XML\dsig11\K1;
25-
use SimpleSAML\XMLSecurity\XML\dsig11\K2;
26-
use SimpleSAML\XMLSecurity\XML\dsig11\K3;
2722
use SimpleSAML\XMLSecurity\XML\dsig11\M;
2823
use SimpleSAML\XMLSecurity\XML\dsig11\Order;
29-
use SimpleSAML\XMLSecurity\XML\dsig11\P;
30-
use SimpleSAML\XMLSecurity\XML\dsig11\PnB;
31-
use SimpleSAML\XMLSecurity\XML\dsig11\Prime;
3224
use SimpleSAML\XMLSecurity\XML\dsig11\Seed;
3325
use SimpleSAML\XMLSecurity\XML\dsig11\TnB;
3426
use SimpleSAML\XMLSecurity\XML\dsig11\ValidationData;
@@ -66,25 +58,11 @@ public static function setUpBeforeClass(): void
6658
public function testMarshalling(): void
6759
{
6860
// Build FieldID
69-
$p = new P('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
70-
$prime = new Prime($p);
71-
7261
$m = new M(1024);
7362
$k = new K(64);
7463
$tnb = new TnB($m, $k);
7564

76-
$k1 = new K1(128);
77-
$k2 = new K2(256);
78-
$k3 = new K3(512);
79-
$pnb = new PnB($m, $k1, $k2, $k3);
80-
81-
$gnb = new GnB($m);
82-
83-
$chunk = new Chunk(DOMDocumentFactory::fromString(
84-
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
85-
)->documentElement);
86-
87-
$fieldId = new FieldID($prime, $tnb, $pnb, $gnb, [$chunk]);
65+
$fieldId = new FieldID($tnb);
8866

8967
// Build Curve
9068
$a = new A('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');

tests/XML/dsig11/FieldIDTest.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@
66

77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\TestCase;
9-
use SimpleSAML\XML\Chunk;
109
use SimpleSAML\XML\DOMDocumentFactory;
1110
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1211
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
1312
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractFieldIDType;
1413
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;
2114
use SimpleSAML\XMLSecurity\XML\dsig11\P;
22-
use SimpleSAML\XMLSecurity\XML\dsig11\PnB;
2315
use SimpleSAML\XMLSecurity\XML\dsig11\Prime;
24-
use SimpleSAML\XMLSecurity\XML\dsig11\TnB;
2516

2617
use function dirname;
2718
use function strval;
@@ -58,22 +49,7 @@ public function testMarshalling(): void
5849
$p = new P('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
5950
$prime = new Prime($p);
6051

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]);
52+
$fieldId = new FieldID($prime);
7753

7854
$this->assertEquals(
7955
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),

tests/resources/xml/dsig11_ECKeyValue.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
<dsig11:ECKeyValue xmlns:dsig11="http://www.w3.org/2009/xmldsig11#" Id="phpunit">
22
<dsig11:ECParameters xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">
33
<dsig11:FieldID>
4-
<dsig11:Prime>
5-
<dsig11:P>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:P>
6-
</dsig11:Prime>
7-
<dsig11:TnB>
8-
<dsig11:M>1024</dsig11:M>
9-
<dsig11:K>64</dsig11:K>
10-
</dsig11:TnB>
114
<dsig11:PnB>
125
<dsig11:M>1024</dsig11:M>
136
<dsig11:K1>128</dsig11:K1>
147
<dsig11:K2>256</dsig11:K2>
158
<dsig11:K3>512</dsig11:K3>
169
</dsig11:PnB>
17-
<dsig11:GnB>
18-
<dsig11:M>1024</dsig11:M>
19-
</dsig11:GnB>
20-
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>
2110
</dsig11:FieldID>
2211
<dsig11:Curve>
2312
<dsig11:A>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:A>

tests/resources/xml/dsig11_ECParameters.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
<dsig11:ECParameters xmlns:dsig11="http://www.w3.org/2009/xmldsig11#">
22
<dsig11:FieldID>
3-
<dsig11:Prime>
4-
<dsig11:P>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:P>
5-
</dsig11:Prime>
63
<dsig11:TnB>
74
<dsig11:M>1024</dsig11:M>
85
<dsig11:K>64</dsig11:K>
96
</dsig11:TnB>
10-
<dsig11:PnB>
11-
<dsig11:M>1024</dsig11:M>
12-
<dsig11:K1>128</dsig11:K1>
13-
<dsig11:K2>256</dsig11:K2>
14-
<dsig11:K3>512</dsig11:K3>
15-
</dsig11:PnB>
16-
<dsig11:GnB>
17-
<dsig11:M>1024</dsig11:M>
18-
</dsig11:GnB>
19-
<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>
207
</dsig11:FieldID>
218
<dsig11:Curve>
229
<dsig11:A>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:A>

0 commit comments

Comments
 (0)