yiisoft/auth
Composer 安装命令:
composer require yiisoft/auth
包简介
Yii auth
关键字:
README 文档
README
Yii Auth
The package provides various authentication methods, a set of abstractions to implement in your application, and a PSR-15 middleware to authenticate an identity.
Requirements
- PHP 8.1 - 8.5.
Installation
composer require yiisoft/auth
General usage
Configure a middleware and add it to your middleware stack:
$identityRepository = getIdentityWithTokenRepository(); // \Yiisoft\Auth\IdentityRepositoryInterface $authenticationMethod = new \Yiisoft\Auth\Method\HttpBasic($identityRepository); $middleware = new \Yiisoft\Auth\Middleware\Authentication( $authenticationMethod, $responseFactory, // PSR-17 ResponseFactoryInterface $failureHandler // optional, \Yiisoft\Auth\Handler\AuthenticationFailureHandler by default ); $middlewareDispatcher->addMiddleware($middleware);
In order to get an identity instance in the following middleware use getAttribute() method of the request instance:
public function actionIndex(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface { $identity = $request->getAttribute(\Yiisoft\Auth\Middleware\Authentication::class); // ... }
HTTP basic authentication
Basic HTTP authentication is typically used for entering login and password in the browser.
Credentials are passed as $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'].
$authenticationMethod = (new \Yiisoft\Auth\Method\HttpBasic($identityRepository)) ->withRealm('Admin') ->withAuthenticationCallback(static function ( ?string $username, #[\SensitiveParameter] ?string $password, \Yiisoft\Auth\IdentityWithTokenRepositoryInterface $identityRepository ): ?\Yiisoft\Auth\IdentityInterface { return $identityRepository->findIdentityByToken($username, \Yiisoft\Auth\Method\HttpBasic::class); });
Realm is typically what you will see in the browser prompt asking for a login and a password. Custom authentication callback set in the above is the same as default behavior when it is not specified.
HTTP bearer authentication
Bearer HTTP authentication is typically used in APIs. Authentication token is passed in WWW-Authenticate header.
$authenticationMethod = new \Yiisoft\Auth\Method\HttpBearer($identityRepository);
Custom HTTP header authentication
Custom HTTP header could be used if you do not want to leverage bearer token authentication:
$authenticationMethod = (new \Yiisoft\Auth\Method\HttpHeader($identityRepository)) ->withHeaderName('X-Api-Key') ->withPattern('/(.*)/'); // default
In the above we use full value of X-Api-Key header as token.
Query parameter authentication
This authentication method is mainly used by clients unable to send headers. In case you do not have such clients we advise not to use it.
$authenticationMethod = (new \Yiisoft\Auth\Method\QueryParameter($identityRepository)) ->withParameterName('token');
HTTP cookie authentication
$authenticationMethod = (new \Yiisoft\Auth\Method\HttpCookie($identityRepository)) ->withCookieName('access-token');
Typical authentication for websites by storing a token in a browser cookie.
Using multiple authentication methods
To use multiple authentication methods, use Yiisoft\Auth\Method\Composite:
$authenticationMethod = new \Yiisoft\Auth\Method\Composite([ $bearerAuthenticationMethod, $basicAuthenticationMethod ]);
Extension and integration points
\Yiisoft\Auth\IdentityInterfaceshould be implemented by your application identity class. Typically, that isUser.\Yiisoft\Auth\IdentityRepositoryInterfaceshould be implemented by your application identity repository class. Typically, that isUserIdentity.\Yiisoft\Auth\IdentityWithTokenRepositoryInterfacecould be additionally implemented by your application identity repository class in case token-based authentication is needed. Typically, that isUserIdentity.\Yiisoft\Auth\AuthenticationMethodInterfacecould be implemented to provide your own authentication method.
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Auth is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/auth 适用场景与选型建议
yiisoft/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 179.83k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2019 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「auth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/auth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Laravel Multiauth package
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.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
Email Toolkit Plugin for CakePHP
Slim Framework 3 CSRF protection middleware utilities
统计信息
- 总下载量: 179.83k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 24
- 依赖项目数: 16
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-11-14