Total Complexity | 40 |
Total Lines | 318 |
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() |
||
238 | { |
||
239 | $entityId = $this->stateHandler->getAuthenticatingIdp(); |
||
240 | |||
241 | if (!$entityId) { |
||
242 | return null; |
||
243 | } |
||
244 | |||
245 | if (isset($this->authenticatingIdp)) { |
||
246 | return $this->authenticatingIdp; |
||
247 | } |
||
248 | |||
249 | $this->authenticatingIdp = $this->samlEntityService->hasIdentityProvider($entityId) |
||
250 | ? $this->samlEntityService->getIdentityProvider($entityId) |
||
251 | : null; |
||
252 | |||
253 | return $this->authenticatingIdp; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param SecondFactor $secondFactor |
||
258 | */ |
||
259 | public function saveSelectedSecondFactor(SecondFactor $secondFactor) |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @return null|string |
||
268 | */ |
||
269 | public function getSelectedSecondFactor() |
||
270 | { |
||
271 | return $this->stateHandler->getSelectedSecondFactorId(); |
||
272 | } |
||
273 | |||
274 | public function markSecondFactorVerified() |
||
275 | { |
||
276 | $this->stateHandler->setSecondFactorVerified(true); |
||
277 | } |
||
278 | |||
279 | public function finalizeAuthentication() |
||
280 | { |
||
281 | $this->stateHandler->setSelectedSecondFactorId(null); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function isSecondFactorVerified() |
||
288 | { |
||
289 | return $this->stateHandler->getSelectedSecondFactorId() && $this->stateHandler->isSecondFactorVerified(); |
||
290 | } |
||
291 | |||
292 | public function getResponseAction() |
||
293 | { |
||
294 | return $this->stateHandler->getResponseAction(); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Resets some state after the response is sent |
||
299 | * (e.g. resets which second factor was selected and whether it was verified). |
||
300 | */ |
||
301 | public function responseSent() |
||
302 | { |
||
303 | $this->stateHandler->setSecondFactorVerified(false); |
||
304 | $this->stateHandler->setVerifiedBySsoOn2faCookie(false); |
||
305 | $this->stateHandler->setSsoOn2faCookieFingerprint(''); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Retrieve the ResponseContextServiceId from state |
||
310 | * |
||
311 | * Used to determine we are dealing with a SFO or regular authentication. Both have different ResponseContext |
||
312 | * instances, and it's imperative that successive consumers use the correct service. |
||
313 | * |
||
314 | * @return string|null |
||
315 | */ |
||
316 | public function getResponseContextServiceId() |
||
317 | { |
||
318 | return $this->stateHandler->getResponseContextServiceId(); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Either gets the internal-collabPersonId if present or falls back on the regular name id attribute |
||
323 | */ |
||
324 | private function resolveNameIdValue(Assertion $assertion): string |
||
325 | { |
||
326 | $attributes = $assertion->getAttributes(); |
||
327 | if (array_key_exists('urn:mace:surf.nl:attribute-def:internal-collabPersonId', $attributes)) { |
||
328 | return reset($attributes['urn:mace:surf.nl:attribute-def:internal-collabPersonId']); |
||
329 | } |
||
330 | $nameId = $assertion->getNameId(); |
||
331 | if ($nameId && !is_null($nameId->getValue()) && is_string($nameId->getValue())) { |
||
332 | return $nameId->getValue(); |
||
333 | } |
||
334 | |||
335 | throw new RuntimeException('Unable to resolve an identifier from internalCollabPersonId or the Subject NameId'); |
||
336 | } |
||
337 | |||
338 | public function isForceAuthn(): bool |
||
341 | } |
||
342 | |||
343 | public function markVerifiedBySsoOn2faCookie(string $fingerprint) |
||
344 | { |
||
345 | $this->stateHandler->setVerifiedBySsoOn2faCookie(true); |
||
347 | } |
||
348 | |||
349 | public function isVerifiedBySsoOn2faCookie(): bool |
||
352 | } |
||
353 | } |
||
354 |