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 |
||
50 | class StepUpAuthenticationService |
||
51 | { |
||
52 | /** |
||
53 | * @var \Surfnet\StepupBundle\Service\LoaResolutionService |
||
54 | */ |
||
55 | private $loaResolutionService; |
||
56 | |||
57 | /** |
||
58 | * @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactorRepository |
||
59 | */ |
||
60 | private $secondFactorRepository; |
||
61 | |||
62 | /** |
||
63 | * @var \Surfnet\StepupGateway\ApiBundle\Service\YubikeyService |
||
64 | */ |
||
65 | private $yubikeyService; |
||
66 | |||
67 | /** |
||
68 | * @var \Surfnet\StepupBundle\Service\SmsSecondFactorService |
||
69 | */ |
||
70 | private $smsService; |
||
71 | |||
72 | /** @var InstitutionMatchingHelper */ |
||
73 | private $institutionMatchingHelper; |
||
74 | |||
75 | /** |
||
76 | * @var \Symfony\Component\Translation\TranslatorInterface |
||
77 | */ |
||
78 | private $translator; |
||
79 | |||
80 | /** |
||
81 | * @var \Psr\Log\LoggerInterface |
||
82 | */ |
||
83 | private $logger; |
||
84 | |||
85 | /** |
||
86 | * @var SecondFactorTypeService |
||
87 | */ |
||
88 | private $secondFactorTypeService; |
||
89 | |||
90 | /** |
||
91 | * @param LoaResolutionService $loaResolutionService |
||
92 | * @param SecondFactorRepository $secondFactorRepository |
||
93 | * @param YubikeyService $yubikeyService |
||
94 | * @param SmsSecondFactorService $smsService |
||
95 | * @param InstitutionMatchingHelper $institutionMatchingHelper |
||
96 | * @param TranslatorInterface $translator |
||
97 | * @param LoggerInterface $logger |
||
98 | * @param SecondFactorTypeService $secondFactorTypeService |
||
99 | */ |
||
100 | public function __construct( |
||
119 | |||
120 | /** |
||
121 | * @param string $identityNameId |
||
122 | * @param Loa $requiredLoa |
||
123 | * @param WhitelistService $whitelistService |
||
124 | * @return \Doctrine\Common\Collections\Collection |
||
125 | */ |
||
126 | public function determineViableSecondFactors( |
||
161 | |||
162 | /** |
||
163 | * Retrieves the required LoA for the authenticating user |
||
164 | * |
||
165 | * @see StepUpAuthenticationServiceTest::test_resolve_highest_required_loa_conbinations |
||
166 | * The possible flows through the method are tested in this test case. Any additional possible outcomes should |
||
167 | * be covered in this test. |
||
168 | * |
||
169 | * @see https://github.com/OpenConext/Stepup-Deploy/wiki/Institution-Specific-LoA |
||
170 | * The flow of the LoA determination is described in detail in this document. The parameters of this method |
||
171 | * match the inputs described in the flow diagram. |
||
172 | * |
||
173 | * @param string $requestedLoa <SP-LoA> Optional. The value of the AuthnConextClassRef attribute in the |
||
174 | * AuthnRequest from the SP. |
||
175 | * |
||
176 | * Example: 'https://example.com/authentication/loa1' |
||
177 | * |
||
178 | * @param array $spConfiguredLoas <LoAs> Optional. An associative array mapping schacHomeOrganization to LoA. |
||
179 | * This array is configured on the gateway for each SP. All keys in the |
||
180 | * spConfiguredLoas array should be normalized (lower cased). |
||
181 | * |
||
182 | * Example: |
||
183 | * [ |
||
184 | * '__default__' => 'https://example.com/authentication/loa1', |
||
185 | * 'institution_a' => 'https://example.com/authentication/loa2', |
||
186 | * 'institution_b' => 'https://example.com/authentication/loa3', |
||
187 | * ] |
||
188 | * |
||
189 | * @param string $idpSho <IdP-SHO> Optional. Value of the schacHomeOrganization attribute from the |
||
190 | * Assertion from the IdP. The SHO should be normalized (lower cased) as |
||
191 | * it will be used to be compared against the $spConfiguredLoas who have |
||
192 | * also been normalized. |
||
193 | * |
||
194 | * Example: 'institution_a', '' |
||
195 | * |
||
196 | * @param string $userSho <User-SHO> Optional. The schacHomeOrganization that the user belongs to, this is |
||
197 | * the schacHomeOrganization that was provided during registration of the |
||
198 | * token. The SHO should be normalized (lower cased) as it will be used |
||
199 | * to be compared against the $spConfiguredLoas who have also been |
||
200 | * normalized. |
||
201 | * |
||
202 | * Example: 'institution_b', '' |
||
203 | * |
||
204 | * @return Loa |
||
205 | * |
||
206 | * @throws UnknownInstitutionException Raised when neither <User-SHO> or <IdP-SHO> is provided but <LoAs> |
||
207 | * are configured for institutions other than __default__. |
||
208 | * |
||
209 | * @throws InstitutionMismatchException <User-SHO> or <IdP-SHO> are configured and <LoAs> are provided for an |
||
210 | * institution other than __default__ but the <User-SHO> and <IdP-SHO> do |
||
211 | * not match. |
||
212 | * |
||
213 | * @throws LoaCannotBeGivenException Raised when no LoA candidates are found or when none of the candidate |
||
214 | * LoAs are valid (known to the application). |
||
215 | */ |
||
216 | public function resolveHighestRequiredLoa( |
||
302 | |||
303 | /** |
||
304 | * Test if the spConfiguredLoas has institution specific LoA configurations other than the |
||
305 | * default LoA configuration. |
||
306 | * |
||
307 | * @param array $spConfiguredLoas |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | private function hasNonDefaultSpConfiguredLoas(array $spConfiguredLoas) |
||
316 | |||
317 | /** |
||
318 | * Returns whether the given Loa identifier identifies the minimum Loa, intrinsic to being authenticated via an IdP. |
||
319 | * |
||
320 | * @param Loa $loa |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function isIntrinsicLoa(Loa $loa) |
||
327 | |||
328 | /** |
||
329 | * @param VerifyYubikeyOtpCommand $command |
||
330 | * @return YubikeyOtpVerificationResult |
||
331 | */ |
||
332 | public function verifyYubikeyOtp(VerifyYubikeyOtpCommand $command) |
||
362 | |||
363 | /** |
||
364 | * @param string $secondFactorId |
||
365 | * @return string |
||
366 | */ |
||
367 | public function getSecondFactorIdentifier($secondFactorId) |
||
374 | |||
375 | /** |
||
376 | * @return int |
||
377 | */ |
||
378 | public function getSmsOtpRequestsRemainingCount() |
||
382 | |||
383 | /** |
||
384 | * @return int |
||
385 | */ |
||
386 | public function getSmsMaximumOtpRequestsCount() |
||
390 | |||
391 | /** |
||
392 | * @param SendSmsChallengeCommand $command |
||
393 | * @return bool |
||
394 | */ |
||
395 | public function sendSmsChallenge(SendSmsChallengeCommand $command) |
||
410 | |||
411 | /** |
||
412 | * @param VerifyPossessionOfPhoneCommand $command |
||
413 | * @return OtpVerification |
||
414 | */ |
||
415 | public function verifySmsChallenge(VerifyPossessionOfPhoneCommand $command) |
||
419 | |||
420 | public function clearSmsVerificationState() |
||
424 | |||
425 | /** |
||
426 | * Get the schacHomeOrganisation of the authenticating user based on its vetted tokens. |
||
427 | * |
||
428 | * @param string $identityNameId Used to load vetted tokens |
||
429 | * @return string either the SHO or an empty string |
||
430 | */ |
||
431 | public function getUserShoByIdentityNameId($identityNameId) |
||
435 | |||
436 | /** |
||
437 | * In Stepup, the schacHomeOrganization must be in a lower case format. Please note that empty string values are |
||
438 | * considered valid SHO in this method. |
||
439 | * |
||
440 | * @param string $schacHomeOrganization |
||
441 | * @throws InvalidStepupShoFormatException Raised when the SHO is not compatible with the Stepup standard (lower |
||
442 | * cased string) |
||
443 | */ |
||
444 | public function assertValidShoFormat($schacHomeOrganization) |
||
455 | } |
||
456 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.