token.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * User: shish
  4. * Date: 2019/11/20
  5. * Time: 23:21
  6. */
  7. namespace common\components;
  8. use Lcobucci\JWT\Builder;
  9. use Lcobucci\JWT\Signer\Hmac\Sha256;
  10. use Lcobucci\JWT\Parser;
  11. use Lcobucci\JWT\ValidationData;
  12. use Yii;
  13. class token
  14. {
  15. public static function create($uid)
  16. {
  17. // $host = Yii::$app->request->getHostInfo();
  18. // $time = time();
  19. // $token = (new Builder())->issuedBy($host) // Configures the issuer (iss claim)
  20. // ->permittedFor($host) // Configures the audience (aud claim)
  21. // ->identifiedBy('5f1g23a12awP1', true) // Configures the id (jti claim), replicating as a header item
  22. // ->issuedAt($time) // Configures the time that the token was issue (iat claim)
  23. // ->canOnlyBeUsedAfter($time + 60) // Configures the time that the token can be used (nbf claim)
  24. // ->expiresAt($time + 3600) // Configures the expiration time of the token (exp claim)
  25. // ->withClaim('uid', $uid) // Configures a new claim, called "uid"
  26. // ->getToken(); // Retrieves the generated token
  27. // return $token;
  28. }
  29. public static function validate($token)
  30. {
  31. // $token = (new Parser())->parse((string)$token);
  32. // $signer = Sha256();
  33. // //签名不正确
  34. // if (!$token->verify($signer, '2019.1964')) {
  35. // return false;
  36. // }
  37. // $host = Yii::$app->request->getHostInfo();
  38. // $validationData = new ValidationData();
  39. // $validationData->setIssuer($host);
  40. // $validationData->setAudience($host);
  41. // $validationData->setId('sxs-4f1g23a12aa');//自字义标识
  42. //
  43. // return $token->validate($validationData);
  44. }
  45. }