Skip to content

Commit 2a00453

Browse files
authored
Add XML-representation for ds:SignatureMethod element (#10)
1 parent d0a93b1 commit 2a00453

3 files changed

Lines changed: 183 additions & 0 deletions

File tree

src/XML/ds/SignatureMethod.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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:SignatureMethod element.
15+
*
16+
* @package simplesamlphp/xml-security
17+
*/
18+
final class SignatureMethod extends AbstractDsElement
19+
{
20+
/**
21+
* The algorithm.
22+
*
23+
* @var string
24+
*/
25+
protected string $Algorithm;
26+
27+
28+
/**
29+
* Initialize a SignatureMethod 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::SIG_RSA_SHA1,
61+
Constants::SIG_RSA_SHA224,
62+
Constants::SIG_RSA_SHA256,
63+
Constants::SIG_RSA_SHA384,
64+
Constants::SIG_RSA_SHA512,
65+
Constants::SIG_RSA_RIPEMD160,
66+
Constants::SIG_HMAC_SHA1,
67+
Constants::SIG_HMAC_SHA224,
68+
Constants::SIG_HMAC_SHA256,
69+
Constants::SIG_HMAC_SHA384,
70+
Constants::SIG_HMAC_SHA512,
71+
Constants::SIG_HMAC_RIPEMD160,
72+
],
73+
'Invalid canonicalization method',
74+
InvalidArgumentException::class
75+
);
76+
77+
$this->Algorithm = $algorithm;
78+
}
79+
80+
81+
/**
82+
* Convert XML into a SignatureMethod
83+
*
84+
* @param \DOMElement $xml The XML element we should load
85+
* @return self
86+
*
87+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
88+
* If the qualified name of the supplied element is wrong
89+
*/
90+
public static function fromXML(DOMElement $xml): object
91+
{
92+
Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class);
93+
Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class);
94+
95+
$Algorithm = SignatureMethod::getAttribute($xml, 'Algorithm');
96+
97+
return new self($Algorithm);
98+
}
99+
100+
101+
/**
102+
* Convert this SignatureMethod element to XML.
103+
*
104+
* @param \DOMElement|null $parent The element we should append this KeyName element to.
105+
* @return \DOMElement
106+
*/
107+
public function toXML(DOMElement $parent = null): DOMElement
108+
{
109+
$e = $this->instantiateParentElement($parent);
110+
$e->setAttribute('Algorithm', $this->Algorithm);
111+
112+
return $e;
113+
}
114+
}
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\SignatureMethod;
12+
13+
/**
14+
* Class \SimpleSAML\XMLSecurity\XML\ds\SignatureMethodTest
15+
*
16+
* @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
17+
* @covers \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod
18+
*
19+
* @package simplesamlphp/xml-security
20+
*/
21+
final class SignatureMethodTest 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_SignatureMethod.xml'
32+
);
33+
}
34+
35+
36+
/**
37+
*/
38+
public function testMarshalling(): void
39+
{
40+
$SignatureMethod = new SignatureMethod(Constants::SIG_RSA_SHA256);
41+
42+
$this->assertEquals(Constants::SIG_RSA_SHA256, $SignatureMethod->getAlgorithm());
43+
44+
$this->assertEquals($this->document->saveXML($this->document->documentElement), strval($SignatureMethod));
45+
}
46+
47+
48+
/**
49+
*/
50+
public function testUnmarshalling(): void
51+
{
52+
$SignatureMethod = SignatureMethod::fromXML($this->document->documentElement);
53+
54+
$this->assertEquals(Constants::SIG_RSA_SHA256, $SignatureMethod->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(SignatureMethod::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:SignatureMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />

0 commit comments

Comments
 (0)