Jwt.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace api\components;
  3. /**
  4. * Created by PhpStorm.
  5. * User: shizhongqi
  6. * Date: 2019/11/19
  7. * Time: 21:37
  8. */
  9. use Lcobucci\JWT\Builder;
  10. use Lcobucci\JWT\Claim\Factory as ClaimFactory;
  11. use Lcobucci\JWT\Parser;
  12. use Lcobucci\JWT\Parsing\Decoder;
  13. use Lcobucci\JWT\Parsing\Encoder;
  14. use Lcobucci\JWT\Signer\Key;
  15. use Lcobucci\JWT\Token;
  16. use Lcobucci\JWT\ValidationData;
  17. use Yii;
  18. //use yii\base\Component;
  19. use yii\base\InvalidParamException;
  20. class Jwt
  21. {
  22. /**
  23. * @var array 支持的加密算法
  24. */
  25. public $supportedAlgs = [
  26. 'HS256' => 'Lcobucci\JWT\Signer\Hmac\Sha256',
  27. 'HS384' => 'Lcobucci\JWT\Signer\Hmac\Sha384',
  28. 'HS512' => 'Lcobucci\JWT\Signer\Hmac\Sha512',
  29. ];
  30. /**
  31. * 创建JWT生成器
  32. * @param Encoder|null $encoder
  33. * @param ClaimFactory|null $claimFactory
  34. * @return Builder
  35. */
  36. public function getBuilder(Encoder $encoder = null, ClaimFactory $claimFactory = null)
  37. {
  38. return new Builder($encoder, $claimFactory);
  39. }
  40. /**
  41. * 创建JWT解析器
  42. * @param Decoder|null $decoder
  43. * @param ClaimFactory|null $claimFactory
  44. * @return Parser
  45. */
  46. public function getParser(Decoder $decoder = null, ClaimFactory $claimFactory = null)
  47. {
  48. return new Parser($decoder, $claimFactory);
  49. }
  50. /**
  51. * 验证JWT,并返回一个令牌类
  52. * @param $token
  53. * @param bool $validate
  54. * @param bool $verify
  55. * @return Token|null
  56. */
  57. public function validateJwt($token, $validate = true, $verify = true)
  58. {
  59. try {
  60. $token = $this->getParser()->parse((string)$token);
  61. } catch (\RuntimeException $e) {
  62. // Yii::warning("Invalid JWT provided: " . $e->getMessage(), 'jwt');
  63. return null;
  64. } catch (\InvalidArgumentException $e) {
  65. // Yii::warning("Invalid JWT provided: " . $e->getMessage(), 'jwt');
  66. return null;
  67. }
  68. if ($validate && !$this->validateToken($token)) {
  69. return null;
  70. }
  71. if ($verify && !$this->verifyToken($token)) {
  72. return null;
  73. }
  74. return $token;
  75. }
  76. /**
  77. * 数据验证
  78. * @param Token $token
  79. * @param null $currentTime
  80. * @return bool
  81. */
  82. public function validateToken(Token $token, $currentTime = null)
  83. {
  84. $data = new ValidationData($currentTime);
  85. // @todo Add claims for validation
  86. return $token->validate($data);
  87. }
  88. /**
  89. * Validate token
  90. * @param Token $token
  91. * @return bool
  92. */
  93. public function verifyToken(Token $token)
  94. {
  95. $alg = $token->getHeader('alg');
  96. if (empty($this->supportedAlgs[$alg])) {
  97. throw new InvalidParamException('Algorithm not supported');
  98. }
  99. $signer = Yii::createObject($this->supportedAlgs[$alg]);
  100. return $token->verify($signer, new Key('jwt_secret'));
  101. }
  102. }