popphp / pop-auth
Pop PHP 框架的 Pop Auth 组件
Requires
- php: >=8.1.0
- popphp/pop-db: ^6.5.0
- popphp/pop-http: ^5.0.2
Requires (Dev)
- phpunit/phpunit: ^10.0.0
README
概述
pop-auth
为通过不同认证源验证用户提供了适配器。这些适配器具有相同的接口,可以互换使用。可用的适配器有:
- 文件
- 数据库
- HTTP
- LDAP
pop-auth
是 Pop PHP 框架 的一个组件。
安装
使用 Composer 安装 pop-auth
。
composer require popphp/pop-auth
或者在您的 composer.json 文件中引入它
"require": {
"popphp/pop-auth" : "^4.0.0"
}
快速入门
为了验证认证尝试,创建一个指向其认证源的新的认证对象。从那里,您可以尝试使用用户名和密码调用 authenticate()
。
use Pop\Auth; $auth = new Auth\File('/path/to/.htmyauth'); if ($auth->authenticate('admin', 'password')) { // User is authenticated } else { // Handle failed authentication attempt }
如果您需要在应用中稍后引用相同的认证尝试结果,您可以调用 isAuthenticated()
var_dump($auth->isAuthenticated()); // bool
使用文件
使用文件适配器,您需要创建一个包含冒号分隔的用户名和密码列表的文件,或者更理想的是,密码散列
testuser1:PASSWORD_HASH1
testuser2:PASSWORD_HASH2
testuser3:PASSWORD_HASH3
use Pop\Auth; $auth = new Auth\File('/path/to/.htmyauth'); $auth->authenticate('testuser1', 'password'); // Return int if ($auth->isAuthenticated()) { } // Returns bool
使用数据库
使用表适配器,您需要在数据库中创建一个存储用户的表。将需要一个关联的表类,该类扩展 Pop\Db\Record
(有关更多信息,请访问 pop-db
组件)。
为了简单起见,表类被命名为 MyApp\Table\Users
,并有一个名为 username
的列和一个名为 password
的列,但那些列名可以更改。
use Pop\Auth; $auth = new Auth\Table('MyApp\Table\Users'); $auth->authenticate('admin', 'password'); // int if ($auth->isAuthenticated()) { } // bool
如果表中的用户名/密码字段名称不同,也可以更改
use Pop\Auth; $auth = new Auth\Table('MyApp\Table\Users'); $auth->setUsernameField('user_name') ->setPasswordField('password_hash'); $auth->authenticate('admin', 'password'); // int if ($auth->isAuthenticated()) { } // bool
使用 HTTP
使用 HTTP 适配器,用户可以通过 HTTP 将认证请求发送到远程服务器。它将利用 Pop\Http\Client
以及来自 pop-http
组件的支持类。以下示例将设置用户名和密码为 POST 数据的有效负载。
use Pop\Auth\Http; use Pop\Http\Client; $auth = new Http(new Client('https://www.domain.com/auth', ['method' => 'post'])); $auth->authenticate('admin', 'password'); // Returns int if ($auth->isAuthenticated()) { } // Returns bool
以下示例将使用基本认证头
use Pop\Auth\Http; use Pop\Http\Client; use Pop\Http\Auth; $client = new Client( 'https://www.domain.com/auth', ['method' => 'post'], Auth::createBasic('admin', 'password') ); $auth = new Http($client); $auth->authenticate('admin', 'password'); // Returns int if ($auth->isAuthenticated()) { } // Returns bool
以下示例将使用承载令牌认证头
use Pop\Auth\Http; use Pop\Http\Client; use Pop\Http\Auth; $client = new Client( 'https://www.domain.com/auth', ['method' => 'post'], Auth::createBearer('AUTH_TOKEN') ); $auth = new Http($client); $auth->authenticate('admin', 'password'); if ($auth->isAuthenticated()) { } // Returns true
类似于表适配器,如果需要将用户名/密码字段设置为与 HTTP 服务器的需求不同,可以这样做
use Pop\Auth\Http; use Pop\Http\Client; $auth = new Http(new Client('https://www.domain.com/auth', ['method' => 'post'])); $auth->setUsernameField('user_name') ->setPasswordField('password_hash'); $auth->authenticate('admin', 'password'); // Returns int if ($auth->isAuthenticated()) { } // Returns bool
使用 LDAP
使用 LDAP 适配器,用户可以使用 LDAP 向远程服务器发送认证请求。用户可以设置端口号和其他可能必要的与 LDAP 服务器通信的选项。
use Pop\Auth; $auth = new Auth\Ldap('ldap.domain', 389, [LDAP_OPT_PROTOCOL_VERSION => 3]); $auth->authenticate('admin', 'password'); if ($auth->isAuthenticated()) { } // Returns true
获取用户
表和 HTTP 适配器都有一种方法允许您获取可能已返回的任何用户数据。该方法为 getUser()
use Pop\Auth; $auth = new Auth\Table('MyApp\Table\Users'); $auth->authenticate('admin', 'password'); // int if ($auth->isAuthenticated()) { $user = $auth->getUser(); }
这允许您访问认证用户的资料,而无需进行额外的请求。