a4sex / jwt-manager
jwt-manager 用于 FreedomSex 项目
v1.1.1
2023-09-15 12:09 UTC
Requires
- php: ^8.2
- firebase/php-jwt: ^6.0
- symfony/deprecation-contracts: *
Requires (Dev)
- phpunit/phpunit: ^9.5
Suggests
This package is auto-updated.
Last update: 2024-09-15 14:32:41 UTC
README
用于 FreedomSex 项目
供您使用,不保证版本兼容性,以及其他一切
内部使用 Firebase\JWT
您需要创建用于签名令牌的密钥。
mkdir -P /config/keys/
cd config/keys
密钥示例
openssl genrsa -out private.key 1024
openssl rsa -in private.key -pubout -outform PEM -out public.key
在创建管理器实例时指定路径。并将 TimeToLive 生成的令牌作为最后一个参数
$manager = new JWTManager(
'../keys/private.key',
'../keys/public.key',
$this->ttl
);
生成 JWT 令牌
简单的 JWT 令牌。仅过期在有效负载中
$token = $manager->create();
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE2Mzg1NDkwMTN9.GfefYgNnxpUCU_n8t0d37tQP-pjP7euGhrHTDx4T3ta0Eaa5Bedved5KbzZF-yXMUstnXr3TVRu3dkbKCaf0h2OJp13LT1WgvsyrkMIeO2KRG-vwsFrGrzAHRu2O5OKa7WI3sIFDE-oc_khyPFvO01UdiLtpEISOh8ys3Dh32-8
头部
{
"typ": "JWT",
"alg": "RS256"
}
有效负载
{
"exp": 1638549013
}
基于用户实例的有效负载
$token = $manager->create($user);
// getId - uid
// getRoles - roles
// getIdentityId - uuid
// getAccess - access
// getSubject -sub
弃用
uuid
- 自 0.4 版本起弃用:使用 uid
和 id
$token = $manager->create($user);
// getId - id
// getUid - uid
// or
// getIdentityId - uid
// getId - id
// and
// getUid - uid (deprecated)
// and
// getIdentityId - uuid
过期 "替代" 方法
$token = $manager->create($user, 1638549013);
加载有效负载
$payload = $manager->load($token); // object return
$payload = (array) $manager->load($token); // array return
[
"uid" => 1
"exp" => 1638549013
]
继承和覆盖
覆盖 populatePayload
方法来定义自己的结构
public function populatePayload(array $payload, $user = null): array