webdevcave/jwt
Composer 安装命令:
composer require webdevcave/jwt
包简介
Lib for generating Json Web Tokens using PHP
README 文档
README
How to install
composer require webdevcave/jwt
Provided signers
| Algorithm | Version |
|---|---|
| HS256 | 1.0 |
| HS384 | 1.0 |
| HS512 | 1.0 |
| RS256 | 1.1 |
| RS384 | 1.1 |
| RS512 | 1.1 |
Provided claim validators
| Claim | Version | Description | RFC |
|---|---|---|---|
| aud | 1.1 | Audience | https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3 |
| exp | 1.0 | Expiration time (timestamp) | https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4 |
| iss | 1.1 | Issuer | https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1 |
| nbf | 1.0 | Not before (timestamp) | https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5 |
| sub | 1.1 | Subject | https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2 |
- "typ" claim is defined as JWT by default.
- "iat" and "nbf" claims are starts with the current timestamp by default.
- "jti" validator isn't provided but it can be implemented by your application as presented in "Validating your private claims" section
Basic Usage
Generating a token
<?php use Webdevcave\Jwt\Token; use Webdevcave\Jwt\SignerFactory; use \Webdevcave\Jwt\Secrets\HsSecret; $secret = new HsSecret('your_secret_here'); $token = Token::create() ->withSigner(SignerFactory::build('HS256')) //HS256 signer is provided by default. This could be omitted ->with('exp', strtotime('+ 1 hour')) //Expires in one hour ->sign($secret) ->toString();
Validating and reading values from a token
<?php use Webdevcave\Jwt\Token; $token = Token::fromString('xxxx.yyyyy.zzzzz'); $isValid = $token->validate($secret); if ($isValid) { $payload = $token->getPayload(); $headers = $token->getHeaders(); }
RSA Tokens:
First of all, you will need a public/private key pair. If you don't have one, you can generate it easily at the following page: https://cryptotools.net/rsagen
With your public/private key pair in hand, the process will be similar to the hmac tokens in the above example:
<?php use Webdevcave\Jwt\Token; use Webdevcave\Jwt\SignerFactory; use \Webdevcave\Jwt\Secrets\RsSecret; $secret = new RsSecret('private_key', 'public_key'); //Generate a token string $tokenString = Token::create() ->withSigner(SignerFactory::build('RS256')) ->with('exp', strtotime('+ 1 hour')) //Expires in one hour ->sign($secret) ->toString(); //Validating... $token = Token::fromString($tokenString); if ($token->validate($secret)) { //token is valid... $creationDate = date(DATE_RFC3339, $token->getPayload('iat')); $expirationDate = date(DATE_RFC3339, $token->getPayload('exp')); echo "Your token was created at $creationDate."; echo "It will expire at $expirationDate."; }
Validating your private claims
First you have to create your validator
use \Webdevcave\Jwt\Validator\Validator; class MyClaimValidator extends Validator { /** * @return string */ public function validates() : string { return 'my-claim'; //this will validate value inside 'my-claim', when set } /** * @param mixed $value * @return bool */ public function validate(mixed $value) : bool { // this claim must contain value 'a', 'b' or 'c' $valid = in_array($value, ['a', 'b', 'c']); return $valid; } }
Then all you have to do is assign your validator before running validate() method
<?php use Webdevcave\Jwt\Token; $token = Token::fromString('xxxx.yyyyy.zzzzz') ->assignValidator(new MyClaimValidator()); $isValid = $token->validate($mySecret); if ($isValid) { $myClaim = $token->getPayload('my-claim'); }
Shortcuts
You can get an Token instance directly from the Authorization header or through a query parameter with the following methods:
use Webdevcave\Jwt\Token; //Load from authorization bearer $token1 = Token::fromAuthorizationBearer(); //Load from get parameters $token2 = Token::fromQueryString('token'); $token3 = Token::fromQueryString('token2');
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or a pull request on GitHub.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Credits
Original project can be found here
webdevcave/jwt 适用场景与选型建议
webdevcave/jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 webdevcave/jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webdevcave/jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-15