Skip to content

Commit 1b604d9

Browse files
committed
Add testable CustomSignable and CustomSigned
1 parent 2fb8466 commit 1b604d9

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

bin/generate_CustomSignable.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
require_once('../vendor/autoload.php');
4+
5+
use SimpleSAML\XML\Chunk;
6+
use SimpleSAML\XML\DOMDocumentFactory;
7+
use SimpleSAML\XMLSecurity\Test\XML\CustomSignable;
8+
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
9+
use SimpleSAML\XMLSecurity\XMLSecurityKey;
10+
11+
$chunk = new Chunk(DOMDocumentFactory::fromString('<some>Chunk</some>')->documentElement);
12+
$signable = new CustomSignable($chunk);
13+
14+
$privateKey = PEMCertificatesMock::getPrivateKey(XMLSecurityKey::RSA_SHA256, PEMCertificatesMock::SELFSIGNED_PRIVATE_KEY);
15+
$x = $signable->sign($privateKey);
16+
echo $x->ownerDocument->saveXML();

tests/XML/CustomSignable.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XMLSecurity\Test\XML;
6+
7+
use DOMElement;
8+
use SimpleSAML\XML\AbstractXMLElement;
9+
use SimpleSAML\XML\Chunk;
10+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11+
use SimpleSAML\XML\Exception\TooManyElementsException;
12+
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
13+
use SimpleSAML\XMLSecurity\XML\SignableElementTrait;
14+
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
15+
use SimpleSAML\XMLSecurity\XMLSecurityKey;
16+
17+
/**
18+
* @package simplesamlphp\saml2
19+
*/
20+
final class CustomSignable extends AbstractXMLElement implements SignableElementInterface
21+
{
22+
use SignableElementTrait;
23+
24+
/** @var string */
25+
public const NS = 'urn:simplesamlphp:test';
26+
27+
/** @var string */
28+
public const NS_PREFIX = 'ssp';
29+
30+
/** @var \SimpleSAML\XML\Chunk $element */
31+
protected $element;
32+
33+
/**
34+
* Constructor
35+
*
36+
* @param \SimpleSAML\XML\Chunk $elt
37+
*/
38+
public function __construct(Chunk $elt) {
39+
$this->setElement($elt);
40+
}
41+
42+
43+
/**
44+
* Get the namespace for the element.
45+
*
46+
* @return string
47+
*/
48+
public static function getNamespaceURI(): string
49+
{
50+
return static::NS;
51+
}
52+
53+
54+
/**
55+
* Get the namespace-prefix for the element.
56+
*
57+
* @return string
58+
*/
59+
public static function getNamespacePrefix(): string
60+
{
61+
return static::NS_PREFIX;
62+
}
63+
64+
65+
/**
66+
* Collect the value of the $element property
67+
*
68+
* @return \SimpleSAML\XML\XML\Chunk
69+
*/
70+
public function getElement(): Chunk
71+
{
72+
return $this->element;
73+
}
74+
75+
76+
/**
77+
* Set the value of the elment-property
78+
*
79+
* @param \SimpleSAML\XML\Chunk $elt
80+
*/
81+
private function setElement(Chunk $elt): void
82+
{
83+
$this->element = $elt;
84+
}
85+
86+
87+
/**
88+
* Sign the 'Element' and return a 'SignedElement'
89+
*
90+
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signingKey The private key we should use to sign the message
91+
* @param string[] $certificates The certificates should be strings with the PEM encoded data
92+
* @return \SimpleSAML\XMLSecurity\XML\SignedElementInterface
93+
*/
94+
public function sign(XMLSecurityKey $signingKey, array $certificates = []): SignedElementInterface
95+
{
96+
return CustomSigned::fromXML($this->toSignedXML($signingKey, $certificates));
97+
}
98+
99+
100+
/**
101+
* Convert XML into a CustomSignable
102+
*
103+
* @param \DOMElement $xml The XML element we should load
104+
* @return \SimpleSAML\XMLSecurity\Test\XML\CustomSignable
105+
*
106+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
107+
*/
108+
public static function fromXML(DOMElement $xml): object
109+
{
110+
Assert::same($xml->localName, 'CustomSignable', InvalidDOMElementException::class);
111+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
112+
113+
Assert::count($xml->childNodes, 1, TooManyElementsException);
114+
$element = new Chunk($xml->childNodes[0]);
115+
116+
return new self($element);
117+
}
118+
119+
120+
/**
121+
* Convert this CustomSignable to XML.
122+
*
123+
* @param \DOMElement|null $element The element we are converting to XML.
124+
* @return \DOMElement The XML element after adding the data corresponding to this CustomSignable.
125+
*/
126+
public function toXML(DOMElement $parent = null): DOMElement
127+
{
128+
/** @psalm-var \DOMDocument $e->ownerDocument */
129+
$e = $this->instantiateParentElement($parent);
130+
131+
$e->appendChild($e->ownerDocument->importNode($this->element->getXML(), true));
132+
return $e;
133+
}
134+
}

tests/XML/CustomSigned.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\Test\XML;
6+
7+
use DOMElement;
8+
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\TooManyElementsException;
10+
use SimpleSAML\XML\Utils as XMLUtils;
11+
use SimpleSAML\XMLSecurity\XML\ds\Signature;
12+
use SimpleSAML\XMLSecurity\XML\AbstractSignedXMLElement;
13+
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
14+
use SimpleSAML\XMLSecurity\XML\SignedElementTrait;
15+
16+
/**
17+
* @package simplesamlphp\saml2
18+
*/
19+
final class CustomSigned extends AbstractSignedXMLElement
20+
{
21+
use SignedElementTrait;
22+
23+
24+
/**
25+
* Create a class from XML
26+
*
27+
* @param \DOMElement $xml
28+
* @return self
29+
*/
30+
public static function fromXML(DOMElement $xml): object
31+
{
32+
// Empty array
33+
$s = XMLUtils::xpQuery($xml, './ssp:CustomSignable/ds:Signature');
34+
var_dump($s);
35+
36+
// Also empty array
37+
$signature = Signature::getChildrenOfClass($xml);
38+
Assert::count($signature, 1, TooManyElementsException::class);
39+
40+
$element = CustomSignable::getChildrenOfClass($xml);
41+
Assert::count($element, 1, TooManyElementsException::class);
42+
43+
return new self(
44+
$xml,
45+
array_pop($element),
46+
array_pop($signature)
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)