MainController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace mobile\controllers;
  3. use biz\user\services\UserService;
  4. use common\components\token;
  5. use Yii;
  6. use yii\db\Exception;
  7. use yii\web\Controller;
  8. use Lcobucci\JWT\Configuration;
  9. use Lcobucci\JWT\Signer\Hmac\Sha256;
  10. use Lcobucci\JWT\Signer\Key;
  11. use Lcobucci\JWT\Token\Plain;
  12. class MainController extends PublicController
  13. {
  14. public $enableCsrfValidation = false;
  15. public function actionIndex()
  16. {
  17. $host = Yii::$app->request->getHostInfo();
  18. $fullUrl = Yii::$app->request->url;
  19. $token = strpos($fullUrl,'token') === false ? '' : 'hasToken';
  20. return $this->renderPartial('index', ['host' => $host,'token'=>$token]);
  21. }
  22. public function actionJwt()
  23. {
  24. $secretKey = Yii::$app->params['secretKey'];
  25. $key = new Key($secretKey);
  26. $sha = new Sha256();
  27. $config = Configuration::forSymmetricSigner($sha, $key);
  28. $now = new \DateTimeImmutable();
  29. /*
  30. $token = $config->createBuilder()
  31. // Configures the issuer (iss claim)
  32. ->issuedBy('http://example.com')
  33. // Configures the audience (aud claim)
  34. ->permittedFor('http://example.org')
  35. // Configures the id (jti claim)
  36. ->identifiedBy('4f1g23a12aa')
  37. // Configures the time that the token was issue (iat claim)
  38. ->issuedAt($now)
  39. // Configures the time that the token can be used (nbf claim)
  40. ->canOnlyBeUsedAfter($now->modify('+1 minute'))
  41. // Configures the expiration time of the token (exp claim)
  42. ->expiresAt($now->modify('+1 hour'))
  43. // Configures a new claim, called "uid"
  44. ->withClaim('uid', 1)
  45. // Configures a new header, called "foo"
  46. ->withHeader('foo', 'bar')
  47. // Builds a new token
  48. ->getToken($config->getSigner(), $config->getSigningKey());
  49. */
  50. $token = $config->createBuilder()
  51. ->issuedBy('http://example.com')
  52. ->withClaim('uid', 1)
  53. ->withHeader('foo', 'bar')
  54. ->getToken($config->getSigner(), $config->getSigningKey());
  55. $token->headers(); // Retrieves the token headers
  56. $token->claims(); // Retrieves the token claims
  57. echo "<pre>";
  58. echo $token->headers()->get('foo'); // will print "bar"
  59. echo "<br/>";
  60. echo $token->claims()->get('iss'); // will print "http://example.com"
  61. echo "<br/>";
  62. echo $token->claims()->get('uid'); // will print "1"
  63. echo "<br/>";
  64. echo $token; // The string representation of the object is a JWT string
  65. $config->setValidator();
  66. $token = $config->getParser()->parse(
  67. 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImZvbyI6ImJhciJ9.'
  68. . 'eyJpc3MiOiJodHRwOi8vZXhhbXBsZS5jb20iLCJ1aWQiOjF9.'
  69. . '-FXkiIJQ2WXeSfh1ReUIgWDTn6EXxMbOzYL7fFricCo'
  70. );
  71. try {
  72. $config->getValidator()->assert($token);
  73. } catch (InvalidToken $e) {
  74. // list of constraints violation exceptions:
  75. var_dump($e->violations());
  76. }
  77. $token->headers(); // Retrieves the token headers
  78. $r = $token->claims(); // Retrieves the token claims
  79. print_r($r);die;
  80. }
  81. }