Skip to content

Commit d0a93b1

Browse files
authored
Add XML-representation for ds:CanonicalizationMethod element (#9)
1 parent f2d03e7 commit d0a93b1

3 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
use SimpleSAML\XMLSecurity\Constants;
11+
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
12+
13+
/**
14+
* Class representing a ds:CanonicalizationMethod element.
15+
*
16+
* @package simplesamlphp/xml-security
17+
*/
18+
final class CanonicalizationMethod extends AbstractDsElement
19+
{
20+
/**
21+
* The algorithm.
22+
*
23+
* @var string
24+
*/
25+
protected string $Algorithm;
26+
27+
28+
/**
29+
* Initialize a CanonicalizationMethod element.
30+
*
31+
* @param string $algorithm
32+
*/
33+
public function __construct(string $algorithm)
34+
{
35+
$this->setAlgorithm($algorithm);
36+
}
37+
38+
39+
/**
40+
* Collect the value of the Algorithm-property
41+
*
42+
* @return string
43+
*/
44+
public function getAlgorithm(): string
45+
{
46+
return $this->Algorithm;
47+
}
48+
49+
50+
/**
51+
* Set the value of the Algorithm-property
52+
*
53+
* @param string $algorithm
54+
*/
55+
private function setAlgorithm(string $algorithm): void
56+
{
57+
Assert::oneOf(
58+
$algorithm,
59+
[
60+
Constants::C14N_EXCLUSIVE_WITH_COMMENTS,
61+
Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
62+
Constants::C14N_INCLUSIVE_WITH_COMMENTS,
63+
Constants::C14N_INCLUSIVE_WITHOUT_COMMENTS
64+
],
65+
'Invalid canonicalization method',
66+
InvalidArgumentException::class
67+
);
68+
69+
$this->Algorithm = $algorithm;
70+
}
71+
72+
73+
/**
74+
* Convert XML into a CanonicalizationMethod
75+
*
76+
* @param \DOMElement $xml The XML element we should load
77+
* @return self
78+
*
79+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
80+
* If the qualified name of the supplied element is wrong
81+
*/
82+
public static function fromXML(DOMElement $xml): object
83+
{
84+
Assert::same($xml->localName, 'CanonicalizationMethod', InvalidDOMElementException::class);
85+
Assert::same($xml->namespaceURI, CanonicalizationMethod::NS, InvalidDOMElementException::class);
86+
87+
$Algorithm = CanonicalizationMethod::getAttribute($xml, 'Algorithm');
88+
89+
return new self($Algorithm);
90+
}
91+
92+
93+
/**
94+
* Convert this CanonicalizationMethod element to XML.
95+
*
96+
* @param \DOMElement|null $parent The element we should append this KeyName element to.
97+
* @return \DOMElement
98+
*/
99+
public function toXML(DOMElement $parent = null): DOMElement
100+
{
101+
$e = $this->instantiateParentElement($parent);
102+
$e->setAttribute('Algorithm', $this->Algorithm);
103+
104+
return $e;
105+
}
106+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6+
7+
use DOMDocument;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XMLSecurity\Constants;
11+
use SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod;
12+
13+
/**
14+
* Class \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethodTest
15+
*
16+
* @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
17+
* @covers \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod
18+
*
19+
* @package simplesamlphp/xml-security
20+
*/
21+
final class CanonicalizationMethodTest extends TestCase
22+
{
23+
/** @var \DOMDocument */
24+
private DOMDocument $document;
25+
26+
/**
27+
*/
28+
protected function setUp(): void
29+
{
30+
$this->document = DOMDocumentFactory::fromFile(
31+
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_CanonicalizationMethod.xml'
32+
);
33+
}
34+
35+
36+
/**
37+
*/
38+
public function testMarshalling(): void
39+
{
40+
$CanonicalizationMethod = new CanonicalizationMethod(Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS);
41+
42+
$this->assertEquals(Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS, $CanonicalizationMethod->getAlgorithm());
43+
44+
$this->assertEquals($this->document->saveXML($this->document->documentElement), strval($CanonicalizationMethod));
45+
}
46+
47+
48+
/**
49+
*/
50+
public function testUnmarshalling(): void
51+
{
52+
$CanonicalizationMethod = CanonicalizationMethod::fromXML($this->document->documentElement);
53+
54+
$this->assertEquals(Constants::C14N_EXCLUSIVE_WITHOUT_COMMENTS, $CanonicalizationMethod->getAlgorithm());
55+
}
56+
57+
58+
/**
59+
* Test serialization / unserialization
60+
*/
61+
public function testSerialization(): void
62+
{
63+
$this->assertEquals(
64+
$this->document->saveXML($this->document->documentElement),
65+
strval(unserialize(serialize(CanonicalizationMethod::fromXML($this->document->documentElement))))
66+
);
67+
}
68+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ds:CanonicalizationMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />

0 commit comments

Comments
 (0)