-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathBinding.php
More file actions
255 lines (220 loc) · 7.92 KB
/
Binding.php
File metadata and controls
255 lines (220 loc) · 7.92 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
declare(strict_types=1);
namespace SimpleSAML\SAML2;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SimpleSAML\SAML2\Binding\HTTPArtifact;
use SimpleSAML\SAML2\Binding\HTTPPost;
use SimpleSAML\SAML2\Binding\HTTPRedirect;
use SimpleSAML\SAML2\Binding\SOAP;
use SimpleSAML\SAML2\Constants as C;
use SimpleSAML\SAML2\Exception\Protocol\UnsupportedBindingException;
use SimpleSAML\SAML2\XML\samlp\AbstractMessage;
use function array_key_exists;
use function array_keys;
use function array_map;
use function explode;
use function implode;
use function var_export;
/**
* Base class for SAML 2 bindings.
*
* @package simplesamlphp/saml2
*/
abstract class Binding
{
/**
* The schema to be used for schema validation
*
* @var string
*/
protected static string $schemaFile = 'resources/schemas/saml-schema-protocol-2.0.xsd';
/**
* Whether or not to perform schema validation
*
* @var bool
*/
protected bool $schemaValidation = true;
/**
* The RelayState associated with the message.
*
* @var string|null
*/
protected ?string $relayState = null;
/**
* The destination of messages.
*
* This can be null, in which case the destination in the message is used.
* @var string|null
*/
protected ?string $destination = null;
/**
* Retrieve a binding with the given URN.
*
* Will throw an exception if it is unable to locate the binding.
*
* @param string $urn The URN of the binding.
* @throws \SimpleSAML\SAML2\Exception\Protocol\UnsupportedBindingException
* @return \SimpleSAML\SAML2\Binding The binding.
*/
public static function getBinding(string $urn): Binding
{
switch ($urn) {
case C::BINDING_HTTP_POST:
case C::BINDING_HOK_SSO:
return new HTTPPost();
case C::BINDING_HTTP_REDIRECT:
return new HTTPRedirect();
case C::BINDING_HTTP_ARTIFACT:
return new HTTPArtifact();
// ECP ACS is defined with the PAOS binding, but as the IdP, we
// talk to the ECP using SOAP -- if support for ECP as an SP is
// implemented, this logic may need to change
case C::BINDING_PAOS:
return new SOAP();
default:
throw new UnsupportedBindingException('Unsupported binding: ' . var_export($urn, true));
}
}
/**
* Guess the current binding.
*
* This function guesses the current binding and creates an instance
* of \SimpleSAML\SAML2\Binding matching that binding.
*
* An exception will be thrown if it is unable to guess the binding.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @throws \SimpleSAML\SAML2\Exception\Protocol\UnsupportedBindingException
* @return \SimpleSAML\SAML2\Binding The binding.
*/
public static function getCurrentBinding(ServerRequestInterface $request): Binding
{
$method = $request->getMethod();
switch ($method) {
case 'GET':
$query = $request->getQueryParams();
if (array_key_exists('SAMLRequest', $query) || array_key_exists('SAMLResponse', $query)) {
return new Binding\HTTPRedirect();
} elseif (array_key_exists('SAMLart', $query)) {
return new Binding\HTTPArtifact();
}
break;
case 'POST':
$contentType = null;
if ($request->hasHeader('Content-Type')) {
$contentType = $request->getHeader('Content-Type')[0];
$contentType = explode(';', $contentType);
$contentType = $contentType[0]; /* Remove charset. */
}
$query = $request->getParsedBody();
if (array_key_exists('SAMLRequest', $query) || array_key_exists('SAMLResponse', $query)) {
return new Binding\HTTPPost();
} elseif (array_key_exists('SAMLart', $query)) {
return new Binding\HTTPArtifact();
} else {
/**
* The registration information for text/xml is in all respects the same
* as that given for application/xml (RFC 7303 - Section 9.1)
*/
if (
($contentType === 'text/xml' || $contentType === 'application/xml')
// See paragraph 3.2.3 of Binding for SAML2 (OASIS)
|| ($request->hasHeader('SOAPAction')
&& $request->getHeader('SOAPAction')[0] === 'http://www.oasis-open.org/committees/security')
) {
return new Binding\SOAP();
}
}
break;
}
$logger = Utils::getContainer()->getLogger();
$logger->warning('Unable to find the SAML 2 binding used for this request.');
$logger->warning('Request method: ' . var_export($method, true));
if (!empty($query)) {
$logger->warning(
$method . " parameters: '" . implode("', '", array_map('addslashes', array_keys($query))) . "'",
);
}
if ($request->hasHeader('Content-Type')) {
$logger->warning('Content-Type: ' . var_export($request->getHeader('Content-Type')[0], true));
}
throw new UnsupportedBindingException('Unable to find the SAML 2 binding used for this request.');
}
/**
* Retrieve the destination of a message.
*
* @return string|null $destination The destination the message will be delivered to.
*/
public function getDestination(): ?string
{
return $this->destination;
}
/**
* Override the destination of a message.
*
* Set to null to use the destination set in the message.
*
* @param string|null $destination The destination the message should be delivered to.
*/
public function setDestination(?string $destination = null): void
{
$this->destination = $destination;
}
/**
* Set the RelayState associated with the message.
*
* @param string|null $relayState The RelayState.
*/
public function setRelayState(?string $relayState = null): void
{
$this->relayState = $relayState;
}
/**
* Get the RelayState associated with the message.
*
* @return string|null The RelayState.
*/
public function getRelayState(): ?string
{
return $this->relayState;
}
/**
* Set the schema validation for the message.
*
* @param bool $schemaValidation
*/
public function setSchemaValidation(bool $schemaValidation): void
{
$this->schemaValidation = $schemaValidation;
}
/**
* Get the schema validation setting.
*
* @return bool
*/
public function getSchemaValidation(): bool
{
return $this->schemaValidation;
}
/**
* Send a SAML 2 message.
*
* This function will send a message using the specified binding.
* The message will be delivered to the destination set in the message.
*
* @param \SimpleSAML\SAML2\XML\samlp\AbstractMessage $message The message which should be sent.
* @return \Psr\Http\Message\ResponseInterface
*/
abstract public function send(AbstractMessage $message): ResponseInterface;
/**
* Receive a SAML 2 message.
*
* This function will extract the message from the current request.
* An exception will be thrown if we are unable to process the message.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \SimpleSAML\SAML2\XML\samlp\AbstractMessage The received message.
*/
abstract public function receive(ServerRequestInterface $request): AbstractMessage;
}