baophong09/php-jwt

此软件包最新版本(v5.2.0)没有提供许可证信息。

一个简单的PHP库,用于编码和解码JSON Web Tokens(JWT)。应遵循当前规范。

v5.2.0 2016-10-26 16:16 UTC

This package is not auto-updated.

Last update: 2024-09-19 03:02:18 UTC


README

Build Status Latest Stable Version Total Downloads License

PHP-JWT

一个简单的PHP库,用于编码和解码JSON Web Tokens(JWT),符合RFC 7519规范。

安装

使用composer管理依赖并下载PHP-JWT

composer require firebase/php-jwt

示例

<?php
use \Firebase\JWT\JWT;

$key = "example_key";
$token = array(
    "iss" => "http://example.org",
    "aud" => "http://example.com",
    "iat" => 1356999524,
    "nbf" => 1357000000
);

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
 */
$jwt = JWT::encode($token, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));

print_r($decoded);

/*
 NOTE: This will now be an object instead of an associative array. To get
 an associative array, you will need to cast it as such:
*/

$decoded_array = (array) $decoded;

/**
 * You can add a leeway to account for when there is a clock skew times between
 * the signing and verifying servers. It is recommended that this leeway should
 * not be bigger than a few minutes.
 *
 * Source: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
 */
JWT::$leeway = 60; // $leeway in seconds
$decoded = JWT::decode($jwt, $key, array('HS256'));

?>

变更日志

4.0.0 / 2016-07-17

  • 增加对静态绑定晚期的支持。请参阅#88以获取详细信息。感谢@chappy84
  • 使用静态 $timestamp 而不是 time() 来提高单元测试。请参阅#93以获取详细信息。感谢@josephmcdermott
  • 修复异常类的问题。请参阅#81以获取详细信息。感谢@Maks3w
  • 修复PHPDoc。请参阅#76以获取详细信息。感谢@akeeman

3.0.0 / 2015-07-22

  • 最低PHP版本从5.2.0更新到5.3.0
  • 增加\Firebase\JWT命名空间。请参阅#59以获取详细信息。感谢@Dashron
  • 解码和验证JWT时需要非空密钥。请参阅#60以获取详细信息。感谢@sjones608
  • 在代码中清理文档块。请参阅#62以获取详细信息。感谢@johanderuijter

2.2.0 / 2015-06-22

  • 增加对向JWT::encode()添加自定义可选JWT头部的支持。请参阅#53以获取详细信息。感谢@mcocaro

2.1.0 / 2015-05-20

  • 增加对在JWT:decode()中添加一个容差值以考虑签名和验证实体之间的时钟偏移的支持。感谢@lcabral
  • 增加对在JWT::decode()中将实现ArrayAccess接口的对象传递给$keys参数的支持。感谢@aztech-dev

2.0.0 / 2015-04-01

  • 注意:强烈建议您更新到 > v2.0.0,以解决在同时使用对称和非对称密钥时,先前版本中已知的安全漏洞。
  • 更新JWT::decode(...)的签名,以要求使用支持的算法数组来验证令牌签名。

测试

使用phpunit运行测试

$ pear install PHPUnit
$ phpunit --configuration phpunit.xml.dist
PHPUnit 3.7.10 by Sebastian Bergmann.
.....
Time: 0 seconds, Memory: 2.50Mb
OK (5 tests, 5 assertions)

许可证

3-Clause BSD.