From d19edf4fd5b4490c2b0b98a481d921f86a4394e8 Mon Sep 17 00:00:00 2001 From: hgc Date: Mon, 6 Jan 2025 10:47:22 +0800 Subject: [PATCH] jwtRpc --- app/middleware/JwtRpc.php | 88 ++++++ composer.json | 5 +- generated/GRPC/Auth/AccountRole.php | 59 ++++ generated/GRPC/Auth/AuthClient.php | 84 ++++++ generated/GRPC/Auth/AuthInterface.php | 22 ++ generated/GRPC/Auth/JwtClaims.php | 269 +++++++++++++++++++ generated/GRPC/Auth/JwtVerifyResult.php | 87 ++++++ generated/GRPC/Auth/ValidateJwtTokenReq.php | 58 ++++ generated/GRPC/Auth/ValidateJwtTokenResp.php | 143 ++++++++++ generated/GRPC/GPBMetadata/Auth.php | Bin 0 -> 1246 bytes 10 files changed, 814 insertions(+), 1 deletion(-) create mode 100644 app/middleware/JwtRpc.php create mode 100644 generated/GRPC/Auth/AccountRole.php create mode 100644 generated/GRPC/Auth/AuthClient.php create mode 100644 generated/GRPC/Auth/AuthInterface.php create mode 100644 generated/GRPC/Auth/JwtClaims.php create mode 100644 generated/GRPC/Auth/JwtVerifyResult.php create mode 100644 generated/GRPC/Auth/ValidateJwtTokenReq.php create mode 100644 generated/GRPC/Auth/ValidateJwtTokenResp.php create mode 100644 generated/GRPC/GPBMetadata/Auth.php diff --git a/app/middleware/JwtRpc.php b/app/middleware/JwtRpc.php new file mode 100644 index 0000000..0503049 --- /dev/null +++ b/app/middleware/JwtRpc.php @@ -0,0 +1,88 @@ +authClient = new AuthClient($host); + } + + public function process(Request $request, callable $handler): Response + { + + // 从请求头中获取 JWT token + $jwtToken = $request->header('Authorization'); + + if (!$jwtToken) { + return response()->json(['error' => 'Authorization token is missing'], 401); + } + + // 去除 Bearer 前缀 + if (strpos($jwtToken, 'Bearer ') === 0) { + $jwtToken = substr($jwtToken, 7); + } + try { + // 创建 GRPC 请求 + dump($jwtToken); + $grpcRequest = new ValidateJwtTokenReq(); + $grpcRequest->setJwtToken($jwtToken); + // 调用 GRPC 服务 + list($response, $status) = $this->authClient->ValidateJwtToken($grpcRequest)->wait(); + + if ($status->code !== \Grpc\STATUS_OK) { + return Json([ + 'code' => 1, + 'msg' => 'GRPC service error', + 'data' => [] + ]); + } + + // 检查验证结果 + if ($response->getResult() !== JwtVerifyResult::JWT_VERIFY_OK) { + return Json([ + 'code' => 1, + 'msg' => 'Invalid token', + 'data' => [] + ]); + } + + // 将解析的 claims 数据传递给下层业务逻辑 + $claims = $response->getClaims(); + dump($claims); + return Json([ + 'code' => 0, + 'msg' => 'test token well', + 'data' => [] + ]); + $request->jwtClaims = $claims; + +// if ($result['is_valid']) { +// dump("JWT 验证成功: " . json_encode($result['claims'])); +// } else { +// dump("JWT 验证失败: " . $result['error']); +// } + +// if (!$isValid) { +// return response()->json(['error' => 'Invalid token'], 401); +// } + + // 如果验证通过,则继续处理请求 + return $handler($request); + } catch (\Exception $e) { + return new Response(500, [], json_encode(['error' => 'Internal server error'])); + } + } +} diff --git a/composer.json b/composer.json index cc297ea..28f5219 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,9 @@ "phpoffice/phpspreadsheet": "^3.6", "grpc/grpc": "^1.38", "google/protobuf": "^4.29", - "firebase/php-jwt": "^6.10" + "firebase/php-jwt": "^6.10", + "spiral/roadrunner-cli": "^2.6", + "spiral/roadrunner-grpc": "^3.4" }, "suggest": { "ext-event": "For better performance. " @@ -53,6 +55,7 @@ "": "./", "app\\": "./app", "App\\": "./app", + "GRPC\\": "generated/GRPC", "app\\View\\Components\\": "./app/view/components" } }, diff --git a/generated/GRPC/Auth/AccountRole.php b/generated/GRPC/Auth/AccountRole.php new file mode 100644 index 0000000..c2e836f --- /dev/null +++ b/generated/GRPC/Auth/AccountRole.php @@ -0,0 +1,59 @@ +auth.AccountRole + */ +class AccountRole +{ + /** + * Generated from protobuf enum ACCOUNT_ROLE_UNSPECIFIED = 0; + */ + const ACCOUNT_ROLE_UNSPECIFIED = 0; + /** + * 管理员 + * + * Generated from protobuf enum ACCOUNT_ROLE_MANAGER = 1; + */ + const ACCOUNT_ROLE_MANAGER = 1; + /** + * 普通员工 + * + * Generated from protobuf enum ACCOUNT_ROLE_STAFF = 2; + */ + const ACCOUNT_ROLE_STAFF = 2; + + private static $valueToName = [ + self::ACCOUNT_ROLE_UNSPECIFIED => 'ACCOUNT_ROLE_UNSPECIFIED', + self::ACCOUNT_ROLE_MANAGER => 'ACCOUNT_ROLE_MANAGER', + self::ACCOUNT_ROLE_STAFF => 'ACCOUNT_ROLE_STAFF', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } +} + diff --git a/generated/GRPC/Auth/AuthClient.php b/generated/GRPC/Auth/AuthClient.php new file mode 100644 index 0000000..61a8f21 --- /dev/null +++ b/generated/GRPC/Auth/AuthClient.php @@ -0,0 +1,84 @@ + ChannelCredentials::createInsecure() + ]); + } + + parent::__construct($hostname, $opts, $channel); + } + + /** + * 调用 ValidateJwtToken 方法 + * + * @param ValidateJwtTokenReq $request 请求数据 + * @param array $metadata 元数据(可选) + * @param array $options 调用选项(可选) + * + * @return ValidateJwtTokenResp + */ + public function ValidateJwtToken( + ValidateJwtTokenReq $request, + array $metadata = [], + array $options = [] + ): ValidateJwtTokenResp + { + try { + // 调试:输出请求数据 +// dump('aaa',$request->getJwtToken()); + + // 调用 GRPC 服务 + $unaryCall = $this->_simpleRequest( + '/auth.Auth/ValidateJwtToken', // 方法的完整路径 + $request, + ['\GRPC\Auth\ValidateJwtTokenResp', 'decode'], // 解码器 + $metadata, + $options + ); + + // 使用 wait() 获取返回值 + list($response, $status) = $unaryCall->wait(); + + // 调试:输出 GRPC 状态码 + dump($status); + + // 检查请求是否成功 + if ($status->code !== \Grpc\STATUS_OK) { + throw new \Exception('GRPC call failed with status: ' . $status->details, $status->code); + } + + // 返回解析后的响应数据 + return $response; + } catch (\Exception $e) { + // 捕获异常并打印 + dump($e->getMessage()); + throw $e; // 继续抛出异常 + } + } +} + diff --git a/generated/GRPC/Auth/AuthInterface.php b/generated/GRPC/Auth/AuthInterface.php new file mode 100644 index 0000000..d680203 --- /dev/null +++ b/generated/GRPC/Auth/AuthInterface.php @@ -0,0 +1,22 @@ +auth.JwtClaims + */ +class JwtClaims extends \Google\Protobuf\Internal\Message +{ + /** + * 所属应用 + * + * Generated from protobuf field string app = 1; + */ + protected $app = ''; + /** + * 用户id + * + * Generated from protobuf field string uid = 2; + */ + protected $uid = ''; + /** + * 商户id + * + * Generated from protobuf field string merchant_id = 3; + */ + protected $merchant_id = ''; + /** + * 用户在商户下的角色 + * + * Generated from protobuf field .auth.AccountRole role = 4; + */ + protected $role = 0; + /** + * 过期时间戳 + * + * Generated from protobuf field int64 exp = 5; + */ + protected $exp = 0; + /** + * 可刷新时间戳 + * + * Generated from protobuf field int64 refresh = 6; + */ + protected $refresh = 0; + /** + * jwt token版本 + * + * Generated from protobuf field int64 ver = 7; + */ + protected $ver = 0; + + /** + * Constructor. + * + * @param array $data { + * Optional. Data for populating the Message object. + * + * @type string $app + * 所属应用 + * @type string $uid + * 用户id + * @type string $merchant_id + * 商户id + * @type int $role + * 用户在商户下的角色 + * @type int|string $exp + * 过期时间戳 + * @type int|string $refresh + * 可刷新时间戳 + * @type int|string $ver + * jwt token版本 + * } + */ + public function __construct($data = NULL) { + \GRPC\GPBMetadata\Auth::initOnce(); + parent::__construct($data); + } + + /** + * 所属应用 + * + * Generated from protobuf field string app = 1; + * @return string + */ + public function getApp() + { + return $this->app; + } + + /** + * 所属应用 + * + * Generated from protobuf field string app = 1; + * @param string $var + * @return $this + */ + public function setApp($var) + { + GPBUtil::checkString($var, True); + $this->app = $var; + + return $this; + } + + /** + * 用户id + * + * Generated from protobuf field string uid = 2; + * @return string + */ + public function getUid() + { + return $this->uid; + } + + /** + * 用户id + * + * Generated from protobuf field string uid = 2; + * @param string $var + * @return $this + */ + public function setUid($var) + { + GPBUtil::checkString($var, True); + $this->uid = $var; + + return $this; + } + + /** + * 商户id + * + * Generated from protobuf field string merchant_id = 3; + * @return string + */ + public function getMerchantId() + { + return $this->merchant_id; + } + + /** + * 商户id + * + * Generated from protobuf field string merchant_id = 3; + * @param string $var + * @return $this + */ + public function setMerchantId($var) + { + GPBUtil::checkString($var, True); + $this->merchant_id = $var; + + return $this; + } + + /** + * 用户在商户下的角色 + * + * Generated from protobuf field .auth.AccountRole role = 4; + * @return int + */ + public function getRole() + { + return $this->role; + } + + /** + * 用户在商户下的角色 + * + * Generated from protobuf field .auth.AccountRole role = 4; + * @param int $var + * @return $this + */ + public function setRole($var) + { + GPBUtil::checkEnum($var, \GRPC\Auth\AccountRole::class); + $this->role = $var; + + return $this; + } + + /** + * 过期时间戳 + * + * Generated from protobuf field int64 exp = 5; + * @return int|string + */ + public function getExp() + { + return $this->exp; + } + + /** + * 过期时间戳 + * + * Generated from protobuf field int64 exp = 5; + * @param int|string $var + * @return $this + */ + public function setExp($var) + { + GPBUtil::checkInt64($var); + $this->exp = $var; + + return $this; + } + + /** + * 可刷新时间戳 + * + * Generated from protobuf field int64 refresh = 6; + * @return int|string + */ + public function getRefresh() + { + return $this->refresh; + } + + /** + * 可刷新时间戳 + * + * Generated from protobuf field int64 refresh = 6; + * @param int|string $var + * @return $this + */ + public function setRefresh($var) + { + GPBUtil::checkInt64($var); + $this->refresh = $var; + + return $this; + } + + /** + * jwt token版本 + * + * Generated from protobuf field int64 ver = 7; + * @return int|string + */ + public function getVer() + { + return $this->ver; + } + + /** + * jwt token版本 + * + * Generated from protobuf field int64 ver = 7; + * @param int|string $var + * @return $this + */ + public function setVer($var) + { + GPBUtil::checkInt64($var); + $this->ver = $var; + + return $this; + } + +} + diff --git a/generated/GRPC/Auth/JwtVerifyResult.php b/generated/GRPC/Auth/JwtVerifyResult.php new file mode 100644 index 0000000..189080d --- /dev/null +++ b/generated/GRPC/Auth/JwtVerifyResult.php @@ -0,0 +1,87 @@ +auth.JwtVerifyResult + */ +class JwtVerifyResult +{ + /** + * Generated from protobuf enum JWT_VERIFY_UNSPECIFIED = 0; + */ + const JWT_VERIFY_UNSPECIFIED = 0; + /** + * jwt 正常 + * + * Generated from protobuf enum JWT_VERIFY_OK = 1; + */ + const JWT_VERIFY_OK = 1; + /** + * jwt 格式错误 + * + * Generated from protobuf enum JWT_VERIFY_BAD_FORMAT = 2; + */ + const JWT_VERIFY_BAD_FORMAT = 2; + /** + * jwt 签名错误 + * + * Generated from protobuf enum JWT_VERIFY_SIGN_FAILED = 3; + */ + const JWT_VERIFY_SIGN_FAILED = 3; + /** + * jwt 过期 + * + * Generated from protobuf enum JWT_VERIFY_EXPIRED = 4; + */ + const JWT_VERIFY_EXPIRED = 4; + /** + * jwt 被撤销 + * + * Generated from protobuf enum JWT_VERIFY_REVOKED = 5; + */ + const JWT_VERIFY_REVOKED = 5; + /** + * jwt 版本过低 + * + * Generated from protobuf enum JWT_VERSION_LOW = 6; + */ + const JWT_VERSION_LOW = 6; + + private static $valueToName = [ + self::JWT_VERIFY_UNSPECIFIED => 'JWT_VERIFY_UNSPECIFIED', + self::JWT_VERIFY_OK => 'JWT_VERIFY_OK', + self::JWT_VERIFY_BAD_FORMAT => 'JWT_VERIFY_BAD_FORMAT', + self::JWT_VERIFY_SIGN_FAILED => 'JWT_VERIFY_SIGN_FAILED', + self::JWT_VERIFY_EXPIRED => 'JWT_VERIFY_EXPIRED', + self::JWT_VERIFY_REVOKED => 'JWT_VERIFY_REVOKED', + self::JWT_VERSION_LOW => 'JWT_VERSION_LOW', + ]; + + public static function name($value) + { + if (!isset(self::$valueToName[$value])) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no name defined for value %s', __CLASS__, $value)); + } + return self::$valueToName[$value]; + } + + + public static function value($name) + { + $const = __CLASS__ . '::' . strtoupper($name); + if (!defined($const)) { + throw new UnexpectedValueException(sprintf( + 'Enum %s has no value defined for name %s', __CLASS__, $name)); + } + return constant($const); + } +} + diff --git a/generated/GRPC/Auth/ValidateJwtTokenReq.php b/generated/GRPC/Auth/ValidateJwtTokenReq.php new file mode 100644 index 0000000..0362107 --- /dev/null +++ b/generated/GRPC/Auth/ValidateJwtTokenReq.php @@ -0,0 +1,58 @@ +auth.ValidateJwtTokenReq + */ +class ValidateJwtTokenReq extends \Google\Protobuf\Internal\Message +{ + /** + * Generated from protobuf field string jwt_token = 1; + */ + protected $jwt_token = ''; + + /** + * Constructor. + * + * @param array $data { + * Optional. Data for populating the Message object. + * + * @type string $jwt_token + * } + */ + public function __construct($data = NULL) { + \GRPC\GPBMetadata\Auth::initOnce(); + parent::__construct($data); + } + + /** + * Generated from protobuf field string jwt_token = 1; + * @return string + */ + public function getJwtToken() + { + return $this->jwt_token; + } + + /** + * Generated from protobuf field string jwt_token = 1; + * @param string $var + * @return $this + */ + public function setJwtToken($var) + { + GPBUtil::checkString($var, True); + $this->jwt_token = $var; + + return $this; + } + +} + diff --git a/generated/GRPC/Auth/ValidateJwtTokenResp.php b/generated/GRPC/Auth/ValidateJwtTokenResp.php new file mode 100644 index 0000000..884c75c --- /dev/null +++ b/generated/GRPC/Auth/ValidateJwtTokenResp.php @@ -0,0 +1,143 @@ +auth.ValidateJwtTokenResp + */ +class ValidateJwtTokenResp extends \Google\Protobuf\Internal\Message +{ + /** + * 验证结果 + * + * Generated from protobuf field .auth.JwtVerifyResult result = 1; + */ + protected $result = 0; + /** + * 换发的新token + * + * Generated from protobuf field string new_token = 2; + */ + protected $new_token = ''; + /** + * jwt token解析出来的claims数据 + * + * Generated from protobuf field optional .auth.JwtClaims claims = 3; + */ + protected $claims = null; + + /** + * Constructor. + * + * @param array $data { + * Optional. Data for populating the Message object. + * + * @type int $result + * 验证结果 + * @type string $new_token + * 换发的新token + * @type \GRPC\Auth\JwtClaims $claims + * jwt token解析出来的claims数据 + * } + */ + public function __construct($data = NULL) { + \GRPC\GPBMetadata\Auth::initOnce(); + parent::__construct($data); + } + + /** + * 验证结果 + * + * Generated from protobuf field .auth.JwtVerifyResult result = 1; + * @return int + */ + public function getResult() + { + return $this->result; + } + + /** + * 验证结果 + * + * Generated from protobuf field .auth.JwtVerifyResult result = 1; + * @param int $var + * @return $this + */ + public function setResult($var) + { + GPBUtil::checkEnum($var, \GRPC\Auth\JwtVerifyResult::class); + $this->result = $var; + + return $this; + } + + /** + * 换发的新token + * + * Generated from protobuf field string new_token = 2; + * @return string + */ + public function getNewToken() + { + return $this->new_token; + } + + /** + * 换发的新token + * + * Generated from protobuf field string new_token = 2; + * @param string $var + * @return $this + */ + public function setNewToken($var) + { + GPBUtil::checkString($var, True); + $this->new_token = $var; + + return $this; + } + + /** + * jwt token解析出来的claims数据 + * + * Generated from protobuf field optional .auth.JwtClaims claims = 3; + * @return \GRPC\Auth\JwtClaims|null + */ + public function getClaims() + { + return $this->claims; + } + + public function hasClaims() + { + return isset($this->claims); + } + + public function clearClaims() + { + unset($this->claims); + } + + /** + * jwt token解析出来的claims数据 + * + * Generated from protobuf field optional .auth.JwtClaims claims = 3; + * @param \GRPC\Auth\JwtClaims $var + * @return $this + */ + public function setClaims($var) + { + GPBUtil::checkMessage($var, \GRPC\Auth\JwtClaims::class); + $this->claims = $var; + + return $this; + } + +} + diff --git a/generated/GRPC/GPBMetadata/Auth.php b/generated/GRPC/GPBMetadata/Auth.php new file mode 100644 index 0000000000000000000000000000000000000000..acb14b336119907b622b50bfccd9aca0831f21ef GIT binary patch literal 1246 zcmZ`(+iuf95UrCmRVJc!-HY4`sY@wpT0xP*TS`H0;so~+2PaKI3bGt$(-<7vtk;x+ z`oIg~qxb?ogE#(yUAv0hRLzSs=ggcvGv4vb*TE=&$EbllOe~5AsDFj%2%~`b)VF;X z^`qes6J-12z;Q9zL#U>srrtqnP3t^jrJ)}Y8&{Hw=T<~VdvP&TjQC?=le_G&n^i4#ztv{-7T za)Ie&9vms{+#Jj0oFOVD9xIhQCJzuL5l)|u5WzGe-hS+x3tUhB#m=6+a&CMmgTdt3 zI-7NAlE}-p;omHX@nlm&mU4XHnD15x6Cgrg#AfZ&) z{2{>d-PACUo}IA31bi(e&^>tf;zuZ)=Fk{-Uik<4&OX$$h~WjfIG>7 za>RGBoMX{^c`-Rlv28!{sKE#>fxiawEaS7%0?ZLSWQ~pFd<-!53nua$4|abGurS@= zI;F{@Z1z`s9pV-;j_{bGn%++YkJc>(a**F?guEQ8-ED0QmyJ| z&5mj4C+d`U8J2Icr%F?4s0M3+dAY^3J4(GSX3B3N$JbNRK%BlY(pob2X`EfU_4WIx wFgV=!lPSdCf*x1?n-P;B^=0Vi<17ARdvf`oCOef{!F$&)&P+D#8m;M1& literal 0 HcmV?d00001