bradchesney79 / effortless-hs256-jwt
一个具有偏见的库,通过禁止不希望的可能性来简化JWT管理
This package is auto-updated.
Last update: 2024-08-29 05:06:29 UTC
README
我相信我已经将这个库发展到了一个可用和可批评的状态。到目前为止,2020年12月,我将其视为beta版本。
我创建这个包是因为我不喜欢现有的库。此外,这个包只依赖于PDO数据库驱动程序、JSON和mbstring扩展。
因此,这是一个设计用于
- 让你放心算法始终是HS256
- 允许你创建带有标准声明和自定义声明的JWT令牌字符串
- 允许你编辑声明
- 允许你删除声明
- 允许你检索JWT令牌字符串
- 允许你读取令牌声明
- 允许你验证令牌
第一步 - 安装
你需要安装现代版本的PHP,PHP v7.4+。
通过composer添加或git clone,或者wget类文件,或者使用古老的剪切/粘贴和require_once()
composer require bradchesney79/effortless-hs256-jwt
使用以下方式创建带有'secret'参数的对象
$jwt = new Ehjwt($secretString);
第一步a
拒绝使用composer的未经洗练的异教徒可能需要类似这样的东西
require_once 'path/to/Ehjwt.php';
require_once, include, include_once... 只有你自己才知道什么最适合你。
通过composer安装不是必需的--我只是认为这是最好的方式
第二步 - 使用Composer使用
使代码可用
确保进行完全正常的PHP require或require_once vendor/autoload.php
use BradChesney79/EHJWT;
创建令牌,附加/更新声明,获取令牌字符串
$jwtToken = new EHJWT('SuperSecretStringUsedForOneWayEncryption'); // the globally unique ID of this token and its series of potential reissues $jwtToken->addOrUpdateJwtClaim('jti', '1234567890'); // it is a string. nothing more, nothing less. // issued at $jwtToken->addOrUpdateJwtClaim('iat', '305078400'); // my birthday... // when this incarnation of the jwt will die as a UTC timestamp $jwtToken->addOrUpdateJwtClaim('exp', '1887525317'); // when the T-800 comes to kill Sarah Connor // the subject-- I use this for the publicly facing user ID $jwtToken->addOrUpdateJwtClaim('sub', 'bradchesney79@gmail.com'); // ...I'll be honest. I don't use the not before field. // It isn't useful to me in my software designs. // But, it will throw an exception if you try to use it before allowed. // $jwtToken->addOrUpdateJwtClaim('nbf', 0); // January 1st, 1970 // One of many allowable custom, private claims-- but, beware, smaller the better. $jwtToken->addOrUpdateJwtClaim('key','value'); $jwtToken->createToken(); // this internally populates the JWT string property of your instance echo $jwtToken->getToken(); // this gives you the three part, period delimited string stored in the JWT string property
验证令牌,读取令牌声明,删除令牌声明
$jwtToken = new EHJWT('SuperSecretStringUsedForOneWayEncryption'); if ($jwtToken->loadToken('fdsafdsafdsafdsa'.'fdsfdsafdsafdsa'.'fdsafdfadsfdsafdsa')) { $sessionDataArray = $jwtToken->getTokenClaims(); } $this->clearClaims();
步骤A - 测试
确保phpdbg和xdebug扩展可用,以使开发者的生活更容易
使用已安装的PHPUnit运行测试(在开发依赖项中)
您需要设置数据库并提供有效的连接凭据
mysql -u{{dbUser}} -p < schema/ehjwt-mysql.sql
vendor/bin/phpmd src/EHJWT/ html cleancode --suffixes php --reportfile build/phpmd.html
vendor/bin/phpunit
步骤B - 统计代码行数
cloc --exclude-dir=vendor,build .
11/2020 1,800 12/2020 1,638 05/2021 1,048
待办事项
-
将RuntimeException测试的检测也扩展到测试异常消息的特定性
-
使README不再糟糕
-
使用https://gist.github.com/soulmachine/b368ce7292ddd7f91c15accccc02b8df ...作为如何使用此库(超越语法和逻辑流程)的指南的基础
注意事项
-
使用限制在PHP 7.4+平台上
-
我并不确定这个库是否已经准备好投入生产
-
我做出了决策,强制你使用我能够管理的最接近最佳实践的特定密钥使用此库。其他库允许你拥有更多的自由--可能是有害的自由。
-
没有存储谁或什么令牌。你不能用这个库看到是否存在一个令牌。你只能验证和使用返回给你的令牌。