TokenAuth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\util;
  12. use Firebase\JWT\JWT;
  13. use think\facade\Cache;
  14. use think\facade\Env;
  15. use think\Response;
  16. /**
  17. * token工具类
  18. * Class TokenAuth
  19. * @package core\util
  20. */
  21. class TokenAuth
  22. {
  23. /**
  24. *创建token
  25. * @param int $id 编码 一般传入用户id
  26. * @param string $type 类型(admin,site,home)
  27. * @param array $params 参数 传入id, name
  28. * @param int $expire_time 有效期
  29. * @return array
  30. */
  31. public static function createToken(int $id, string $type, array $params = [], int $expire_time = 0): array
  32. {
  33. $host = app()->request->host();
  34. $time = time();
  35. $params += [
  36. 'iss' => $host,
  37. 'aud' => $host,
  38. 'iat' => $time,
  39. 'nbf' => $time,
  40. 'exp' => $time + $expire_time,
  41. ];
  42. $params['jti'] = $id . "_" . $type;
  43. $token = JWT::encode($params, Env::get('app.app_key', 'niucloud456$%^'));
  44. $cache_token = Cache::store("jwt")->get("token_" . $params['jti']) ?: Cache::get("token_" . $params['jti']);
  45. $cache_token_arr = $cache_token ?: [];
  46. // if(!empty($cache_token))
  47. // {
  48. //
  49. // $cache_token_arr[] = $token;
  50. // }
  51. $cache_token_arr[] = $token;
  52. Cache::store("jwt")->tag("token")->set("token_" . $params['jti'], $cache_token_arr);
  53. return compact('token', 'params');
  54. }
  55. /**
  56. * 解析token
  57. * @param string $token
  58. * @param string $type
  59. * @return array
  60. */
  61. public static function parseToken(string $token, string $type): array
  62. {
  63. $payload = JWT::decode($token, Env::get('app.app_key', 'niucloud456$%^'), ['HS256']);
  64. if (!empty($payload)) {
  65. $token_info = json_decode(json_encode($payload), true, 512, JSON_THROW_ON_ERROR);
  66. if (explode("_", $token_info['jti'])[1] != $type) {
  67. return [];
  68. }
  69. $token_cache = Cache::store("jwt")->get("token_" . $token_info['jti']) ?: Cache::get("token_" . $token_info['jti'], []);
  70. if (!empty($token_info) && !in_array($token, $token_cache)) {
  71. return [];
  72. }
  73. return $token_info;
  74. } else {
  75. return [];
  76. }
  77. }
  78. /**
  79. * 清理token
  80. * @param int $id
  81. * @param string $type
  82. * @param string|null $token
  83. * @return Response
  84. */
  85. public static function clearToken(int $id, string $type, ?string $token = '')
  86. {
  87. if (!empty($token)) {
  88. $token_cache = Cache::store("jwt")->get("token_" . $id . "_" . $type) ?: Cache::get("token_" . $id . "_" . $type, []);
  89. //todo 也可以通过修改过期时间来实现
  90. if (!empty($token_cache)) {
  91. if (($key = array_search($token, $token_cache)) !== false) {
  92. array_splice($token_cache, $key, 1);
  93. }
  94. Cache::store("jwt")->set("token_" . $id . "_" . $type, $token_cache);
  95. }
  96. } else {
  97. Cache::store("jwt")->set("token_" . $id . "_" . $type, []);
  98. }
  99. return success();
  100. }
  101. }