|
|
@@ -1,19 +1,103 @@
|
|
|
<?php
|
|
|
+
|
|
|
namespace mobile\controllers;
|
|
|
|
|
|
use biz\user\services\UserService;
|
|
|
use common\components\token;
|
|
|
use Yii;
|
|
|
+use yii\db\Exception;
|
|
|
use yii\web\Controller;
|
|
|
|
|
|
-class MainController extends PublicController{
|
|
|
+use Lcobucci\JWT\Configuration;
|
|
|
+use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
|
+use Lcobucci\JWT\Signer\Key;
|
|
|
+use Lcobucci\JWT\Token\Plain;
|
|
|
|
|
|
+class MainController extends PublicController
|
|
|
+{
|
|
|
+
|
|
|
public $enableCsrfValidation = false;
|
|
|
-
|
|
|
+
|
|
|
public function actionIndex()
|
|
|
{
|
|
|
$host = Yii::$app->request->getHostInfo();
|
|
|
- return $this->renderPartial('index',['host'=>$host]);
|
|
|
+ return $this->renderPartial('index', ['host' => $host]);
|
|
|
}
|
|
|
+
|
|
|
+ public function actionJwt()
|
|
|
+ {
|
|
|
+
|
|
|
+ $secretKey = Yii::$app->params['secretKey'];
|
|
|
+ $key = new Key($secretKey);
|
|
|
+ $sha = new Sha256();
|
|
|
+ $config = Configuration::forSymmetricSigner($sha, $key);
|
|
|
|
|
|
+ $now = new \DateTimeImmutable();
|
|
|
+ /*
|
|
|
+ $token = $config->createBuilder()
|
|
|
+ // Configures the issuer (iss claim)
|
|
|
+ ->issuedBy('http://example.com')
|
|
|
+ // Configures the audience (aud claim)
|
|
|
+ ->permittedFor('http://example.org')
|
|
|
+ // Configures the id (jti claim)
|
|
|
+ ->identifiedBy('4f1g23a12aa')
|
|
|
+ // Configures the time that the token was issue (iat claim)
|
|
|
+ ->issuedAt($now)
|
|
|
+ // Configures the time that the token can be used (nbf claim)
|
|
|
+ ->canOnlyBeUsedAfter($now->modify('+1 minute'))
|
|
|
+ // Configures the expiration time of the token (exp claim)
|
|
|
+ ->expiresAt($now->modify('+1 hour'))
|
|
|
+ // Configures a new claim, called "uid"
|
|
|
+ ->withClaim('uid', 1)
|
|
|
+ // Configures a new header, called "foo"
|
|
|
+ ->withHeader('foo', 'bar')
|
|
|
+ // Builds a new token
|
|
|
+ ->getToken($config->getSigner(), $config->getSigningKey());
|
|
|
+ */
|
|
|
+
|
|
|
+ $token = $config->createBuilder()
|
|
|
+ ->issuedBy('http://example.com')
|
|
|
+ ->withClaim('uid', 1)
|
|
|
+ ->withHeader('foo', 'bar')
|
|
|
+ ->getToken($config->getSigner(), $config->getSigningKey());
|
|
|
+
|
|
|
+ $token->headers(); // Retrieves the token headers
|
|
|
+ $token->claims(); // Retrieves the token claims
|
|
|
+ echo "<pre>";
|
|
|
+ echo $token->headers()->get('foo'); // will print "bar"
|
|
|
+ echo "<br/>";
|
|
|
+ echo $token->claims()->get('iss'); // will print "http://example.com"
|
|
|
+ echo "<br/>";
|
|
|
+ echo $token->claims()->get('uid'); // will print "1"
|
|
|
+ echo "<br/>";
|
|
|
+ echo $token; // The string representation of the object is a JWT string
|
|
|
+
|
|
|
+ $config->setValidator();
|
|
|
+
|
|
|
+
|
|
|
+ $token = $config->getParser()->parse(
|
|
|
+ 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImZvbyI6ImJhciJ9.'
|
|
|
+ . 'eyJpc3MiOiJodHRwOi8vZXhhbXBsZS5jb20iLCJ1aWQiOjF9.'
|
|
|
+ . '-FXkiIJQ2WXeSfh1ReUIgWDTn6EXxMbOzYL7fFricCo'
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ $config->getValidator()->assert($token);
|
|
|
+ } catch (InvalidToken $e) {
|
|
|
+ // list of constraints violation exceptions:
|
|
|
+ var_dump($e->violations());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $token->headers(); // Retrieves the token headers
|
|
|
+ $r = $token->claims(); // Retrieves the token claims
|
|
|
+ print_r($r);die;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|