psecio/jwt

A JWT (JSON Web Token) 编码与解码库

1.7 2015-05-13 15:54 UTC

This package is auto-updated.

Last update: 2024-09-15 10:07:14 UTC


README

Build Status

此库允许创建和解析 JWT (JSON Web Tokens)。

安装

此工具可以通过 Composer 安装

{
	"require": {
		"psecio/jwt": "1.*"
	}
}

示例用法

在以下示例中,创建了 JWT 对象并分配了一个 Header 实例(必需)。然后 JWT 对象被分配了几个声明:发行者、受众、签发时间和不得早于,以定义数据和如何处理。然后调用 encode 方法并使用 key 返回一个 JWT 格式的字符串。

注意:JWT 令牌将按照声明提供的顺序生成。后台不会进行排序。

然后可以调用 decode 方法对数据进行解码,并使用 key 返回一个与 jwt 对象状态匹配的对象。

<?php

require_once 'vendor/autoload.php';

$key = "example_key";

$header = new \Psecio\Jwt\Header($key);
$jwt = new \Psecio\Jwt\Jwt($header);

$jwt
    ->issuer('http://example.org')
    ->audience('http://example.com')
	->issuedAt(1356999524)
	->notBefore(1357000000)
	->expireTime(time()+3600)
	->jwtId('id123456')
	->type('https://example.com/register');

$result = $jwt->encode();
echo 'ENCODED: '.print_r($result)."\n\n";
echo 'DECODED: '.var_export($jwt->decode($result), true);

?>

通过 OpenSSL 加密

JWT 库也支持加密生成的 JWT 格式化字符串。以下是一个使用示例

<?php

require_once 'vendor/autoload.php';

$key = 'example_key';
$encryptKey = 'my-encryption-key';

$header = new \Psecio\Jwt\Header($key);
$jwt = new \Psecio\Jwt\Jwt($header);

$jwt
    ->issuer('http://example.org')
    ->audience('http://example.com')
	->issuedAt(1356999524)
	->notBefore(1357000000)
	->expireTime(time()+3600)
	->jwtId('id123456')
	->type('https://example.com/register');

$result = $jwt->encrypt('AES-256-CBC', '1234567812345678', $encryptKey);

echo 'ENCRYPTED: '.var_export($result, true)."\n";
echo "DECRYPTED: ".var_export($jwt->decrypt($result, 'AES-256-CBC', '1234567812345678', $encryptKey), true)."\n";

?>

自定义声明值

您还可以使用 custom 方法将自定义声明值添加到 JWT 负载数据中。第一个参数是值,第二个是声明“类型”(键)

<?php
require_once 'vendor/autoload.php';

$key = "example_key";

$header = new \Psecio\Jwt\Header($key);

$jwt = new \Psecio\Jwt\Jwt($header);
$jwt->custom('foobar', 'custom-claim');

// Or, you can add more than one at the same time with an array
$jwt->custom(array(
    'custom-claim' => 'foorbar',
    'key1' => 'value1'
));

$result = $jwt->encode();
echo 'ENCODED: '.print_r($result)."\n\n";
echo 'DECODED: '.var_export($jwt->decode($result), true);
?>

您可以使用系统上提供的任何 OpenSSL 密码方法。

支持的声明类型

  • 受众 (aud)
  • 过期时间 (exp)
  • 签发时间 (iat)
  • 发行者 (iss)
  • JwtId (jit)
  • 不得早于 (nbf)
  • 主题 (sub)
  • 私有

哈希类型

默认情况下,此 JWT 工具使用 HMAC 哈希(HS256)来生成请求的签名。还有其他选项可以使用 OpenSSL 功能来使用公钥和私钥进行这些方法

  • HS256
  • HS384
  • HS512
  • ES256
  • ES384
  • ES512
  • RS256
  • RS384
  • RS512

您不能像使用 HMAC 哈希一样使用简单的文本字符串作为密钥,因此您必须为库提供一个有效的密钥实例。以下是一个使用 .pem 私钥文件和 RS256 哈希的示例

<?php
$key = openssl_pkey_get_private('file://'.__DIR__.'/private.pem', 'test1234');

$header = new \Psecio\Jwt\Header($key);
$header->setAlgorithm('RS256');

// or you can define the hash algorithm on the init too:
$header = new \Psecio\Jwt\Header($key, 'RS256');
?>

如果密钥无效且无法用于签名请求,将抛出异常(\Psecio\Jwt\Exception\InvalidKeyException)。如果在实际签名消息过程中发生错误,将抛出异常 \Psecio\Jwt\Exception\SignatureErrorException

JSON Web Tokens 文档