level51 / silverstripe-jwt-utils
处理 SilverStripe 中 JWT 的工具类
1.0.0
2023-09-18 15:32 UTC
Requires
- firebase/php-jwt: ^6.0.0
- nesbot/carbon: ^2.70.0
- ramsey/uuid: ^4.7.4
Requires (Dev)
- phpunit/phpunit: ^9.6.12
- silverstripe/sqlite3: ^3.0.0
This package is auto-updated.
Last update: 2024-09-18 17:42:52 UTC
README
提供了一组辅助类,用于在 SilverStripe 配置中处理 JWT。
安装
composer require level51/silverstripe-jwt-utils
示例
use Level51\JWTUtils\JWTUtils; use Level51\JWTUtils\JWTUtilsException; class MyTokenController extends Controller { private static $allowed_actions = ['token']; public function token() { try { $payload = JWTUtils::inst()->byBasicAuth($this->request); return json_encode($payload); } catch(JWTUtilsException $e) { return $this->httpError(403, $e->getMessage()); } } }
配置和凭据正确时,将生成如下负载
{ "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6MSwiaXNzIjoiaHR0cDpcL1wvc2lsdmVyZ3JvdW5kLm1lXC8iLCJleHAiOjE1MTgyNzMwMjIsImlhdCI6MTUxNzY2ODIyMiwicmF0IjoxNTE3NjY4MjIyLCJqdGkiOiI0ZjIyMjViNS0wMzE5LTQ3YTMtYWNjMy1jOWJlNDk4MDc1NTIifQ.vQLLzmB7rWkwQDomAuC6Bfm-J0ITsIfFq4wL8UMAAJs", "member": { "id": 1, "email": "js@lvl51.de", "firstName": "Julian", "surname": "Scheuchenzuber" } }
配置
确保包含环境变量 JWT_SECRET
并选择一个随机值。
Level51\JWTUtils\JWTUtils: secret: '`JWT_SECRET`' # Secret for signature. This is mandatory and there is no default value lifetime_in_days: 7 # Term of validity renew_threshold_in_minutes: 60 # Keep JWT for at least 60 minutes
API
static inst()
: 获取单例实例byBasicAuth($request, $includeMemberData: bool = true): array
: 从基本认证成员数据创建新令牌renew($token: string): string
: 检查给定的令牌是否有效且需要续订check($token: string): bool
: 检查令牌是否有效且未过期
维护者
- JZubero js@lvl51.de