Skip to content

Commit bf59081

Browse files
committed
Further finalize abstract
1 parent 1a051f6 commit bf59081

File tree

5 files changed

+96
-22
lines changed

5 files changed

+96
-22
lines changed

src/XML/AbstractSignedXMLElement.php

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace SimpleSAML\XML;
5+
namespace SimpleSAML\XMLSecurity\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;
129

1310
/**
1411
* Abstract class to be implemented by all signed classes
@@ -19,12 +16,6 @@ abstract class AbstractSignedXMLElement implements SignedElementInterface
1916
{
2017
use SignedElementTrait;
2118

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

src/XML/SignableElementInterface.php

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

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

5-
use SimpleSAML\XMLSecurity\SignedElementInterface;
67
use SimpleSAML\XMLSecurity\XMLSecurityKey;
78

89
/**
@@ -17,7 +18,7 @@ interface SignableElementInterface
1718
*
1819
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signingKey The private key we should use to sign the message
1920
* @param string[] $certificates The certificates should be strings with the PEM encoded data
20-
* @return \SimpleSAML\XMLSecurity\SignedElementInterface
21+
* @return \SimpleSAML\XMLSecurity\XML\SignedElementInterface
2122
*/
2223
public function sign(XMLSecurityKey $signingKey, array $certificates): SignedElementInterface;
2324
}

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

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

57
use SimpleSAML\XMLSecurity\XMLSecurityKey;
68

src/XML/SignedElementTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @package simplesamlphp/xml-security
1616
*/
17-
trait SignableElementTrait
17+
trait SignedElementTrait
1818
{
1919
/**
2020
* The signature of this element.

0 commit comments

Comments
 (0)