germania-kg/permanent-authentication
Composer 安装命令:
composer require germania-kg/permanent-authentication
包简介
README 文档
README
This package was destilled from legacy code!
Requirements
- Anthony Ferrara's ircmaxell/RandomLib
Installation with Composer
$ composer require germania-kg/permanent-authentication
MySQL users may install the table auth_logins using auth_logins.sql.txt in sql/ directory.
Create a persistent login
This Callable stores a selector and token pair in a cookie on the client side and stores the selected and hashed token in the database. Five ingredients are required:
rnd: An instance of RandomLib Generator for creating secure random numbers and strings.
client_storage: A Callable that stores the selector and token pair on the client-side. On error, it should throw an Exception implementing Germania\PermanentAuth\Exceptions\StorageExceptionInterface. – Create your own or try the implementation described in ClientStorage section below
hasher: A Callable that securely hashes the random token created by RandomLib. It is recommended to use PHP's password_hash.
server_storage: A Callable that stores selector and token hash in the database. On error, it should throw an Exception implementing Germania\PermanentAuth\Exceptions\StorageExceptionInterface. – Create your own or try the implementation described in PdoStorage section below.
valid_until: A PHP DateTime object which holds the expiration date and time.
<?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;
Authenticate a user with permanent login
This Callable tries to retrieve and return a persistent login selector and token. It does not validate the user! — In other words, it tells you who the re-visiting user claims to be.
<?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;
Helpers
AuthUserInterface
Defines interceptors for the User ID. Required by PermanentAuth\Middleware which expects a user object-
<?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
This PSR-style Middleware identifies a user and validates the claimed login selector against database. On success, assign found User ID to user object.
Requires a PermanentAuth\AuthUserInterface instance.
<?php use Germania\PermanentAuth\Middleware; use Slim\App; $app = new App; $user = new AppUser; $middleware = new Middleware( $user, ... ); $app->add( $middleware );
ClientStorage
Store selector and token on the client-side. Random-generated selector and token are base64-encoded and sent to the Client as cookie, together with expiration date.
<?php use Germania\PermanentAuth\ClientStorage;
PdoStorage
Store selector and token hash in the database, together with expiration date.
<?php use Germania\PermanentAuth\PdoStorage;
PdoValidator
Validate a login selector and token against token hash in the database.
<?php use Germania\PermanentAuth\PdoValidator;
PdoDelete
Remove all permanent logins for a given user.
<?php use Germania\PermanentAuth\PdoDelete;
Development
$ git clone https://github.com/GermaniaKG/PermanentAuth.git
$ cd PermanentAuth
$ composer install
Unit tests
Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test # or $ vendor/bin/phpunit
Setup a MySQL table auth_logins as in sql/auth_logins.sql.txt.
In phpunit.xml, edit the database credentials:
<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>
germania-kg/permanent-authentication 适用场景与选型建议
germania-kg/permanent-authentication 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 126 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 germania-kg/permanent-authentication 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 germania-kg/permanent-authentication 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 126
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-12-14