旅程 / 身份验证
适用于微框架的小巧灵活的身份验证系统
Requires
- php: >=5.6.0
- league/csv: 7.1.0
- phpunit/phpunit: ^5.7
This package is not auto-updated.
Last update: 2024-09-25 15:31:43 UTC
README
原因
通常微框架需要较小的用户群,无论是用于管理设置还是限制内容访问,这个身份验证类存在就是为了让微框架作者在设置身份验证系统时不需要超过几秒钟。
用法
安装
要将身份验证添加到您的项目中,只需使用composer
composer require journey/authentication dev-master
配置
配置身份验证模块最简单的方法是在项目的引导文件中
# bootstrap.php Journey\Authentication::config([ 'users' => array( ... ) # (required) See details below ]);
在上面的例子中,这些配置选项将适用于通过运行时调用的所有身份验证实例。有几个不同的配置选项,允许极大的灵活性和易用性
用户列表
配置选项 users
允许您提供一个有效的用户列表进行身份验证。所有列表都需要每个用户三个参数 username
、password
和 level
,其中密码是一个有效的散列。列表可以通过多种灵活的方法提供
数组
提供用户列表最简单的方法是显式的数组。一个包含用户数组的顺序数组。
# bootstrap.php $users = [ [ 'username' => 'some-username', # a username 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', # md5 hash of of the password 'level' => 1 # permission level ], [ 'username' => 'another-user', 'password' => '48cccca3bab2ad18832233ee8dff1b0b', 'level' => 1 ] ]; Journey\Authentication::config([ 'users' => $users ]);
逗号分隔值
用户列表可以是一个指向.csv文件的路径。
# bootstrap.php $users = 'path/to/users.csv'; Journey\Authentication::config([ 'users' => $users ]);
# users.csv
some-username,5f4dcc3b5aa765d61d8327deb882cf99,1
another-user,48cccca3bab2ad18832233ee8dff1b0b,1
注意:由于csv文件没有键,预期它们将按照 username
、password
、level
的顺序排列。如果它们不是这样,您可以提供一个次要的配置选项 columns
,它期望一个包含三个必需键的数组,按照它们在csv中使用时的顺序。
初始化文件 (.ini)
用户列表也可以是一个简单的.ini文件。
# bootstrap.php $users = 'path/to/users.ini'; Journey\Authentication::config([ 'users' => $users ]);
# users.ini
username[] = some-username
password[] = 5f4dcc3b5aa765d61d8327deb882cf99
level[] = 1
username[] = another-user
password[] = 48cccca3bab2ad18832233ee8dff1b0b
level[] = 1
数据库
A PDOStatement 也可以提供用户列表。该语句应表示整个用户表,当然,应包含 username
、password
和 level
列。
# MyLogic.php use Journey\Authentication; use PDO; class MyLogic { public function __construct() { $db = new PDO("sqlite: /path/to/database.db"); Authentication::config([ 'users' => $db->query('SELECT * FROM users') ]); } }
Authenticatable
最健壮的选项是提供一个实现了 Authenticatable 接口 的对象。这将控制用户列表和用户查找的控制权委托给您的外部类。
# MyAuthenticator.php use Journey\Authenticatable; class MyAuthenticator implements Authenticatable { public function authenticate($username, $password) { $users = $this->getUsersHoweverIWant(); foreach ($users as $user) { if ($user['username'] == $username && $password == $password) { return $user; # returned user must be an array containing username, password, and level } } return false; } ... }
# bootstrap.php Journey\Authentication::config([ 'users' => new MyAuthenticatable() ]);
注意:当提供 Authenticatable 类而不是用户列表时,将不使用 salt
和 hash
配置属性。由您的类提供用户列表,并验证用户名和密码。
认证用户
一旦您的用户已配置,实际的身份验证就非常简单。有四种常用的方法 authenticate()
、restrict()
、isAtLeast()
和 is()
。在用户权限可以检查之前,他们必须被 authenticate
或登录
# login.php ... use Journey\Authentication; $auth = new Authentication(); if ($auth->authenticate($_POST['username'], $_POST['password'])) { echo "You're logged in!"; } else { echo "Woops. Bad username or password"; }
一旦用户被认证,浏览器会话将被设置为让他们保持登录状态。在命令行中,他们将在运行时剩余时间内保持认证状态。认证后,限制访问只需要调用 restrict()
。
要注销或取消认证,请使用: Authentication::unauthenticate();
# sensitive.php use Journey\Authentication; class MySensitiveThings { public function __construct() { Authentication::restrict(1); } }
如果 restrict()
方法失败,应用程序 将 死亡以防止进一步执行。在发出 die() 命令之前将调用配置选项 block
(默认情况下,block
包含重定向到 GET /login
)。要检查访问而不杀死应用程序,请使用 isAtLeast()
或 is()
,它们只返回布尔值。
注意:三种访问控制方法都可以从配置文件接收一个等级映射字符串,例如:Authentication::isAtLeast('editor');