JwtTokenService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use crmeb\exceptions\AuthException;
  13. use Firebase\JWT\BeforeValidException;
  14. use Firebase\JWT\ExpiredException;
  15. use Firebase\JWT\JWT;
  16. use Firebase\JWT\SignatureInvalidException;
  17. use think\facade\Config;
  18. use UnexpectedValueException;
  19. class JwtTokenService
  20. {
  21. /**
  22. * @param int $id
  23. * @param string $type
  24. * @param $exp
  25. * @param array $params
  26. * @return array
  27. * @author xaboy
  28. * @day 2020/10/13
  29. */
  30. public function createToken(int $id, string $type, $exp, array $params = [])
  31. {
  32. $time = time();
  33. $host = app('request')->host();
  34. $params += [
  35. 'iss' => $host,
  36. 'aud' => $host,
  37. 'iat' => $time,
  38. 'nbf' => $time,
  39. 'exp' => $exp,
  40. ];
  41. $params['jti'] = [$id, $type];
  42. $token = JWT::encode($params, Config::get('app.app_key', 'default'));
  43. $params['token'] = $token;
  44. $params['out'] = $exp - time();
  45. return $params;
  46. }
  47. /**
  48. * @param string $token
  49. * @return object
  50. * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
  51. * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
  52. * @throws BeforeValidException Provided JWT is trying to be used before it's been created as defined by 'iat'
  53. * @throws ExpiredException Provided JWT has since expired, as defined by the 'exp' claim
  54. * @throws UnexpectedValueException Provided JWT was invalid
  55. * @author xaboy
  56. * @day 2020-04-09
  57. */
  58. public function parseToken(string $token)
  59. {
  60. return JWT::decode($token, Config::get('app.app_key', 'default'), array('HS256'));
  61. }
  62. /**
  63. * @param string $token
  64. * @return object
  65. * @author xaboy
  66. * @day 2020-04-10
  67. */
  68. public function decode(string $token)
  69. {
  70. $tks = explode('.', $token);
  71. if (count($tks) != 3)
  72. throw new AuthException('Invalid token');
  73. if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($tks[1])))
  74. throw new AuthException('Invalid token');
  75. return $payload;
  76. }
  77. }