caridea / auth
一个认证库的小虾米
3.0.0
2018-01-06 20:23 UTC
Requires
- php: >=7.1.0
- caridea/event: ^3.0.0
- caridea/session: ^3.0.0
- psr/http-message: ^1.0.0
- psr/log: ^1.0.0
Requires (Dev)
- phpunit/phpunit: ^6.0.0
README
Caridea 是一个微小的 PHP 应用程序库。这个小虾米是当你只需要一些帮助而不需要完整的框架时使用的。
这是它的认证组件。它提供了一种验证主体并存储其身份的方法。它将为任何监听器广播认证事件。它与任何 PSR-7 的实现一起工作。
包含三个通过 MongoDB、PDO 和 X.509 客户端 SSL 证书进行认证的适配器。你可以轻松地为其他认证源(如 IMAP、LDAP 或 OAuth2)编写自己的适配器。
安装
你可以使用 Composer 安装此库
$ composer require caridea/auth
- 此项目的 master 分支(版本 3.x)需要 PHP 7.1,并依赖于
caridea/event
、caridea/session
、psr/log
和psr/http-message
。 - 此项目的 2.x 版本需要 PHP 7.0,并依赖于
caridea/event
、caridea/session
、psr/log
和psr/http-message
。 - 此项目的 1.x 版本需要 PHP 5.5,并依赖于
caridea/event
、caridea/session
、psr/log
和psr/http-message
。
合规性
此库的版本将符合 语义版本控制。
我们的代码旨在符合 PSR-1、PSR-2 和 PSR-4。如果您发现与标准合规性相关的问题,请发送 pull request!
文档
- 请访问 Read the Docs
示例
这里有一些快速示例。
登录
// Let's say $session is a \Caridea\Session\Session, such as \Caridea\Session\NativeSession // Let's say $publisher is a \Caridea\Event\Publisher, such as \Caridea\Container\Objects $service = new \Caridea\Auth\Service($session, $publisher); // Let's say $collection is a \MongoCollection $adapter = new \Caridea\Auth\Adapter\Mongo($collection, 'username', 'password'); // Let's say $request is a \Psr\Http\Message\RequestInterface if ($service->login($request, $adapter)) { $principal = $service->getPrincipal(); $username = $principal->getUsername(); $details = $principal->getDetails()); // $details = [ // 'id' => '1234567890', // 'ua' => 'Mozilla/5.0', // 'ip' => '192.168.1.1' // ]; }
登录时,如果已设置 $publisher
,则 Service
将广播一个 Caridea\Auth\Event\Login
。
恢复
// Let's say $session is a \Caridea\Session\Session, such as \Caridea\Session\NativeSession // Let's say $publisher is a \Caridea\Event\Publisher, such as \Caridea\Container\Objects $service = new \Caridea\Auth\Service($session, $publisher); if ($service->resume()) { $principal = $service->getPrincipal(); }
恢复时,如果已设置 $publisher
,则 Service
将广播一个 Caridea\Auth\Event\Resume
。
注销
// Let's say $session is a \Caridea\Session\Session, such as \Caridea\Session\NativeSession // Let's say $publisher is a \Caridea\Event\Publisher, such as \Caridea\Container\Objects $service = new \Caridea\Auth\Service($session, $publisher); // Let's say $collection is a \MongoCollection $adapter = new \Caridea\Auth\Adapter\Mongo($collection, 'username', 'password'); if ($service->logout()) { // anonymous! }
注销时,如果已设置 $publisher
,则 Service
将广播一个 Caridea\Auth\Event\Logout
。
登录超时
包括了一个组件 TimeoutListener
,它可以在 Caridea\Event\Publisher
中注册。
它监听 Caridea\Auth\Event\Resume
,如果认证会话持续时间过长或空闲时间过长,则将用户注销。