Skip to content

Commit 4c93903

Browse files
jaimepereztvdijen
andauthored
Add ds:SignatureValue (#15)
* Add ds:SignatureValue * Use the new SignatureValue * Fix test value * Bump dependency on our assert library * Fix test namespace * Fix namespaces Co-authored-by: Tim van Dijen <tvdijen@gmail.com>
1 parent 55e334d commit 4c93903

7 files changed

Lines changed: 193 additions & 12 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"ext-dom": "*",
4040

4141
"robrichards/xmlseclibs": "^3.1.1",
42-
"simplesamlphp/assert": "~0.2.1",
42+
"simplesamlphp/assert": "~0.2.2",
4343
"simplesamlphp/xml-common": "~0.3.2"
4444
},
4545
"require-dev": {

src/XML/ds/Signature.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use DOMElement;
88
use Exception;
99
use SimpleSAML\Assert\Assert;
10-
use SimpleSAML\XML\AbstractXMLElement;
1110
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12-
use SimpleSAML\XML\Exception\MissingAttributeException;
1311
use SimpleSAML\XML\Utils as XMLUtils;
1412
use SimpleSAML\XMLSecurity\Utils\Certificate;
1513
use SimpleSAML\XMLSecurity\Utils\Security;
@@ -29,6 +27,9 @@ final class Signature extends AbstractDsElement
2927
/** @var string[] */
3028
protected array $certificates = [];
3129

30+
/** @var SignatureValue */
31+
protected SignatureValue $value;
32+
3233
/** @var \SimpleSAML\XMLSecurity\XMLSecurityKey|null */
3334
protected ?XMLSecurityKey $key;
3435

@@ -47,6 +48,7 @@ final class Signature extends AbstractDsElement
4748
*/
4849
public function __construct(
4950
string $algorithm,
51+
SignatureValue $value,
5052
array $certificates = [],
5153
?XMLSecurityKey $key = null
5254
) {
@@ -121,6 +123,28 @@ protected function setKey(?XMLSecurityKey $key): void
121123
}
122124

123125

126+
/**
127+
* Get the SignatureValue corresponding to this signature.
128+
*
129+
* @return SignatureValue
130+
*/
131+
public function getSignatureValue(): SignatureValue
132+
{
133+
return $this->value;
134+
}
135+
136+
137+
/**
138+
* Set the SignatureValue.
139+
*
140+
* @param SignatureValue $value
141+
*/
142+
protected function setSignatureValue(SignatureValue $value): void
143+
{
144+
$this->value = $value;
145+
}
146+
147+
124148
/**
125149
* @return XMLSecurityDSig
126150
*/
@@ -165,7 +189,10 @@ public static function fromXML(DOMElement $xml): object
165189
);
166190
}
167191

168-
$signature = new self(self::getAttribute($sigMethod, 'Algorithm'), $certificates);
192+
$value = SignatureValue::getChildrenOfClass($xml);
193+
Assert::count($value, 1, 'ds:Signature needs exactly one ds:SignatureValue');
194+
195+
$signature = new self(self::getAttribute($sigMethod, 'Algorithm'), $value[0], $certificates);
169196

170197
$signature->signer->sigNode = $xml;
171198

src/XML/ds/SignatureValue.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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:SignatureValue element.
13+
*
14+
* @package simplesaml/xml-security
15+
*/
16+
final class SignatureValue extends AbstractDsElement
17+
{
18+
/** @var string */
19+
protected string $value;
20+
21+
22+
/**
23+
* Initialize a SignatureValue element.
24+
*
25+
* @param string $value
26+
*/
27+
public function __construct(string $value) {
28+
$this->setValue($value);
29+
}
30+
31+
32+
/**
33+
* Get the signature value.
34+
*
35+
* @return string
36+
*/
37+
public function getValue(): string
38+
{
39+
return $this->value;
40+
}
41+
42+
43+
/**
44+
* Set the value.
45+
*
46+
* @param string $value
47+
*/
48+
private function setValue(string $value): void
49+
{
50+
Assert::notEmpty($value, 'SignatureValue cannot be empty');
51+
Assert::stringPlausibleBase64($value, 'SignatureValue is not a valid Base64 encoded string');
52+
$this->value = $value;
53+
}
54+
55+
56+
/**
57+
* Convert XML into a SignatureValue
58+
*
59+
* @param DOMElement $xml The XML element we should load
60+
* @return self
61+
*
62+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException If the qualified name of the supplied element is
63+
* wrong.
64+
*/
65+
public static function fromXML(DOMElement $xml): object
66+
{
67+
Assert::same($xml->localName, 'SignatureValue', InvalidDOMElementException::class);
68+
Assert::same($xml->namespaceURI, SignatureValue::NS, InvalidDOMElementException::class);
69+
70+
return new self($xml->textContent);
71+
}
72+
73+
74+
/**
75+
* Convert this SignatureValue to XML.
76+
*
77+
* @param DOMElement|null $parent The element we should append this SignatureValue element to.
78+
* @return DOMElement
79+
*/
80+
public function toXML(DOMElement $parent = null): \DOMElement
81+
{
82+
$e = $this->instantiateParentElement($parent);
83+
$e->textContent = $this->value;
84+
85+
return $e;
86+
}
87+
}

tests/XML/ds/KeyNameTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
66

7-
use DOMDocument;
87
use PHPUnit\Framework\TestCase;
98
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
109
use SimpleSAML\XML\DOMDocumentFactory;
1110
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
12-
use SimpleSAML\XMLSecurity\XMLSecurityDSig;
1311

1412
/**
1513
* Class \SimpleSAML\XMLSecurity\XML\ds\KeyNameTest
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\Test\XML\ds;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XMLSecurity\XML\ds\SignatureValue;
11+
12+
/**
13+
* Class \SimpleSAML\XMLSecurity\XML\ds\SignatureValue
14+
*
15+
* @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
16+
* @covers \SimpleSAML\XMLSecurity\\XML\ds\SignatureValue
17+
*
18+
* @package simplesamlphp/xml-security
19+
*/
20+
final class SignatureValueTest extends TestCase
21+
{
22+
use SerializableXMLTestTrait;
23+
24+
25+
/**
26+
* Set up the test.
27+
*/
28+
protected function setUp(): void
29+
{
30+
$this->testedClass = SignatureValue::class;
31+
32+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
33+
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_SignatureValue.xml'
34+
);
35+
}
36+
37+
38+
/**
39+
* Test creating a SignatureValue from scratch.
40+
*/
41+
public function testMarshalling(): void
42+
{
43+
$this->assertEquals(
44+
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
45+
strval(new SignatureValue(
46+
'j14G9v6AnsOiEJYgkTg864DG3e/KLqoGpuybPGSGblVTn7ST6M/BsvP7YiVZjLqJEuEvWmf2mW4DPb+pbArzzDcsLWEtNveMrw+F' .
47+
'kWehDUQV9oe20iepo+W46wmj7zB/eWL+Z8MrGvlycoTndJU6CVwHTLsB+dq2FDa7JV4pAPjMY32JZTbiwKhzqw3nEi/eVrujJE4Y' .
48+
'RrlW28D+rXhITfoUAGGvsqPzcwGzp02lnMe2SmXADY1u9lbVjOhUrJpgvWfn9YuiCR+wjvaGMwIwzfJxChLJZOBV+1ad1CyNTiu6' .
49+
'qAblxZ4F8cWlMWJ7f0KkWvtw66HOf2VNR6Qan2Ra7Q=='))
50+
);
51+
}
52+
53+
54+
/**
55+
* Test creating a SignatureValue object from XML.
56+
*/
57+
public function testUnmarshalling(): void
58+
{
59+
$signatureValue = SignatureValue::fromXML($this->xmlRepresentation->documentElement);
60+
$this->assertEquals(
61+
'j14G9v6AnsOiEJYgkTg864DG3e/KLqoGpuybPGSGblVTn7ST6M/BsvP7YiVZjLqJEuEvWmf2mW4DPb+pbArzzDcsLWEtNveMrw+F' .
62+
'kWehDUQV9oe20iepo+W46wmj7zB/eWL+Z8MrGvlycoTndJU6CVwHTLsB+dq2FDa7JV4pAPjMY32JZTbiwKhzqw3nEi/eVrujJE4Y' .
63+
'RrlW28D+rXhITfoUAGGvsqPzcwGzp02lnMe2SmXADY1u9lbVjOhUrJpgvWfn9YuiCR+wjvaGMwIwzfJxChLJZOBV+1ad1CyNTiu6' .
64+
'qAblxZ4F8cWlMWJ7f0KkWvtw66HOf2VNR6Qan2Ra7Q==',
65+
$signatureValue->getValue()
66+
);
67+
}
68+
}

tests/XML/ds/TransformTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
declare(strict_types=1);
44

5-
namespace SimpleSAML\Test\SAML2\XML\ds;
5+
namespace SimpleSAML\Test\XML\ds;
66

77
use DOMDocument;
88
use PHPUnit\Framework\TestCase;
9-
use SimpleSAML\SAML2\Constants;
9+
use SimpleSAML\Constants;
1010
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
1111
use SimpleSAML\XML\DOMDocumentFactory;
1212
use SimpleSAML\XML\Chunk;
1313
use SimpleSAML\XML\Utils as XMLUtils;
1414
use SimpleSAML\XMLSecurity\XML\ds\Transform;
1515

1616
/**
17-
* Class \SAML2\XML\ds\TransformTest
17+
* Class \SimpleSAML\XMLSecurity\XML\ds\TransformTest
1818
*
19-
* @covers \SimpleSAML\SAML2\XML\ds\Transform
20-
* @covers \SimpleSAML\SAML2\XML\ds\AbstractDsElement
19+
* @covers \SimpleSAML\XMLSecurity\XML\ds\Transform
20+
* @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
2121
*
22-
* @package simplesamlphp/saml2
22+
* @package simplesamlphp/xml-security
2323
*/
2424
final class TransformTest extends TestCase
2525
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<ds:SignatureValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#">j14G9v6AnsOiEJYgkTg864DG3e/KLqoGpuybPGSGblVTn7ST6M/BsvP7YiVZjLqJEuEvWmf2mW4DPb+pbArzzDcsLWEtNveMrw+FkWehDUQV9oe20iepo+W46wmj7zB/eWL+Z8MrGvlycoTndJU6CVwHTLsB+dq2FDa7JV4pAPjMY32JZTbiwKhzqw3nEi/eVrujJE4YRrlW28D+rXhITfoUAGGvsqPzcwGzp02lnMe2SmXADY1u9lbVjOhUrJpgvWfn9YuiCR+wjvaGMwIwzfJxChLJZOBV+1ad1CyNTiu6qAblxZ4F8cWlMWJ7f0KkWvtw66HOf2VNR6Qan2Ra7Q==</ds:SignatureValue>

0 commit comments

Comments
 (0)