From 157c233ed66daadb9829473802a006be6c1b8be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 26 Nov 2021 11:27:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kiri-engine/Jwt/JWTAuthMiddleware.php | 2 +- kiri-engine/Jwt/Jwt.php | 17 +++++++++---- kiri-engine/Jwt/JwtHelper.php | 36 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/kiri-engine/Jwt/JWTAuthMiddleware.php b/kiri-engine/Jwt/JWTAuthMiddleware.php index 733e56a0..aebab309 100644 --- a/kiri-engine/Jwt/JWTAuthMiddleware.php +++ b/kiri-engine/Jwt/JWTAuthMiddleware.php @@ -12,7 +12,7 @@ use Kiri\Kiri; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; -use Server\Constrict\ResponseInterface; +use Http\Constrict\ResponseInterface; /** * Class CoreMiddleware diff --git a/kiri-engine/Jwt/Jwt.php b/kiri-engine/Jwt/Jwt.php index e4865e8c..4b2bc3fd 100644 --- a/kiri-engine/Jwt/Jwt.php +++ b/kiri-engine/Jwt/Jwt.php @@ -8,8 +8,10 @@ use Exception; use Http\Constrict\RequestInterface; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; +use Kiri\Cache\Redis; use Kiri\Core\Json; use Kiri\Exception\ConfigException; +use Kiri\Kiri; /** @@ -80,7 +82,13 @@ class Jwt extends Component if (!isset($this->data['source'])) { $this->data['source'] = 'browser'; } - return $this->createEncrypt($unionId); + $token = $this->createEncrypt($unionId); + if ($this->oos) { + $redis = Kiri::getDi()->get(Redis::class); + $redis->set('_jwt:token:' . $unionId, $token); + $redis->expire('_jwt:token:' . $unionId, $this->timeout); + } + return $token; } @@ -89,14 +97,13 @@ class Jwt extends Component */ private function jwtHeader(): string { - $string = openssl_encrypt( - json_encode(['type' => 'openssl', 'encrypt' => $this->encrypt]), + return openssl_encrypt( + json_encode(['type' => 'openssl', 'encrypt' => $this->encrypt], JSON_UNESCAPED_UNICODE), $this->encrypt, $this->key, 0, $this->iv ); - return str_replace('=', '', $string); } @@ -107,7 +114,7 @@ class Jwt extends Component */ private function jwtBody($unionId): string { - $json = json_encode(['unionId' => $unionId, 'createTime' => time(), 'expire_at' => time() + $this->timeout]); + $json = json_encode(['unionId' => $unionId, 'createTime' => time(), 'expire_at' => time() + $this->timeout], JSON_UNESCAPED_UNICODE); openssl_private_encrypt($json, $encode, $this->private); return base64_encode($encode); } diff --git a/kiri-engine/Jwt/JwtHelper.php b/kiri-engine/Jwt/JwtHelper.php index 306fef6b..ddcd3c58 100644 --- a/kiri-engine/Jwt/JwtHelper.php +++ b/kiri-engine/Jwt/JwtHelper.php @@ -32,6 +32,12 @@ trait JwtHelper private string $iv = ''; + private string $iss = 'jwt.service'; + + + private bool $oos = false; + + private ?string $public = '-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6BuML3gtLGde7QKNuNST UCB9gdHC7XIpOc7Wx2I64Esj3UxWHTgp3URj0ge8zpy7A3FfBdppR7d1nwoD6Xad @@ -70,7 +76,37 @@ kIHG5j+Qga2CgXqI2Y5URXGz5XlsNbMNFUrnWcbpqEbW5O6/BgHLLSDEyQgwbygN mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY= -----END RSA PRIVATE KEY-----'; + /** + * @return string + */ + public function getIss(): string + { + return $this->iss; + } + /** + * @param string $iss + */ + public function setIss(string $iss): void + { + $this->iss = $iss; + } + + /** + * @return bool + */ + public function isOos(): bool + { + return $this->oos; + } + + /** + * @param bool $oos + */ + public function setOos(bool $oos): void + { + $this->oos = $oos; + } /**