|
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\Response as SAMLResponse; |
|
22
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\RequesterFailureException; |
|
23
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\ResponseFailureException; |
|
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\Gateway\ConsumeAssertionService; |
|
25
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\Gateway\FailedResponseService; |
|
26
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\Gateway\LoginService; |
|
27
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\Gateway\RespondService; |
|
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
31
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Entry point for the Stepup login flow. |
|
35
|
|
|
* |
|
36
|
|
|
* See docs/GatewayState.md for a high-level diagram on how this controller |
|
37
|
|
|
* interacts with outside actors and other parts of Stepup. |
|
38
|
|
|
*/ |
|
39
|
|
|
class GatewayController extends Controller |
|
40
|
|
|
{ |
|
41
|
|
|
const RESPONSE_CONTEXT_SERVICE_ID = 'gateway.proxy.response_context'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Receive an AuthnRequest from a service provider. |
|
45
|
|
|
* |
|
46
|
|
|
* The service provider is either a Stepup component (SelfService, RA) or |
|
47
|
|
|
* an external service provider. |
|
48
|
|
|
* |
|
49
|
|
|
* This single sign-on action will start a new SAML request to the remote |
|
50
|
|
|
* IDP configured in Stepup (most likely to be an instance of OpenConext |
|
51
|
|
|
* EngineBlock). |
|
52
|
|
|
* |
|
53
|
|
|
* @param Request $httpRequest |
|
54
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
|
55
|
|
|
*/ |
|
56
|
|
|
public function ssoAction(Request $httpRequest) |
|
57
|
|
|
{ |
|
58
|
|
|
/** @var \Psr\Log\LoggerInterface $logger */ |
|
59
|
|
|
$logger = $this->get('logger'); |
|
60
|
|
|
|
|
61
|
|
|
$redirectBinding = $this->get('surfnet_saml.http.redirect_binding'); |
|
62
|
|
|
$gatewayLoginService = $this->getGatewayLoginService(); |
|
63
|
|
|
|
|
64
|
|
|
$logger->notice('Received AuthnRequest, started processing'); |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
$proxyRequest = $gatewayLoginService->singleSignOn($httpRequest); |
|
68
|
|
|
} catch (RequesterFailureException $e) { |
|
69
|
|
|
$response = $this->getGatewayFailedResponseService()->createRequesterFailureResponse($this->getResponseContext()); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $this->renderSamlResponse('consumeAssertion', $response); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $redirectBinding->createResponseFor($proxyRequest); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
*/ |
|
80
|
|
|
public function proxySsoAction() |
|
81
|
|
|
{ |
|
82
|
|
|
throw new HttpException(418, 'Not Yet Implemented'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Receive an AuthnResponse from an identity provider. |
|
87
|
|
|
* |
|
88
|
|
|
* The AuthnRequest started in ssoAction() resulted in an AuthnResponse |
|
89
|
|
|
* from the IDP. This method handles the assertion and forwards the user |
|
90
|
|
|
* using an internal redirect to the SecondFactorController to start the |
|
91
|
|
|
* actual second factor verification. |
|
92
|
|
|
* |
|
93
|
|
|
* @param Request $request |
|
94
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
95
|
|
|
*/ |
|
96
|
|
|
public function consumeAssertionAction(Request $request) |
|
97
|
|
|
{ |
|
98
|
|
|
$responseContext = $this->getResponseContext(); |
|
99
|
|
|
$gatewayLoginService = $this->getGatewayConsumeAssertionService(); |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
$gatewayLoginService->consumeAssertion($request, $responseContext); |
|
103
|
|
|
} catch (ResponseFailureException $e) { |
|
104
|
|
|
$response = $this->getGatewayFailedResponseService()->createResponseFailureResponse($responseContext); |
|
105
|
|
|
|
|
106
|
|
|
return $this->renderSamlResponse('unprocessableResponse', $response); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Send a SAML response back to the service provider. |
|
114
|
|
|
* |
|
115
|
|
|
* Second factor verification handled by SecondFactorController is |
|
116
|
|
|
* finished. The user was forwarded back to this action with an internal |
|
117
|
|
|
* redirect. This method sends a AuthnResponse back to the service |
|
118
|
|
|
* provider in response to the AuthnRequest received in ssoAction(). |
|
119
|
|
|
*/ |
|
120
|
|
|
public function respondAction() |
|
121
|
|
|
{ |
|
122
|
|
|
$responseContext = $this->getResponseContext(); |
|
123
|
|
|
$gatewayLoginService = $this->getGatewayRespondService(); |
|
124
|
|
|
|
|
125
|
|
|
$response = $gatewayLoginService->respond($responseContext); |
|
126
|
|
|
$gatewayLoginService->resetRespondState($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->getGatewayFailedResponseService(); |
|
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->getGatewayFailedResponseService(); |
|
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 LoginService |
|
215
|
|
|
*/ |
|
216
|
|
|
private function getGatewayLoginService() |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->get('gateway.service.gateway.login'); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return ConsumeAssertionService |
|
223
|
|
|
*/ |
|
224
|
|
|
private function getGatewayConsumeAssertionService() |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->get('gateway.service.gateway.consume_assertion'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return RespondService |
|
231
|
|
|
*/ |
|
232
|
|
|
private function getGatewayRespondService() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->get('gateway.service.gateway.respond'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return FailedResponseService |
|
239
|
|
|
*/ |
|
240
|
|
|
private function getGatewayFailedResponseService() |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->get('gateway.service.gateway.failed_response'); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.