| Total Complexity | 41 |
| Total Lines | 327 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ResponseContext often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ResponseContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ResponseContext |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var IdentityProvider |
||
| 38 | */ |
||
| 39 | private $hostedIdentityProvider; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \Surfnet\StepupGateway\GatewayBundle\Service\SamlEntityService |
||
| 43 | */ |
||
| 44 | private $samlEntityService; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ProxyStateHandler |
||
| 48 | */ |
||
| 49 | private $stateHandler; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var LoggerInterface |
||
| 53 | */ |
||
| 54 | private $logger; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var DateTime |
||
| 58 | */ |
||
| 59 | private $generationTime; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var IdentityProvider|null |
||
| 63 | */ |
||
| 64 | private $authenticatingIdp; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ServiceProvider |
||
| 68 | */ |
||
| 69 | private $targetServiceProvider; |
||
| 70 | |||
| 71 | public function __construct( |
||
| 72 | IdentityProvider $identityProvider, |
||
| 73 | SamlEntityService $samlEntityService, |
||
| 74 | ProxyStateHandler $stateHandler, |
||
| 75 | LoggerInterface $logger, |
||
| 76 | DateTime $now = null |
||
| 77 | ) { |
||
| 78 | $this->hostedIdentityProvider = $identityProvider; |
||
| 79 | $this->samlEntityService = $samlEntityService; |
||
| 80 | $this->stateHandler = $stateHandler; |
||
| 81 | $this->logger = $logger; |
||
| 82 | $this->generationTime = is_null($now) ? new DateTime('now', new DateTimeZone('UTC')): $now; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getDestination() |
||
| 89 | { |
||
| 90 | $requestAcsUrl = $this->stateHandler->getRequestAssertionConsumerServiceUrl(); |
||
| 91 | |||
| 92 | return $this->getServiceProvider()->determineAcsLocation($requestAcsUrl, $this->logger); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getDestinationForAdfs() |
||
| 99 | { |
||
| 100 | $requestAcsUrl = $this->stateHandler->getRequestAssertionConsumerServiceUrl(); |
||
| 101 | |||
| 102 | return $this->getServiceProvider()->determineAcsLocationForAdfs($requestAcsUrl); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getIssuer(): Issuer |
||
| 106 | { |
||
| 107 | $issuer = new Issuer(); |
||
| 108 | $issuer->setValue($this->hostedIdentityProvider->getEntityId()); |
||
| 109 | return $issuer; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | public function getIssueInstant() |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return null|string |
||
| 122 | */ |
||
| 123 | public function getInResponseTo() |
||
| 124 | { |
||
| 125 | return $this->stateHandler->getRequestId(); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return null|string |
||
| 130 | */ |
||
| 131 | public function getExpectedInResponseTo() |
||
| 132 | { |
||
| 133 | return $this->stateHandler->getGatewayRequestId(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return null|string |
||
| 138 | */ |
||
| 139 | public function getRequiredLoa() |
||
| 140 | { |
||
| 141 | return $this->stateHandler->getRequiredLoaIdentifier(); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return IdentityProvider |
||
| 146 | */ |
||
| 147 | public function getIdentityProvider() |
||
| 148 | { |
||
| 149 | return $this->hostedIdentityProvider; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return null|ServiceProvider |
||
| 154 | */ |
||
| 155 | public function getServiceProvider() |
||
| 156 | { |
||
| 157 | if (isset($this->targetServiceProvider)) { |
||
| 158 | return $this->targetServiceProvider; |
||
| 159 | } |
||
| 160 | |||
| 161 | $serviceProviderId = $this->stateHandler->getRequestServiceProvider(); |
||
| 162 | |||
| 163 | return $this->targetServiceProvider = $this->samlEntityService->getServiceProvider($serviceProviderId); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return null|string |
||
| 168 | */ |
||
| 169 | public function getRelayState() |
||
| 170 | { |
||
| 171 | return $this->stateHandler->getRelayState(); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param Assertion $assertion |
||
| 176 | */ |
||
| 177 | public function saveAssertion(Assertion $assertion) |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return Assertion |
||
| 198 | */ |
||
| 199 | public function reconstituteAssertion() |
||
| 200 | { |
||
| 201 | $assertionAsXML = $this->stateHandler->getAssertion(); |
||
| 202 | $assertionDocument = new DOMDocument(); |
||
| 203 | $assertionDocument->loadXML($assertionAsXML); |
||
| 204 | |||
| 205 | return new Assertion($assertionDocument->documentElement); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return null|string |
||
| 210 | */ |
||
| 211 | public function getIdentityNameId(): string |
||
| 212 | { |
||
| 213 | return $this->stateHandler->getIdentityNameId(); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Return the lower-cased schacHomeOrganization value from the assertion. |
||
| 218 | * |
||
| 219 | * Comparisons on SHO values should always be case insensitive. Stepup |
||
| 220 | * configuration always contains SHO values lower-cased, so this getter |
||
| 221 | * can be used to compare the SHO with configured values. |
||
| 222 | * |
||
| 223 | * @see StepUpAuthenticationService::resolveHighestRequiredLoa() |
||
| 224 | * |
||
| 225 | * @return null|string |
||
| 226 | */ |
||
| 227 | public function getNormalizedSchacHomeOrganization() |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return null|IdentityProvider |
||
| 236 | */ |
||
| 237 | public function getAuthenticatingIdp() |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param SecondFactor $secondFactor |
||
| 258 | * @param bool $authenticatedWithSsoOn2faCookie Marks if the authentication was performed with the SSO cookie or |
||
| 259 | * by performing an actual Second Factor authentication. |
||
| 260 | */ |
||
| 261 | public function saveSelectedSecondFactor(SecondFactor $secondFactor, bool $authenticatedWithSsoOn2faCookie) |
||
| 262 | { |
||
| 267 | } |
||
| 268 | |||
| 269 | public function wasAuthenticatedWithSsoOn2faCookie(): bool |
||
| 270 | { |
||
| 271 | return $this->stateHandler->wasAuthenticatedUsingSsoOn2faCookie(); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return null|string |
||
| 276 | */ |
||
| 277 | public function getSelectedSecondFactor() |
||
| 278 | { |
||
| 279 | return $this->stateHandler->getSelectedSecondFactorId(); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function markSecondFactorVerified() |
||
| 283 | { |
||
| 284 | $this->stateHandler->setSecondFactorVerified(true); |
||
| 285 | } |
||
| 286 | |||
| 287 | public function finalizeAuthentication() |
||
| 288 | { |
||
| 289 | $this->stateHandler->setSelectedSecondFactorId(null); |
||
| 290 | $this->stateHandler->setAuthenticatedWithSsoOn2faCookie(null); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function isSecondFactorVerified() |
||
| 297 | { |
||
| 298 | return $this->stateHandler->getSelectedSecondFactorId() && $this->stateHandler->isSecondFactorVerified(); |
||
| 299 | } |
||
| 300 | |||
| 301 | public function getResponseAction() |
||
| 302 | { |
||
| 303 | return $this->stateHandler->getResponseAction(); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Resets some state after the response is sent |
||
| 308 | * (e.g. resets which second factor was selected and whether it was verified). |
||
| 309 | */ |
||
| 310 | public function responseSent() |
||
| 311 | { |
||
| 312 | $this->stateHandler->setSecondFactorVerified(false); |
||
| 313 | $this->stateHandler->setVerifiedBySsoOn2faCookie(false); |
||
| 314 | $this->stateHandler->setSsoOn2faCookieFingerprint(''); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Retrieve the ResponseContextServiceId from state |
||
| 319 | * |
||
| 320 | * Used to determine we are dealing with a SFO or regular authentication. Both have different ResponseContext |
||
| 321 | * instances, and it's imperative that successive consumers use the correct service. |
||
| 322 | * |
||
| 323 | * @return string|null |
||
| 324 | */ |
||
| 325 | public function getResponseContextServiceId() |
||
| 326 | { |
||
| 327 | return $this->stateHandler->getResponseContextServiceId(); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Either gets the internal-collabPersonId if present or falls back on the regular name id attribute |
||
| 332 | */ |
||
| 333 | private function resolveNameIdValue(Assertion $assertion): string |
||
| 334 | { |
||
| 335 | $attributes = $assertion->getAttributes(); |
||
| 336 | if (array_key_exists('urn:mace:surf.nl:attribute-def:internal-collabPersonId', $attributes)) { |
||
| 337 | return reset($attributes['urn:mace:surf.nl:attribute-def:internal-collabPersonId']); |
||
| 338 | } |
||
| 339 | $nameId = $assertion->getNameId(); |
||
| 340 | if ($nameId && !is_null($nameId->getValue()) && is_string($nameId->getValue())) { |
||
| 341 | return $nameId->getValue(); |
||
| 342 | } |
||
| 343 | |||
| 344 | throw new RuntimeException('Unable to resolve an identifier from internalCollabPersonId or the Subject NameId'); |
||
| 345 | } |
||
| 346 | |||
| 347 | public function isForceAuthn(): bool |
||
| 350 | } |
||
| 351 | |||
| 352 | public function markVerifiedBySsoOn2faCookie(string $fingerprint) |
||
| 356 | } |
||
| 357 | |||
| 358 | public function isVerifiedBySsoOn2faCookie(): bool |
||
| 361 | } |
||
| 362 | } |
||
| 363 |