-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathNameID.php
More file actions
215 lines (190 loc) · 7.34 KB
/
Copy pathNameID.php
File metadata and controls
215 lines (190 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
declare(strict_types=1);
namespace SimpleSAML\SAML2\XML\saml;
use SimpleSAML\SAML2\Assert\Assert;
use SimpleSAML\SAML2\Constants as C;
use SimpleSAML\SAML2\Exception\ArrayValidationException;
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
use SimpleSAML\SAML2\Type\SAMLStringValue;
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
use SimpleSAML\XML\SchemaValidatableElementInterface;
use SimpleSAML\XML\SchemaValidatableElementTrait;
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
use function array_change_key_case;
use function array_filter;
use function array_key_exists;
use function array_keys;
/**
* Class representing the saml:NameID element.
*
* @package simplesamlphp/saml2
*/
final class NameID extends NameIDType implements
EncryptableElementInterface,
SchemaValidatableElementInterface
{
use EncryptableElementTrait;
use SchemaValidatableElementTrait;
/**
* Initialize a saml:NameID
*
* @param \SimpleSAML\SAML2\Type\SAMLStringValue $value
* @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $NameQualifier
* @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPNameQualifier
* @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $Format
* @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPProvidedID
*/
public function __construct(
SAMLStringValue $value,
?SAMLStringValue $NameQualifier = null,
?SAMLStringValue $SPNameQualifier = null,
?SAMLAnyURIValue $Format = null,
?SAMLStringValue $SPProvidedID = null,
) {
if ($Format !== null) {
switch ($Format->getValue()) {
case C::NAMEID_EMAIL_ADDRESS:
Assert::email(
$value->getValue(),
"The content %s of the NameID was not in the format specified by the Format attribute",
);
break;
case C::NAMEID_ENTITY:
/* 8.3.6: the NameQualifier, SPNameQualifier, and SPProvidedID attributes MUST be omitted. */
Assert::null($NameQualifier, "Entity Identifier included a disallowed NameQualifier attribute.");
Assert::null(
$SPNameQualifier,
"Entity Identifier included a disallowed SPNameQualifier attribute.",
);
Assert::null($SPProvidedID, "Entity Identifier included a disallowed SPProvidedID attribute.");
break;
case C::NAMEID_PERSISTENT:
/* 8.3.7: Persistent name identifier values MUST NOT exceed a length of 256 characters. */
Assert::maxLength(
$value->getValue(),
256,
"Persistent name identifier values MUST NOT exceed a length of 256 characters.",
);
break;
case C::NAMEID_TRANSIENT:
/* 8.3.8: Transient name identifier values MUST NOT exceed a length of 256 characters. */
Assert::maxLength(
$value->getValue(),
256,
"Transient name identifier values MUST NOT exceed a length of 256 characters.",
);
break;
}
}
parent::__construct($value, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID);
}
/**
* @return \SimpleSAML\XMLSecurity\Backend\EncryptionBackend
*/
public function getEncryptionBackend(): ?EncryptionBackend
{
// return the encryption backend you want to use,
// or null if you are fine with the default
return null;
}
/**
* Create a class from an array
*
* @param array{
* 'value': string,
* 'NameQualifier'?: string,
* 'SPNameQualifier'?: string,
* 'Format'?: string,
* 'SPProvidedID'?: string,
* } $data
*/
public static function fromArray(array $data): static
{
$data = self::processArrayContents($data);
return new static(
SAMLStringValue::fromString($data['value']),
$data['NameQualifier'] ? SAMLStringValue::fromString($data['NameQualifier']) : null,
$data['SPNameQualifier'] ? SAMLStringValue::fromString($data['SPNameQualifier']) : null,
$data['Format'] ? SAMLAnyURIValue::fromString($data['Format']) : null,
$data['SPProvidedID'] ? SAMLStringValue::fromString($data['SPProvidedID']) : null,
);
}
/**
* Validates an array representation of this object and returns the same array with
* rationalized keys (casing) and parsed sub-elements.
*
* @param array{
* 'value': string,
* 'NameQualifier'?: string,
* 'SPNameQualifier'?: string,
* 'Format'?: string,
* 'SPProvidedID'?: string,
* } $data
* @return array{
* 'value': string,
* 'NameQualifier'?: string,
* 'SPNameQualifier'?: string,
* 'Format'?: string,
* 'SPProvidedID'?: string,
* }
*/
private static function processArrayContents(array $data): array
{
$data = array_change_key_case($data, CASE_LOWER);
// Make sure the array keys are known for this kind of object
Assert::allOneOf(
array_keys($data),
[
'value',
'format',
'namequalifier',
'spnamequalifier',
'spprovidedid',
],
ArrayValidationException::class,
);
Assert::keyExists($data, 'value', ArrayValidationException::class);
Assert::string($data['value'], ArrayValidationException::class);
$retval = ['value' => $data['value']];
if (array_key_exists('format', $data)) {
Assert::string($data['format'], ArrayValidationException::class);
$retval['Format'] = $data['format'];
}
if (array_key_exists('namequalifier', $data)) {
Assert::string($data['namequalifier'], ArrayValidationException::class);
$retval['NameQualifier'] = $data['namequalifier'];
}
if (array_key_exists('spnamequalifier', $data)) {
Assert::string($data['spnamequalifier'], ArrayValidationException::class);
$retval['SPNameQualifier'] = $data['spnamequalifier'];
}
if (array_key_exists('spprovidedid', $data)) {
Assert::string($data['spprovidedid'], ArrayValidationException::class);
$retval['SPProvidedID'] = $data['spprovidedid'];
}
return $retval;
}
/**
* Create an array from this class
*
* @return array{
* 'value': string,
* 'NameQualifier'?: string,
* 'SPNameQualifier'?: string,
* 'Format'?: string,
* 'SPProvidedID'?: string,
* }
*/
public function toArray(): array
{
$data = [
'value' => $this->getContent()->getValue(),
'Format' => $this->getFormat()?->getValue(),
'NameQualifier' => $this->getNameQualifier()?->getValue(),
'SPNameQualifier' => $this->getSPNameQualifier()?->getValue(),
'SPProvidedID' => $this->getSPProvidedID()?->getValue(),
];
return array_filter($data);
}
}