| Conditions | 9 |
| Paths | 7 |
| Total Lines | 58 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 35 | public function executeBefore(Request $request, array $hookData) |
||
| 36 | { |
||
| 37 | // some URIs are allowed as they are used for either logging in, or |
||
| 38 | // verifying the OTP key |
||
| 39 | $allowedUris = [ |
||
| 40 | '/_form/auth/verify', |
||
| 41 | '/_form/auth/logout', |
||
| 42 | '/_two_factor/auth/verify/totp', |
||
| 43 | '/_two_factor/auth/verify/yubi', |
||
| 44 | '/_oauth/token', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | if (in_array($request->getPathInfo(), $allowedUris) && 'POST' === $request->getRequestMethod()) { |
||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!array_key_exists('auth', $hookData)) { |
||
| 52 | throw new HttpException('authentication hook did not run before', 500); |
||
| 53 | } |
||
| 54 | $userId = $hookData['auth']; |
||
| 55 | |||
| 56 | if ($this->session->has('_two_factor_verified')) { |
||
| 57 | if ($userId !== $this->session->get('_two_factor_verified')) { |
||
| 58 | throw new HttpException('two-factor code not bound to authenticated user', 400); |
||
| 59 | } |
||
| 60 | |||
| 61 | return true; |
||
| 62 | } |
||
| 63 | |||
| 64 | $hasTotpSecret = $this->serverClient->get('has_totp_secret', ['user_id' => $userId]); |
||
| 65 | $hasYubiId = $this->serverClient->get('has_yubi_key_id', ['user_id' => $userId]); |
||
| 66 | |||
| 67 | // check if the user is enrolled for 2FA, if not we are fine, for this |
||
| 68 | // session we assume we are verified! |
||
| 69 | if (!$hasTotpSecret && !$hasYubiId) { |
||
| 70 | $this->session->regenerate(true); |
||
| 71 | $this->session->set('_two_factor_verified', $userId); |
||
| 72 | |||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | // if not Yubi, then TOTP |
||
| 77 | $templateName = $hasYubiId ? 'twoFactorYubiKeyOtp' : 'twoFactorTotp'; |
||
| 78 | |||
| 79 | // any other URL, enforce 2FA |
||
| 80 | $response = new Response(200, 'text/html'); |
||
| 81 | $response->setBody( |
||
| 82 | $this->tpl->render( |
||
| 83 | $templateName, |
||
| 84 | [ |
||
| 85 | '_two_factor_auth_invalid' => false, |
||
| 86 | '_two_factor_auth_redirect_to' => $request->getUri(), |
||
| 87 | ] |
||
| 88 | ) |
||
| 89 | ); |
||
| 90 | |||
| 91 | return $response; |
||
| 92 | } |
||
| 93 | } |
||
| 94 |