*/ namespace App\Utils; use Firebase\JWT\JWT; use Firebase\JWT\Key; use function Hyperf\Config\config; class Token { /** * 生成token * Author: cfn * @param array $data * @param int $expireIn * @return string */ public static function buildToken(array $data, int $expireIn): string { $payload = [ 'iss' => config("jwt.iss"), 'aud' => config("jwt.aud"), 'iat' => time(), 'exp' => time() + $expireIn, 'data' => $data ]; $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap(config('jwt.private'), 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----"; return JWT::encode($payload, $privateKey, 'RS256'); } /** * 解析token * Author: cfn * @param $token * @return array */ static function parseToken($token): array { try { $publicKey = "-----BEGIN PUBLIC KEY-----\n" . wordwrap(config('jwt.public'), 64, "\n", true) . "\n-----END PUBLIC KEY-----"; $decoded = JWT::decode($token, new Key($publicKey, 'RS256')); $decoded_array = (array)$decoded; return (array)$decoded_array['data']; } catch (\Exception $exception) { } return []; } }