germania-kg / permanent-authentication
Requires
- php: ^5.6|^7.0
- paragonie/random-lib: ^2.0
- psr/http-message: ^1.0
- psr/log: ^1.0
Requires (Dev)
- monolog/monolog: ^1.16
- php-coveralls/php-coveralls: ^2.0
- phpunit/dbunit: ^2.0|^3.0
- phpunit/phpunit: ^5.7|^6.0
README
此包是从旧代码中提炼出来的!
要求
- Anthony Ferrara的 ircmaxell/RandomLib
使用Composer安装
$ composer require germania-kg/permanent-authentication
MySQL用户 可以使用 sql/
目录中的 auth_logins.sql.txt
安装 auth_logins 表。
创建持久登录
此Callable将选择器和令牌对存储在客户端的cookie中,并将选定的和散列的令牌存储在数据库中。需要以下五个组件
rnd: 一个 RandomLib 生成器实例,用于创建安全的随机数字和字符串。
client_storage: 一个Callable,将选择器和令牌对存储在客户端。错误时,应抛出一个实现 Germania\PermanentAuth\Exceptions\StorageExceptionInterface. 的Exception。 - 创建自己的或尝试下面ClientStorage部分中描述的实现
hasher: 一个Callable,安全地散列由RandomLib创建的随机令牌。建议使用PHP的 password_hash。
server_storage: 一个Callable,将选择器和令牌散列存储在数据库中。错误时,应抛出一个实现 Germania\PermanentAuth\Exceptions\StorageExceptionInterface. 的Exception。 - 创建自己的或尝试下面PdoStorage部分中描述的实现。
valid_until: 一个PHP DateTime 对象,它包含过期日期和时间。
<?php use Germania\PermanentAuth\CreatePersistentLogin; use RandomLib\Factory; use RandomLib\Generator; // Create Random generator $factory = new RandomLib\Factory; $random = $factory->getMediumStrengthGenerator(); // Setup expiration $expire = new \DateTime; date_add($expire, date_interval_create_from_date_string('10 days')); // Setup hash function; this goes to database $hasher = function( $token ) { return password_hash( $token) }; // On error, throw Germania\PermanentAuth\Exceptions\StorageExceptionInterface $client_storage = function( $selector, $token, $expire) { return setcookie( ... ); }; $server_storage = function($user_id, $selector, $token_hash, $expire) { $sql = 'INSERT INTO ...'; }; // Optional: PSR-3 Logger $create = new CreatePersistentLogin( $random, $client_storage, $hasher, $server_storage, $expire); $create = new CreatePersistentLogin( $random, $client_storage, $hasher, $server_storage, $expire, $logger); // Action $user_id = 99; $created = $create( $user_id ) // Evaluate if ($created): // Success! endif;
使用持久登录验证用户
此Callable尝试检索并返回一个持久登录选择器和令牌。 它不验证用户! - 换句话说,它告诉您再次访问的用户声称是谁。
<?php use Germania\PermanentAuth\ClientAuthentication; // Setup: // 1. Retrieve the cookie value $cookie_getter = function( $cookie_name ) { // return $_COOKIE[ $name ] return "foo:bar"; }; // 2: Split into selector and token part $cookie_parser = function( $value ) { $parts = explode(":", $value); return (object) array( 'selector' => $parts[0], 'token' => $parts[1] ); }; $auth = new ClientAuthentication( $cookie_getter, $cookie_parser, "persistent"); // Invoke $selector_token = $auth(); // Evaluate if ($selector_token): // Check if selector and token are valid on server-side endif;
辅助工具
AuthUserInterface
定义了用户ID的拦截器。由 PermanentAuth\Middleware 需要,它期望一个用户对象-
<?php use Germania\PermanentAuth\AuthUserInterface; class AppUser implements AuthUserInterface { public $id; /** * Returns the User ID. * @return mixed */ public function getId() { return $this->id; } /** * Sets the User ID. * @param mixed $id */ public function setId( $id ) { $this->id = $id; } }
Middleware
这种PSR风格的Middleware识别用户并验证数据库中的声明的登录选择器。成功后,将找到的用户ID分配给用户对象。
需要 PermanentAuth\AuthUserInterface 实例。
<?php use Germania\PermanentAuth\Middleware; use Slim\App; $app = new App; $user = new AppUser; $middleware = new Middleware( $user, ... ); $app->add( $middleware );
ClientStorage
在客户端存储选择器和令牌。随机生成的选择器和令牌是base64编码的,并作为cookie与过期日期一起发送到客户端。
<?php use Germania\PermanentAuth\ClientStorage;
PdoStorage
在数据库中存储选择器和令牌散列,以及过期日期。
<?php use Germania\PermanentAuth\PdoStorage;
PdoValidator
验证登录选择器和令牌是否与数据库中的令牌散列匹配。
<?php use Germania\PermanentAuth\PdoValidator;
PdoDelete
删除给定用户的全部持久登录。
<?php use Germania\PermanentAuth\PdoDelete;
开发
$ git clone https://github.com/GermaniaKG/PermanentAuth.git
$ cd PermanentAuth
$ composer install
单元测试
要么将 phpunit.xml.dist
复制到 phpunit.xml
并根据您的需要进行调整,要么保留不变。运行PhpUnit测试或类似这样的composer脚本
$ composer test # or $ vendor/bin/phpunit
按照 sql/auth_logins.sql.txt
中的说明设置MySQL表 auth_logins
。在 phpunit.xml
中,编辑数据库凭据
<php> <var name="DB_DSN" value="mysql:host=localhost;dbname=DBNAME;charset=utf8" /> <var name="DB_USER" value="DBUSER" /> <var name="DB_PASSWD" value="DBPASS" /> <var name="DB_DBNAME" value="DBNAME" /> </php>