MainController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. return $this->renderPartial('index', ['host' => $host]);
  19. }
  20. public function actionJwt()
  21. {
  22. $secretKey = Yii::$app->params['secretKey'];
  23. $key = new Key($secretKey);
  24. $sha = new Sha256();
  25. $config = Configuration::forSymmetricSigner($sha, $key);
  26. $now = new \DateTimeImmutable();
  27. /*
  28. $token = $config->createBuilder()
  29. // Configures the issuer (iss claim)
  30. ->issuedBy('http://example.com')
  31. // Configures the audience (aud claim)
  32. ->permittedFor('http://example.org')
  33. // Configures the id (jti claim)
  34. ->identifiedBy('4f1g23a12aa')
  35. // Configures the time that the token was issue (iat claim)
  36. ->issuedAt($now)
  37. // Configures the time that the token can be used (nbf claim)
  38. ->canOnlyBeUsedAfter($now->modify('+1 minute'))
  39. // Configures the expiration time of the token (exp claim)
  40. ->expiresAt($now->modify('+1 hour'))
  41. // Configures a new claim, called "uid"
  42. ->withClaim('uid', 1)
  43. // Configures a new header, called "foo"
  44. ->withHeader('foo', 'bar')
  45. // Builds a new token
  46. ->getToken($config->getSigner(), $config->getSigningKey());
  47. */
  48. $token = $config->createBuilder()
  49. ->issuedBy('http://example.com')
  50. ->withClaim('uid', 1)
  51. ->withHeader('foo', 'bar')
  52. ->getToken($config->getSigner(), $config->getSigningKey());
  53. $token->headers(); // Retrieves the token headers
  54. $token->claims(); // Retrieves the token claims
  55. echo "<pre>";
  56. echo $token->headers()->get('foo'); // will print "bar"
  57. echo "<br/>";
  58. echo $token->claims()->get('iss'); // will print "http://example.com"
  59. echo "<br/>";
  60. echo $token->claims()->get('uid'); // will print "1"
  61. echo "<br/>";
  62. echo $token; // The string representation of the object is a JWT string
  63. $config->setValidator();
  64. $token = $config->getParser()->parse(
  65. 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImZvbyI6ImJhciJ9.'
  66. . 'eyJpc3MiOiJodHRwOi8vZXhhbXBsZS5jb20iLCJ1aWQiOjF9.'
  67. . '-FXkiIJQ2WXeSfh1ReUIgWDTn6EXxMbOzYL7fFricCo'
  68. );
  69. try {
  70. $config->getValidator()->assert($token);
  71. } catch (InvalidToken $e) {
  72. // list of constraints violation exceptions:
  73. var_dump($e->violations());
  74. }
  75. $token->headers(); // Retrieves the token headers
  76. $r = $token->claims(); // Retrieves the token claims
  77. print_r($r);die;
  78. }
  79. }