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
|
|
|
$stateHandler |
86
|
|
|
->setRequestId($originalRequestId) |
87
|
|
|
->setRequestServiceProvider($originalRequest->getServiceProvider()) |
88
|
|
|
->setRelayState($httpRequest->get(AuthnRequest::PARAMETER_RELAY_STATE, '')) |
89
|
|
|
->setResponseAction('SurfnetStepupGatewaySecondFactorOnlyBundle:SecondFactorOnly:respond') |
90
|
|
|
->setResponseContextServiceId('second_factor_only.response_context'); |
91
|
|
|
|
92
|
|
|
// Check if the NameID is provided and we may use it. |
93
|
|
|
$nameId = $originalRequest->getNameId(); |
94
|
|
|
$secondFactorOnlyNameIdValidator = $this->get('second_factor_only.validate_nameid')->with($logger); |
|
|
|
|
95
|
|
|
$serviceProviderMayUseSecondFactorOnly = $secondFactorOnlyNameIdValidator->validate( |
|
|
|
|
96
|
|
|
$originalRequest->getServiceProvider(), |
97
|
|
|
$nameId |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
if (!$serviceProviderMayUseSecondFactorOnly) { |
101
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Service\ResponseRenderingService $responseRendering */ |
102
|
|
|
$responseRendering = $this->get('second_factor_only.response_rendering'); |
103
|
|
|
|
104
|
|
|
return $responseRendering->renderRequesterFailureResponse($this->getResponseContext()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$stateHandler->saveIdentityNameId($nameId); |
108
|
|
|
|
109
|
|
|
// Check if the requested Loa is provided and supported. |
110
|
|
|
$loaId = $this->get('second_factor_only.loa_resolution')->with($logger)->resolve( |
111
|
|
|
$originalRequest->getAuthenticationContextClassRef() |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
if (empty($loaId)) { |
115
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Service\ResponseRenderingService $responseRendering */ |
116
|
|
|
$responseRendering = $this->get('second_factor_only.response_rendering'); |
117
|
|
|
|
118
|
|
|
return $responseRendering->renderRequesterFailureResponse($this->getResponseContext()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$stateHandler->setRequiredLoaIdentifier($loaId); |
122
|
|
|
|
123
|
|
|
$logger->notice('Forwarding to second factor controller for loa determination and handling'); |
124
|
|
|
|
125
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return Response |
130
|
|
|
*/ |
131
|
|
|
public function respondAction() |
132
|
|
|
{ |
133
|
|
|
$responseContext = $this->getResponseContext(); |
134
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
135
|
|
|
|
136
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
137
|
|
|
|
138
|
|
|
if (!$this->getParameter('second_factor_only')) { |
139
|
|
|
$logger->notice(sprintf( |
140
|
|
|
'Access to %s denied, second_factor_only parameter set to false.', |
141
|
|
|
__METHOD__ |
142
|
|
|
)); |
143
|
|
|
throw $this->createAccessDeniedException('Second Factor Only feature disabled'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$logger->notice('Creating second-factor-only Response'); |
147
|
|
|
|
148
|
|
|
$selectedSecondFactorUuid = $this->getResponseContext()->getSelectedSecondFactor(); |
149
|
|
|
if (!$selectedSecondFactorUuid) { |
150
|
|
|
throw new BadRequestHttpException('Cannot verify possession of an unknown second factor.'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!$responseContext->isSecondFactorVerified()) { |
154
|
|
|
throw new BadRequestHttpException( |
155
|
|
|
'Second factor was not verified' |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$secondFactor = $this->get('gateway.service.second_factor_service') |
160
|
|
|
->findByUuid($selectedSecondFactorUuid); |
161
|
|
|
$secondFactorTypeService = $this->get('surfnet_stepup.service.second_factor_type'); |
162
|
|
|
$grantedLoa = $this->get('surfnet_stepup.service.loa_resolution') |
163
|
|
|
->getLoaByLevel($secondFactor->getLoaLevel($secondFactorTypeService)); |
164
|
|
|
|
165
|
|
|
/** @var LoaAliasLookupService $loaAliasLookup */ |
166
|
|
|
$loaAliasLookup = $this->get('second_factor_only.loa_alias_lookup'); |
167
|
|
|
$authnContextClassRef = $loaAliasLookup->findAliasByLoa($grantedLoa); |
168
|
|
|
|
169
|
|
|
/** @var ResponseFactory $response_factory */ |
170
|
|
|
$responseFactory = $this->get('second_factor_only.saml_response_factory'); |
171
|
|
|
$response = $responseFactory->createSecondFactorOnlyResponse( |
172
|
|
|
$responseContext->getIdentityNameId(), |
173
|
|
|
$responseContext->getServiceProvider(), |
174
|
|
|
$authnContextClassRef |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$responseContext->responseSent(); |
178
|
|
|
|
179
|
|
|
$logger->notice(sprintf( |
180
|
|
|
'Responding to request "%s" with newly created response "%s"', |
181
|
|
|
$responseContext->getInResponseTo(), |
182
|
|
|
$response->getId() |
183
|
|
|
)); |
184
|
|
|
|
185
|
|
|
$responseRendering = $this->get('second_factor_only.response_rendering'); |
186
|
|
|
|
187
|
|
|
$adfsHelper = $this->get('second_factor_only.adfs.response_helper'); |
188
|
|
|
if ($adfsHelper->isAdfsResponse($originalRequestId)) { |
189
|
|
|
$xmlResponse = $responseRendering->getResponseAsXML($response); |
190
|
|
|
try { |
191
|
|
|
$adfsParameters = $adfsHelper->retrieveAdfsParameters(); |
192
|
|
|
} catch (Exception $e) { |
193
|
|
|
throw new InvalidAdfsResponseException( |
194
|
|
|
sprintf('Could not process ADFS Response parameters, error: "%s"', $e->getMessage()) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$logger->notice('Sending ACS Response to ADFS plugin'); |
199
|
|
|
return $this->render( |
200
|
|
|
'@SurfnetStepupGatewaySecondFactorOnly/Adfs/consumeAssertion.html.twig', |
201
|
|
|
[ |
202
|
|
|
'acu' => $adfsParameters->getAssertionConsumerServiceUrl(), |
203
|
|
|
'samlResponse' => $xmlResponse, |
204
|
|
|
'context' => $adfsParameters->getContext(), |
205
|
|
|
'authMethod' => $adfsParameters->getAuthMethod(), |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
return $responseRendering->renderResponse($responseContext, $response); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
214
|
|
|
*/ |
215
|
|
|
public function getResponseContext() |
216
|
|
|
{ |
217
|
|
|
return $this->get('second_factor_only.response_context'); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.