1 | <?php |
||
32 | class SecondFactorOnlyController extends Controller |
||
33 | { |
||
34 | const RESPONSE_CONTEXT_SERVICE_ID = 'second_factor_only.response_context'; |
||
35 | |||
36 | /** |
||
37 | * @param Request $httpRequest |
||
38 | * @return Response |
||
39 | */ |
||
40 | public function ssoAction(Request $httpRequest) |
||
41 | { |
||
42 | $logger = $this->get('logger'); |
||
43 | |||
44 | if (!$this->getParameter('second_factor_only')) { |
||
45 | $logger->notice(sprintf( |
||
46 | 'Access to %s denied, second_factor_only parameter set to false.', |
||
47 | __METHOD__ |
||
48 | )); |
||
49 | throw $this->createAccessDeniedException('Second Factor Only feature disabled'); |
||
50 | } |
||
51 | |||
52 | $logger->notice( |
||
53 | 'Received AuthnRequest on second-factor-only endpoint, started processing' |
||
54 | ); |
||
55 | |||
56 | /** @var \Surfnet\SamlBundle\Http\RedirectBinding $redirectBinding */ |
||
57 | $redirectBinding = $this->get('second_factor_only.http.redirect_binding'); |
||
58 | |||
59 | try { |
||
60 | $originalRequest = $redirectBinding->processSignedRequest($httpRequest); |
||
61 | } catch (Exception $e) { |
||
62 | $logger->critical(sprintf('Could not process Request, error: "%s"', $e->getMessage())); |
||
63 | |||
64 | return $this->render( |
||
65 | 'SurfnetStepupGatewayGatewayBundle:Gateway:unrecoverableError.html.twig' |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | $originalRequestId = $originalRequest->getRequestId(); |
||
70 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
71 | $logger->notice(sprintf( |
||
72 | 'AuthnRequest processing complete, received AuthnRequest from "%s", request ID: "%s"', |
||
73 | $originalRequest->getServiceProvider(), |
||
74 | $originalRequest->getRequestId() |
||
75 | )); |
||
76 | |||
77 | $stateHandler = $this->get('gateway.proxy.state_handler'); |
||
78 | |||
79 | $stateHandler |
||
80 | ->setRequestId($originalRequestId) |
||
81 | ->setRequestServiceProvider($originalRequest->getServiceProvider()) |
||
82 | ->setRelayState($httpRequest->get(AuthnRequest::PARAMETER_RELAY_STATE, '')) |
||
83 | ->setResponseAction('SurfnetStepupGatewaySecondFactorOnlyBundle:SecondFactorOnly:respond') |
||
84 | ->setResponseContextServiceId(static::RESPONSE_CONTEXT_SERVICE_ID); |
||
85 | |||
86 | // Check if the NameID is provided and we may use it. |
||
87 | $nameId = $originalRequest->getNameId(); |
||
88 | if (!$this->isNameIdAllowedToUseSecondFactorOnly($originalRequest->getServiceProvider(), $nameId, $logger)) { |
||
89 | /** @var \Surfnet\StepupGateway\GatewayBundle\Service\ResponseRenderingService $responseRendering */ |
||
90 | $responseRendering = $this->get('second_factor_only.response_rendering'); |
||
91 | return $responseRendering->renderRequesterFailureResponse( |
||
92 | $this->getResponseContext() |
||
93 | ); |
||
94 | } |
||
95 | $stateHandler->saveIdentityNameId($nameId); |
||
96 | |||
97 | // Check if the requested Loa is provided and supported. |
||
98 | $authnContextClassRef = $originalRequest->getAuthenticationContextClassRef(); |
||
99 | $loaId = $this->verifyAuthnContextClassRef($authnContextClassRef, $logger); |
||
100 | if (!$loaId) { |
||
101 | /** @var \Surfnet\StepupGateway\GatewayBundle\Service\ResponseRenderingService $responseRendering */ |
||
102 | $responseRendering = $this->get('second_factor_only.response_rendering'); |
||
103 | return $responseRendering->renderRequesterFailureResponse( |
||
104 | $this->getResponseContext() |
||
105 | ); |
||
106 | } |
||
107 | $stateHandler->setRequiredLoaIdentifier($loaId); |
||
108 | |||
109 | $logger->notice( |
||
110 | 'Forwarding to second factor controller for loa determination and handling' |
||
111 | ); |
||
112 | return $this->forward( |
||
113 | 'SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification' |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $authnContextClassRef |
||
119 | * @param LoggerInterface $logger |
||
120 | * @return string |
||
|
|||
121 | */ |
||
122 | private function verifyAuthnContextClassRef( |
||
159 | |||
160 | /** |
||
161 | * @param string $spEntityId |
||
162 | * @param string $nameId |
||
163 | * @param LoggerInterface $logger |
||
164 | * @return bool |
||
165 | */ |
||
166 | private function isNameIdAllowedToUseSecondFactorOnly($spEntityId, $nameId, LoggerInterface $logger) |
||
167 | { |
||
168 | if (!$nameId) { |
||
169 | $logger->info( |
||
170 | 'No NameID provided, sending response with status Requester Error' |
||
171 | ); |
||
172 | return false; |
||
173 | } |
||
174 | |||
175 | $entityService = $this->get('second_factor_only.entity_service'); |
||
176 | $serviceProvider = $entityService->getServiceProvider($spEntityId); |
||
177 | |||
178 | if (!$serviceProvider->isAllowedToUseSecondFactorOnlyFor($nameId)) { |
||
179 | $logger->info( |
||
180 | 'No NameID provided, sending response with status Requester Error' |
||
181 | ); |
||
182 | return false; |
||
183 | } |
||
184 | |||
185 | return true; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return Response |
||
190 | */ |
||
191 | public function respondAction() |
||
192 | { |
||
193 | $responseContext = $this->getResponseContext(); |
||
194 | $originalRequestId = $responseContext->getInResponseTo(); |
||
195 | |||
196 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
197 | |||
198 | if (!$this->getParameter('second_factor_only')) { |
||
199 | $logger->notice(sprintf( |
||
200 | 'Access to %s denied, second_factor_only parameter set to false.', |
||
201 | __METHOD__ |
||
202 | )); |
||
203 | throw $this->createAccessDeniedException('Second Factor Only feature disabled'); |
||
204 | } |
||
205 | |||
206 | $logger->notice('Creating second-factor-only Response'); |
||
207 | |||
208 | $selectedSecondFactorUuid = $this->getResponseContext()->getSelectedSecondFactor(); |
||
209 | if (!$selectedSecondFactorUuid) { |
||
210 | $logger->error( |
||
211 | 'Cannot verify possession of an unknown second factor' |
||
212 | ); |
||
213 | |||
214 | throw new BadRequestHttpException('Cannot verify possession of an unknown second factor.'); |
||
215 | } |
||
216 | |||
217 | if (!$responseContext->isSecondFactorVerified()) { |
||
218 | $logger->error('Second factor was not verified'); |
||
219 | throw new BadRequestHttpException( |
||
220 | 'Cannot verify possession of an unknown second factor.' |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | $secondFactor = $this->get('gateway.service.second_factor_service') |
||
225 | ->findByUuid($selectedSecondFactorUuid); |
||
226 | |||
227 | $grantedLoa = $this->get('surfnet_stepup.service.loa_resolution') |
||
228 | ->getLoaByLevel($secondFactor->getLoaLevel()); |
||
229 | |||
230 | /** @var LoaAliasLookupService $loaAliasLookup */ |
||
231 | $loaAliasLookup = $this->get('second_factor_only.loa_alias_lookup'); |
||
232 | $authnContextClassRef = $loaAliasLookup->findAliasByLoa($grantedLoa); |
||
233 | |||
234 | /** @var ResponseFactory $response_factory */ |
||
235 | $responseFactory = $this->get('second_factor_only.saml_response_factory'); |
||
236 | $response = $responseFactory->createSecondFactorOnlyResponse( |
||
237 | $responseContext->getIdentityNameId(), |
||
238 | $responseContext->getServiceProvider(), |
||
239 | $authnContextClassRef |
||
240 | ); |
||
241 | |||
242 | $responseContext->responseSent(); |
||
243 | |||
244 | $logger->notice(sprintf( |
||
245 | 'Responding to request "%s" with newly created response "%s"', |
||
246 | $responseContext->getInResponseTo(), |
||
247 | $response->getId() |
||
248 | )); |
||
249 | |||
250 | $responseRendering = $this->get('second_factor_only.response_rendering'); |
||
251 | return $responseRendering->renderResponse($responseContext, $response); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @return \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
||
256 | */ |
||
257 | public function getResponseContext() |
||
261 | } |
||
262 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.