data_center/app/api/service/JwtTokenService.php

92 lines
3.3 KiB
PHP
Raw Normal View History

2023-11-10 18:09:36 +08:00
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\service;
use app\common\cache\UserTokenCache;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Firebase\JWT\SignatureInvalidException;
use Firebase\JWT\BeforeValidException;
use Firebase\JWT\ExpiredException;
use think\facade\Config;
class JwtTokenService
{
/**
* @param int $id
* @param string $type
* @param $exp
* @param array $params
* @return array
* @author xaboy
* @day 2020/10/13
*/
public static function createToken(int $id, string $phone, array $params = [])
{
$time = time();
$host = app('request')->host();
$params += [
'iss' => $host,
'aud' => $host,
'iat' => $time,
'nbf' => $time,
'exp' => $time + 7 * 24 * 3600,
];
$params['data'] = [
'uid' => $id,
'phone' => $phone
];
$token = JWT::encode($params, env('app.app_key', '123456'), 'HS256');
$tokenInfo = [
'uid' => $id,
'phone' => $phone,
'token' => $token
];
return $tokenInfo;
}
/**
* @param string $token
* @return object
* @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
* @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
* @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
* @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
* @throws UnexpectedValueException Provided JWT was invalid
* @author xaboy
* @day 2020-04-09
*/
public static function parseToken(string $token)
{
try {
JWT::$leeway = 10; //当前时间减去10秒时间留点余地
$decoded = JWT::decode($token, new Key(env('app.app_key', '123456'), 'HS256'));
$decodedArray = json_decode(json_encode($decoded), true);
$jwtData = $decodedArray['data'];
return $jwtData;
} catch(\Firebase\JWT\SignatureInvalidException $e) {
throw new \think\Exception('签名错误');
2023-11-10 18:10:37 +08:00
} catch(\Firebase\JWT\BeforeValidException $e) {
2023-11-10 18:09:36 +08:00
throw new \think\Exception('token无效');
2023-11-10 18:10:37 +08:00
} catch(\Firebase\JWT\ExpiredException $e) {
2023-11-10 18:09:36 +08:00
throw new \think\Exception('token已过期');
2023-11-10 18:10:37 +08:00
} catch(\Exception $e) {
2023-11-10 18:09:36 +08:00
throw new \think\Exception('非法请求');
}
}
}