function test_simple_auth() { $verifier = function ($user, $pass) { return true; }; $method = function ($callback) { return $callback('', ''); }; $this->assertTrue(simple_auth($verifier, $method)); }
<?php if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) { header("WWW-Authenticate: Basic realm=\"Secure Page\""); header("HTTP\\ 1.1 401 Unauthorized"); header("Content-Type: application/json"); echo '{ auth: 0 }'; exit; } if (simple_auth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) { // Do nothing } else { header("WWW-Authenticate: Basic realm=\"Secure Page\""); header("HTTP\\ 1.1 401 Unauthorized"); header("Content-Type: application/json"); echo '{ auth: 0 }'; exit; } /********************************************/ function simple_auth($user, $pass) { require_once '../config.php'; if (htmlentities($user) == htmlentities(API_USER) && htmlentities($pass) == htmlentities(API_PASS)) { return 1; } else { return 0; } }
function smf_updateMemberData($username, $info) { $api = new SmfRestClient(simple_auth(SMF_SEC_KEY, 'ENCODE')); $result = $api->update_memberData($username, $info); return $result->data == 'true'; }
/** * Simplifies authorization down to: * * <?php * * if (! User::require_login ()) { * // unauthorized * } * * ?> */ public static function require_login() { $class = get_called_class(); return simple_auth(array($class, 'verifier'), array($class, 'method')); }
/** * Simplifies authorization for admins down to: * * <?php * * if (! User::require_admin ()) { * // unauthorized * } * * ?> */ public static function require_admin() { if (is_object(self::$user)) { if (self::$user->session_id == $_SESSION['session_id']) { if (self::$user->type == 'admin') { return true; } return false; } } else { $class = get_called_class(); $res = simple_auth(array($class, 'verifier'), array($class, 'method')); if ($res && self::$user->type == 'admin') { return true; } } return false; }
/** * Require authentication via custom callbacks that are passed to `simple_auth()`. * If the second callback is missing, the first will be assumed to be an array * containing the two callbacks. * * See `apps/user/lib/Auth` for built-in auth handlers. */ public function require_auth($verifier, $method = false) { if ($method === false) { list($verifier, $method) = $verifier; } return simple_auth($verifier, $method); }