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\SecondFactorOnlyBundle\Controller; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Surfnet\SamlBundle\SAML2\AuthnRequest; |
23
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\Exception\InvalidAdfsRequestException; |
24
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\Exception\InvalidAdfsResponseException; |
25
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Saml\ResponseFactory; |
26
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Service\LoaAliasLookupService; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
31
|
|
|
|
32
|
|
|
class SecondFactorOnlyController extends Controller |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @param Request $httpRequest |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
|
|
public function ssoAction(Request $httpRequest) |
39
|
|
|
{ |
40
|
|
|
$logger = $this->get('logger'); |
41
|
|
|
|
42
|
|
|
if (!$this->getParameter('second_factor_only')) { |
43
|
|
|
$logger->notice('Access to ssoAction denied, second_factor_only parameter set to false.'); |
44
|
|
|
|
45
|
|
|
throw $this->createAccessDeniedException('Second Factor Only feature is disabled'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$logger->notice('Received AuthnRequest on second-factor-only endpoint, started processing'); |
49
|
|
|
|
50
|
|
|
/** @var \Surfnet\SamlBundle\Http\RedirectBinding $redirectBinding */ |
51
|
|
|
$bindingFactory = $this->get('second_factor_only.http.binding_factory'); |
52
|
|
|
|
53
|
|
|
$logger->notice('Determine what type of Binding is used in the Request'); |
54
|
|
|
$binding = $bindingFactory->build($httpRequest); |
55
|
|
|
|
56
|
|
|
/** @var \Surfnet\SamlBundle\SAML2\ReceivedAuthnRequest $originalRequest */ |
57
|
|
|
$originalRequest = $binding->receiveSignedAuthnRequestFrom($httpRequest); |
58
|
|
|
|
59
|
|
|
$originalRequestId = $originalRequest->getRequestId(); |
60
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
61
|
|
|
$logger->notice(sprintf( |
62
|
|
|
'AuthnRequest processing complete, received AuthnRequest from "%s", request ID: "%s"', |
63
|
|
|
$originalRequest->getServiceProvider(), |
64
|
|
|
$originalRequest->getRequestId() |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
// ADFS support |
68
|
|
|
$adfsHelper = $this->get('second_factor_only.adfs.request_helper'); |
69
|
|
|
if ($adfsHelper->isAdfsRequest($httpRequest)) { |
70
|
|
|
$logger->notice('Received AuthnRequest from an ADFS'); |
71
|
|
|
try { |
72
|
|
|
$httpRequest = $adfsHelper->transformRequest( |
73
|
|
|
$httpRequest, |
74
|
|
|
$originalRequest->getRequestId(), |
75
|
|
|
$originalRequest->getAssertionConsumerServiceURL() |
76
|
|
|
); |
77
|
|
|
} catch (Exception $e) { |
78
|
|
|
throw new InvalidAdfsRequestException( |
79
|
|
|
sprintf('Could not process ADFS Request, error: "%s"', $e->getMessage()) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$stateHandler = $this->get('gateway.proxy.state_handler'); |
85
|
|
|
|
86
|
|
|
// Clear the state of the previous SSO action. Request data of previous |
87
|
|
|
// SSO actions should not have any effect in subsequent SSO actions. |
88
|
|
|
$stateHandler->clear(); |
89
|
|
|
|
90
|
|
|
$stateHandler |
91
|
|
|
->setRequestId($originalRequestId) |
92
|
|
|
->setRequestServiceProvider($originalRequest->getServiceProvider()) |
93
|
|
|
->setRelayState($httpRequest->get(AuthnRequest::PARAMETER_RELAY_STATE, '')) |
94
|
|
|
->setResponseAction('SurfnetStepupGatewaySecondFactorOnlyBundle:SecondFactorOnly:respond') |
95
|
|
|
->setResponseContextServiceId('second_factor_only.response_context'); |
96
|
|
|
|
97
|
|
|
// Check if the NameID is provided and we may use it. |
98
|
|
|
$nameId = $originalRequest->getNameId(); |
99
|
|
|
$secondFactorOnlyNameIdValidator = $this->get('second_factor_only.validate_nameid')->with($logger); |
|
|
|
|
100
|
|
|
$serviceProviderMayUseSecondFactorOnly = $secondFactorOnlyNameIdValidator->validate( |
|
|
|
|
101
|
|
|
$originalRequest->getServiceProvider(), |
102
|
|
|
$nameId |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
if (!$serviceProviderMayUseSecondFactorOnly) { |
106
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Service\ResponseRenderingService $responseRendering */ |
107
|
|
|
$responseRendering = $this->get('second_factor_only.response_rendering'); |
108
|
|
|
|
109
|
|
|
return $responseRendering->renderRequesterFailureResponse($this->getResponseContext()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$stateHandler->saveIdentityNameId($nameId); |
113
|
|
|
|
114
|
|
|
// Check if the requested Loa is provided and supported. |
115
|
|
|
$loaId = $this->get('second_factor_only.loa_resolution')->with($logger)->resolve( |
116
|
|
|
$originalRequest->getAuthenticationContextClassRef() |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
if (empty($loaId)) { |
120
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Service\ResponseRenderingService $responseRendering */ |
121
|
|
|
$responseRendering = $this->get('second_factor_only.response_rendering'); |
122
|
|
|
|
123
|
|
|
return $responseRendering->renderRequesterFailureResponse($this->getResponseContext()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$stateHandler->setRequiredLoaIdentifier($loaId); |
127
|
|
|
|
128
|
|
|
$logger->notice('Forwarding to second factor controller for loa determination and handling'); |
129
|
|
|
|
130
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Response |
135
|
|
|
*/ |
136
|
|
|
public function respondAction() |
137
|
|
|
{ |
138
|
|
|
$responseContext = $this->getResponseContext(); |
139
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
140
|
|
|
|
141
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
142
|
|
|
|
143
|
|
|
if (!$this->getParameter('second_factor_only')) { |
144
|
|
|
$logger->notice(sprintf( |
145
|
|
|
'Access to %s denied, second_factor_only parameter set to false.', |
146
|
|
|
__METHOD__ |
147
|
|
|
)); |
148
|
|
|
throw $this->createAccessDeniedException('Second Factor Only feature disabled'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$logger->notice('Creating second-factor-only Response'); |
152
|
|
|
|
153
|
|
|
$selectedSecondFactorUuid = $this->getResponseContext()->getSelectedSecondFactor(); |
154
|
|
|
if (!$selectedSecondFactorUuid) { |
155
|
|
|
throw new BadRequestHttpException('Cannot verify possession of an unknown second factor.'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!$responseContext->isSecondFactorVerified()) { |
159
|
|
|
throw new BadRequestHttpException( |
160
|
|
|
'Second factor was not verified' |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$secondFactor = $this->get('gateway.service.second_factor_service') |
165
|
|
|
->findByUuid($selectedSecondFactorUuid); |
166
|
|
|
$secondFactorTypeService = $this->get('surfnet_stepup.service.second_factor_type'); |
167
|
|
|
$grantedLoa = $this->get('surfnet_stepup.service.loa_resolution') |
168
|
|
|
->getLoaByLevel($secondFactor->getLoaLevel($secondFactorTypeService)); |
169
|
|
|
|
170
|
|
|
/** @var LoaAliasLookupService $loaAliasLookup */ |
171
|
|
|
$loaAliasLookup = $this->get('second_factor_only.loa_alias_lookup'); |
172
|
|
|
$authnContextClassRef = $loaAliasLookup->findAliasByLoa($grantedLoa); |
173
|
|
|
|
174
|
|
|
/** @var ResponseFactory $response_factory */ |
175
|
|
|
$responseFactory = $this->get('second_factor_only.saml_response_factory'); |
176
|
|
|
$response = $responseFactory->createSecondFactorOnlyResponse( |
177
|
|
|
$responseContext->getIdentityNameId(), |
178
|
|
|
$responseContext->getServiceProvider(), |
179
|
|
|
$authnContextClassRef |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$responseContext->responseSent(); |
183
|
|
|
|
184
|
|
|
$logger->notice(sprintf( |
185
|
|
|
'Responding to request "%s" with newly created response "%s"', |
186
|
|
|
$responseContext->getInResponseTo(), |
187
|
|
|
$response->getId() |
188
|
|
|
)); |
189
|
|
|
|
190
|
|
|
$responseRendering = $this->get('second_factor_only.response_rendering'); |
191
|
|
|
|
192
|
|
|
$adfsHelper = $this->get('second_factor_only.adfs.response_helper'); |
193
|
|
|
if ($adfsHelper->isAdfsResponse($originalRequestId)) { |
194
|
|
|
$xmlResponse = $responseRendering->getResponseAsXML($response); |
195
|
|
|
try { |
196
|
|
|
$adfsParameters = $adfsHelper->retrieveAdfsParameters(); |
197
|
|
|
} catch (Exception $e) { |
198
|
|
|
throw new InvalidAdfsResponseException( |
199
|
|
|
sprintf('Could not process ADFS Response parameters, error: "%s"', $e->getMessage()) |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$logger->notice('Sending ACS Response to ADFS plugin'); |
204
|
|
|
return $this->render( |
205
|
|
|
'@SurfnetStepupGatewaySecondFactorOnly/Adfs/consumeAssertion.html.twig', |
206
|
|
|
[ |
207
|
|
|
'acu' => $adfsParameters->getAssertionConsumerServiceUrl(), |
208
|
|
|
'samlResponse' => $xmlResponse, |
209
|
|
|
'context' => $adfsParameters->getContext(), |
210
|
|
|
'authMethod' => $adfsParameters->getAuthMethod(), |
211
|
|
|
] |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
return $responseRendering->renderResponse($responseContext, $response); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
219
|
|
|
*/ |
220
|
|
|
public function getResponseContext() |
221
|
|
|
{ |
222
|
|
|
return $this->get('second_factor_only.response_context'); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.