JwtAuth.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\utils;
  12. use crmeb\exceptions\AdminException;
  13. use crmeb\services\CacheService;
  14. use Firebase\JWT\JWT;
  15. use think\facade\Env;
  16. /**
  17. * Jwt
  18. * Class JwtAuth
  19. * @package crmeb\utils
  20. */
  21. class JwtAuth
  22. {
  23. /**
  24. * token
  25. * @var string
  26. */
  27. protected $token;
  28. /**
  29. * @var string
  30. */
  31. protected $app_key = 'crmeb_app_key';
  32. /**
  33. * 获取token
  34. * @param int $id
  35. * @param string $type
  36. * @param array $params
  37. * @return array
  38. */
  39. public function getToken(int $id, string $type, array $params = []): array
  40. {
  41. $host = app()->request->host();
  42. $time = time();
  43. $exp_time = strtotime('+ 7day');
  44. if (app()->request->isApp()) {
  45. $exp_time = strtotime('+ 30day');
  46. }
  47. if ($type == 'out') {
  48. $exp_time = strtotime('+ 1day');
  49. }
  50. $params += [
  51. 'iss' => $host,
  52. 'aud' => $host,
  53. 'iat' => $time,
  54. 'nbf' => $time,
  55. 'exp' => $exp_time,
  56. ];
  57. $params['jti'] = compact('id', 'type');
  58. $token = JWT::encode($params, Env::get('app.app_key', $this->app_key) ?: $this->app_key);
  59. return compact('token', 'params');
  60. }
  61. /**
  62. * 解析token
  63. * @param string $jwt
  64. * @return array
  65. */
  66. public function parseToken(string $jwt): array
  67. {
  68. $this->token = $jwt;
  69. [$headb64, $bodyb64, $cryptob64] = explode('.', $this->token);
  70. $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64));
  71. return [$payload->jti->id, $payload->jti->type, $payload->auth ?? ''];
  72. }
  73. /**
  74. * 验证token
  75. */
  76. public function verifyToken()
  77. {
  78. JWT::$leeway = 60;
  79. JWT::decode($this->token, Env::get('app.app_key', $this->app_key) ?: $this->app_key, array('HS256'));
  80. $this->token = null;
  81. }
  82. /**
  83. * 获取token并放入令牌桶
  84. * @param int $id
  85. * @param string $type
  86. * @param array $params
  87. * @return array
  88. */
  89. public function createToken(int $id, string $type, array $params = [])
  90. {
  91. $tokenInfo = $this->getToken($id, $type, $params);
  92. $exp = $tokenInfo['params']['exp'] - $tokenInfo['params']['iat'] + 60;
  93. $res = CacheService::setTokenBucket(md5($tokenInfo['token']), ['uid' => $id, 'type' => $type, 'token' => $tokenInfo['token'], 'exp' => $exp], (int)$exp, $type);
  94. if (!$res) {
  95. throw new AdminException(ApiErrorCode::ERR_SAVE_TOKEN);
  96. }
  97. return $tokenInfo;
  98. }
  99. }