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 |
||
| 39 | class GatewayController extends Controller |
||
| 40 | { |
||
| 41 | const RESPONSE_CONTEXT_SERVICE_ID = 'gateway.proxy.response_context'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Receive an AuthnRequest from a service provider. |
||
| 45 | * |
||
| 46 | * The service provider is either a Stepup component (SelfService, RA) or |
||
| 47 | * an external service provider. |
||
| 48 | * |
||
| 49 | * This single sign-on action will start a new SAML request to the remote |
||
| 50 | * IDP configured in Stepup (most likely to be an instance of OpenConext |
||
| 51 | * EngineBlock). |
||
| 52 | * |
||
| 53 | * @param Request $httpRequest |
||
| 54 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response |
||
| 55 | */ |
||
| 56 | public function ssoAction(Request $httpRequest) |
||
| 57 | { |
||
| 58 | /** @var \Psr\Log\LoggerInterface $logger */ |
||
| 59 | $logger = $this->get('logger'); |
||
| 60 | |||
| 61 | $redirectBinding = $this->get('surfnet_saml.http.redirect_binding'); |
||
| 62 | $gatewayLoginService = $this->getGatewayLoginService(); |
||
| 63 | |||
| 64 | $logger->notice('Received AuthnRequest, started processing'); |
||
| 65 | |||
| 66 | /** @var \Surfnet\SamlBundle\Http\RedirectBinding $redirectBinding */ |
||
| 67 | $originalRequest = $redirectBinding->receiveSignedAuthnRequestFrom($httpRequest); |
||
| 68 | |||
| 69 | try { |
||
| 70 | $proxyRequest = $gatewayLoginService->singleSignOn($httpRequest, $originalRequest); |
||
| 71 | } catch (RequesterFailureException $e) { |
||
| 72 | $response = $this->getGatewayFailedResponseService()->createRequesterFailureResponse($this->getResponseContext()); |
||
|
|
|||
| 73 | |||
| 74 | return $this->renderSamlResponse('consumeAssertion', $response); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $redirectBinding->createResponseFor($proxyRequest); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * |
||
| 82 | */ |
||
| 83 | public function proxySsoAction() |
||
| 84 | { |
||
| 85 | throw new HttpException(418, 'Not Yet Implemented'); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Receive an AuthnResponse from an identity provider. |
||
| 90 | * |
||
| 91 | * The AuthnRequest started in ssoAction() resulted in an AuthnResponse |
||
| 92 | * from the IDP. This method handles the assertion and forwards the user |
||
| 93 | * using an internal redirect to the SecondFactorController to start the |
||
| 94 | * actual second factor verification. |
||
| 95 | * |
||
| 96 | * @param Request $request |
||
| 97 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 98 | */ |
||
| 99 | public function consumeAssertionAction(Request $request) |
||
| 100 | { |
||
| 101 | $responseContext = $this->getResponseContext(); |
||
| 102 | $gatewayLoginService = $this->getGatewayConsumeAssertionService(); |
||
| 103 | |||
| 104 | try { |
||
| 105 | $gatewayLoginService->consumeAssertion($request, $responseContext); |
||
| 106 | } catch (ResponseFailureException $e) { |
||
| 107 | $response = $this->getGatewayFailedResponseService()->createResponseFailureResponse($responseContext); |
||
| 108 | |||
| 109 | return $this->renderSamlResponse('unprocessableResponse', $response); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $this->forward('SurfnetStepupGatewayGatewayBundle:SecondFactor:selectSecondFactorForVerification'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Send a SAML response back to the service provider. |
||
| 117 | * |
||
| 118 | * Second factor verification handled by SecondFactorController is |
||
| 119 | * finished. The user was forwarded back to this action with an internal |
||
| 120 | * redirect. This method sends a AuthnResponse back to the service |
||
| 121 | * provider in response to the AuthnRequest received in ssoAction(). |
||
| 122 | */ |
||
| 123 | public function respondAction() |
||
| 124 | { |
||
| 125 | $responseContext = $this->getResponseContext(); |
||
| 126 | $gatewayLoginService = $this->getGatewayRespondService(); |
||
| 127 | |||
| 128 | $response = $gatewayLoginService->respond($responseContext); |
||
| 129 | $gatewayLoginService->respondReset($responseContext); |
||
| 130 | |||
| 131 | return $this->renderSamlResponse('consumeAssertion', $response); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return Response |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function sendLoaCannotBeGivenAction() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return Response |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function sendAuthenticationCancelledByUserAction() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $view |
||
| 162 | * @param SAMLResponse $response |
||
| 163 | * @return Response |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function renderSamlResponse($view, SAMLResponse $response) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $view |
||
| 178 | * @param array $parameters |
||
| 179 | * @param Response $response |
||
| 180 | * @return Response |
||
| 181 | */ |
||
| 182 | public function render($view, array $parameters = array(), Response $response = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return \Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext |
||
| 193 | */ |
||
| 194 | public function getResponseContext() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param SAMLResponse $response |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | private function getResponseAsXML(SAMLResponse $response) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return LoginService |
||
| 218 | */ |
||
| 219 | private function getGatewayLoginService() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return ConsumeAssertionService |
||
| 226 | */ |
||
| 227 | private function getGatewayConsumeAssertionService() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return RespondService |
||
| 234 | */ |
||
| 235 | private function getGatewayRespondService() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return FailedResponseService |
||
| 242 | */ |
||
| 243 | private function getGatewayFailedResponseService() |
||
| 247 | } |
||
| 248 |
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.