Skip to content

Commit 07f62c1

Browse files
committed
Further finalize abstract
1 parent aa0d97a commit 07f62c1

4 files changed

Lines changed: 96 additions & 19 deletions

File tree

src/XML/AbstractSignedXMLElement.php

Lines changed: 85 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
namespace SimpleSAML\XML;
66

77
use DOMElement;
8-
use SimpleSAML\XML\DOMDocumentFactory;
9-
use SimpleSAML\XML\Exception\MissingAttributeException;
10-
use Serializable;
11-
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\XMLSecurity\XML\ds\Signature;
9+
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
10+
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
1211

1312
/**
1413
* Abstract class to be implemented by all signed classes
@@ -19,12 +18,6 @@ abstract class AbstractSignedXMLElement implements SignedElementInterface
1918
{
2019
use SignedElementTrait;
2120

22-
/**
23-
* Create a document structure for this element
24-
*
25-
* @param \DOMElement|null $parent The element we should append to.
26-
* @return \DOMElement
27-
*/
2821
/**
2922
* The signed DOM structure.
3023
*
@@ -35,7 +28,87 @@ abstract class AbstractSignedXMLElement implements SignedElementInterface
3528
/**
3629
* The unsigned elelement.
3730
*
38-
* @var \SimpleSAML\XML\AbstractXMLElement
31+
* @var \SimpleSAML\XMLSecurity\SignableElementInterface
32+
*/
33+
protected SignableElementInterface $element;
34+
35+
36+
/**
37+
* Create/parse an alg:SigningMethod element.
38+
*
39+
* @param \DOMElement $xml
40+
* @param \SimpleSAML\XMLSecurity\XML\SignedElementInterface $elt
41+
* @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature
42+
*/
43+
private function __construct(DOMElement $xml, SignedElementInterface $elt, Signature $signature)
44+
{
45+
$this->setStructure($xml);
46+
$this->setElement($elt);
47+
$this->setSignature($signature);
48+
}
49+
50+
51+
/**
52+
* Collect the value of the unsigned element
53+
*
54+
* @return \SimpleSAML\XMLSecurity\XML\SignableElementInterface
55+
*/
56+
public function getElement(): SignableElementInterface
57+
{
58+
return $this->element;
59+
}
60+
61+
62+
/**
63+
* Set the value of the elment-property
64+
*
65+
* @param \SimpleSAML\XMLSecurity\XML\SignableElementInterface $elt
66+
*/
67+
private function setElement(SignableElementInterface $elt): void
68+
{
69+
$this->element = $elt;
70+
}
71+
72+
73+
/**
74+
* Collect the value of the structure-property
75+
*
76+
* @return \DOMElement
77+
*/
78+
public function getStructure(): DOMElement
79+
{
80+
return $this->structure;
81+
}
82+
83+
84+
/**
85+
* Set the value of the structure-property
86+
*
87+
* @param \DOMElement $structure
88+
*/
89+
private function setStructure(DOMElement $structure): void
90+
{
91+
$this->structure = $structure;
92+
}
93+
94+
95+
/**
96+
* Create XML from this class
97+
*
98+
* @param \DOMElement|null $parent
99+
* @return \DOMElement
100+
*/
101+
public function toXML(DOMElement $parent = null): DOMElement
102+
{
103+
return $this->structure;
104+
}
105+
106+
107+
/**
108+
* Create a class from XML
109+
*
110+
* @param \DOMElement $xml
111+
* @return self
39112
*/
40-
protected AbstractXMLElement $elt;
113+
abstract public static function fromXML(DOMElement $xml): object;
41114
}

src/XML/SignableElementInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
namespace SimpleSAML\SAML2\XML;
3+
declare(strict_types=1);
44

5-
use SimpleSAML\XMLSecurity\SignedElementInterface;
5+
namespace SimpleSAML\XMLSecurity\XML;
6+
7+
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
68
use SimpleSAML\XMLSecurity\XMLSecurityKey;
79

810
/**
@@ -17,7 +19,7 @@ interface SignableElementInterface
1719
*
1820
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signingKey The private key we should use to sign the message
1921
* @param string[] $certificates The certificates should be strings with the PEM encoded data
20-
* @return \SimpleSAML\XMLSecurity\SignedElementInterface
22+
* @return \SimpleSAML\XMLSecurity\XML\SignedElementInterface
2123
*/
2224
public function sign(XMLSecurityKey $signingKey, array $certificates): SignedElementInterface;
2325
}

src/XML/SignableElementTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ trait SignableElementTrait
2222
/**
2323
* Sign the given XML element.
2424
*
25-
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signKey The private key used for signing.
25+
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signingKey The private key used for signing.
2626
* @param array $certificates Any public key to be added to the ds:Signature
2727
* @param \DOMNode|null $insertBefore A specific node in the DOM structure where the ds:Signature should be put in front.
2828
* @return \DOMElement The signed element.
2929
* @throws \Exception If an error occurs while trying to sign.
3030
*/
31-
private function toSignedXML(XMLSecurityKey $signKey, array $certificates, DOMNode $insertBefore = null): DOMElement
31+
private function toSignedXML(XMLSecurityKey $signingKey, array $certificates, DOMNode $insertBefore = null): DOMElement
3232
{
3333
$root = $this->toXML();
3434

3535
if ($insertBefore !== null) {
36-
XMLSecurityUtils::insertSignature($this->signingKey, $this->certificates, $root, $insertBefore);
36+
XMLSecurityUtils::insertSignature($signingKey, $certificates, $root, $insertBefore);
3737
$doc = clone $root->ownerDocument;
3838
} else {
39-
$signature = new Signature($this->signingKey->getAlgorithm(), $this->certificates, $this->signingKey);
39+
$signature = new Signature($signingKey->getAlgorithm(), $certificates, $signingKey);
4040
$signature->toXML($root);
4141
}
4242

src/XML/SignedElementInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace SimpleSAML\SAML2\XML;
46

57
use SimpleSAML\XMLSecurity\XMLSecurityKey;

0 commit comments

Comments
 (0)