1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupGateway\GatewayBundle\Controller; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use SAML2\Constants; |
23
|
|
|
use SAML2\Response as SAMLResponse; |
24
|
|
|
use Surfnet\SamlBundle\SAML2\AuthnRequest; |
25
|
|
|
use Surfnet\SamlBundle\SAML2\AuthnRequestFactory; |
26
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException; |
27
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\AssertionAdapter; |
28
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\Exception\UnknownInResponseToException; |
29
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
31
|
|
|
use Symfony\Component\HttpFoundation\Response; |
32
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Entry point for the Stepup login flow. |
36
|
|
|
* |
37
|
|
|
* See docs/GatewayState.md for a high-level diagram on how this controller |
38
|
|
|
* interacts with outside actors and other parts of Stepup. |
39
|
|
|
*/ |
40
|
|
|
class GatewayController extends Controller |
41
|
|
|
{ |
42
|
|
|
const RESPONSE_CONTEXT_SERVICE_ID = 'gateway.proxy.response_context'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Receive an AuthnRequest from a service provider. |
46
|
|
|
* |
47
|
|
|
* The service provider is either a Stepup component (SelfService, RA) or |
48
|
|
|
* an external service provider. |
49
|
|
|
* |
50
|
|
|
* This single sign-on action will start a new SAML request to the remote |
51
|
|
|
* IDP configured in Stepup (most likely to be an instance of OpenConext |
52
|
|
|
* EngineBlock). |
53
|
|
|
* |
54
|
|
|
* @param Request $httpRequest |
55
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
56
|
|
|
*/ |
57
|
|
|
public function ssoAction(Request $httpRequest) |
58
|
|
|
{ |
59
|
|
|
/** @var \Psr\Log\LoggerInterface $logger */ |
60
|
|
|
$logger = $this->get('logger'); |
61
|
|
|
$logger->notice('Received AuthnRequest, started processing'); |
62
|
|
|
|
63
|
|
|
/** @var \Surfnet\SamlBundle\Http\RedirectBinding $redirectBinding */ |
64
|
|
|
$redirectBinding = $this->get('surfnet_saml.http.redirect_binding'); |
65
|
|
|
|
66
|
|
|
$originalRequest = $redirectBinding->receiveSignedAuthnRequestFrom($httpRequest); |
67
|
|
|
|
68
|
|
|
$originalRequestId = $originalRequest->getRequestId(); |
69
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
70
|
|
|
$logger->notice(sprintf( |
71
|
|
|
'AuthnRequest processing complete, received AuthnRequest from "%s", request ID: "%s"', |
72
|
|
|
$originalRequest->getServiceProvider(), |
73
|
|
|
$originalRequest->getRequestId() |
74
|
|
|
)); |
75
|
|
|
|
76
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Saml\Proxy\ProxyStateHandler $stateHandler */ |
77
|
|
|
$stateHandler = $this->get('gateway.proxy.state_handler'); |
78
|
|
|
|
79
|
|
|
// Clear the state of the previous SSO action. Request data of previous |
80
|
|
|
// SSO actions should not have any effect in subsequent SSO actions. |
81
|
|
|
$stateHandler->clear(); |
82
|
|
|
|
83
|
|
|
$stateHandler |
84
|
|
|
->setRequestId($originalRequestId) |
85
|
|
|
->setRequestServiceProvider($originalRequest->getServiceProvider()) |
|
|
|
|
86
|
|
|
->setRequestAssertionConsumerServiceUrl($originalRequest->getAssertionConsumerServiceURL()) |
87
|
|
|
->setRelayState($httpRequest->get(AuthnRequest::PARAMETER_RELAY_STATE, '')) |
88
|
|
|
->setResponseAction('SurfnetStepupGatewayGatewayBundle:Gateway:respond') |
89
|
|
|
->setResponseContextServiceId(static::RESPONSE_CONTEXT_SERVICE_ID); |
90
|
|
|
|
91
|
|
|
// check if the requested Loa is supported |
92
|
|
|
$requiredLoa = $originalRequest->getAuthenticationContextClassRef(); |
93
|
|
|
if ($requiredLoa && !$this->get('surfnet_stepup.service.loa_resolution')->hasLoa($requiredLoa)) { |
94
|
|
|
$logger->info(sprintf( |
95
|
|
|
'Requested required Loa "%s" does not exist, sending response with status Requester Error', |
96
|
|
|
$requiredLoa |
97
|
|
|
)); |
98
|
|
|
|
99
|
|
|
$response = $this->createRequesterFailureResponse(); |
100
|
|
|
|
101
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$stateHandler->setRequiredLoaIdentifier($requiredLoa); |
105
|
|
|
|
106
|
|
|
$proxyRequest = AuthnRequestFactory::createNewRequest( |
107
|
|
|
$this->get('surfnet_saml.hosted.service_provider'), |
108
|
|
|
$this->get('surfnet_saml.remote.idp') |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$proxyRequest->setScoping([$originalRequest->getServiceProvider()]); |
112
|
|
|
$stateHandler->setGatewayRequestId($proxyRequest->getRequestId()); |
113
|
|
|
|
114
|
|
|
$logger->notice(sprintf( |
115
|
|
|
'Sending Proxy AuthnRequest with request ID: "%s" for original AuthnRequest "%s"', |
116
|
|
|
$proxyRequest->getRequestId(), |
117
|
|
|
$originalRequest->getRequestId() |
118
|
|
|
)); |
119
|
|
|
|
120
|
|
|
return $redirectBinding->createResponseFor($proxyRequest); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function proxySsoAction() |
124
|
|
|
{ |
125
|
|
|
throw new HttpException(418, 'Not Yet Implemented'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Receive an AuthnResponse from an identity provider. |
130
|
|
|
* |
131
|
|
|
* The AuthnRequest started in ssoAction() resulted in an AuthnResponse |
132
|
|
|
* from the IDP. This method handles the assertion and forwards the user |
133
|
|
|
* using an internal redirect to the SecondFactorController to start the |
134
|
|
|
* actual second factor verification. |
135
|
|
|
* |
136
|
|
|
* @param Request $request |
137
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
138
|
|
|
*/ |
139
|
|
|
public function consumeAssertionAction(Request $request) |
140
|
|
|
{ |
141
|
|
|
$responseContext = $this->getResponseContext(); |
142
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
143
|
|
|
|
144
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
145
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
146
|
|
|
$logger->notice('Received SAMLResponse, attempting to process for Proxy Response'); |
147
|
|
|
|
148
|
|
|
try { |
149
|
|
|
/** @var \SAML2\Assertion $assertion */ |
150
|
|
|
$assertion = $this->get('surfnet_saml.http.post_binding')->processResponse( |
151
|
|
|
$request, |
152
|
|
|
$this->get('surfnet_saml.remote.idp'), |
153
|
|
|
$this->get('surfnet_saml.hosted.service_provider') |
154
|
|
|
); |
155
|
|
|
} catch (Exception $exception) { |
156
|
|
|
$logger->error(sprintf('Could not process received Response, error: "%s"', $exception->getMessage())); |
157
|
|
|
|
158
|
|
|
$response = $this->createResponseFailureResponse($responseContext); |
159
|
|
|
|
160
|
|
|
return $this->renderSamlResponse('unprocessableResponse', $response); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$adaptedAssertion = new AssertionAdapter($assertion); |
164
|
|
|
$expectedInResponseTo = $responseContext->getExpectedInResponseTo(); |
165
|
|
|
if (!$adaptedAssertion->inResponseToMatches($expectedInResponseTo)) { |
166
|
|
|
throw new UnknownInResponseToException( |
167
|
|
|
$adaptedAssertion->getInResponseTo(), |
168
|
|
|
$expectedInResponseTo |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$logger->notice('Successfully processed SAMLResponse'); |
173
|
|
|
|
174
|
|
|
$responseContext->saveAssertion($assertion); |
175
|
|
|
|
176
|
|
|
$logger->notice(sprintf('Forwarding to second factor controller for loa determination and handling')); |
177
|
|
|
|
178
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Send a SAML response back to the service provider. |
183
|
|
|
* |
184
|
|
|
* Second factor verification handled by SecondFactorController is |
185
|
|
|
* finished. The user was forwarded back to this action with an internal |
186
|
|
|
* redirect. This method sends a AuthnResponse back to the service |
187
|
|
|
* provider in response to the AuthnRequest received in ssoAction(). |
188
|
|
|
*/ |
189
|
|
|
public function respondAction() |
190
|
|
|
{ |
191
|
|
|
$responseContext = $this->getResponseContext(); |
192
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
193
|
|
|
|
194
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
195
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
196
|
|
|
$logger->notice('Creating Response'); |
197
|
|
|
|
198
|
|
|
$grantedLoa = null; |
199
|
|
|
if ($responseContext->isSecondFactorVerified()) { |
200
|
|
|
$secondFactor = $this->get('gateway.service.second_factor_service')->findByUuid( |
201
|
|
|
$responseContext->getSelectedSecondFactor() |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
$secondFactorTypeService = $this->get('surfnet_stepup.service.second_factor_type'); |
205
|
|
|
$grantedLoa = $this->get('surfnet_stepup.service.loa_resolution')->getLoaByLevel( |
206
|
|
|
$secondFactor->getLoaLevel($secondFactorTypeService) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Service\ProxyResponseService $proxyResponseService */ |
211
|
|
|
$proxyResponseService = $this->get('gateway.service.response_proxy'); |
212
|
|
|
|
213
|
|
|
$response = $proxyResponseService->createProxyResponse( |
214
|
|
|
$responseContext->reconstituteAssertion(), |
215
|
|
|
$responseContext->getDestination(), |
216
|
|
|
(string)$grantedLoa |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
$responseContext->responseSent(); |
220
|
|
|
|
221
|
|
|
$logger->notice(sprintf( |
222
|
|
|
'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
223
|
|
|
$responseContext->getInResponseTo(), |
224
|
|
|
$response->getId() |
225
|
|
|
)); |
226
|
|
|
|
227
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
View Code Duplication |
public function sendLoaCannotBeGivenAction() |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
$responseContext = $this->getResponseContext(); |
233
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
234
|
|
|
|
235
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
236
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
237
|
|
|
$logger->notice('Loa cannot be given, creating Response with NoAuthnContext status'); |
238
|
|
|
|
239
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder $responseBuilder */ |
240
|
|
|
$responseBuilder = $this->get('gateway.proxy.response_builder'); |
241
|
|
|
|
242
|
|
|
$response = $responseBuilder |
243
|
|
|
->createNewResponse($responseContext) |
244
|
|
|
->setResponseStatus(Constants::STATUS_RESPONDER, Constants::STATUS_NO_AUTHN_CONTEXT) |
245
|
|
|
->get(); |
246
|
|
|
|
247
|
|
|
$logger->notice(sprintf( |
248
|
|
|
'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
249
|
|
|
$responseContext->getInResponseTo(), |
250
|
|
|
$response->getId() |
251
|
|
|
)); |
252
|
|
|
|
253
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
View Code Duplication |
public function sendAuthenticationCancelledByUserAction() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$responseContext = $this->getResponseContext(); |
259
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
260
|
|
|
|
261
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
262
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
263
|
|
|
$logger->notice('Authentication was cancelled by the user, creating Response with AuthnFailed status'); |
264
|
|
|
|
265
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder $responseBuilder */ |
266
|
|
|
$responseBuilder = $this->get('gateway.proxy.response_builder'); |
267
|
|
|
|
268
|
|
|
$response = $responseBuilder |
269
|
|
|
->createNewResponse($responseContext) |
270
|
|
|
->setResponseStatus( |
271
|
|
|
Constants::STATUS_RESPONDER, |
272
|
|
|
Constants::STATUS_AUTHN_FAILED, |
273
|
|
|
'Authentication cancelled by user' |
274
|
|
|
) |
275
|
|
|
->get(); |
276
|
|
|
|
277
|
|
|
$logger->notice(sprintf( |
278
|
|
|
'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
279
|
|
|
$responseContext->getInResponseTo(), |
280
|
|
|
$response->getId() |
281
|
|
|
)); |
282
|
|
|
|
283
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param string $view |
288
|
|
|
* @param SAMLResponse $response |
289
|
|
|
* @return Response |
290
|
|
|
*/ |
291
|
|
View Code Duplication |
public function renderSamlResponse($view, SAMLResponse $response) |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
$responseContext = $this->getResponseContext(); |
294
|
|
|
|
295
|
|
|
return $this->render($view, [ |
296
|
|
|
'acu' => $responseContext->getDestination(), |
297
|
|
|
'response' => $this->getResponseAsXML($response), |
298
|
|
|
'relayState' => $responseContext->getRelayState() |
299
|
|
|
]); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param string $view |
304
|
|
|
* @param array $parameters |
305
|
|
|
* @param Response $response |
|
|
|
|
306
|
|
|
* @return Response |
307
|
|
|
*/ |
308
|
|
|
public function render($view, array $parameters = array(), Response $response = null) |
309
|
|
|
{ |
310
|
|
|
return parent::render( |
311
|
|
|
'SurfnetStepupGatewayGatewayBundle:Gateway:' . $view . '.html.twig', |
312
|
|
|
$parameters, |
313
|
|
|
$response |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
319
|
|
|
*/ |
320
|
|
|
public function getResponseContext() |
321
|
|
|
{ |
322
|
|
|
$stateHandler = $this->get('gateway.proxy.state_handler'); |
323
|
|
|
$responseContextServiceId = $stateHandler->getResponseContextServiceId(); |
324
|
|
|
|
325
|
|
|
if (!$responseContextServiceId) { |
326
|
|
|
return $this->get(static::RESPONSE_CONTEXT_SERVICE_ID); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $this->get($responseContextServiceId); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param SAMLResponse $response |
334
|
|
|
* @return string |
335
|
|
|
*/ |
336
|
|
|
private function getResponseAsXML(SAMLResponse $response) |
337
|
|
|
{ |
338
|
|
|
return base64_encode($response->toUnsignedXML()->ownerDocument->saveXML()); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return SAMLResponse |
343
|
|
|
*/ |
344
|
|
|
private function createRequesterFailureResponse() |
345
|
|
|
{ |
346
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder $responseBuilder */ |
347
|
|
|
$responseBuilder = $this->get('gateway.proxy.response_builder'); |
348
|
|
|
$context = $this->getResponseContext(); |
349
|
|
|
|
350
|
|
|
$response = $responseBuilder |
351
|
|
|
->createNewResponse($context) |
352
|
|
|
->setResponseStatus(Constants::STATUS_REQUESTER, Constants::STATUS_REQUEST_UNSUPPORTED) |
353
|
|
|
->get(); |
354
|
|
|
|
355
|
|
|
return $response; |
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param $context |
361
|
|
|
* @return SAMLResponse |
362
|
|
|
*/ |
363
|
|
|
private function createResponseFailureResponse($context) |
364
|
|
|
{ |
365
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder $responseBuilder */ |
366
|
|
|
$responseBuilder = $this->get('gateway.proxy.response_builder'); |
367
|
|
|
|
368
|
|
|
$response = $responseBuilder |
369
|
|
|
->createNewResponse($context) |
370
|
|
|
->setResponseStatus(Constants::STATUS_RESPONDER) |
371
|
|
|
->get(); |
372
|
|
|
|
373
|
|
|
return $response; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.