123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- // +----------------------------------------------------------------------
- // | Niucloud-admin 企业快速开发的saas管理平台
- // +----------------------------------------------------------------------
- // | 官方网址:https://www.niucloud.com
- // +----------------------------------------------------------------------
- // | niucloud团队 版权所有 开源版本可自由商用
- // +----------------------------------------------------------------------
- // | Author: Niucloud Team
- // +----------------------------------------------------------------------
- namespace core\util;
- use Firebase\JWT\JWT;
- use think\facade\Cache;
- use think\facade\Env;
- use think\Response;
- /**
- * token工具类
- * Class TokenAuth
- * @package core\util
- */
- class TokenAuth
- {
- /**
- *创建token
- * @param int $id 编码 一般传入用户id
- * @param string $type 类型(admin,site,home)
- * @param array $params 参数 传入id, name
- * @param int $expire_time 有效期
- * @return array
- */
- public static function createToken(int $id, string $type, array $params = [], int $expire_time = 0): array
- {
- $host = app()->request->host();
- $time = time();
- $params += [
- 'iss' => $host,
- 'aud' => $host,
- 'iat' => $time,
- 'nbf' => $time,
- 'exp' => $time + $expire_time,
- ];
- $params['jti'] = $id . "_" . $type;
- $token = JWT::encode($params, Env::get('app.app_key', 'niucloud456$%^'));
- $cache_token = Cache::store("jwt")->get("token_" . $params['jti']) ?: Cache::get("token_" . $params['jti']);
- $cache_token_arr = $cache_token ?: [];
- // if(!empty($cache_token))
- // {
- //
- // $cache_token_arr[] = $token;
- // }
- $cache_token_arr[] = $token;
- Cache::store("jwt")->tag("token")->set("token_" . $params['jti'], $cache_token_arr);
- return compact('token', 'params');
- }
- /**
- * 解析token
- * @param string $token
- * @param string $type
- * @return array
- */
- public static function parseToken(string $token, string $type): array
- {
- $payload = JWT::decode($token, Env::get('app.app_key', 'niucloud456$%^'), ['HS256']);
- if (!empty($payload)) {
- $token_info = json_decode(json_encode($payload), true, 512, JSON_THROW_ON_ERROR);
- if (explode("_", $token_info['jti'])[1] != $type) {
- return [];
- }
- $token_cache = Cache::store("jwt")->get("token_" . $token_info['jti']) ?: Cache::get("token_" . $token_info['jti'], []);
- if (!empty($token_info) && !in_array($token, $token_cache)) {
- return [];
- }
- return $token_info;
- } else {
- return [];
- }
- }
- /**
- * 清理token
- * @param int $id
- * @param string $type
- * @param string|null $token
- * @return Response
- */
- public static function clearToken(int $id, string $type, ?string $token = '')
- {
- if (!empty($token)) {
- $token_cache = Cache::store("jwt")->get("token_" . $id . "_" . $type) ?: Cache::get("token_" . $id . "_" . $type, []);
- //todo 也可以通过修改过期时间来实现
- if (!empty($token_cache)) {
- if (($key = array_search($token, $token_cache)) !== false) {
- array_splice($token_cache, $key, 1);
- }
- Cache::store("jwt")->set("token_" . $id . "_" . $type, $token_cache);
- }
- } else {
- Cache::store("jwt")->set("token_" . $id . "_" . $type, []);
- }
- return success();
- }
- }
|