Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 49 | class SamlProxyController extends Controller |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * Proxy a GSSP authentication request to the remote GSSP SSO endpoint. |
||
| 53 | * |
||
| 54 | * The user is about to be sent to the remote GSSP application. An authn |
||
| 55 | * request was created in ::sendSecondFactorVerificationAuthnRequestAction() and this method |
||
| 56 | * proxies the authn request to the remote SSO URL. The remote application |
||
| 57 | * will send an assertion back to consumeAssertionAction(). |
||
| 58 | * |
||
| 59 | * @param string $provider |
||
| 60 | * @param Request $httpRequest |
||
| 61 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 62 | */ |
||
| 63 | public function singleSignOnAction($provider, Request $httpRequest) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Start a GSSP single sign-on. |
||
| 140 | * |
||
| 141 | * The user has selected a second factor token and the token happens to be |
||
| 142 | * a GSSP token. The SecondFactorController therefor did an internal |
||
| 143 | * redirect (see SecondFactorController::verifyGssfAction) to this method. |
||
| 144 | * |
||
| 145 | * In this method, an authn request is created. This authn request is not |
||
| 146 | * sent directly to the GSSP SSO URL, but proxied trough the gateway first |
||
| 147 | * (see SamlProxyController::ssoAction). |
||
| 148 | * |
||
| 149 | * @param $provider |
||
| 150 | * @param $subjectNameId |
||
| 151 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
| 152 | */ |
||
| 153 | public function sendSecondFactorVerificationAuthnRequestAction($provider, $subjectNameId) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Process an assertion received from the remote GSSP application. |
||
| 190 | * |
||
| 191 | * The GSSP application sent an assertion back to the gateway. When |
||
| 192 | * successful, the user is sent back to the |
||
| 193 | * SecondFactorController:gssfVerifiedAction. |
||
| 194 | * |
||
| 195 | * @param string $provider |
||
| 196 | * @param Request $httpRequest |
||
| 197 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 198 | */ |
||
| 199 | public function consumeAssertionAction($provider, Request $httpRequest) |
||
| 200 | { |
||
| 201 | $provider = $this->getProvider($provider); |
||
| 202 | $stateHandler = $provider->getStateHandler(); |
||
| 203 | $originalRequestId = $stateHandler->getRequestId(); |
||
| 204 | |||
| 205 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
| 206 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 207 | |||
| 208 | $action = $stateHandler->hasSubject() ? 'Second Factor Verification' : 'Proxy Response'; |
||
| 209 | $logger->notice( |
||
| 210 | sprintf('Received SAMLResponse, attempting to process for %s', $action) |
||
| 211 | ); |
||
| 212 | |||
| 213 | try { |
||
| 214 | /** @var \SAML2\Assertion $assertion */ |
||
| 215 | $assertion = $this->get('surfnet_saml.http.post_binding')->processResponse( |
||
| 216 | $httpRequest, |
||
| 217 | $provider->getRemoteIdentityProvider(), |
||
| 218 | $provider->getServiceProvider() |
||
| 219 | ); |
||
| 220 | } catch (Exception $exception) { |
||
| 221 | $logger->error(sprintf('Could not process received Response, error: "%s"', $exception->getMessage())); |
||
| 222 | |||
| 223 | $response = $this->createResponseFailureResponse( |
||
| 224 | $provider, |
||
| 225 | $this->getDestination($provider->getStateHandler()) |
||
| 226 | ); |
||
| 227 | |||
| 228 | return $this->renderSamlResponse('consumeAssertion', $stateHandler, $response); |
||
| 229 | } |
||
| 230 | |||
| 231 | $adaptedAssertion = new AssertionAdapter($assertion); |
||
| 232 | $expectedResponse = $stateHandler->getGatewayRequestId(); |
||
| 233 | if (!$adaptedAssertion->inResponseToMatches($expectedResponse)) { |
||
| 234 | throw new UnknownInResponseToException( |
||
| 235 | $adaptedAssertion->getInResponseTo(), |
||
| 236 | $expectedResponse |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | $authenticatedNameId = $assertion->getNameId(); |
||
| 241 | $isSubjectRequested = $stateHandler->hasSubject(); |
||
| 242 | if ($isSubjectRequested && ($stateHandler->getSubject() !== $authenticatedNameId->value)) { |
||
| 243 | $logger->critical(sprintf( |
||
| 244 | 'Requested Subject NameID "%s" and Response NameID "%s" do not match', |
||
| 245 | $stateHandler->getSubject(), |
||
| 246 | $authenticatedNameId->value |
||
| 247 | )); |
||
| 248 | |||
| 249 | return $this->renderSamlResponse( |
||
| 250 | 'recoverableError', |
||
| 251 | $stateHandler, |
||
| 252 | $this->createAuthnFailedResponse( |
||
| 253 | $provider, |
||
| 254 | $this->getDestination($provider->getStateHandler()) |
||
| 255 | ) |
||
| 256 | ); |
||
| 257 | } |
||
| 258 | |||
| 259 | $logger->notice('Successfully processed SAMLResponse'); |
||
| 260 | |||
| 261 | if ($stateHandler->secondFactorVerificationRequested()) { |
||
| 262 | $logger->notice( |
||
| 263 | 'Second Factor verification was requested and was successful, forwarding to SecondFactor handling' |
||
| 264 | ); |
||
| 265 | |||
| 266 | return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:gssfVerified'); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** @var \Surfnet\StepupGateway\SamlStepupProviderBundle\Saml\ProxyResponseFactory $proxyResponseFactory */ |
||
| 270 | $targetServiceProvider = $this->getServiceProvider($stateHandler->getRequestServiceProvider()); |
||
| 271 | $proxyResponseFactory = $this->get('gssp.provider.' . $provider->getName() . '.response_proxy'); |
||
| 272 | $response = $proxyResponseFactory->createProxyResponse($assertion, $targetServiceProvider); |
||
| 273 | |||
| 274 | $logger->notice(sprintf( |
||
| 275 | 'Responding to request "%s" with response based on response from the remote IdP with response "%s"', |
||
| 276 | $stateHandler->getRequestId(), |
||
| 277 | $response->getId() |
||
| 278 | )); |
||
| 279 | |||
| 280 | return $this->renderSamlResponse('consumeAssertion', $stateHandler, $response); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $provider |
||
| 285 | * @return XMLResponse |
||
| 286 | */ |
||
| 287 | public function metadataAction($provider) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $provider |
||
| 299 | * @return \Surfnet\StepupGateway\SamlStepupProviderBundle\Provider\Provider |
||
| 300 | */ |
||
| 301 | private function getProvider($provider) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param StateHandler $stateHandler |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | private function getDestination(StateHandler $stateHandler) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $view |
||
| 334 | * @param StateHandler $stateHandler |
||
| 335 | * @param SAMLResponse $response |
||
| 336 | * @return Response |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function renderSamlResponse($view, StateHandler $stateHandler, SAMLResponse $response) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $view |
||
| 354 | * @param array $parameters |
||
| 355 | * @param Response $response |
||
| 356 | * @return Response |
||
| 357 | */ |
||
| 358 | public function render($view, array $parameters = array(), Response $response = null) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param SAMLResponse $response |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | private function getResponseAsXML(SAMLResponse $response) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Response that indicates that an error occurred in the responder (the gateway). Used to indicate that we could |
||
| 378 | * not process the response we received from the upstream GSSP |
||
| 379 | * |
||
| 380 | * @param Provider $provider |
||
| 381 | * @param string $destination |
||
| 382 | * @return SAMLResponse |
||
| 383 | */ |
||
| 384 | private function createResponseFailureResponse(Provider $provider, $destination) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Response that indicates that the authentication could not be performed correctly. In this context it means |
||
| 394 | * that the upstream GSSP did not responsd with the same NameID as we request to authenticate in the AuthnRequest |
||
| 395 | * |
||
| 396 | * @param Provider $provider |
||
| 397 | * @param string $destination |
||
| 398 | * @return SAMLResponse |
||
| 399 | */ |
||
| 400 | private function createAuthnFailedResponse(Provider $provider, $destination) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Creates a standard response with default status Code (success) |
||
| 413 | * |
||
| 414 | * @param Provider $provider |
||
| 415 | * @param string $destination |
||
| 416 | * @return SAMLResponse |
||
| 417 | */ |
||
| 418 | private function createResponse(Provider $provider, $destination) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $serviceProvider |
||
| 431 | * @return \Surfnet\SamlBundle\Entity\ServiceProvider |
||
| 432 | */ |
||
| 433 | private function getServiceProvider($serviceProvider) |
||
| 441 | } |
||
| 442 |
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.