Skip to content

Commit ab35543

Browse files
authored
Add XML-representation for ds:Reference element (#14)
* Add XML-representation for ds:Reference element
1 parent ba8c751 commit ab35543

19 files changed

Lines changed: 393 additions & 168 deletions

src/XML/ds/Reference.php

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\ds;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
11+
/**
12+
* Class representing a ds:Reference element.
13+
*
14+
* @package simplesamlphp/xml-security
15+
*/
16+
final class Reference extends AbstractDsElement
17+
{
18+
/** @var \SimpleSAML\XMLSecurity\XML\ds\Transforms|null */
19+
protected Transforms $transforms;
20+
21+
/** @var \SimpleSAML\XMLSecurity\XML\ds\DigestMethod */
22+
protected DigestMethod $digestMethod;
23+
24+
/** @var \SimpleSAML\XMLSecurity\XML\ds\DigestValue */
25+
protected DigestValue $digestValue;
26+
27+
/** @var string|null $Id */
28+
protected ?string $Id;
29+
30+
/** @var string|null $type */
31+
protected ?string $Type;
32+
33+
/** @var string|null $URI */
34+
protected ?string $URI;
35+
36+
37+
/**
38+
* Initialize a ds:Reference
39+
*
40+
* @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod
41+
* @param \SimpleSAML\XMLSecurity\XML\ds\DigestValue $digestValue
42+
* @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
43+
* @param string|null $Id
44+
* @param string|null $Type
45+
* @param string|null $URI
46+
*/
47+
public function __construct(
48+
DigestMethod $digestMethod,
49+
DigestValue $digestValue,
50+
?Transforms $transforms = null,
51+
?string $Id = null,
52+
?string $Type = null,
53+
?string $URI = null
54+
) {
55+
$this->setTransforms($transforms);
56+
$this->setDigestMethod($digestMethod);
57+
$this->setDigestValue($digestValue);
58+
$this->setId($Id);
59+
$this->setType($Type);
60+
$this->setURI($URI);
61+
}
62+
63+
64+
/**
65+
* @return \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
66+
*/
67+
public function getTransforms(): ?Transforms
68+
{
69+
return $this->transforms;
70+
}
71+
72+
73+
/**
74+
* @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
75+
*/
76+
protected function setTransforms(?Transforms $transforms): void
77+
{
78+
$this->transforms = $transforms;
79+
}
80+
81+
82+
/**
83+
* @return \SimpleSAML\XMLSecurity\XML\ds\DigestMethod
84+
*/
85+
public function getDigestMethod(): DigestMethod
86+
{
87+
return $this->digestMethod;
88+
}
89+
90+
91+
/**
92+
* @param \SimpleSAML\XMLSecurity\XML\ds\DigestMethod $digestMethod
93+
*/
94+
private function setDigestMethod(DigestMethod $digestMethod): void
95+
{
96+
$this->digestMethod = $digestMethod;
97+
}
98+
99+
100+
/**
101+
* @return \SimpleSAML\XMLSecurity\XML\ds\DigestValue
102+
*/
103+
public function getDigestValue(): DigestValue
104+
{
105+
return $this->digestValue;
106+
}
107+
108+
109+
/**
110+
* @param \SimpleSAML\XMLSecurity\XML\ds\DigestValue $digestValue
111+
*/
112+
private function setDigestValue(DigestValue $digestValue): void
113+
{
114+
$this->digestValue = $digestValue;
115+
}
116+
117+
118+
/**
119+
* @return string|null
120+
*/
121+
public function getId(): ?string
122+
{
123+
return $this->Id;
124+
}
125+
126+
127+
/**
128+
* @param string|null $Id
129+
*/
130+
private function setId(?string $Id): void
131+
{
132+
$this->Id = $Id;
133+
}
134+
135+
136+
/**
137+
* @return string|null
138+
*/
139+
public function getType(): ?string
140+
{
141+
return $this->Type;
142+
}
143+
144+
145+
/**
146+
* @param string|null $Type
147+
*/
148+
private function setType(?string $Type): void
149+
{
150+
$this->Type = $Type;
151+
}
152+
153+
154+
/**
155+
* @return string|null
156+
*/
157+
public function getURI(): ?string
158+
{
159+
return $this->URI;
160+
}
161+
162+
163+
/**
164+
* @param string|null $URI
165+
*/
166+
private function setURI(?string $URI): void
167+
{
168+
$this->URI = $URI;
169+
}
170+
171+
172+
/**
173+
* Convert XML into a Reference element
174+
*
175+
* @param \DOMElement $xml The XML element we should load
176+
* @return self
177+
*
178+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
179+
* If the qualified name of the supplied element is wrong
180+
*/
181+
public static function fromXML(DOMElement $xml): object
182+
{
183+
Assert::same($xml->localName, 'Reference', InvalidDOMElementException::class);
184+
Assert::same($xml->namespaceURI, Reference::NS, InvalidDOMElementException::class);
185+
186+
$Id = self::getAttribute($xml, 'Id', null);
187+
$Type = self::getAttribute($xml, 'Type', null);
188+
$URI = self::getAttribute($xml, 'URI', null);
189+
190+
$transforms = Transforms::getChildrenOfClass($xml);
191+
Assert::maxCount($transforms, 1, 'A <ds:Reference> may contain just one <ds:Transforms>.');
192+
193+
$digestMethod = DigestMethod::getChildrenOfClass($xml);
194+
Assert::count($digestMethod, 1, 'A <ds:Reference> must contain a <ds:DigestMethod>.');
195+
196+
$digestValue = DigestValue::getChildrenOfClass($xml);
197+
Assert::count($digestValue, 1, 'A <ds:Reference> must contain a <ds:DigestValue>.');
198+
199+
return new self(
200+
array_pop($digestMethod),
201+
array_pop($digestValue),
202+
empty($transforms) ? null : array_pop($transforms),
203+
$Id,
204+
$Type,
205+
$URI
206+
);
207+
}
208+
209+
210+
/**
211+
* Convert this Reference element to XML.
212+
*
213+
* @param \DOMElement|null $parent The element we should append this Reference element to.
214+
* @return \DOMElement
215+
*/
216+
public function toXML(DOMElement $parent = null): DOMElement
217+
{
218+
$e = $this->instantiateParentElement($parent);
219+
if ($this->Id !== null) {
220+
$e->setAttribute('Id', $this->Id);
221+
}
222+
if ($this->Type !== null) {
223+
$e->setAttribute('Type', $this->Type);
224+
}
225+
if ($this->URI !== null) {
226+
$e->setAttribute('URI', $this->URI);
227+
}
228+
229+
if ($this->transforms !== null) {
230+
$this->transforms->toXML($e);
231+
}
232+
233+
$this->digestMethod->toXML($e);
234+
$this->digestValue->toXML($e);
235+
236+
return $e;
237+
}
238+
}

src/XML/ds/Transform.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ private function setElements(array $elements): void
8383
}
8484

8585

86-
/**
87-
* Test if an object, at the state it's in, would produce an empty XML-element
88-
*
89-
* @return bool
90-
*/
91-
public function isEmptyElement(): bool
92-
{
93-
return empty($this->elements);
94-
}
95-
96-
9786
/**
9887
* Convert XML into a Transform element
9988
*

tests/XML/ds/CanonicalizationMethodTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public function testMarshalling(): void
4141
{
4242
$canonicalizationMethod = new CanonicalizationMethod(Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS);
4343

44-
$canonicalizationMethodElement = $canonicalizationMethod->toXML();
45-
$this->assertEquals(Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS, $canonicalizationMethodElement->getAttribute('Algorithm'));
46-
4744
$this->assertEquals(
4845
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
4946
strval($canonicalizationMethod)
@@ -55,8 +52,8 @@ public function testMarshalling(): void
5552
*/
5653
public function testUnmarshalling(): void
5754
{
58-
$CanonicalizationMethod = CanonicalizationMethod::fromXML($this->xmlRepresentation->documentElement);
55+
$canonicalizationMethod = CanonicalizationMethod::fromXML($this->xmlRepresentation->documentElement);
5956

60-
$this->assertEquals(Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS, $CanonicalizationMethod->getAlgorithm());
57+
$this->assertEquals(Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS, $canonicalizationMethod->getAlgorithm());
6158
}
6259
}

tests/XML/ds/KeyInfoTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,6 @@ public function testMarshalling(): void
9595
'abc123'
9696
);
9797

98-
$keyInfoElement = $keyInfo->toXML();
99-
$this->assertCount(4, $keyInfoElement->childNodes);
100-
$this->assertEquals('abc123', $keyInfoElement->getAttribute('Id'));
101-
102-
$keyNameElement = XMLUtils::xpQuery($keyInfoElement, './ds:KeyName');
103-
$this->assertCount(1, $keyNameElement);
104-
105-
$x509DataElement = XMLUtils::xpQuery($keyInfoElement, './ds:X509Data');
106-
$this->assertCount(1, $x509DataElement);
107-
10898
$this->assertEquals(
10999
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
110100
strval($keyInfo)

tests/XML/ds/KeyNameTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ public function testMarshalling(): void
3939
{
4040
$keyName = new KeyName('testkey');
4141

42-
$keyNameElement = $keyName->toXML();
43-
$this->assertEquals('testkey', $keyNameElement->textContent);
44-
4542
$this->assertEquals(
4643
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
4744
strval($keyName)

0 commit comments

Comments
 (0)