indigophp / guardian
此包已被弃用且不再维护。作者建议使用guardianphp/guardian包。
简单灵活的认证框架
dev-master / 0.1.x-dev
2015-04-20 18:42 UTC
Requires
- php: >=5.4
- beberlei/assert: ~2.3
- indigophp/hash-compat: ~1.0
- ircmaxell/password-compat: ~1.0
- ramsey/array_column: ~1.1
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: ~1.0
- phpspec/phpspec: ~2.1
- sobstel/sesshin: 1.1.*@dev
Suggests
- sobstel/sesshin: Used in Sesshin session adapter
This package is not auto-updated.
Last update: 2022-02-01 12:44:52 UTC
README
简单灵活的认证框架。
安装
通过Composer
$ composer require indigophp/guardian
用法
此库提供了一个简单的方法来认证任何实体,无论是否持久化,并称之为“登录”。
简单的登录示例
use Indigo\Guardian\Identifier\InMemory; use Indigo\Guardian\Authenticator\UserPassword; use Indigo\Guardian\Hasher\Plaintext; use Indigo\Guardian\SessionAuth; use Indigo\Guardian\Session\Native; $identifier = new InMemory([ 1 => [ 'username' => 'john.doe', 'password' => 'secret', 'name' => 'John Doe', ], ]); $authenticator = new UserPassword(new Plaintext); $session = new Native; $auth = new SessionAuth($identifier, $authenticator, $session); // returns true to indicate success $auth->login([ 'username' => 'john.doe', 'password' => 'secret', ]);
之后,当登录成功时,检查当前登录状态
// returns true/false $auth->check(); // returns the current caller $caller = $auth->getCurrentCaller();
并在最后登出
// returns true/false $auth->logout();
API认证
由于Guardian是一个认证库,您可以使用它轻松地认证API请求而不需要持久化。要实现这一点,请参阅以下简单的认证服务
use Indigo\Guardian\Identifier\InMemory; use Indigo\Guardian\Authenticator\UserPassword; use Indigo\Guardian\Hasher\Plaintext; use Indigo\Guardian\RequestAuth; $identifier = new InMemory([ 1 => [ 'username' => 'john.doe', 'password' => 'secret', 'name' => 'John Doe', ], ]); $authenticator = new UserPassword(new Plaintext); $auth = new RequestAuth($identifier, $authenticator); $subject = [ 'username' => 'john.doe', 'password' => 'secret', ]; // returns true to indicate success $auth->authenticate($subject); // returns the caller object if identify succeeds $caller = $auth->authenticateAndReturn($subject);
测试
$ phpspec run
贡献
请参阅CONTRIBUTING以获取详细信息。
致谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。