1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\XML\AbstractXMLElement; |
10
|
|
|
use SimpleSAML\XML\Exception\MissingElementException; |
11
|
|
|
use SimpleSAML\XML\Exception\TooManyElementsException; |
12
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Signature; |
13
|
|
|
|
14
|
|
|
use function array_pop; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Abstract class to be implemented by all signed classes |
18
|
|
|
* |
19
|
|
|
* @package simplesamlphp/xml-security |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractSignedXMLElement extends AbstractXMLElement implements SignedElementInterface |
22
|
|
|
{ |
23
|
|
|
use SignedElementTrait; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The signed DOM structure. |
27
|
|
|
* |
28
|
|
|
* @var \DOMElement |
29
|
|
|
*/ |
30
|
|
|
protected DOMElement $structure; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The unsigned elelement. |
34
|
|
|
* |
35
|
|
|
* @var \SimpleSAML\XMLSecurity\XML\SignableElementInterface |
36
|
|
|
*/ |
37
|
|
|
protected SignableElementInterface $element; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create/parse an alg:SigningMethod element. |
42
|
|
|
* |
43
|
|
|
* @param \DOMElement $xml |
44
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\ds\Signature $signature |
45
|
|
|
*/ |
46
|
|
|
public function __construct(DOMElement $xml, Signature $signature) |
47
|
|
|
{ |
48
|
|
|
$this->setStructure($xml); |
49
|
|
|
$this->setSignature($signature); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Output the class as an XML-formatted string |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function __toString(): string |
59
|
|
|
{ |
60
|
|
|
return $this->structure->ownerDocument->saveXML(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the value of the structure-property |
66
|
|
|
* |
67
|
|
|
* @param \DOMElement $structure |
68
|
|
|
*/ |
69
|
|
|
private function setStructure(DOMElement $structure): void |
70
|
|
|
{ |
71
|
|
|
$this->structure = $structure; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create XML from this class |
77
|
|
|
* |
78
|
|
|
* @param \DOMElement|null $parent |
79
|
|
|
* @return \DOMElement |
80
|
|
|
*/ |
81
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
82
|
|
|
{ |
83
|
|
|
return $this->structure; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a class from XML |
89
|
|
|
* |
90
|
|
|
* @param \DOMElement $xml |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
|
|
public static function fromXML(DOMElement $xml): object |
94
|
|
|
{ |
95
|
|
|
$original = $xml->ownerDocument->cloneNode(true); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$signature = Signature::getChildrenOfClass($xml); |
98
|
|
|
Assert::minCount($signature, 1, MissingElementException::class); |
99
|
|
|
Assert::maxCount($signature, 1, TooManyElementsException::class); |
100
|
|
|
|
101
|
|
|
return new static($original->documentElement, array_pop($signature)); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|