picios/tokenizer
Composer 安装命令:
composer require picios/tokenizer
包简介
Easy uri tokens creator and validator
README 文档
README
This library helps to create and validate url valid tokens and store some data within it. Such token can not be changed, otherwise it's not valid anymore.
Installation
Install with composer
composer require picios/tokenizer
Usage
Creating a new token
You need to create the token first, to e.g. send it as a query parameter in en email message
<?php use Picios\Tokenizer\TokenManager; require_once __DIR__ . '/vendor/autoload.php'; $tm = new TokenManager('seed', [ 'id', // information about fields, that the token will contain. In this case only 'id' ]); $token = $tm->createToken([ 'id' => 5, ]); // call token __toString() function echo "{$token}";
Parsing the token
Incoming token must be validated by the TokenManager::parse() function
<?php use Picios\Tokenizer\TokenManager; use Picios\Tokenizer\TokenNotValidException; require_once __DIR__ . '/vendor/autoload.php'; $tm = new TokenManager('seed', [ 'id', // information about fields, that the token will contain. In this case only 'id' ]); // incoming token e.g. from the Request::query object // or just $_GET array $stringToken = 'NDc5NWI4MjAxOS0wOC0wNlQwODozNjoxMCswMjowMHsiaWQiOjV9'; try { $token = $tm->parse($stringToken); echo "This is a secret content taken by token id {$token->get('id')}"; } catch (TokenNotValidException $e) { echo "Invalid token"; exit(); }
Creation time
You can easily check the token creation time to consider its expiration
<?php use Picios\Tokenizer\TokenManager; use Picios\Tokenizer\TokenNotValidException; require_once __DIR__ . '/vendor/autoload.php'; $tm = new TokenManager('seed', [ 'id' // information about fields, that the token will contain. In this case only 'id' ]); // incoming token e.g. from the Request::query object // or just $_GET array $stringToken = 'NDc5NWI4MjAxOS0wOC0wNlQwODozNjoxMCswMjowMHsiaWQiOjV9'; try { $token = $tm->parse($stringToken); echo 'The token was created on ' . $token->getStartTime()->format('Y-m-d H:i:s'); } catch (TokenNotValidException $e) { echo "Invalid token"; exit(); }
How it works
The token allows you to store a limited amount of data in one string in a safe way.
To create a new token, you need to get TokenManager object with parameters SEED and a structure of the data in token. It's a simple array with names of the token data fields. The SEED and the structure must be the same during creating the token and when you parse it.
When the TokenManager object is set, you may call its createToken(array data) function, of which the only required parameter is the associeted array with the data, you want to store in the token. The data array must contain exactly the same fields, as in the structure from the TokenManager object.
The data in the token are explicit, just encoded with base64 algorythm. They can be easly decoded. For the above example token
NDc5NWI4MjAxOS0wOC0wNlQwODozNjoxMCswMjowMHsiaWQiOjV9
a decoded string is
4795b82019-08-06T08:36:10+02:00{"id":5}
First 6 characters is a substring of a md5 hash created from all the data in the token plus the secret seed known only on the server side. The next 25 characters is a date of creating the token in ATOM format. The rest is the data encoded with the JSON format.
It's safety and unchangeability comes from the simple hash, that is a kind of a checksum of our token.
Troubleshooting
- you need to remember that all the data in the token are easily decodable, so don't place any confidential data in it.
- the capacity of the token is limited to the purpose, you use it for. For example if you put it as a part of a URL query, according to the HTTP spec, there is no limit to a URL's length, but keep your URLs under 2048 characters so it would work well with IE browser.
Testing
To test the class, run:
phpunit test
Homepage
You can read more at Picios.pl
picios/tokenizer 适用场景与选型建议
picios/tokenizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「composer」 「token」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 picios/tokenizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 picios/tokenizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 picios/tokenizer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for validating Google Cloud's JWT provided by webhooks
JSON Web Token Authentication for Laravel and Lumen
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
JSON Web Token Authentication for Laravel and Lumen
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: CC-BY-3.0
- 更新时间: 2019-08-05