Skip to content

Commit 61ef931

Browse files
committed
Add dsig11:ValidationData-element
1 parent 0fdf0ef commit 61ef931

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\SchemaViolationException;
10+
11+
/**
12+
* Abstract class representing a dsig11:ECValidationDataType
13+
*
14+
* @package simplesaml/xml-security
15+
*/
16+
abstract class AbstractECValidationDataType extends AbstractDsig11Element
17+
{
18+
/**
19+
* Initialize a ECValidationDataType element.
20+
*
21+
* @param \SimpleSAML\XMLSecurity\XML\dsig11\Seed $seed
22+
* @param string $hashAlgorithm
23+
*/
24+
public function __construct(
25+
protected Seed $seed,
26+
protected string $hashAlgorithm,
27+
) {
28+
Assert::validURI($hashAlgorithm, SchemaViolationException::class);
29+
}
30+
31+
32+
/**
33+
* Collect the value of the seed-property
34+
*
35+
* @return \SimpleSAML\XMLSecurity\XML\dsig11\Seed
36+
*/
37+
public function getSeed(): Seed
38+
{
39+
return $this->seed;
40+
}
41+
42+
43+
/**
44+
* Collect the value of the hashAlgorithm-property
45+
*
46+
* @return string
47+
*/
48+
public function getHashAlgorithm(): string
49+
{
50+
return $this->hashAlgorithm;
51+
}
52+
53+
54+
/**
55+
* Convert this ECValidationDataType element to XML.
56+
*
57+
* @param \DOMElement|null $parent The element we should append this ECValidationDataType element to.
58+
* @return \DOMElement
59+
*/
60+
public function toXML(?DOMElement $parent = null): DOMElement
61+
{
62+
$e = $this->instantiateParentElement($parent);
63+
$e->setAttribute('hashAlgorithm', $this->getHashAlgorithm());
64+
65+
$this->getSeed()->toXML($e);
66+
67+
return $e;
68+
}
69+
}

src/XML/dsig11/ValidationData.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10+
use SimpleSAML\XML\Exception\MissingElementException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XML\SchemaValidatableElementInterface;
13+
use SimpleSAML\XML\SchemaValidatableElementTrait;
14+
15+
use function array_pop;
16+
17+
/**
18+
* Class representing a dsig11:ValidationData element.
19+
*
20+
* @package simplesaml/xml-security
21+
*/
22+
final class ValidationData extends AbstractECValidationDataType implements SchemaValidatableElementInterface
23+
{
24+
use SchemaValidatableElementTrait;
25+
26+
/**
27+
* Convert XML into a ValidationData element
28+
*
29+
* @param \DOMElement $xml The XML element we should load
30+
* @return static
31+
*
32+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
33+
* If the qualified name of the supplied element is wrong
34+
*/
35+
public static function fromXML(DOMElement $xml): static
36+
{
37+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
38+
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
39+
40+
$seed = Seed::getChildrenOfClass($xml);
41+
Assert::minCount($seed, 1, MissingElementException::class);
42+
Assert::maxCount($seed, 1, TooManyElementsException::class);
43+
44+
return new static(
45+
array_pop($seed),
46+
self::getAttribute($xml, 'hashAlgorithm'),
47+
);
48+
}
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML\dsig11;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\XML\DOMDocumentFactory;
10+
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
11+
use SimpleSAML\XMLSecurity\Constants as C;
12+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractDsig11Element;
13+
use SimpleSAML\XMLSecurity\XML\dsig11\AbstractECValidationDataType;
14+
use SimpleSAML\XMLSecurity\XML\dsig11\Seed;
15+
use SimpleSAML\XMLSecurity\XML\dsig11\ValidationData;
16+
17+
use function dirname;
18+
use function strval;
19+
20+
/**
21+
* Class \SimpleSAML\XMLSecurity\Test\XML\dsig11\ValidationDataTest
22+
*
23+
* @package simplesamlphp/xml-security
24+
*/
25+
#[CoversClass(AbstractDsig11Element::class)]
26+
#[CoversClass(AbstractECValidationDataType::class)]
27+
#[CoversClass(ValidationData::class)]
28+
final class ValidationDataTest extends TestCase
29+
{
30+
use SerializableElementTestTrait;
31+
32+
33+
/**
34+
*/
35+
public static function setUpBeforeClass(): void
36+
{
37+
self::$testedClass = ValidationData::class;
38+
39+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
40+
dirname(__FILE__, 3) . '/resources/xml/dsig11_ValidationData.xml',
41+
);
42+
}
43+
44+
45+
/**
46+
*/
47+
public function testMarshalling(): void
48+
{
49+
$seed = new Seed('6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=');
50+
$validationData = new ValidationData($seed, C::DIGEST_SHA1);
51+
52+
$this->assertEquals(
53+
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
54+
strval($validationData),
55+
);
56+
}
57+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<dsig11:ValidationData xmlns:dsig11="http://www.w3.org/2009/xmldsig11#" hashAlgorithm="http://www.w3.org/2000/09/xmldsig#sha1">
2+
<dsig11:Seed>6tN39Q9d6IevlAWLeM7lQGazUnVlJOe1wCk3sro2rfE=</dsig11:Seed>
3+
</dsig11:ValidationData>

0 commit comments

Comments
 (0)