ingenerator/tokenista
最新稳定版本:v1.8.0
Composer 安装命令:
composer require ingenerator/tokenista
包简介
Simple signed and expiring token generator and validator - for password reset, CSRF, authentication, whatever
README 文档
README
Tokenista is a lightweight library for generating and validating signed tokens that can be used for password reset links, authentication, CSRF or anything else you may require. It aims to be secure (though you should always review all security related code) and to have minimum external dependencies.
Installation
Add tokenista to your composer.json and run composer update to install it.
{ "require": { "ingenerator/tokenista": "^1.4" } } Basic Usage
$secret = 'some-constant-secret-value'; $tokenista = new \Ingenerator\Tokenista($secret, array('lifetime' => 3600)); // Generate with default lifetime from constructor options $token = $tokenista->generate(); // Overall check if token is valid if ($tokenista->isValid($token)) { // Do whatever } // Or for more control use: $tokenista->isExpired($token); $tokenista->isTampered($token);
Tokenista generates tokens as a single string of the form {random}-{expirytime}-{signature}, base64 encoded so suitable for inclusion in most places.
Verifying additional values
You may want to use Tokenista's signing mechanism to verify that some additional data has not been tampered with. For example, you could use this to include email address or other confirmation information in a URL rather than having to store a record of the mapping between token and user server side.
$token = $tokenista->generate(3600, ['user_id' => 9123]); // Then, later: if ($tokenista->isValid($_GET['token'], ['user_id' => $_GET['user_id']]) { // You can now trust user_id, even if it came through the URL, because it matches the value you originally signed // for this token. }
Rotating secrets
It's good practice to occasionally rotate secrets - but without invalidating signatures that haven't yet expired. This is easily done - add an old_secrets config option with any previous secrets that should still be valid. Tokenista will start using the new secret to produce new tokens while still accepting tokens signed with an older value.
Once your maximum token expiry liftime has passed you can then remove the old secret from your list and Tokenista will stop accepting it.
Testing and developing
tokenista has a full suite of PHPUnit unit tests - run them with ./vendor/bin/phpunit. Contributions will only be accepted if they are accompanied by well structured unit tests. Installing with composer should get you everything you need to work on the project.
License
tokenista is copyright 2014 inGenerator Ltd and released under the BSD license.
统计信息
- 总下载量: 90.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-01-04