From a6dc955eefcd528eba648fb20d9320d2aa10562c Mon Sep 17 00:00:00 2001 From: liyuzhao <562405704@qq.com> Date: Fri, 2 Feb 2024 20:55:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=201=E3=80=81=E4=BF=AE=E5=A4=8Dmake?= =?UTF-8?q?=E5=9C=A8hyperf=E9=AB=98=E7=89=88=E6=9C=AC=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=97=B6=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=202?= =?UTF-8?q?=E3=80=81=E4=BF=AE=E5=A4=8DgetSceneByToken=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=97=B6=E5=87=BA=E7=8E=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs: #70 --- composer.json | 5 ++++- src/AbstractJWT.php | 4 ++-- src/Functions.php | 25 +++++++++++++++++++++++++ src/JWT.php | 15 ++++++++------- src/Util/JWTUtil.php | 7 +++---- 5 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 src/Functions.php diff --git a/composer.json b/composer.json index fdbf2c3..64af432 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,10 @@ "autoload": { "psr-4": { "Phper666\\JWTAuth\\": "src/" - } + }, + "files": [ + "src/Functions.php" + ] }, "autoload-dev": {}, "require": { diff --git a/src/AbstractJWT.php b/src/AbstractJWT.php index 1d927d4..18ad296 100644 --- a/src/AbstractJWT.php +++ b/src/AbstractJWT.php @@ -52,9 +52,9 @@ abstract function matchRoute(string $scene, string $requestMethod, string $reque * 获取jwt中的场景值 * * @param string $token - * @return bool + * @return mixed */ - abstract function getSceneByToken(string $token): bool; + abstract function getSceneByToken(string $token): array; /** * 刷新jwt token diff --git a/src/Functions.php b/src/Functions.php new file mode 100644 index 0000000..c0e58ab --- /dev/null +++ b/src/Functions.php @@ -0,0 +1,25 @@ +make($name, $parameters); + } + } + $parameters = array_values($parameters); + return new $name(...$parameters); +} diff --git a/src/JWT.php b/src/JWT.php index 6273fae..612a8ac 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -88,7 +88,7 @@ class JWT extends AbstractJWT public function __construct() { - $config = make(ConfigInterface::class); + $config = hyperf_make(ConfigInterface::class); $jwtConfig = $config->get(JWTConstant::CONFIG_NAME, []); $scenes = $jwtConfig['scene']; foreach ($scenes as $key => $scene) { @@ -98,9 +98,9 @@ public function __construct() } $this->jwtConfig = $config->get(JWTConstant::CONFIG_NAME, []); - $this->cache = make(CacheInterface::class); - $this->request = make(RequestInterface::class); - $this->pathMatch = make(PathMatch::class); + $this->cache = hyperf_make(CacheInterface::class); + $this->request = hyperf_make(RequestInterface::class); + $this->pathMatch = hyperf_make(PathMatch::class); } /** @@ -196,7 +196,7 @@ public function getToken(string $scene, array $claims): Plain * * @return mixed */ - public function getJwtSceneConfig(string $scene = null) { + public function getJwtSceneConfig(string $scene = null): array { if ($scene == null) { return $this->jwtConfig[$this->getScene()]; } @@ -501,10 +501,11 @@ public function getTTL(string $token): int return (int)$sceneConfig['ttl']; } - public function getSceneByToken(string $token): bool + public function getSceneByToken(string $token): array { if($token == null) { $token = JWTUtil::getToken($this->request); + if (!$token) return []; } $token = $this->tokenToPlain($token); $scene = $this->getSceneByTokenPlain($token); @@ -572,7 +573,7 @@ protected function validationConstraints(DataSet $claims, Configuration $configu * 通过token获取当前场景的配置 * * @param Plain $token - * @return string + * @return array */ protected function getSceneConfigByToken(Plain $token): array { diff --git a/src/Util/JWTUtil.php b/src/Util/JWTUtil.php index 759edac..0d3203c 100644 --- a/src/Util/JWTUtil.php +++ b/src/Util/JWTUtil.php @@ -25,7 +25,7 @@ class JWTUtil * @param $claims * @return mixed */ - public static function claimsToArray(DataSet $claims) + public static function claimsToArray(DataSet $claims): array { return $claims->all(); } @@ -38,8 +38,7 @@ public static function claimsToArray(DataSet $claims) public static function getToken(ServerRequestInterface $request) { $token = $request->getHeaderLine('Authorization') ?? ''; - $token = self::handleToken($token); - return $token; + return self::handleToken($token); } /** @@ -47,7 +46,7 @@ public static function getToken(ServerRequestInterface $request) * @param ServerRequestInterface $request * @return array */ - public static function getParserData(ServerRequestInterface $request) + public static function getParserData(ServerRequestInterface $request): array { $token = $request->getHeaderLine('Authorization') ?? ''; $token = self::handleToken($token); From abbb1427d67d636933802d22f204af898e849f4a Mon Sep 17 00:00:00 2001 From: liyuzhao <562405704@qq.com> Date: Fri, 2 Feb 2024 21:22:56 +0800 Subject: [PATCH 2/2] fix: update refs: #70 --- src/Functions.php | 33 ++++++++++++++++++++++----------- src/JWT.php | 8 ++++---- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Functions.php b/src/Functions.php index c0e58ab..1943c87 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -9,17 +9,28 @@ namespace Phper666\JWTAuth; -use Hyperf\Context\ApplicationContext; - -function hyperf_make(string $name, array $parameters = []) -{ - if (ApplicationContext::hasContainer()) { - /** @var \Hyperf\Di\Container $container */ - $container = ApplicationContext::getContainer(); - if (method_exists($container, 'make')) { - return $container->make($name, $parameters); +if (!function_exists('make')) { + function make(string $name, array $parameters = []) + { + if (class_exists('\Hyperf\Context\ApplicationContext')) { + if (\Hyperf\Context\ApplicationContext::hasContainer()) { + /** @var \Hyperf\Di\Container $container */ + $container = \Hyperf\Context\ApplicationContext::getContainer(); + if (method_exists($container, 'make')) { + return $container->make($name, $parameters); + } + } + } elseif (class_exists('\Hyperf\Utils\ApplicationContext')) { + if (\Hyperf\Utils\ApplicationContext::hasContainer()) { + /** @var \Hyperf\Di\Container $container */ + $container = \Hyperf\Utils\ApplicationContext::getContainer(); + if (method_exists($container, 'make')) { + return $container->make($name, $parameters); + } + } } + + $parameters = array_values($parameters); + return new $name(...$parameters); } - $parameters = array_values($parameters); - return new $name(...$parameters); } diff --git a/src/JWT.php b/src/JWT.php index 612a8ac..1357aa0 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -88,7 +88,7 @@ class JWT extends AbstractJWT public function __construct() { - $config = hyperf_make(ConfigInterface::class); + $config = make(ConfigInterface::class); $jwtConfig = $config->get(JWTConstant::CONFIG_NAME, []); $scenes = $jwtConfig['scene']; foreach ($scenes as $key => $scene) { @@ -98,9 +98,9 @@ public function __construct() } $this->jwtConfig = $config->get(JWTConstant::CONFIG_NAME, []); - $this->cache = hyperf_make(CacheInterface::class); - $this->request = hyperf_make(RequestInterface::class); - $this->pathMatch = hyperf_make(PathMatch::class); + $this->cache = make(CacheInterface::class); + $this->request = make(RequestInterface::class); + $this->pathMatch = make(PathMatch::class); } /**