popphp/pop-auth
Composer 安装命令:
composer require popphp/pop-auth
包简介
Pop Auth Component for Pop PHP Framework
README 文档
README
Overview
pop-auth provides adapters to authenticate users via different authentication sources.
The adapters share the same interface and are interchangeable. The available available
adapters are:
- File
- Database
- HTTP
- LDAP
pop-auth is a component of the Pop PHP Framework.
Install
Install pop-auth using Composer.
composer require popphp/pop-auth
Or, require it in your composer.json file
"require": {
"popphp/pop-auth" : "^4.0.3"
}
Quickstart
To verify an authentication attempt, create a new auth object pointed at its authentication source.
From there, you can attempt to call the authenticate() with a username and password.
use Pop\Auth; $auth = new Auth\File('/path/to/.htmyauth'); if ($auth->authenticate('admin', 'password')) { // User is authenticated } else { // Handle failed authentication attempt }
If you need to reference the same authentication attempt result at a later time in the application,
you can call isAuthenticated():
var_dump($auth->isAuthenticated()); // bool
Using a File
Using the file adapter, you would need to create we use a file containing a colon-delimited list of usernames and passwords or, preferably, password hashes:
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
Using a Database
Using the table adapter, you would need to create a table in a database that stores the users.
There would need to be a correlating table class that extends Pop\Db\Record (for more on this,
visit the pop-db component.)
For simplicity, the table class has been named MyApp\Table\Users and has a column called
username and a column called password, but those column names can be changed.
use Pop\Auth; $auth = new Auth\Table('MyApp\Table\Users'); $auth->authenticate('admin', 'password'); // int if ($auth->isAuthenticated()) { } // bool
If the username/password fields are called something different in the table, that can be changed:
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
Using HTTP
Using the HTTP adapter, the user can send an authentication request over HTTP to a remote server.
It will utilize the Pop\Http\Client and its supporting classes from the pop-http component.
The following example will set the username and password as POST data in the payload.
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
The following example will use a basic authorization header:
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
The following example will use a bearer token authorization header:
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
Like the Table adapter, if the username/password fields need to be set to something different to meet the requirements of the HTTP server, you can do that:
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
Using LDAP
Using the LDAP adapter, the user can send an authentication request using LDAP to a remote server. The user can set the port and other various options that may be necessary to communicate with the LDAP server.
use Pop\Auth; $auth = new Auth\Ldap('ldap.domain', 389, [LDAP_OPT_PROTOCOL_VERSION => 3]); $auth->authenticate('admin', 'password'); if ($auth->isAuthenticated()) { } // Returns true
Getting the User
Both the table and HTTP adapters have a method that allow you to get any possible user data that
may have been returned. That method is getUser():
use Pop\Auth; $auth = new Auth\Table('MyApp\Table\Users'); $auth->authenticate('admin', 'password'); // int if ($auth->isAuthenticated()) { $user = $auth->getUser(); }
This allows you access to the authenticated user's data without having to make an additional request.
popphp/pop-auth 适用场景与选型建议
popphp/pop-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.43k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「authorization」 「Authentication」 「pop」 「pop php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 popphp/pop-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 popphp/pop-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 popphp/pop-auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 7.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-07