|
|
@@ -8,10 +8,11 @@ use Yii;
|
|
|
use yii\db\Exception;
|
|
|
use yii\web\Controller;
|
|
|
|
|
|
-use Lcobucci\JWT\Configuration;
|
|
|
-use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
|
+use Lcobucci\JWT\Builder;
|
|
|
+use Lcobucci\JWT\Parser;
|
|
|
+use Lcobucci\JWT\ValidationData;
|
|
|
use Lcobucci\JWT\Signer\Key;
|
|
|
-use Lcobucci\JWT\Token\Plain;
|
|
|
+use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
|
|
|
|
class MainController extends PublicController
|
|
|
{
|
|
|
@@ -22,83 +23,70 @@ class MainController extends PublicController
|
|
|
{
|
|
|
$host = Yii::$app->request->getHostInfo();
|
|
|
$fullUrl = Yii::$app->request->url;
|
|
|
- $token = strpos($fullUrl,'token') === false ? '' : 'hasToken';
|
|
|
- return $this->renderPartial('index', ['host' => $host,'token'=>$token]);
|
|
|
+ $token = strpos($fullUrl, 'token') === false ? '' : 'hasToken';
|
|
|
+ return $this->renderPartial('index', ['host' => $host, 'token' => $token]);
|
|
|
}
|
|
|
|
|
|
public function actionJwt()
|
|
|
{
|
|
|
+ $time = time();
|
|
|
+ $signer = new Sha256();
|
|
|
+ $key = new Key('testing');
|
|
|
+ $token = (new Builder())->issuedBy('http://example.com')//发布者的url地址
|
|
|
|
|
|
- $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
|
|
|
+ ->canOnlyBeUsedBy('http://example.org')//接受者的url地址
|
|
|
+ ->identifiedBy('12358', true)//该jwt的唯一ID编号
|
|
|
+ ->issuedAt($time)//该jwt的发布时间
|
|
|
+ ->canOnlyBeUsedAfter($time + 60)//该jwt的使用时间不能早于该时间
|
|
|
+ ->expiresAt($time + 3600)//该jwt销毁的时间
|
|
|
+ ->with('uid', 1)
|
|
|
+ ->set('username', 'lili')//设置一个变量
|
|
|
+ ->sign($signer, $key)//设置一个变量,同set
|
|
|
+ ->getToken();
|
|
|
+ echo '<pre>';
|
|
|
+ echo $token;
|
|
|
|
|
|
- $config->setValidator();
|
|
|
+ echo '<br />';
|
|
|
+ $token->getHeaders(); // Retrieves the token headers
|
|
|
+ $token->getClaims(); // Retrieves the token claims
|
|
|
+ echo '<br />';
|
|
|
+ echo $token->getHeader('jti'); // will print "4f1g23a12aa"
|
|
|
+ echo '<br />';
|
|
|
+ echo $token->getClaim('iss'); // will print "http://example.com"
|
|
|
|
|
|
-
|
|
|
- $token = $config->getParser()->parse(
|
|
|
- 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImZvbyI6ImJhciJ9.'
|
|
|
- . 'eyJpc3MiOiJodHRwOi8vZXhhbXBsZS5jb20iLCJ1aWQiOjF9.'
|
|
|
- . '-FXkiIJQ2WXeSfh1ReUIgWDTn6EXxMbOzYL7fFricCo'
|
|
|
- );
|
|
|
+ $token = (new Parser())->parse((string)'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjEyMzU4In0.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLmNvbSIsImF1ZCI6Imh0dHA6XC9cL2V4YW1wbGUub3JnIiwianRpIjoiMTIzNTgiLCJpYXQiOjE1NzQzNDk5OTYsIm5iZiI6MTU3NDM1MDA1NiwiZXhwIjoxNTc0MzUzNTk2LCJ1aWQiOjF9.6FtTHmMlqhI1BNoFJiJrmm1nXXtc5-u2tVt-A8s8IN8
|
|
|
+');
|
|
|
|
|
|
+ $token->getHeaders(); // Retrieves the token header
|
|
|
+ $token->getClaims(); // Retrieves the token claims
|
|
|
+ echo "<br />....<br />";
|
|
|
+ echo $token->getHeader('jti'); // will print "4f1g23a12aa"
|
|
|
+ echo '<br />';
|
|
|
+ echo $token->getClaim('iss'); // will print "http://example.com"
|
|
|
+ echo '<br />';
|
|
|
+ echo $token->getClaim('uid'); // will print "1"
|
|
|
+ echo '<br />';
|
|
|
|
|
|
+ $data = new ValidationData(); // It will use the current time to validate (iat, nbf and exp)
|
|
|
|
|
|
+ $data->setIssuer('http://example.com');
|
|
|
+ $data->setAudience('http://example.org');
|
|
|
+ $data->setId('12358');
|
|
|
|
|
|
+ //先验证私钥
|
|
|
+ var_dump($token->verify($signer, $key));
|
|
|
|
|
|
- try {
|
|
|
- $config->getValidator()->assert($token);
|
|
|
- } catch (InvalidToken $e) {
|
|
|
- // list of constraints violation exceptions:
|
|
|
- var_dump($e->violations());
|
|
|
- }
|
|
|
+ //失败,因为token在60秒后方可验证
|
|
|
+ var_dump($token->validate($data));
|
|
|
|
|
|
+ $data->setCurrentTime($time + 61); // changing the validation time to future
|
|
|
|
|
|
+ // true
|
|
|
+ var_dump($token->validate($data));
|
|
|
|
|
|
- $token->headers(); // Retrieves the token headers
|
|
|
- $r = $token->claims(); // Retrieves the token claims
|
|
|
- print_r($r);die;
|
|
|
+ $data->setCurrentTime(time() + 4000);
|
|
|
+ //false,token过期
|
|
|
+ var_dump($token->validate($data));
|
|
|
|
|
|
}
|
|
|
|