gobline/auth
Composer 安装命令:
composer require gobline/auth
包简介
Authentication component
README 文档
README
Most oftenly, your server needs a way to identify the user making the current request. The authentication component provides a means for representing the user making the request and for authenticating this user against a DB or other datasource.
- the
CurrentUserInterfaceallows the server to identify the user making the current request. - the
AuthenticatorInterfaceallows the server to authenticate the user with provided credentials.
Usage
In order to authenticate a user, you will first need to create a CurrentUser object.
$user = new Gobline\Auth\CurrentUser();
Once we have a user, we can authenticate the latter using an adapter implementing AuthenticatorInterface.
One implementation is already provided, authenticating the user against a database: DbAuthenticator.
Firstly, we will have to set up the adapter. The DbAuthenticator requires a
PDO instance and
an instance of TableMetadata wrapping some metadata about the DB table we are authenticating against.
use Gobline\Auth\Authenticator\Db\TableMetadata; use Gobline\Auth\Authenticator\Db\DbAuthenticator; $authenticator = new DbAuthenticator($pdo, new TableMetadata('users'));
By default, TableMetadata assumes that
- the column
idcontains the id of the user - the column
emailcontains the login of the user - the column
passwordcontains the password of the user - the password encryption is bcrypt (possible values are: bcrypt, md5, sha1 or clear)
Customize these metadata for your database.
use Gobline\Auth\Authenticator\Db\TableMetadata; use Gobline\Auth\Authenticator\Db\DbAuthenticator; $metadata = new TableMetadata('users'); $metadata ->setColumnId('user_id') ->setColumnLogin('user_email') ->setColumnRole('user_type') ->setColumnPassword('user_password') ->setPasswordEncryption('md5'); $authenticator = new DbAuthenticator($pdo, $metadata);
You will also notice that besides the id, login and password, it is possible to specify a column containing the role (or group, type) of the user. This is especially useful when combining the authentication component with an ACL component (which is usually the case).
Now that we have set up our adapter, it's time to authenticate the user.
$authenticator->setIdentity('mdecaffmeyer@gmail.com'); $authenticator->setCredential('123456'); $user->isAuthenticated(); // returns false if ($authenticator->authenticate($user)) { echo $user->getId(); // prints the user id from DB $user->isAuthenticated(); // returns true }
The authenticator sets the id and the login of the user. It can additionally add the user's role (for ACL) and other column data.
An authenticated user is a user whose id is not null.
Persist the user's data among requests
While the above works perfectly for the first request, the user's data will be lost on subsequent requests.
One way to achieve persistence is to store the user data in session.
Gobline\Auth\Persistence\CurrentUser acts like a decorator over Gobline\Auth\CurrentUser (or any
Gobline\Auth\CurrentUserInterface implementation), allowing to persist the data in session.
$user = new Gobline\Auth\CurrentUser(); $user = new Gobline\Auth\Persistence\CurrentUser($user);
Installation
You can install Gobline Auth using the dependency management tool Composer. Run the require command to resolve and download the dependencies:
composer require gobline/auth
gobline/auth 适用场景与选型建议
gobline/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 140 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 「auth」 「authenticator」 「gobline」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gobline/auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gobline/auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gobline/auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
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.
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.
Basic CAS (SSO) authenticator for Symfony 5.4, 6.0, 7.0 and 8.0
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
统计信息
- 总下载量: 140
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-12-03