kelvinmo/simplejwt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kelvinmo/simplejwt

Composer 安装命令:

composer require kelvinmo/simplejwt

包简介

A simple JSON Web Token library for PHP.

README 文档

README

SimpleJWT is a simple JSON web token library written in PHP.

Latest Stable Version CI

Features

  • JSON web token RFC7519, JSON web signatures RFC7515 and JSON web encryption RFC7516
  • JSON web keys RFC7517
  • COSE key objects RFC9053
  • Signature algorithms
    • HMAC family (HS256, HS384, HS512)
    • RSA family (RS256, RS384, RS512)
    • ECDSA family (ES256, ES384, ES512)
    • EdDSA
  • Key management algorithms
    • Key agreement or direct encryption
    • RSAES-PKCS1-v1_5 (RSA1_5)
    • RSAES with OAEP (RSA-OAEP, RSA-OAEP-256)
    • AES key wrap (A128KW, A192KW, A256KW, A128GCMKW, A192GCMKW, A256GCMKW)
    • PBES2 (PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KW)
    • Elliptic Curve Diffie-Hellman (ECDH-ES), including X25519
  • Content encryption algorithms
    • AES_CBC_HMAC_SHA2 family (A128CBC-HS256, A192CBC-HS384, A256CBC-HS512)
    • AES GCM family (A128GCM, A192GCM, A256GCM)

Requirements

  • PHP 8.0 or later
  • gmp extension
  • hash extension
  • openssl extension
  • sodium extension for EdDSA and X25519 support

Installation

You can install via Composer.

composer require kelvinmo/simplejwt

Usage

Key set

Keys used to sign or verify a JWT must firstly be added to a KeySet. You can add keys in the following ways:

  1. By loading a JSON object formatted as a JWK Set object as per RFC7517:
$set = new SimpleJWT\Keys\KeySet();
$set->load(file_get_contents('private.json'));
  1. By adding a key manually:
$set = new SimpleJWT\Keys\KeySet();

// JWK format
$key = new SimpleJWT\Keys\RSAKey(file_get_contents('jwk.json'), 'json');

// PEM format - note raw key only, no X.509 certificates
$key = new SimpleJWT\Keys\RSAKey(file_get_contents('rsa.pem'), 'pem');

$set->add($key);
  1. For a secret used in HMAC signatures, directly:
$set = SimpleJWT\Keys\KeySet::createFromSecret('secret123');

// The above is a shortcut for the following:
$set = new SimpleJWT\Keys\KeySet();
$key = new SimpleJWT\Keys\SymmetricKey('secret123', 'bin');
$set->add($key);

Creating a JWT

To create a JWT, set up the desired headers and claims as separate arrays, then create a JWT object:

// Note $headers['alg'] is required
$headers = ['alg' => 'HS256', 'typ' => 'JWT'];
$claims = ['iss' => 'me', 'exp' => 1234567];
$jwt = new SimpleJWT\JWT($headers, $claims);

The JWT can then be signed and encoded:

try {
    print $jwt->encode($set);
} catch (\RuntimeException $e) {

}

By default, SimpleJWT will automatically include a kid (Key ID) header and a iat (Issued At) claim in all JWTs. If the key used to sign the JWT does not have a kid assigned (e.g. if it is imported from a PEM file), a kid is generated. You can disable this behaviour by specifying $auto_complete to false when calling SimpleJWT\JWT::encode().

Verifying a JWT

To consume and verify a JWT, use the decode function. Note that you will need to supply the expected alg parameter that has been previously agreed out-of-band.

try {
    $jwt = SimpleJWT\JWT::decode('abc.def.ghigjghr', $set, 'HS256');
} catch (SimpleJWT\InvalidTokenException $e) {

}

print $jwt->getHeader('alg');
print $jwt->getClaim('sub');

Deserialising a JWT

You can also deserialise a JWT without verifying it using the deserialise function. Note that you should not trust the contents of the data contained in a JWT without verifying them.

try {
    $result = SimpleJWT\JWT::deserialise('abc.def.ghigjghr');
} catch (SimpleJWT\InvalidTokenException $e) {

}

print $result['claims']['sub'];
print $result['signatures'][0]['headers']['alg'];
print $result['signatures'][0]['signing_input'];  // abc.def
print $result['signatures'][0]['signature'];      // ghigjghr
// Additional indices under $result['signatures'] if the JWT has more than
// one signature

Creating a JWE

To create a JWE, set up the desired header array and plaintext, then create a JWE object:

// Note $headers['alg'] and $headers['enc'] are required
$headers = ['alg' => 'PBES2-HS256+A128KW', 'enc' => 'A128CBC-HS256'];
$plaintext = 'This is the plaintext I want to encrypt.';
$jwt = new SimpleJWT\JWE($headers, $plaintext);

The JWE can then be encrypted:

try {
    print $jwt->encrypt($set);
} catch (\RuntimeException $e) {

}

Decrypting a JWE

To decrypt a JWE, use the decrypt function:

try {
    $jwt = SimpleJWT\JWE::decrypt('abc.def.ghi.klm.nop', $set, 'PBES2-HS256+A128KW');
} catch (SimpleJWT\InvalidTokenException $e) {

}

print $jwt->getHeader('alg');
print $jwt->getPlaintext();

Licence

BSD 3 clause

kelvinmo/simplejwt 适用场景与选型建议

kelvinmo/simplejwt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.26M 次下载、GitHub Stars 达 70, 最近一次更新时间为 2015 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「jwt」 「JWE」 「JOSE」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kelvinmo/simplejwt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kelvinmo/simplejwt 我们能提供哪些服务?
定制开发 / 二次开发

基于 kelvinmo/simplejwt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.26M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 70
  • 点击次数: 19
  • 依赖项目数: 17
  • 推荐数: 0

GitHub 信息

  • Stars: 70
  • Watchers: 5
  • Forks: 21
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2015-07-31