Skip to content

Commit 56a532b

Browse files
authored
Feature/ds missing elements (#58)
* Refactor X509SerialNumber * Implement IntegerElementTrait * Add HMACOutputLength element * Refactor ds:SignatureMethod * Add ds:MgmtData element * Add ds:PGPKeyID element * Add ds:PGPKeyPacket element * Add ds:PGPData element * Add ds:MgmtData element * Add ds:SPKISexp element * Add ds:SPKIData element
1 parent 2f478b2 commit 56a532b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1132
-87
lines changed

src/XML/ds/AbstractKeyInfoType.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ abstract class AbstractKeyInfoType extends AbstractDsElement
3535
* \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
3636
* \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
3737
* \SimpleSAML\XMLSecurity\XML\ds\X509Data|
38+
* \SimpleSAML\XMLSecurity\XML\ds\PGPData|
39+
* \SimpleSAML\XMLSecurity\XML\ds\SPKIData|
40+
* \SimpleSAML\XMLSecurity\XML\ds\MgmtData|
3841
* \SimpleSAML\XML\SerializableElementInterface
3942
* )[] $info
4043
* @param string|null $Id
@@ -64,7 +67,15 @@ final public function __construct(
6467
if ($item instanceof AbstractDsElement) {
6568
Assert::isInstanceOfAny(
6669
$item,
67-
[KeyName::class, KeyValue::class, RetrievalMethod::class, X509Data::class],
70+
[
71+
KeyName::class,
72+
KeyValue::class,
73+
RetrievalMethod::class,
74+
X509Data::class,
75+
PGPData::class,
76+
SPKIData::class,
77+
MgmtData::class,
78+
],
6879
SchemaViolationException::class,
6980
);
7081
}

src/XML/ds/AbstractPGPDataType.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XML\ExtendableElementTrait;
13+
use SimpleSAML\XML\XsNamespace as NS;
14+
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15+
16+
use function array_pop;
17+
18+
/**
19+
* Abstract class representing the PGPDataType.
20+
*
21+
* @package simplesamlphp/xml-security
22+
*/
23+
abstract class AbstractPGPDataType extends AbstractDsElement
24+
{
25+
use ExtendableElementTrait;
26+
27+
/** @var \SimpleSAML\XML\XsNamespace */
28+
public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
29+
30+
31+
/**
32+
* Initialize a PGPData element.
33+
*
34+
* @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null $pgpKeyId
35+
* @param \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null $pgpKeyPacket
36+
* @param array<\SimpleSAML\XML\SerializableElementInterface> $children
37+
*/
38+
final public function __construct(
39+
protected ?PGPKeyID $pgpKeyId = null,
40+
protected ?PGPKeyPacket $pgpKeyPacket = null,
41+
array $children = [],
42+
) {
43+
if ($pgpKeyId === null && $pgpKeyPacket === null) {
44+
throw new SchemaViolationException("ds:PGPKeyID and ds:PGPKeyPacket can't both be null.");
45+
}
46+
47+
$this->setElements($children);
48+
}
49+
50+
51+
/**
52+
* Collect the value of the PGPKeyID-property
53+
*
54+
* @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyID|null
55+
*/
56+
public function getPGPKeyID(): ?PGPKeyID
57+
{
58+
return $this->pgpKeyId;
59+
}
60+
61+
62+
/**
63+
* Collect the value of the PGPKeyPacket-property
64+
*
65+
* @return \SimpleSAML\XMLSecurity\XML\ds\PGPKeyPacket|null
66+
*/
67+
public function getPGPKeyPacket(): ?PGPKeyPacket
68+
{
69+
return $this->pgpKeyPacket;
70+
}
71+
72+
73+
/**
74+
* Convert XML into a PGPData
75+
*
76+
* @param \DOMElement $xml The XML element we should load
77+
* @return static
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): static
83+
{
84+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
85+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
86+
87+
$pgpKeyId = PGPKeyID::getChildrenOfClass($xml);
88+
Assert::maxCount($pgpKeyId, 1, TooManyElementsException::class);
89+
90+
$pgpKeyPacket = PGPKeyPacket::getChildrenOfClass($xml);
91+
Assert::maxCount($pgpKeyPacket, 1, TooManyElementsException::class);
92+
93+
return new static(
94+
array_pop($pgpKeyId),
95+
array_pop($pgpKeyPacket),
96+
self::getChildElementsFromXML($xml),
97+
);
98+
}
99+
100+
101+
/**
102+
* Convert this PGPData to XML.
103+
*
104+
* @param \DOMElement|null $parent The element we should append this PGPData to.
105+
* @return \DOMElement
106+
*/
107+
public function toXML(?DOMElement $parent = null): DOMElement
108+
{
109+
$e = $this->instantiateParentElement($parent);
110+
111+
$this->getPGPKeyId()?->toXML($e);
112+
$this->getPGPKeyPacket()?->toXML($e);
113+
114+
foreach ($this->getElements() as $elt) {
115+
$elt->toXML($e);
116+
}
117+
118+
return $e;
119+
}
120+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\Chunk;
10+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11+
use SimpleSAML\XML\Exception\SchemaViolationException;
12+
use SimpleSAML\XML\Registry\ElementRegistry;
13+
use SimpleSAML\XML\SerializableElementInterface;
14+
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15+
16+
/**
17+
* Abstract class representing the SPKIDataType.
18+
*
19+
* @package simplesamlphp/xml-security
20+
*/
21+
abstract class AbstractSPKIDataType extends AbstractDsElement
22+
{
23+
/**
24+
* Initialize a SPKIData element.
25+
*
26+
* @param array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}} $tuples
27+
*/
28+
final public function __construct(
29+
protected array $tuples,
30+
) {
31+
Assert::allIsArray($tuples, SchemaViolationException::class);
32+
Assert::allCount($tuples, 2);
33+
34+
foreach ($tuples as $tuple) {
35+
Assert::isInstanceOf($tuple[0], SPKISexp::class, SchemaViolationException::class);
36+
Assert::nullOrIsInstanceOf($tuple[1], SerializableElementInterface::class, SchemaViolationException::class);
37+
}
38+
}
39+
40+
41+
/**
42+
* Collect the value of the SPKISexp-property
43+
*
44+
* @return array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}}
45+
*/
46+
public function getTuples(): array
47+
{
48+
return $this->tuples;
49+
}
50+
51+
52+
/**
53+
* Convert XML into a SPKIData
54+
*
55+
* @param \DOMElement $xml The XML element we should load
56+
* @return static
57+
*
58+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
59+
* If the qualified name of the supplied element is wrong
60+
*/
61+
public static function fromXML(DOMElement $xml): static
62+
{
63+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
64+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
65+
66+
$registry = ElementRegistry::getInstance();
67+
$tuples = [];
68+
$tuple = [null, null];
69+
foreach ($xml->childNodes as $node) {
70+
if ($node instanceof DOMElement) {
71+
if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') {
72+
if ($tuple[0] !== null) {
73+
$tuples[] = $tuple;
74+
}
75+
$tuple = [SPKISexp::fromXML($node), null];
76+
} elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) {
77+
$handler = $registry->getElementHandler($node->namespaceURI, $node->localName);
78+
$tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node);
79+
$tuples[] = $tuple;
80+
$tuple = [null, null];
81+
}
82+
}
83+
}
84+
85+
if ($tuple[0] !== null) {
86+
$tuples[] = $tuple;
87+
}
88+
89+
return new static($tuples);
90+
}
91+
92+
93+
/**
94+
* Convert this SPKIData to XML.
95+
*
96+
* @param \DOMElement|null $parent The element we should append this SPKIData to.
97+
* @return \DOMElement
98+
*/
99+
public function toXML(?DOMElement $parent = null): DOMElement
100+
{
101+
$e = $this->instantiateParentElement($parent);
102+
103+
foreach ($this->getTuples() as $tuple) {
104+
$tuple[0]->toXML($e);
105+
$tuple[1]?->toXML($e);
106+
}
107+
108+
return $e;
109+
}
110+
}

src/XML/ds/HMACOutputLength.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\ds;
6+
7+
use SimpleSAML\XML\IntegerElementTrait;
8+
9+
/**
10+
* Class representing a ds:HMACOutputLength element.
11+
*
12+
* @package simplesamlphp/xml-security
13+
*/
14+
final class HMACOutputLength extends AbstractDsElement
15+
{
16+
use IntegerElementTrait;
17+
18+
19+
/**
20+
* @param string $length
21+
*/
22+
public function __construct(string $length)
23+
{
24+
$this->setContent($length);
25+
}
26+
}

src/XML/ds/KeyInfo.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ public static function fromXML(DOMElement $xml): static
3737
$keyValue = KeyValue::getChildrenOfClass($xml);
3838
$retrievalMethod = RetrievalMethod::getChildrenOfClass($xml);
3939
$x509Data = X509Data::getChildrenOfClass($xml);
40-
//$pgpData = PGPData::getChildrenOfClass($xml);
41-
//$spkiData = SPKIData::getChildrenOfClass($xml);
42-
//$mgmtData = MgmtData::getChildrenOfClass($xml);
40+
$pgpData = PGPData::getChildrenOfClass($xml);
41+
$spkiData = SPKIData::getChildrenOfClass($xml);
42+
$mgmtData = MgmtData::getChildrenOfClass($xml);
4343
$other = self::getChildElementsFromXML($xml);
4444

4545
$info = array_merge(
4646
$keyName,
4747
$keyValue,
4848
$retrievalMethod,
4949
$x509Data,
50-
//$pgpdata,
51-
//$spkidata,
52-
//$mgmtdata,
50+
$pgpData,
51+
$spkiData,
52+
$mgmtData,
5353
$other,
5454
);
5555

src/XML/ds/MgmtData.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\ds;
6+
7+
use SimpleSAML\XML\StringElementTrait;
8+
9+
/**
10+
* Class representing a ds:MgmtData element.
11+
*
12+
* @package simplesamlphp/xml-security
13+
*/
14+
final class MgmtData extends AbstractDsElement
15+
{
16+
use StringElementTrait;
17+
18+
19+
/**
20+
* @param string $content
21+
*/
22+
public function __construct(string $content)
23+
{
24+
$this->setContent($content);
25+
}
26+
}

src/XML/ds/PGPData.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\ds;
6+
7+
/**
8+
* Class representing a ds:PGPData element.
9+
*
10+
* @package simplesaml/xml-security
11+
*/
12+
final class PGPData extends AbstractPGPDataType
13+
{
14+
}

src/XML/ds/PGPKeyID.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\XML\ds;
6+
7+
use SimpleSAML\XML\Base64ElementTrait;
8+
9+
/**
10+
* Class representing a ds:PGPKeyID element.
11+
*
12+
* @package simplesaml/xml-security
13+
*/
14+
final class PGPKeyID extends AbstractDsElement
15+
{
16+
use Base64ElementTrait;
17+
18+
19+
/**
20+
* @param string $content
21+
*/
22+
public function __construct(string $content)
23+
{
24+
$this->setContent($content);
25+
}
26+
}

0 commit comments

Comments
 (0)