|
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\StepupSelfService\SelfServiceBundle\Controller\Registration; |
|
20
|
|
|
|
|
21
|
|
|
use Exception; |
|
22
|
|
|
use Surfnet\SamlBundle\Http\XMLResponse; |
|
23
|
|
|
use Surfnet\SamlBundle\SAML2\AuthnRequestFactory; |
|
24
|
|
|
use Surfnet\SamlBundle\SAML2\Response\Assertion\InResponseTo; |
|
25
|
|
|
use Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\Provider; |
|
26
|
|
|
use Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\ViewConfig; |
|
27
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Controller\Controller; |
|
28
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\StatusGssfType; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
32
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Controls registration with Generic SAML Stepup Providers (GSSPs), yielding Generic SAML Second Factors (GSSFs). |
|
36
|
|
|
*/ |
|
|
|
|
|
|
37
|
|
|
final class GssfController extends Controller |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
|
|
|
|
|
40
|
|
|
* Render the status form. |
|
41
|
|
|
* |
|
42
|
|
|
* This action has two parameters: |
|
43
|
|
|
* |
|
44
|
|
|
* - authenticationFailed (default false), will trigger an error message |
|
45
|
|
|
* and is used when a SAML failure response was received, for example |
|
46
|
|
|
* when the users cancelled the registration |
|
47
|
|
|
* |
|
48
|
|
|
* - proofOfPossessionFailed (default false), will trigger an error message |
|
49
|
|
|
* when possession was not proven, but the SAML response was successful |
|
50
|
|
|
* |
|
51
|
|
|
* @return array|Response |
|
52
|
|
|
*/ |
|
|
|
|
|
|
53
|
|
|
#[Route( |
|
54
|
|
|
path: '/registration/gssf/{provider}/status', |
|
55
|
|
|
name: 'ss_registration_gssf_status_report', |
|
56
|
|
|
defaults: ['authenticationFailed' => false, 'proofOfPossessionFailed'=> false ], |
|
57
|
|
|
methods: ['GET'], |
|
58
|
|
|
)] |
|
59
|
|
|
|
|
60
|
|
|
public function status(Request $request, string $provider): \Symfony\Component\HttpFoundation\Response |
|
61
|
|
|
{ |
|
62
|
|
|
$this->assertSecondFactorEnabled($provider); |
|
63
|
|
|
|
|
64
|
|
|
return $this->renderStatusForm( |
|
65
|
|
|
$provider, |
|
66
|
|
|
[ |
|
67
|
|
|
'authenticationFailed' => (bool) $request->query->get('authenticationFailed'), |
|
68
|
|
|
'proofOfPossessionFailed' => (bool) $request->query->get('proofOfPossessionFailed'), |
|
69
|
|
|
] |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
|
|
|
|
|
74
|
|
|
* @param string $provider |
|
|
|
|
|
|
75
|
|
|
* @return array|Response |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
|
|
#[Route( |
|
78
|
|
|
path: '/registration/gssf/{provider}/authenticate', |
|
79
|
|
|
name: 'ss_registration_gssf_authenticate', |
|
80
|
|
|
methods: ['POST'], |
|
81
|
|
|
)] |
|
82
|
|
|
public function authenticate($provider) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertSecondFactorEnabled($provider); |
|
85
|
|
|
|
|
86
|
|
|
$provider = $this->getProvider($provider); |
|
87
|
|
|
|
|
88
|
|
|
$authnRequest = AuthnRequestFactory::createNewRequest( |
|
89
|
|
|
$provider->getServiceProvider(), |
|
90
|
|
|
$provider->getRemoteIdentityProvider() |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$attributeService = $this->get('surfnet_stepup_self_service_self_service.service.gsspuserattributes'); |
|
|
|
|
|
|
94
|
|
|
$attributeService->addGsspUserAttributes( |
|
95
|
|
|
$authnRequest, |
|
96
|
|
|
$provider, |
|
97
|
|
|
$this->get('security.token_storage')->getToken()->getUser() |
|
98
|
|
|
); |
|
99
|
|
|
$stateHandler = $provider->getStateHandler(); |
|
100
|
|
|
$stateHandler->setRequestId($authnRequest->getRequestId()); |
|
101
|
|
|
|
|
102
|
|
|
/** @var \Surfnet\SamlBundle\Http\RedirectBinding $redirectBinding */ |
|
|
|
|
|
|
103
|
|
|
$redirectBinding = $this->get('surfnet_saml.http.redirect_binding'); |
|
104
|
|
|
|
|
105
|
|
|
$this->getLogger()->notice(sprintf( |
|
|
|
|
|
|
106
|
|
|
'Sending AuthnRequest with request ID: "%s" to GSSP "%s" at "%s"', |
|
107
|
|
|
$authnRequest->getRequestId(), |
|
108
|
|
|
$provider->getName(), |
|
109
|
|
|
$provider->getRemoteIdentityProvider()->getSsoUrl() |
|
110
|
|
|
)); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
return $redirectBinding->createRedirectResponseFor($authnRequest); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
|
|
|
|
|
116
|
|
|
* @param string $provider |
|
|
|
|
|
|
117
|
|
|
* @return array|Response |
|
|
|
|
|
|
118
|
|
|
*/ |
|
119
|
|
|
#[Route( |
|
120
|
|
|
path: '/registration/gssf/{provider}/consume-assertion', |
|
121
|
|
|
name: 'ss_registration_gssf_consume_assertion', |
|
122
|
|
|
methods: ['POST'], |
|
123
|
|
|
)] |
|
124
|
|
|
public function consumeAssertion(Request $httpRequest, $provider) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->assertSecondFactorEnabled($provider); |
|
127
|
|
|
|
|
128
|
|
|
$provider = $this->getProvider($provider); |
|
129
|
|
|
|
|
130
|
|
|
$this->get('logger')->notice( |
|
131
|
|
|
sprintf('Received GSSP "%s" SAMLResponse through Gateway, attempting to process', $provider->getName()) |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
try { |
|
135
|
|
|
/** @var \Surfnet\SamlBundle\Http\PostBinding $postBinding */ |
|
|
|
|
|
|
136
|
|
|
$postBinding = $this->get('surfnet_saml.http.post_binding'); |
|
137
|
|
|
$assertion = $postBinding->processResponse( |
|
138
|
|
|
$httpRequest, |
|
139
|
|
|
$provider->getRemoteIdentityProvider(), |
|
140
|
|
|
$provider->getServiceProvider() |
|
141
|
|
|
); |
|
142
|
|
|
} catch (Exception $exception) { |
|
143
|
|
|
$provider->getStateHandler()->clear(); |
|
144
|
|
|
|
|
145
|
|
|
$this->getLogger()->error( |
|
146
|
|
|
sprintf('Could not process received Response, error: "%s"', $exception->getMessage()) |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
return $this->redirectToStatusReportForm( |
|
150
|
|
|
$provider, |
|
151
|
|
|
['authenticationFailed' => true] |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$expectedResponseTo = $provider->getStateHandler()->getRequestId(); |
|
156
|
|
|
$provider->getStateHandler()->clear(); |
|
157
|
|
|
|
|
158
|
|
|
if (!InResponseTo::assertEquals($assertion, $expectedResponseTo)) { |
|
159
|
|
|
$this->getLogger()->critical(sprintf( |
|
|
|
|
|
|
160
|
|
|
'Received Response with unexpected InResponseTo, %s', |
|
161
|
|
|
($expectedResponseTo ? 'expected "' . $expectedResponseTo . '"' : ' no response expected') |
|
162
|
|
|
)); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
return $this->redirectToStatusReportForm( |
|
165
|
|
|
$provider, |
|
166
|
|
|
['authenticationFailed' => true] |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$this->get('logger')->notice( |
|
171
|
|
|
sprintf('Processed GSSP "%s" SAMLResponse received through Gateway successfully', $provider->getName()) |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
/** @var \Surfnet\StepupSelfService\SelfServiceBundle\Service\GssfService $service */ |
|
|
|
|
|
|
175
|
|
|
$service = $this->get('surfnet_stepup_self_service_self_service.service.gssf'); |
|
176
|
|
|
/** @var \Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary $attributeDictionary */ |
|
|
|
|
|
|
177
|
|
|
$attributeDictionary = $this->get('surfnet_saml.saml.attribute_dictionary'); |
|
178
|
|
|
$gssfId = $attributeDictionary->translate($assertion)->getNameID(); |
|
179
|
|
|
|
|
180
|
|
|
$secondFactorId = $service->provePossession($this->getIdentity()->id, $provider->getName(), $gssfId); |
|
181
|
|
|
|
|
182
|
|
|
if ($secondFactorId) { |
|
183
|
|
|
$this->getLogger()->notice('GSSF possession has been proven successfully'); |
|
184
|
|
|
|
|
185
|
|
|
if ($this->emailVerificationIsRequired()) { |
|
186
|
|
|
return $this->redirectToRoute( |
|
187
|
|
|
'ss_registration_email_verification_email_sent', |
|
188
|
|
|
['secondFactorId' => $secondFactorId] |
|
189
|
|
|
); |
|
190
|
|
|
} else { |
|
191
|
|
|
return $this->redirectToRoute( |
|
192
|
|
|
'ss_second_factor_vetting_types', |
|
193
|
|
|
['secondFactorId' => $secondFactorId] |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$this->getLogger()->error('Unable to prove GSSF possession'); |
|
199
|
|
|
|
|
200
|
|
|
return $this->redirectToStatusReportForm( |
|
201
|
|
|
$provider, |
|
202
|
|
|
['proofOfPossessionFailed' => true] |
|
203
|
|
|
); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
private function redirectToStatusReportForm(Provider $provider, array $options): \Symfony\Component\HttpFoundation\RedirectResponse |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
|
|
return $this->redirectToRoute( |
|
209
|
|
|
'ss_registration_gssf_status_report', |
|
210
|
|
|
$options + [ |
|
211
|
|
|
'provider' => $provider->getName(), |
|
212
|
|
|
] |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
|
|
|
|
|
217
|
|
|
* @param string $provider |
|
|
|
|
|
|
218
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
|
|
|
|
|
219
|
|
|
*/ |
|
220
|
|
|
#[Route( |
|
221
|
|
|
path: '/registration/gssf/{provider}/metadata', |
|
222
|
|
|
name: 'ss_registration_gssf_saml_metadata', |
|
223
|
|
|
methods: ['GET'], |
|
224
|
|
|
)] |
|
225
|
|
|
public function metadata($provider): \Surfnet\SamlBundle\Http\XMLResponse |
|
226
|
|
|
{ |
|
227
|
|
|
$this->assertSecondFactorEnabled($provider); |
|
228
|
|
|
|
|
229
|
|
|
$provider = $this->getProvider($provider); |
|
230
|
|
|
|
|
231
|
|
|
/** @var \Surfnet\SamlBundle\Metadata\MetadataFactory $factory */ |
|
|
|
|
|
|
232
|
|
|
$factory = $this->get('gssp.provider.' . $provider->getName() . '.metadata.factory'); |
|
233
|
|
|
|
|
234
|
|
|
return new XMLResponse($factory->generate()); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
|
|
|
|
|
238
|
|
|
* @param string $provider |
|
|
|
|
|
|
239
|
|
|
* @return \Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\Provider |
|
|
|
|
|
|
240
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
|
|
|
|
|
241
|
|
|
*/ |
|
242
|
|
|
private function getProvider($provider) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
/** @var \Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\ProviderRepository $providerRepository */ |
|
|
|
|
|
|
245
|
|
|
$providerRepository = $this->get('gssp.provider_repository'); |
|
246
|
|
|
|
|
247
|
|
|
if (!$providerRepository->has($provider)) { |
|
248
|
|
|
$this->get('logger')->info(sprintf('Requested GSSP "%s" does not exist or is not registered', $provider)); |
|
249
|
|
|
|
|
250
|
|
|
throw new NotFoundHttpException('Requested provider does not exist'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $providerRepository->get($provider); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
|
|
|
|
|
257
|
|
|
* @return \Psr\Log\LoggerInterface |
|
258
|
|
|
*/ |
|
259
|
|
|
private function getLogger() |
|
|
|
|
|
|
260
|
|
|
{ |
|
261
|
|
|
return $this->get('logger'); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
private function renderStatusForm(string $provider, array $parameters = []): Response |
|
|
|
|
|
|
265
|
|
|
{ |
|
266
|
|
|
/** @var ViewConfig $secondFactorConfig */ |
|
|
|
|
|
|
267
|
|
|
$secondFactorConfig = $this->get("gssp.view_config.{$provider}"); |
|
268
|
|
|
|
|
269
|
|
|
$form = $this->createForm( |
|
270
|
|
|
StatusGssfType::class, |
|
271
|
|
|
null, |
|
272
|
|
|
[ |
|
273
|
|
|
'provider' => $provider, |
|
274
|
|
|
/** @Ignore from translation message extraction */ |
|
|
|
|
|
|
275
|
|
|
'label' => $secondFactorConfig->getInitiateButton() |
|
276
|
|
|
] |
|
277
|
|
|
); |
|
278
|
|
|
/** @var ViewConfig $secondFactorConfig */ |
|
|
|
|
|
|
279
|
|
|
$templateParameters = array_merge( |
|
280
|
|
|
$parameters, |
|
281
|
|
|
[ |
|
282
|
|
|
'form' => $form->createView(), |
|
283
|
|
|
'provider' => $provider, |
|
284
|
|
|
'secondFactorConfig' => $secondFactorConfig, |
|
285
|
|
|
'verifyEmail' => $this->emailVerificationIsRequired(), |
|
286
|
|
|
] |
|
287
|
|
|
); |
|
288
|
|
|
return $this->render( |
|
289
|
|
|
'SurfnetStepupSelfServiceSelfServiceBundle:registration/gssf:status.html.twig', |
|
290
|
|
|
$templateParameters |
|
291
|
|
|
); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|