Complex classes like Authenticator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Authenticator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Authenticator |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Constants. |
||
| 20 | */ |
||
| 21 | const CONFIG_PACKAGE_NAME = 'google2fa'; |
||
| 22 | |||
| 23 | const SESSION_AUTH_PASSED = 'auth_passed'; |
||
| 24 | |||
| 25 | const SESSION_AUTH_TIME = 'auth_time'; |
||
| 26 | |||
| 27 | const SESSION_OTP_TIMESTAMP = 'otp_timestamp'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The auth instance. |
||
| 31 | * |
||
| 32 | * @var |
||
| 33 | */ |
||
| 34 | protected $auth; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The request instance. |
||
| 38 | * |
||
| 39 | * @var |
||
| 40 | */ |
||
| 41 | protected $request; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The current password. |
||
| 45 | * |
||
| 46 | * @var |
||
| 47 | */ |
||
| 48 | protected $password; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Authenticator constructor. |
||
| 52 | * |
||
| 53 | * @param Request $request |
||
| 54 | */ |
||
| 55 | public function __construct(Request $request) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Authenticator boot. |
||
| 62 | * |
||
| 63 | * @param $request |
||
| 64 | * |
||
| 65 | * @return Authenticator |
||
| 66 | */ |
||
| 67 | public function boot($request) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Check if it is already logged in or passable without checking for an OTP. |
||
| 74 | * |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | protected function canPassWithoutCheckingOTP() |
||
| 78 | { |
||
| 79 | return |
||
| 80 | !$this->isEnabled() || |
||
| 81 | $this->noUserIsAuthenticated() || |
||
| 82 | $this->twoFactorAuthStillValid(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get a config value. |
||
| 87 | * |
||
| 88 | * @param $string |
||
| 89 | * @param array $children |
||
| 90 | * |
||
| 91 | * @throws \Exception |
||
| 92 | * |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | protected function config($string, $children = []) |
||
| 96 | { |
||
| 97 | if (is_null(config(static::CONFIG_PACKAGE_NAME))) { |
||
| 98 | throw new \Exception('Config not found'); |
||
| 99 | } |
||
| 100 | |||
| 101 | return config( |
||
| 102 | implode('.', array_merge([static::CONFIG_PACKAGE_NAME, $string], (array) $children)) |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Create an error bag and store a message on int. |
||
| 108 | * |
||
| 109 | * @param $message |
||
| 110 | * |
||
| 111 | * @return MessageBag |
||
| 112 | */ |
||
| 113 | protected function createErrorBagForMessage($message) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get or make an auth instance. |
||
| 122 | * |
||
| 123 | * @return \Illuminate\Foundation\Application|mixed |
||
| 124 | */ |
||
| 125 | protected function getAuth() |
||
| 126 | { |
||
| 127 | if (is_null($this->auth)) { |
||
| 128 | $this->auth = app($this->config('auth')); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $this->auth; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get a message bag with a message for a particular status code. |
||
| 136 | * |
||
| 137 | * @param $statusCode |
||
| 138 | * |
||
| 139 | * @return MessageBag |
||
| 140 | */ |
||
| 141 | protected function getErrorBagForStatusCode($statusCode) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the user Google2FA secret. |
||
| 156 | * |
||
| 157 | * @throws InvalidSecretKey |
||
| 158 | * |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | protected function getGoogle2FASecretKey() |
||
| 162 | { |
||
| 163 | $secret = $this->getUser()->{$this->config('otp_secret_column')}; |
||
| 164 | |||
| 165 | if (is_null($secret) || empty($secret)) { |
||
| 166 | throw new InvalidSecretKey('Secret key cannot be empty.'); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $secret; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the previous OTP. |
||
| 174 | * |
||
| 175 | * @return null|void |
||
| 176 | */ |
||
| 177 | protected function getOldOneTimePassword() |
||
| 178 | { |
||
| 179 | $oldPassword = $this->config('forbid_old_passwords') === true |
||
| 180 | ? $this->sessionGet(self::SESSION_OTP_TIMESTAMP) |
||
| 181 | : null; |
||
| 182 | |||
| 183 | return $oldPassword; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the OTP from user input. |
||
| 188 | * |
||
| 189 | * @throws InvalidOneTimePassword |
||
| 190 | * |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | protected function getOneTimePassword() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the request instance. |
||
| 210 | * |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public function getRequest() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the OTP view. |
||
| 220 | * |
||
| 221 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 222 | */ |
||
| 223 | private function getView() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Keep this OTP session alive. |
||
| 230 | */ |
||
| 231 | protected function keepAlive() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Make a session var name for. |
||
| 240 | * |
||
| 241 | * @param null $name |
||
| 242 | * |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | protected function makeSessionVarName($name = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Check if the request input has the OTP. |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | protected function inputHasOneTimePassword() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Make a JSON response. |
||
| 262 | * |
||
| 263 | * @param $statusCode |
||
| 264 | * |
||
| 265 | * @return JsonResponse |
||
| 266 | */ |
||
| 267 | protected function makeJsonResponse($statusCode) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Make the status code, to respond accordingly. |
||
| 277 | * |
||
| 278 | * @return int |
||
| 279 | */ |
||
| 280 | protected function makeStatusCode() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Make a web response. |
||
| 290 | * |
||
| 291 | * @param $statusCode |
||
| 292 | * |
||
| 293 | * @return \Illuminate\Http\Response |
||
| 294 | */ |
||
| 295 | protected function makeHtmlResponse($statusCode) |
||
| 303 | |||
| 304 | protected function minutesSinceLastActivity() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | protected function noUserIsAuthenticated() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Check if OTP has expired. |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | protected function passwordExpired() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get a session var value. |
||
| 339 | * |
||
| 340 | * @param null $var |
||
| 341 | * |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | public function sessionGet($var = null) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Put a var value to the current session. |
||
| 353 | * |
||
| 354 | * @param $var |
||
| 355 | * @param $value |
||
| 356 | * |
||
| 357 | * @return mixed |
||
| 358 | */ |
||
| 359 | protected function sessionPut($var, $value) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Forget a session var. |
||
| 371 | * |
||
| 372 | * @param null $var |
||
| 373 | */ |
||
| 374 | protected function sessionForget($var = null) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Set the request property. |
||
| 383 | * |
||
| 384 | * @param mixed $request |
||
| 385 | * |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | public function setRequest($request) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Set current auth as valid. |
||
| 397 | */ |
||
| 398 | protected function storeAuthPassed() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Store the old OTP. |
||
| 407 | * |
||
| 408 | * @param $key |
||
| 409 | * |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | protected function storeOldOneTimePassord($key) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Verifies, in the current session, if a 2fa check has already passed. |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | protected function twoFactorAuthStillValid() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Get the current user. |
||
| 431 | * |
||
| 432 | * @return mixed |
||
| 433 | */ |
||
| 434 | protected function getUser() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Check if the current use is authenticated via OTP. |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | public function isAuthenticated() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Check if the module is enabled. |
||
| 454 | * |
||
| 455 | * @return mixed |
||
| 456 | */ |
||
| 457 | protected function isEnabled() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Check if the input OTP is valid. |
||
| 464 | * |
||
| 465 | * @return bool |
||
| 466 | */ |
||
| 467 | protected function checkOTP() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * OTP logout. |
||
| 482 | */ |
||
| 483 | public function logout() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Create a response to request the OTP. |
||
| 490 | * |
||
| 491 | * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse |
||
| 492 | */ |
||
| 493 | public function makeRequestOneTimePasswordResponse() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Update the current auth time. |
||
| 505 | */ |
||
| 506 | protected function updateCurrentAuthTime() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Verify the OTP. |
||
| 513 | * |
||
| 514 | * @return mixed |
||
| 515 | */ |
||
| 516 | protected function verifyGoogle2FA() |
||
| 528 | } |
||
| 529 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: