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 namespace Myth\Api\Auth; |
||
| 41 | class APIAuthentication extends LocalAuthentication { |
||
| 42 | |||
| 43 | protected $logged_in = false; |
||
| 44 | |||
| 45 | protected $realm = 'WallyWorld'; |
||
| 46 | |||
| 47 | protected $email = null; |
||
| 48 | |||
| 49 | //-------------------------------------------------------------------- |
||
| 50 | |||
| 51 | public function __construct($ci=null) |
||
| 70 | |||
| 71 | //-------------------------------------------------------------------- |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Sets the realm used by the authentication. The system truly only |
||
| 75 | * supports a single realm across the entire application, but this |
||
| 76 | * allows it to be set by the controller. |
||
| 77 | * |
||
| 78 | * @param $realm |
||
| 79 | * |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function setRealm($realm) |
||
| 87 | |||
| 88 | //-------------------------------------------------------------------- |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Checks to see if someone is authorized via HTTP Basic Authentication. |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function tryBasicAuthentication() |
||
| 138 | |||
| 139 | //-------------------------------------------------------------------- |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Checks to see if someone is authorized via HTTP Digest Authentication. |
||
| 143 | * |
||
| 144 | * NOTE: This requires that a new field, 'digest_key', be added to the user's |
||
| 145 | * table and, during new user creation, or password reset, that the digest_key |
||
| 146 | * be calculated as md5({username}:{realm}:{password}) |
||
| 147 | * |
||
| 148 | * References: |
||
| 149 | * - http://www.faqs.org/rfcs/rfc2617.html |
||
| 150 | * - http://www.sitepoint.com/understanding-http-digest-access-authentication/ |
||
| 151 | */ |
||
| 152 | public function tryDigestAuthentication() |
||
| 226 | |||
| 227 | //-------------------------------------------------------------------- |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Attempts to log a user into the API via the configured 'api.auth_type' |
||
| 231 | * config variable in config/api.php. |
||
| 232 | * |
||
| 233 | * NOTE: Since this is intended for API use, it is a STATELESS implementation |
||
| 234 | * and does not support remember me functionality. |
||
| 235 | * |
||
| 236 | * This basically replaces the login() method due to the way the AuthTrait |
||
| 237 | * works. |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function viaRemember() |
||
| 285 | |||
| 286 | //-------------------------------------------------------------------- |
||
| 287 | |||
| 288 | //-------------------------------------------------------------------- |
||
| 289 | // Protected Methods |
||
| 290 | //-------------------------------------------------------------------- |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Checks the client's IP address against any IP addresses specified |
||
| 294 | * in the api config file. If any are found, the client is refused |
||
| 295 | * access immediately. |
||
| 296 | */ |
||
| 297 | View Code Duplication | public function checkIPBlacklist() |
|
| 312 | |||
| 313 | //-------------------------------------------------------------------- |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Checks the client's IP address against any IP addresses specified |
||
| 317 | * in the api config file. If the client is not accessing the site |
||
| 318 | * from one of those addresses then their access is denied. |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function checkIPWhitelist() |
|
| 337 | |||
| 338 | //-------------------------------------------------------------------- |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Handles the nitty gritty of actually logging our user into the system. |
||
| 342 | * Does NOT perform the authentication, just sets the system up so that |
||
| 343 | * it knows we're here. |
||
| 344 | * |
||
| 345 | * @param $user |
||
| 346 | */ |
||
| 347 | protected function loginUser($user) |
||
| 363 | |||
| 364 | //-------------------------------------------------------------------- |
||
| 365 | |||
| 366 | //-------------------------------------------------------------------- |
||
| 367 | // UNUSED METHOD OVERRIDES |
||
| 368 | //-------------------------------------------------------------------- |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Attempt to log a user into the system. |
||
| 372 | * |
||
| 373 | * $credentials is an array of key/value pairs needed to log the user in. |
||
| 374 | * This is often email/password, or username/password. |
||
| 375 | * |
||
| 376 | * NOTE: Since this is intended for API use, it is a STATELESS implementation |
||
| 377 | * and does not support remember me functionality. |
||
| 378 | * |
||
| 379 | * Valid credentials: |
||
| 380 | * - username |
||
| 381 | |||
| 382 | * - realm |
||
| 383 | * |
||
| 384 | * @param $credentials |
||
| 385 | * @param bool $remember |
||
| 386 | * |
||
| 387 | * @return bool|mixed|void |
||
| 388 | */ |
||
| 389 | public function login($credentials, $remember=false) |
||
| 393 | |||
| 394 | //-------------------------------------------------------------------- |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Logs a user out and removes all session information. |
||
| 398 | * |
||
| 399 | * NOTE: Since this is intended for API use, it is a STATELESS implementation |
||
| 400 | * and does not support remember me functionality. |
||
| 401 | * |
||
| 402 | * @return mixed |
||
| 403 | */ |
||
| 404 | public function logout() |
||
| 408 | |||
| 409 | //-------------------------------------------------------------------- |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Checks whether a user is logged in or not. |
||
| 413 | * |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | public function isLoggedIn() |
||
| 420 | |||
| 421 | //-------------------------------------------------------------------- |
||
| 422 | |||
| 423 | } |
||
| 424 |