1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\samlp; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use SimpleSAML\Assert\Assert; |
9
|
|
|
use SimpleSAML\SAML2\Exception\ArrayValidationException; |
10
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
11
|
|
|
use SimpleSAML\XML\Exception\SchemaViolationException; |
12
|
|
|
|
13
|
|
|
use function array_change_key_case; |
14
|
|
|
use function array_filter; |
15
|
|
|
use function array_key_exists; |
16
|
|
|
use function array_keys; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class for handling SAML2 IDPEntry. |
20
|
|
|
* |
21
|
|
|
* @package simplesamlphp/saml2 |
22
|
|
|
*/ |
23
|
|
|
final class IDPEntry extends AbstractSamlpElement |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Initialize an IDPEntry element. |
27
|
|
|
* |
28
|
|
|
* @param string $providerId |
29
|
|
|
* @param string|null $name |
30
|
|
|
* @param string|null $loc |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
protected string $providerId, |
34
|
|
|
protected ?string $name = null, |
35
|
|
|
protected ?string $loc = null, |
36
|
|
|
) { |
37
|
|
|
Assert::validURI($providerId, SchemaViolationException::class); // Covers the empty string |
38
|
|
|
Assert::nullOrNotWhitespaceOnly($name); |
39
|
|
|
Assert::nullOrValidURI($loc, SchemaViolationException::class); // Covers the empty string |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getProviderId(): string |
47
|
|
|
{ |
48
|
|
|
return $this->providerId; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string|null |
54
|
|
|
*/ |
55
|
|
|
public function getName(): ?string |
56
|
|
|
{ |
57
|
|
|
return $this->name; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string|null |
63
|
|
|
*/ |
64
|
|
|
public function getLoc(): ?string |
65
|
|
|
{ |
66
|
|
|
return $this->loc; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Convert XML into a IDPEntry-element |
72
|
|
|
* |
73
|
|
|
* @param \DOMElement $xml The XML element we should load |
74
|
|
|
* @return static |
75
|
|
|
* |
76
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
77
|
|
|
* if the qualified name of the supplied element is wrong |
78
|
|
|
* @throws \SimpleSAML\XML\Exception\MissingAttributeException |
79
|
|
|
* if the supplied element is missing one of the mandatory attributes |
80
|
|
|
*/ |
81
|
|
|
public static function fromXML(DOMElement $xml): static |
82
|
|
|
{ |
83
|
|
|
Assert::same($xml->localName, 'IDPEntry', InvalidDOMElementException::class); |
84
|
|
|
Assert::same($xml->namespaceURI, IDPEntry::NS, InvalidDOMElementException::class); |
85
|
|
|
|
86
|
|
|
$providerId = self::getAttribute($xml, 'ProviderID'); |
87
|
|
|
$name = self::getOptionalAttribute($xml, 'Name', null); |
88
|
|
|
$loc = self::getOptionalAttribute($xml, 'Loc', null); |
89
|
|
|
|
90
|
|
|
return new static($providerId, $name, $loc); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Convert this IDPEntry to XML. |
96
|
|
|
* |
97
|
|
|
* @param \DOMElement|null $parent The element we should append this IDPEntry to. |
98
|
|
|
* @return \DOMElement |
99
|
|
|
*/ |
100
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
101
|
|
|
{ |
102
|
|
|
$e = $this->instantiateParentElement($parent); |
103
|
|
|
$e->setAttribute('ProviderID', $this->getProviderId()); |
104
|
|
|
|
105
|
|
|
if ($this->getName() !== null) { |
106
|
|
|
$e->setAttribute('Name', $this->getName()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($this->getLoc() !== null) { |
110
|
|
|
$e->setAttribute('Loc', $this->getLoc()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $e; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Create a class from an array |
119
|
|
|
* |
120
|
|
|
* @param array $data |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
|
|
public static function fromArray(array $data): static |
124
|
|
|
{ |
125
|
|
|
$data = self::processArrayContents($data); |
126
|
|
|
|
127
|
|
|
return new static( |
128
|
|
|
$data['ProviderID'], |
129
|
|
|
$data['Name'] ?? null, |
130
|
|
|
$data['Loc'] ?? null, |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Validates an array representation of this object and returns the same array with |
137
|
|
|
* rationalized keys (casing) and parsed sub-elements. |
138
|
|
|
* |
139
|
|
|
* @param array $data |
140
|
|
|
* @return array $data |
141
|
|
|
*/ |
142
|
|
|
private static function processArrayContents(array $data): array |
143
|
|
|
{ |
144
|
|
|
$data = array_change_key_case($data, CASE_LOWER); |
145
|
|
|
|
146
|
|
|
// Make sure the array keys are known for this kind of object |
147
|
|
|
Assert::allOneOf( |
148
|
|
|
array_keys($data), |
149
|
|
|
[ |
150
|
|
|
'providerid', |
151
|
|
|
'name', |
152
|
|
|
'loc', |
153
|
|
|
], |
154
|
|
|
ArrayValidationException::class, |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
Assert::keyExists($data, 'providerid', ArrayValidationException::class); |
158
|
|
|
Assert::string($data['providerid'], ArrayValidationException::class); |
159
|
|
|
|
160
|
|
|
$retval = ['ProviderID' => $data['providerid']]; |
161
|
|
|
|
162
|
|
|
if (array_key_exists('name', $data)) { |
163
|
|
|
Assert::string($data['name'], ArrayValidationException::class); |
164
|
|
|
$retval['Name'] = $data['name']; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (array_key_exists('loc', $data)) { |
168
|
|
|
Assert::string($data['loc'], ArrayValidationException::class); |
169
|
|
|
$retval['Loc'] = $data['loc']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $retval; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Create an array from this class |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public function toArray(): array |
182
|
|
|
{ |
183
|
|
|
$data = [ |
184
|
|
|
'ProviderID' => $this->getProviderID(), |
185
|
|
|
'Name' => $this->getName(), |
186
|
|
|
'Loc' => $this->getLoc(), |
187
|
|
|
]; |
188
|
|
|
|
189
|
|
|
return array_filter($data); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|