zozlak/auth
Composer 安装命令:
composer require zozlak/auth
包简介
Very simple yet flexible authentication framework
关键字:
README 文档
README
A simple yet flexible library for authenticating against different providers.
Currently supported authorization providers:
- HTTP basic
- HTTP digest
- user login and data fetched from HTTP headers (e.g. when set by Shibboleth)
- Google access_token
- fixed data (e.g. a fallback guest user)
Currently supported users database backends:
- PDO
Usage
Simple example trying to authenticate with Google, then with HTTP basic and finally using a fixed
zzz user as a fallback.
namespace zozlak\auth; require '/vendor/autoload.php'; $db = new usersDb\PdoDb('sqlite::memory:'); // init users $db->putUser('aaa', authMethod\HttpBasic::pswdData('1234')); $db->putUser('bbb', authMethod\HttpBasic::pswdData('1234')); // create auth controller and add auth methods // (comment/uncomment $ctl->addMethod() lines to test different combinations) $ctl = new AuthController($db); $header = new TrustedHeader('HTTP_EPPN'); $ctl->addMethod($header); $token = new GoogleToken(filter_input(INPUT_GET, 'token') ?? ''); $ctl->addMethod($token); $shb = new Shibboleth('HTTP_EPPN', '', [], 'https://my.app/Shibboleth.sso/Login', 'https://my.app/url'); //$ctl->addMethod($shb, AuthController::ADVERTISE_ONCE); $googleAppCfg = [ 'client_id' => 'appid.apps.googleusercontent.com', 'client_secret' => 'appsecret', 'redirect_uris' => ['https://my.app/url'] ]; $googleAuthCfg = ['access_type' => 'offline', 'refresh_time' => 600]; $google = new Google(filter_input(INPUT_GET, 'token') ?? '', $googleAppCfg, $googleAuthCfg); //$ctl->addMethod($google, AuthController::ADVERTISE_ONCE); $basic = new HttpBasic('my realm'); $ctl->addMethod($basic, AuthController::ADVERTISE_ONCE); $digest = new HttpDigest('realm'); //$ctl->addMethod($digest, AuthController::ADVERTISE_ONCE); $guest = new Guest('zzz'); $ctl->addMethod($guest); // try to authenticate if ($ctl->authenticate()) { print_r([$ctl->getUserName(), $ctl->getUserData()]); } else { // if not authenticated, advertise available method $ctl->advertise(); header('HTTP/1.1 401 Unauthorized'); echo "Authentication failed\n"; }
Combining many authentication methods
Chaining many authentication methods is easy until it's only checking credentials provided by a client in his request.
The problem starts when request contains no (valid) credentials and we want to explicitely ask user to include them. The problem is in most cases we can advertise only one auth method at once. This is because different auth methods use conflicting advertisment mechanism, e.g.
- all OAuth2 (Google, etc.) and SAML (Shibboleth) methods use a
Locationheader to redirect user to a login page and we can't return many redirects to different locations in one response - presence of an HTTP Basic or HTTP Digest auth header in a response forces all GUI clients to prompt user for login and password and skip the rest of a response
Control over advertising auth methods is provided in the following way:
- You can assign each method in the chain one of three advertisment levels:
AuthMethod::ADVERTISE_NONEauth method is never advertisedAuthMethod::ADVERTISE_ONCEauth method is advertised only if a request contained no credentials for this method (and if a request contained wrong credentials for this method, the method is not advertised again)AuthMethod::ADVERTISE_ALWAYSauth method is always advertised
- When you call the
AutController::advertise()method a first auth method in the chain which fulfills its advertisment conditions is advertised.
You assigne the advertisment level when adding it to the auth chain using the second parameter
of the AutController::addMethod(AuthMethodInterface $method, int $advertise) method.
By default it's AuthMethod::ADVERTISE_NONE
Remember Guest, GoogleToken and TrustedHeaders don't support advertisment.
HTTP Digest method
HTTP Digest is difficult to combine with any other auth method. Unlike other methods the HTTP Digest has to be advertised to the client before his request so he can prepare valid credentials. And once it is advertised all GUI clients (most notably web browsers) will keep asking user for a login and password until valid ones are provided making it impossible to use any other authentication method.
(Poor) workarounds for this problem are:
- Putting HTTP Digest at the end of the auth chain allowing any other auth method to be checked first.
- Setting up HTTP Digest provider's advertise setting to
ADVERTISE_ONCE. In such a case it will be advertised only when a client doesn't provide HTTP Digest credentials in his request and if credentials are provided (no matter if they are good or wrong) the HTTP Digest method won't be advertised again. It allows to resolve auth providers staying after the HTTP Digest in the auth chain at the cost of giving user only one chance to input a correct login and password.
zozlak/auth 适用场景与选型建议
zozlak/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.05k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zozlak/auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zozlak/auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zozlak/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.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Email Toolkit Plugin for CakePHP
A list of dangerous usernames
Simple Laravel signup/login module, with simple user roles.
统计信息
- 总下载量: 8.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-19