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