adhocore/jwt
Composer 安装命令:
composer require adhocore/jwt
包简介
Ultra lightweight JSON web token (JWT) library for PHP5.5+.
README 文档
README
If you are new to JWT or want to refresh your familiarity with it, please check jwt.io
- Lightweight JSON Web Token (JWT) library for PHP7, PHP8 and beyond.
- Zero dependency (no vendor bloat).
- If you still use PHP5.6, use version 0.1.2
Installation
# PHP7.x, PHP8.x composer require adhocore/jwt # PHP5.6 (deprecated) composer require adhocore/jwt:0.1.2 # For PHP5.4-5.5 (deprecated), use version 0.1.2 with a polyfill for https://php.net/hash_equals
Features
- Six algorithms supported:
'HS256', 'HS384', 'HS512', 'RS256', 'RS384', 'RS512'
kidsupport.- Leeway support 0-120 seconds.
- Timestamp spoofing for tests.
- Passphrase support for
RS*algos.
Usage
use Ahc\Jwt\JWT; // Instantiate with key, algo, maxAge and leeway. $jwt = new JWT('secret', 'HS256', 3600, 10);
Only the key is required. Defaults will be used for the rest:
$jwt = new JWT('secret'); // algo = HS256, maxAge = 3600, leeway = 0
For
RS*algo, the key should be either a resource like below:
$key = openssl_pkey_new([ 'digest_alg' => 'sha256', 'private_key_bits' => 1024, 'private_key_type' => OPENSSL_KEYTYPE_RSA, ]);
OR, a string with full path to the RSA private key like below:
$key = '/path/to/rsa.key'; // Then, instantiate JWT with this key and RS* as algo: $jwt = new JWT($key, 'RS384');
Pro You dont need to specify pub key path, that is deduced from priv key.
Generate JWT token from payload array:
$token = $jwt->encode([ 'uid' => 1, 'aud' => 'http://site.com', 'scopes' => ['user'], 'iss' => 'http://api.mysite.com', ]);
Retrieve the payload array:
$payload = $jwt->decode($token);
Oneliner:
$token = (new JWT('topSecret', 'HS512', 1800))->encode(['uid' => 1, 'scopes' => ['user']]); $payload = (new JWT('topSecret', 'HS512', 1800))->decode($token);
Pro
Can pass extra headers into encode() with second parameter:
$token = $jwt->encode($payload, ['hdr' => 'hdr_value']);
Test mocking
Spoof time() for testing token expiry:
$jwt->setTestTimestamp(time() + 10000); // Throws Exception. $jwt->parse($token);
Call again without parameter to stop spoofing time():
$jwt->setTestTimestamp();
Examples with kid
$jwt = new JWT(['key1' => 'secret1', 'key2' => 'secret2']); // Use key2 $token = $jwt->encode(['a' => 1, 'exp' => time() + 1000], ['kid' => 'key2']); $payload = $jwt->decode($token); $token = $jwt->encode(['a' => 1, 'exp' => time() + 1000], ['kid' => 'key3']); // -> Exception with message Unknown key ID key3
Stabillity
The library is now marked at version 1.*.* as being stable in functionality and API.
Integration
Phalcon
Check adhocore/phalcon-ext.
Consideration
Be aware of some security related considerations as outlined here which can be valid for any JWT implementations.
adhocore/jwt 适用场景与选型建议
adhocore/jwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.77M 次下载、GitHub Stars 达 302, 最近一次更新时间为 2017 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「auth」 「token」 「jwt」 「jwt-php」 「json-web-token」 「jwt-auth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adhocore/jwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adhocore/jwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adhocore/jwt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A simple library to decode and parse Apple Sign In client tokens.
A package for validating Google Cloud's JWT provided by webhooks
JSON Web Token Authentication for Laravel and Lumen
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
统计信息
- 总下载量: 1.77M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 304
- 点击次数: 25
- 依赖项目数: 27
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-13