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 SAML2\Constants; |
22
|
|
|
use SAML2\Response as SAMLResponse; |
23
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\RequesterFailureException; |
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\ResponseFailureException; |
25
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\GatewayLoginService; |
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
28
|
|
|
use Symfony\Component\HttpFoundation\Response; |
29
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Entry point for the Stepup login flow. |
33
|
|
|
* |
34
|
|
|
* See docs/GatewayState.md for a high-level diagram on how this controller |
35
|
|
|
* interacts with outside actors and other parts of Stepup. |
36
|
|
|
*/ |
37
|
|
|
class GatewayController extends Controller |
38
|
|
|
{ |
39
|
|
|
const RESPONSE_CONTEXT_SERVICE_ID = 'gateway.proxy.response_context'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Receive an AuthnRequest from a service provider. |
43
|
|
|
* |
44
|
|
|
* The service provider is either a Stepup component (SelfService, RA) or |
45
|
|
|
* an external service provider. |
46
|
|
|
* |
47
|
|
|
* This single sign-on action will start a new SAML request to the remote |
48
|
|
|
* IDP configured in Stepup (most likely to be an instance of OpenConext |
49
|
|
|
* EngineBlock). |
50
|
|
|
* |
51
|
|
|
* @param Request $httpRequest |
52
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
53
|
|
|
*/ |
54
|
|
|
public function ssoAction(Request $httpRequest) |
55
|
|
|
{ |
56
|
|
|
/** @var \Psr\Log\LoggerInterface $logger */ |
57
|
|
|
$logger = $this->get('logger'); |
58
|
|
|
|
59
|
|
|
$redirectBinding = $this->get('surfnet_saml.http.redirect_binding'); |
60
|
|
|
$gatewayLoginService = $this->getGatewayLoginService(); |
61
|
|
|
|
62
|
|
|
$logger->notice('Received AuthnRequest, started processing'); |
63
|
|
|
|
64
|
|
|
/** @var \Surfnet\SamlBundle\Http\RedirectBinding $redirectBinding */ |
65
|
|
|
$originalRequest = $redirectBinding->receiveSignedAuthnRequestFrom($httpRequest); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$proxyRequest = $gatewayLoginService->singleSignOn($httpRequest, $originalRequest); |
69
|
|
|
} catch (RequesterFailureException $e) { |
70
|
|
|
$response = $this->createRequesterFailureResponse(); |
71
|
|
|
|
72
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $redirectBinding->createResponseFor($proxyRequest); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
|
|
public function proxySsoAction() |
82
|
|
|
{ |
83
|
|
|
throw new HttpException(418, 'Not Yet Implemented'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Receive an AuthnResponse from an identity provider. |
88
|
|
|
* |
89
|
|
|
* The AuthnRequest started in ssoAction() resulted in an AuthnResponse |
90
|
|
|
* from the IDP. This method handles the assertion and forwards the user |
91
|
|
|
* using an internal redirect to the SecondFactorController to start the |
92
|
|
|
* actual second factor verification. |
93
|
|
|
* |
94
|
|
|
* @param Request $request |
95
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
96
|
|
|
*/ |
97
|
|
|
public function consumeAssertionAction(Request $request) |
98
|
|
|
{ |
99
|
|
|
$responseContext = $this->getResponseContext(); |
100
|
|
|
$gatewayLoginService = $this->getGatewayLoginService(); |
101
|
|
|
|
102
|
|
|
try { |
103
|
|
|
$gatewayLoginService->consumeAssertion($request, $responseContext); |
104
|
|
|
} catch (ResponseFailureException $e) { |
105
|
|
|
$response = $this->createResponseFailureResponse($responseContext); |
106
|
|
|
|
107
|
|
|
return $this->renderSamlResponse('unprocessableResponse', $response); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Send a SAML response back to the service provider. |
115
|
|
|
* |
116
|
|
|
* Second factor verification handled by SecondFactorController is |
117
|
|
|
* finished. The user was forwarded back to this action with an internal |
118
|
|
|
* redirect. This method sends a AuthnResponse back to the service |
119
|
|
|
* provider in response to the AuthnRequest received in ssoAction(). |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function respondAction() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$responseContext = $this->getResponseContext(); |
124
|
|
|
$gatewayLoginService = $this->getGatewayLoginService(); |
125
|
|
|
|
126
|
|
|
$response = $gatewayLoginService->respond($responseContext); |
127
|
|
|
|
128
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return Response |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function sendLoaCannotBeGivenAction() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$responseContext = $this->getResponseContext(); |
137
|
|
|
$gatewayLoginService = $this->getGatewayLoginService(); |
138
|
|
|
|
139
|
|
|
$response = $gatewayLoginService->sendLoaCannotBeGiven($responseContext); |
140
|
|
|
|
141
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return Response |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public function sendAuthenticationCancelledByUserAction() |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$responseContext = $this->getResponseContext(); |
150
|
|
|
$gatewayLoginService = $this->getGatewayLoginService(); |
151
|
|
|
|
152
|
|
|
$response = $gatewayLoginService->sendAuthenticationCancelledByUser($responseContext); |
153
|
|
|
|
154
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $view |
159
|
|
|
* @param SAMLResponse $response |
160
|
|
|
* @return Response |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function renderSamlResponse($view, SAMLResponse $response) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$responseContext = $this->getResponseContext(); |
165
|
|
|
|
166
|
|
|
return $this->render($view, [ |
167
|
|
|
'acu' => $responseContext->getDestination(), |
168
|
|
|
'response' => $this->getResponseAsXML($response), |
169
|
|
|
'relayState' => $responseContext->getRelayState() |
170
|
|
|
]); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $view |
175
|
|
|
* @param array $parameters |
176
|
|
|
* @param Response $response |
|
|
|
|
177
|
|
|
* @return Response |
178
|
|
|
*/ |
179
|
|
|
public function render($view, array $parameters = array(), Response $response = null) |
180
|
|
|
{ |
181
|
|
|
return parent::render( |
182
|
|
|
'SurfnetStepupGatewayGatewayBundle:Gateway:' . $view . '.html.twig', |
183
|
|
|
$parameters, |
184
|
|
|
$response |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
190
|
|
|
*/ |
191
|
|
|
public function getResponseContext() |
192
|
|
|
{ |
193
|
|
|
$stateHandler = $this->get('gateway.proxy.state_handler'); |
194
|
|
|
|
195
|
|
|
$responseContextServiceId = $stateHandler->getResponseContextServiceId(); |
196
|
|
|
|
197
|
|
|
if (!$responseContextServiceId) { |
198
|
|
|
return $this->get(static::RESPONSE_CONTEXT_SERVICE_ID); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this->get($responseContextServiceId); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param SAMLResponse $response |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
private function getResponseAsXML(SAMLResponse $response) |
209
|
|
|
{ |
210
|
|
|
return base64_encode($response->toUnsignedXML()->ownerDocument->saveXML()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return SAMLResponse |
215
|
|
|
*/ |
216
|
|
|
private function createRequesterFailureResponse() |
217
|
|
|
{ |
218
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder $responseBuilder */ |
219
|
|
|
$responseBuilder = $this->get('gateway.proxy.response_builder'); |
220
|
|
|
|
221
|
|
|
$context = $this->getResponseContext(); |
222
|
|
|
|
223
|
|
|
$response = $responseBuilder |
224
|
|
|
->createNewResponse($context) |
225
|
|
|
->setResponseStatus(Constants::STATUS_REQUESTER, Constants::STATUS_REQUEST_UNSUPPORTED) |
226
|
|
|
->get(); |
227
|
|
|
|
228
|
|
|
return $response; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param $context |
233
|
|
|
* @return SAMLResponse |
234
|
|
|
*/ |
235
|
|
|
private function createResponseFailureResponse($context) |
236
|
|
|
{ |
237
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseBuilder $responseBuilder */ |
238
|
|
|
$responseBuilder = $this->get('gateway.proxy.response_builder'); |
239
|
|
|
|
240
|
|
|
$response = $responseBuilder |
241
|
|
|
->createNewResponse($context) |
242
|
|
|
->setResponseStatus(Constants::STATUS_RESPONDER) |
243
|
|
|
->get(); |
244
|
|
|
|
245
|
|
|
return $response; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return GatewayLoginService |
250
|
|
|
*/ |
251
|
|
|
private function getGatewayLoginService() |
252
|
|
|
{ |
253
|
|
|
return $this->get('gateway.service.login'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.