intervi/simpleauth

提供简单的身份验证系统和CSRF保护功能。

1.2 2019-03-12 20:05 UTC

This package is not auto-updated.

Last update: 2019-08-05 23:29:15 UTC


README

提供简单的身份验证系统和CSRF保护功能。请参阅API。

API

需要启动会话,否则方法将抛出异常。

  • __construct($salt, $hashalgo) - Salt是您的随机字符串,hashalgo是哈希算法(请参阅php文档
  • get_csrf() - 返回字符串,生成新的或获取旧的CSRF值。生成的值将保存到会话中。
  • is_valid_csrf($csrf) - 返回布尔值,检查有效的CSRF
  • is_authed() - 返回布尔值,检查是否已验证
  • get_hash($password) - 返回字符串,哈希后的密码。$password是原始字符串密码。
  • auth($password, $hash) - 返回布尔值,尝试验证。$password是原始密码(字符串),$hash是哈希后的密码(字符串)。
  • logout() - 登出

示例

use InterVi\SimpleAuth\SimpleAuth;

session_start();
$user = new User(); //you custom user

$auth = new SimpleAuth('JKhggghgFdfs33ds', 'sha256');
if ($auth->is_authed()) {
    //example CSRF checking
    $csrf = filter_input(INPUT_POST, 'csrf');
    if (!$auth->is_valid_csrf($csrf)) {
        http_response_code(401);
        die('401 Unauthorized');
    }
} else {
    //example auth
    $data = json_decode(file_get_contents('php://input'), true);
    if (!$auth->auth($data['password'])) {
        http_response_code(401);
        die('401 Unauthorized');
    }
}