1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SAML2\XML\ds; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SAML2\XML\Chunk; |
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class representing a ds:KeyInfo element. |
13
|
|
|
* |
14
|
|
|
* @package SimpleSAMLphp |
15
|
|
|
*/ |
16
|
|
|
final class KeyInfo extends AbstractDsElement |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The Id attribute on this element. |
20
|
|
|
* |
21
|
|
|
* @var string|null |
22
|
|
|
*/ |
23
|
|
|
protected $Id = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The various key information elements. |
27
|
|
|
* |
28
|
|
|
* Array with various elements describing this key. |
29
|
|
|
* Unknown elements will be represented by \SAML2\XML\Chunk. |
30
|
|
|
* |
31
|
|
|
* @var (\SAML2\XML\Chunk|\SAML2\XML\ds\KeyName|\SAML2\XML\ds\X509Data)[] |
32
|
|
|
*/ |
33
|
|
|
protected $info = []; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialize a KeyInfo element. |
38
|
|
|
* |
39
|
|
|
* @param (\SAML2\XML\Chunk|\SAML2\XML\ds\KeyName|\SAML2\XML\ds\X509Data)[] $info |
40
|
|
|
* @param string|null $Id |
41
|
|
|
*/ |
42
|
|
|
public function __construct(array $info, $Id = null) |
43
|
|
|
{ |
44
|
|
|
$this->setInfo($info); |
45
|
|
|
$this->setId($Id); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Collect the value of the Id-property |
51
|
|
|
* |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
|
|
public function getId(): ?string |
55
|
|
|
{ |
56
|
|
|
return $this->Id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set the value of the Id-property |
62
|
|
|
* |
63
|
|
|
* @param string|null $id |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
private function setId(string $id = null): void |
67
|
|
|
{ |
68
|
|
|
$this->Id = $id; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Collect the value of the info-property |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getInfo(): array |
78
|
|
|
{ |
79
|
|
|
return $this->info; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the value of the info-property |
85
|
|
|
* |
86
|
|
|
* @param array $info |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
private function setInfo(array $info): void |
90
|
|
|
{ |
91
|
|
|
Assert::allIsInstanceOfAny( |
92
|
|
|
$info, |
93
|
|
|
[Chunk::class, KeyName::class, X509Data::class], |
94
|
|
|
'KeyInfo can only contain instances of KeyName, X509Data or Chunk.' |
95
|
|
|
); |
96
|
|
|
$this->info = $info; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add the value to the info-property |
102
|
|
|
* |
103
|
|
|
* @param \SAML2\XML\Chunk|\SAML2\XML\ds\KeyName|\SAML2\XML\ds\X509Data $info |
104
|
|
|
* @return void |
105
|
|
|
* |
106
|
|
|
* @throws \InvalidArgumentException if assertions are false |
107
|
|
|
*/ |
108
|
|
|
public function addInfo($info): void |
109
|
|
|
{ |
110
|
|
|
Assert::isInstanceOfAny( |
111
|
|
|
$info, |
112
|
|
|
[Chunk::class, KeyName::class, X509Data::class], |
113
|
|
|
'KeyInfo can only contain instances of KeyName, X509Data or Chunk.' |
114
|
|
|
); |
115
|
|
|
$this->info[] = $info; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Convert XML into a KeyInfo |
121
|
|
|
* |
122
|
|
|
* @param \DOMElement $xml The XML element we should load |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
|
|
public static function fromXML(DOMElement $xml): object |
126
|
|
|
{ |
127
|
|
|
Assert::same($xml->localName, 'KeyInfo'); |
128
|
|
|
Assert::same($xml->namespaceURI, KeyInfo::NS); |
129
|
|
|
|
130
|
|
|
$Id = $xml->hasAttribute('Id') ? $xml->getAttribute('Id') : null; |
131
|
|
|
$info = []; |
132
|
|
|
|
133
|
|
|
foreach ($xml->childNodes as $n) { |
134
|
|
|
if (!($n instanceof \DOMElement)) { |
135
|
|
|
continue; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($n->namespaceURI !== self::NS) { |
139
|
|
|
$info[] = new Chunk($n); |
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
switch ($n->localName) { |
144
|
|
|
case 'KeyName': |
145
|
|
|
$info[] = KeyName::fromXML($n); |
146
|
|
|
break; |
147
|
|
|
case 'X509Data': |
148
|
|
|
$info[] = X509Data::fromXML($n); |
149
|
|
|
break; |
150
|
|
|
default: |
151
|
|
|
$info[] = new Chunk($n); |
152
|
|
|
break; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return new self($info, $Id); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Convert this KeyInfo to XML. |
161
|
|
|
* |
162
|
|
|
* @param \DOMElement|null $parent The element we should append this KeyInfo to. |
163
|
|
|
* @return \DOMElement |
164
|
|
|
*/ |
165
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
166
|
|
|
{ |
167
|
|
|
$e = $this->instantiateParentElement($parent); |
168
|
|
|
|
169
|
|
|
if ($this->Id !== null) { |
170
|
|
|
$e->setAttribute('Id', $this->Id); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
foreach ($this->info as $n) { |
174
|
|
|
$n->toXML($e); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $e; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|