From 67eca4d18e44730a967187572600b8aa78314414 Mon Sep 17 00:00:00 2001 From: huanglei19951029 <81128461+huanglei19951029@users.noreply.github.com> Date: Fri, 12 Dec 2025 17:54:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E6=B7=BB=E5=8A=A0=20token=20?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=8A=9F=E8=83=BD=E5=92=8C=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 AuthToken 方法用于校验用户 token - 在 app 接口中添加 AuthToken 方法定义 - 在 defaultApp 结构体中实现 AuthToken 方法 - 修改 GetVerifyCode、Login、Register 接口的 tags 为 user_public - 新增 /user POST 接口用于获取用户详细信息 - 在 app.pb.go 文件中新增 AuthInfoResp、AuthReq 等相关结构体定义 - 更新 proto 文件中的消息类型索引以适应新增结构体 - 新增 ClaimStrings 和 NumericDate 结构体支持 JWT 相关字段 - 新增 RegisteredClaims 结构体用于表示 JWT 声明集 --- api/app.json | 38 +- api/desc/app/user.api | 22 +- api/etc/app.yaml | 4 +- api/go.mod | 2 + api/go.sum | 4 + api/internal/config/config.go | 5 + api/internal/handler/routes.go | 36 +- .../user/get_user_info_by_id_handler.go | 45 ++ .../handler/user/get_verify_code_handler.go | 2 +- api/internal/handler/user/login_handler.go | 45 ++ api/internal/handler/user/register_handler.go | 2 +- .../logic/user/get_user_info_by_id_logic.go | 30 + .../middleware/authority_middleware.go | 160 ++++- api/internal/svc/service_context.go | 8 +- api/internal/types/types.go | 4 + rpc/app.proto | 43 ++ rpc/appclient/app.go | 13 + rpc/desc/app/auth.proto | 61 ++ rpc/etc/app.yaml | 8 +- rpc/internal/logic/auth/auth_token_logic.go | 68 +++ rpc/internal/server/app_server.go | 7 + rpc/types/app/app.pb.go | 573 ++++++++++++++---- rpc/types/app/app_grpc.pb.go | 42 ++ 23 files changed, 1067 insertions(+), 155 deletions(-) create mode 100644 api/internal/handler/user/get_user_info_by_id_handler.go create mode 100644 api/internal/handler/user/login_handler.go create mode 100644 api/internal/logic/user/get_user_info_by_id_logic.go create mode 100644 rpc/desc/app/auth.proto create mode 100644 rpc/internal/logic/auth/auth_token_logic.go diff --git a/api/app.json b/api/app.json index 906c5e1..09ee2ff 100644 --- a/api/app.json +++ b/api/app.json @@ -22,7 +22,7 @@ "post": { "description": "GetVerifyCode | 获取验证码", "tags": [ - "user" + "user_public" ], "summary": "GetVerifyCode | 获取验证码", "operationId": "GetVerifyCode", @@ -68,7 +68,7 @@ "post": { "description": "Login | 登录", "tags": [ - "user" + "user_public" ], "summary": "Login | 登录", "operationId": "Login", @@ -96,7 +96,7 @@ "post": { "description": "Register | 注册", "tags": [ - "user" + "user_public" ], "summary": "Register | 注册", "operationId": "Register", @@ -119,6 +119,34 @@ } } } + }, + "/user": { + "post": { + "description": "Get userInfo detail by ID | 获取用户信息", + "tags": [ + "user" + ], + "summary": "Get userInfo detail by ID | 获取用户信息", + "operationId": "GetUserInfoById", + "parameters": [ + { + "name": "body", + "in": "body", + "schema": { + "type": "object", + "$ref": "#/definitions/IDReq" + } + } + ], + "responses": { + "200": { + "description": "UserInfo", + "schema": { + "$ref": "#/definitions/UserInfo" + } + } + } + } } }, "definitions": { @@ -868,6 +896,10 @@ }, "x-go-package": "mingyang-admin-app-api/internal/types" }, + "UserInfoReq": { + "type": "object", + "x-go-package": "mingyang-admin-app-api/internal/types" + }, "VerifyCodeReq": { "type": "object", "properties": { diff --git a/api/desc/app/user.api b/api/desc/app/user.api index 97ac72b..5866b41 100644 --- a/api/desc/app/user.api +++ b/api/desc/app/user.api @@ -50,7 +50,6 @@ type ( } - RegisterResp{ // token信息 AuthToken *AuthToken `json:"authToken,optional"` @@ -109,13 +108,16 @@ type ( AuthToken *AuthToken UserInfo *UserInfo } + + UserInfoReq{ + } + ) @server ( - jwt: Auth - group: user - middleware: Authority + group: user_public ) + service App { // GetVerifyCode | 获取验证码 @handler getVerifyCode @@ -128,3 +130,15 @@ service App { post /login (LoginReq) returns (LoginResp) } + +@server ( + group: user + middleware: Authority +) + +service App { + // Get userInfo detail by ID | 获取用户信息 + @handler getUserInfoById + post /user (IDReq) returns (UserInfo) +} + diff --git a/api/etc/app.yaml b/api/etc/app.yaml index fc13b18..6fefb3c 100644 --- a/api/etc/app.yaml +++ b/api/etc/app.yaml @@ -3,7 +3,6 @@ Host: 0.0.0.0 Port: 9902 Timeout: 4000 - CROSConf: Address: '*' @@ -29,6 +28,9 @@ AppRpc: Target: 127.0.0.1:9901 Enabled: true +JWTConf: + AccessSecret: KdAj3ZnmHpVvGKBthWXmTNPRcdZaWP7cnbsvmJSYRadN8PebaaAQENVKDD6WCm + I18nConf: Dir: \ No newline at end of file diff --git a/api/go.mod b/api/go.mod index 17da826..533a1b3 100644 --- a/api/go.mod +++ b/api/go.mod @@ -3,6 +3,8 @@ module mingyang-admin-app-api go 1.25.3 require ( + github.com/golang-jwt/jwt/v4 v4.5.2 + github.com/pkg/errors v0.9.1 github.com/redis/go-redis/v9 v9.16.0 github.com/saas-mingyang/mingyang-admin-common v0.3.3 github.com/zeromicro/go-zero v1.9.1 diff --git a/api/go.sum b/api/go.sum index 4a92910..0b28e1f 100644 --- a/api/go.sum +++ b/api/go.sum @@ -88,6 +88,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI= +github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= @@ -149,6 +151,8 @@ github.com/openzipkin/zipkin-go v0.4.3 h1:9EGwpqkgnwdEIJ+Od7QVSEIH+ocmm5nPat0G7s github.com/openzipkin/zipkin-go v0.4.3/go.mod h1:M9wCJZFWCo2RiY+o1eBCEMe0Dp2S5LDHcMZmk3RmK7c= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/api/internal/config/config.go b/api/internal/config/config.go index f70ebde..666d049 100644 --- a/api/internal/config/config.go +++ b/api/internal/config/config.go @@ -16,4 +16,9 @@ type Config struct { CROSConf config.CROSConf I18nConf i18n.Conf AppRpc zrpc.RpcClientConf + Auth JWTConfig `json:"JWTConf"` +} + +type JWTConfig struct { + AccessSecret string } diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 352ce08..d200cc2 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -8,6 +8,7 @@ import ( base "mingyang-admin-app-api/internal/handler/base" user "mingyang-admin-app-api/internal/handler/user" + user_public "mingyang-admin-app-api/internal/handler/user_public" "mingyang-admin-app-api/internal/svc" "github.com/zeromicro/go-zero/rest" @@ -24,27 +25,36 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, ) + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodPost, + Path: "/get/verifyCode", + Handler: user_public.GetVerifyCodeHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/register", + Handler: user_public.RegisterHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/login", + Handler: user_public.LoginHandler(serverCtx), + }, + }, + ) + server.AddRoutes( rest.WithMiddlewares( []rest.Middleware{serverCtx.Authority}, []rest.Route{ { Method: http.MethodPost, - Path: "/get/verifyCode", - Handler: user.GetVerifyCodeHandler(serverCtx), - }, - { - Method: http.MethodPost, - Path: "/register", - Handler: user.RegisterHandler(serverCtx), - }, - { - Method: http.MethodPost, - Path: "/login", - Handler: user.LoginHandler(serverCtx), + Path: "/user", + Handler: user.GetUserInfoByIdHandler(serverCtx), }, }..., ), - //rest.WithJwt(serverCtx.Config.Auth.AccessSecret), ) } diff --git a/api/internal/handler/user/get_user_info_by_id_handler.go b/api/internal/handler/user/get_user_info_by_id_handler.go new file mode 100644 index 0000000..1fbf13c --- /dev/null +++ b/api/internal/handler/user/get_user_info_by_id_handler.go @@ -0,0 +1,45 @@ +package user + +import ( + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + + "mingyang-admin-app-api/internal/logic/user" + "mingyang-admin-app-api/internal/svc" + "mingyang-admin-app-api/internal/types" +) + +// swagger:route post /user user GetUserInfoById +// +// Get userInfo detail by ID | 获取用户信息 +// +// Get userInfo detail by ID | 获取用户信息 +// +// Parameters: +// + name: body +// require: true +// in: body +// type: IDReq +// +// Responses: +// 200: UserInfo + +func GetUserInfoByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.IDReq + if err := httpx.Parse(r, &req, true); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := user.NewGetUserInfoByIdLogic(r.Context(), svcCtx) + resp, err := l.GetUserInfoById(&req) + if err != nil { + err = svcCtx.Trans.TransError(r.Context(), err) + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/api/internal/handler/user/get_verify_code_handler.go b/api/internal/handler/user/get_verify_code_handler.go index 4f837df..92984bb 100644 --- a/api/internal/handler/user/get_verify_code_handler.go +++ b/api/internal/handler/user/get_verify_code_handler.go @@ -1,11 +1,11 @@ package user import ( + "mingyang-admin-app-api/internal/logic/user" "net/http" "github.com/zeromicro/go-zero/rest/httpx" - "mingyang-admin-app-api/internal/logic/user" "mingyang-admin-app-api/internal/svc" "mingyang-admin-app-api/internal/types" ) diff --git a/api/internal/handler/user/login_handler.go b/api/internal/handler/user/login_handler.go new file mode 100644 index 0000000..54e3530 --- /dev/null +++ b/api/internal/handler/user/login_handler.go @@ -0,0 +1,45 @@ +package user + +import ( + "mingyang-admin-app-api/internal/logic/user" + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + + "mingyang-admin-app-api/internal/svc" + "mingyang-admin-app-api/internal/types" +) + +// swagger:route post /login user Login +// +// Login | 登录 +// +// Login | 登录 +// +// Parameters: +// + name: body +// require: true +// in: body +// type: LoginReq +// +// Responses: +// 200: LoginResp + +func LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.LoginReq + if err := httpx.Parse(r, &req, true); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := user.NewLoginLogic(r.Context(), svcCtx) + resp, err := l.Login(&req) + if err != nil { + err = svcCtx.Trans.TransError(r.Context(), err) + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/api/internal/handler/user/register_handler.go b/api/internal/handler/user/register_handler.go index 925d397..919dc4d 100644 --- a/api/internal/handler/user/register_handler.go +++ b/api/internal/handler/user/register_handler.go @@ -1,11 +1,11 @@ package user import ( + "mingyang-admin-app-api/internal/logic/user" "net/http" "github.com/zeromicro/go-zero/rest/httpx" - "mingyang-admin-app-api/internal/logic/user" "mingyang-admin-app-api/internal/svc" "mingyang-admin-app-api/internal/types" ) diff --git a/api/internal/logic/user/get_user_info_by_id_logic.go b/api/internal/logic/user/get_user_info_by_id_logic.go new file mode 100644 index 0000000..36b55dd --- /dev/null +++ b/api/internal/logic/user/get_user_info_by_id_logic.go @@ -0,0 +1,30 @@ +package user + +import ( + "context" + + "mingyang-admin-app-api/internal/svc" + "mingyang-admin-app-api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetUserInfoByIdLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetUserInfoByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoByIdLogic { + return &GetUserInfoByIdLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetUserInfoByIdLogic) GetUserInfoById(req *types.IDReq) (resp *types.UserInfo, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/api/internal/middleware/authority_middleware.go b/api/internal/middleware/authority_middleware.go index 2c5b87a..9e741dc 100644 --- a/api/internal/middleware/authority_middleware.go +++ b/api/internal/middleware/authority_middleware.go @@ -1,28 +1,170 @@ +// internal/middleware/authority.go package middleware import ( + "context" + "encoding/json" + "errors" + "fmt" + "github.com/golang-jwt/jwt/v4" "github.com/redis/go-redis/v9" + "mingyang-admin-app-api/internal/types" + "mingyang-admin-app-rpc/appclient" + "mingyang-admin-app-rpc/types/app" "net/http" + "strings" + "time" +) + +// 定义上下文键类型 +type contextKey string + +const ( + UserIDKey contextKey = "user_id" + UserInfoKey contextKey = "user_info" ) type AuthorityMiddleware struct { - Rds redis.UniversalClient + Rds redis.UniversalClient + AppRpc appclient.App } -func NewAuthorityMiddleware(rds redis.UniversalClient) *AuthorityMiddleware { +// NewAuthorityMiddleware 创建认证中间件 +func NewAuthorityMiddleware(appRpc appclient.App, rds redis.UniversalClient) *AuthorityMiddleware { return &AuthorityMiddleware{ - Rds: rds, + AppRpc: appRpc, + Rds: rds, } } +// writeResult 写入统一格式的响应 +func writeResult(w http.ResponseWriter, statusCode int, result *types.BaseDataInfo) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(statusCode) + json.NewEncoder(w).Encode(result) +} + +// writeError 写入错误响应(简化版) +func writeError(w http.ResponseWriter, statusCode int, message string) { + writeResult(w, statusCode, &types.BaseDataInfo{ + Msg: message, + Code: 500, + }) +} + +// Handle 中间件处理函数 func (m *AuthorityMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - /* // get the path - obj := r.URL.Path - // get the method - act := r.Method - // get the role id - roleIds := strings.Split(r.Context().Value("roleId").(string), ",")*/ + startTime := time.Now() + + // 检查是否为公开路径(可选) + if m.isPublicPath(r.URL.Path) { + next(w, r) + return + } + + // 1. 从 Authorization Header 中提取 Bearer Token + authHeader := r.Header.Get("Authorization") + if authHeader == "" { + writeError(w, 401, "Authorization header is required") + return + } + + // 2. 验证 Bearer Token 格式 + if !strings.HasPrefix(authHeader, "Bearer ") { + writeError(w, 401, "Invalid authorization format, must be 'Bearer '") + return + } + + // 3. 提取 Token + tokenString := strings.TrimPrefix(authHeader, "Bearer ") + if tokenString == "" { + writeError(w, 401, "Token cannot be empty") + return + } + + // 4. 检查 Redis 缓存(可选) + if m.Rds != nil { + cacheKey := fmt.Sprintf("token:%s", tokenString) + cachedUserID, err := m.Rds.Get(r.Context(), cacheKey).Result() + if err == nil && cachedUserID != "" { + // 从缓存中获取到用户ID + ctx := context.WithValue(r.Context(), UserIDKey, cachedUserID) + r = r.WithContext(ctx) + next(w, r) + return + } + } + + // 5. 调用 RPC 验证 Token + fmt.Printf("Validating token: %s\n", tokenString) + token, err := m.AppRpc.AuthToken(r.Context(), &app.AuthReq{Token: tokenString}) + if err != nil { + fmt.Printf("Error validating token: %v\n", err) + // 根据错误类型返回不同的错误信息 + var jwtErr *jwt.ValidationError + if errors.As(err, &jwtErr) { + switch { + case jwtErr.Errors&jwt.ValidationErrorExpired != 0: + writeError(w, 401, "Token has expired") + case jwtErr.Errors&jwt.ValidationErrorMalformed != 0: + writeError(w, 401, "Invalid token format") + case jwtErr.Errors&jwt.ValidationErrorSignatureInvalid != 0: + writeError(w, 401, "Invalid token signature") + case jwtErr.Errors&jwt.ValidationErrorNotValidYet != 0: + writeError(w, 401, "Token not valid yet") + default: + writeError(w, 401, "Invalid token") + } + } else { + // 网络错误或其他错误 + writeError(w, 500, "Token validation service unavailable") + } + return + } + + // 6. 获取用户ID + id := token.UserId + + // 7. 缓存到 Redis(可选) + if m.Rds != nil { + cacheKey := fmt.Sprintf("token:%s", tokenString) + // 设置缓存,过期时间30分钟 + m.Rds.Set(r.Context(), cacheKey, id, 30*time.Minute) + } + + // 8. 设置到上下文 + ctx := r.Context() + ctx = context.WithValue(ctx, UserIDKey, id) + ctx = context.WithValue(ctx, UserInfoKey, token) + + // 9. 记录请求日志(可选) + fmt.Printf("[%s] %s - UserID: %d - Duration: %v\n", + time.Now().Format("2006-01-02 15:04:05"), + r.URL.Path, + id, + time.Since(startTime)) + + // 10. 继续处理请求 + r = r.WithContext(ctx) next(w, r) } } + +// isPublicPath 检查是否为公开路径 +func (m *AuthorityMiddleware) isPublicPath(path string) bool { + publicPaths := []string{ + "/api/login", + "/api/register", + "/api/public/", + "/health", + "/metrics", + } + + for _, publicPath := range publicPaths { + if strings.HasPrefix(path, publicPath) { + return true + } + } + return false +} diff --git a/api/internal/svc/service_context.go b/api/internal/svc/service_context.go index 4665f2e..348e176 100644 --- a/api/internal/svc/service_context.go +++ b/api/internal/svc/service_context.go @@ -1,6 +1,7 @@ package svc import ( + "github.com/redis/go-redis/v9" "github.com/saas-mingyang/mingyang-admin-common/i18n" "github.com/zeromicro/go-zero/rest" "github.com/zeromicro/go-zero/zrpc" @@ -15,15 +16,18 @@ type ServiceContext struct { Trans *i18n.Translator Authority rest.Middleware AppRpc appclient.App + Redis redis.UniversalClient } func NewServiceContext(c config.Config) *ServiceContext { trans := i18n.NewTranslator(c.I18nConf, i18n2.LocaleFS) + appRpc := appclient.NewApp(zrpc.NewClientIfEnable(c.AppRpc)) rds := c.RedisConf.MustNewUniversalRedis() return &ServiceContext{ - Authority: middleware.NewAuthorityMiddleware(rds).Handle, + Authority: middleware.NewAuthorityMiddleware(appRpc, rds).Handle, Config: c, + Redis: rds, Trans: trans, - AppRpc: appclient.NewApp(zrpc.NewClientIfEnable(c.AppRpc)), + AppRpc: appRpc, } } diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 2d3fd15..34ad305 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -362,3 +362,7 @@ type LoginResp struct { AuthToken *AuthToken UserInfo *UserInfo } + +// swagger:model UserInfoReq +type UserInfoReq struct { +} diff --git a/rpc/app.proto b/rpc/app.proto index cab1eff..d777d34 100644 --- a/rpc/app.proto +++ b/rpc/app.proto @@ -27,6 +27,18 @@ enum VerifyCodeType { CHANGE_PAY_PASSWORD = 8; } +// AuthInfoResp 是认证信息的响应 +message AuthInfoResp { + uint64 user_id = 1; + string type = 2; + RegisteredClaims claims = 3; +} + +message AuthReq { + string token = 1; + google.protobuf.Timestamp time = 2; +} + // 认证令牌 message AuthToken { string access_token = 1; @@ -54,6 +66,11 @@ message BaseUUIDResp { string msg = 2; } +// ClaimStrings 用于表示 JWT 中的 audience 字段 +message ClaimStrings { + repeated string values = 1; +} + // base message message Empty {} @@ -78,6 +95,11 @@ message LoginResponse { AuthToken auth_token = 2; } +// NumericDate 使用 timestamp 表示 JWT 中的时间字段 +message NumericDate { + google.protobuf.Timestamp timestamp = 1; +} + message PageInfoReq { uint64 page = 1; uint64 page_size = 2; @@ -118,6 +140,24 @@ message RegisterUserResponse { bool phone_verification_required = 4; } +// RegisteredClaims 是 JWT 声明集的结构化版本 +message RegisteredClaims { + // iss (Issuer) - 签发者 + string issuer = 1; + // sub (Subject) - 主题 + string subject = 2; + // aud (Audience) - 受众 + ClaimStrings audience = 3; + // exp (Expiration Time) - 过期时间 + NumericDate expires_at = 4; + // nbf (Not Before) - 生效时间 + NumericDate not_before = 5; + // iat (Issued At) - 签发时间 + NumericDate issued_at = 6; + // jti (JWT ID) - JWT ID + string id = 7; +} + message UUIDReq { string id = 1; } @@ -162,6 +202,9 @@ message VerifyCodeResp { // App 服务定义 service App { + // 校验token + // group: auth + rpc AuthToken(AuthReq) returns (AuthInfoResp); // 获取验证码 // group: code rpc GetVerifyCode(VerifyCodeReq) returns (VerifyCodeResp); diff --git a/rpc/appclient/app.go b/rpc/appclient/app.go index f899c88..4f3e6e2 100644 --- a/rpc/appclient/app.go +++ b/rpc/appclient/app.go @@ -13,21 +13,26 @@ import ( ) type ( + AuthInfoResp = app.AuthInfoResp + AuthReq = app.AuthReq AuthToken = app.AuthToken BaseIDResp = app.BaseIDResp BaseMsg = app.BaseMsg BaseResp = app.BaseResp BaseUUIDResp = app.BaseUUIDResp + ClaimStrings = app.ClaimStrings Empty = app.Empty IDReq = app.IDReq IDsReq = app.IDsReq LoginRequest = app.LoginRequest LoginResponse = app.LoginResponse + NumericDate = app.NumericDate PageInfoReq = app.PageInfoReq PageUserRequest = app.PageUserRequest PageUserResponse = app.PageUserResponse RegisterUserRequest = app.RegisterUserRequest RegisterUserResponse = app.RegisterUserResponse + RegisteredClaims = app.RegisteredClaims UUIDReq = app.UUIDReq UUIDsReq = app.UUIDsReq UserInfo = app.UserInfo @@ -35,6 +40,8 @@ type ( VerifyCodeResp = app.VerifyCodeResp App interface { + // 校验token + AuthToken(ctx context.Context, in *AuthReq, opts ...grpc.CallOption) (*AuthInfoResp, error) // 获取验证码 GetVerifyCode(ctx context.Context, in *VerifyCodeReq, opts ...grpc.CallOption) (*VerifyCodeResp, error) // 用户注册 @@ -57,6 +64,12 @@ func NewApp(cli zrpc.Client) App { } } +// 校验token +func (m *defaultApp) AuthToken(ctx context.Context, in *AuthReq, opts ...grpc.CallOption) (*AuthInfoResp, error) { + client := app.NewAppClient(m.cli.Conn()) + return client.AuthToken(ctx, in, opts...) +} + // 获取验证码 func (m *defaultApp) GetVerifyCode(ctx context.Context, in *VerifyCodeReq, opts ...grpc.CallOption) (*VerifyCodeResp, error) { client := app.NewAppClient(m.cli.Conn()) diff --git a/rpc/desc/app/auth.proto b/rpc/desc/app/auth.proto new file mode 100644 index 0000000..b2964a1 --- /dev/null +++ b/rpc/desc/app/auth.proto @@ -0,0 +1,61 @@ +syntax = "proto3"; + + +package app; +option go_package = "./app"; +import "google/protobuf/timestamp.proto"; + + +// ClaimStrings 用于表示 JWT 中的 audience 字段 +message ClaimStrings { + repeated string values = 1; +} + +// NumericDate 使用 timestamp 表示 JWT 中的时间字段 +message NumericDate { + google.protobuf.Timestamp timestamp = 1; +} + +// RegisteredClaims 是 JWT 声明集的结构化版本 +message RegisteredClaims { + // iss (Issuer) - 签发者 + string issuer = 1; + + // sub (Subject) - 主题 + string subject = 2; + + // aud (Audience) - 受众 + ClaimStrings audience = 3; + + // exp (Expiration Time) - 过期时间 + NumericDate expires_at = 4; + + // nbf (Not Before) - 生效时间 + NumericDate not_before = 5; + + // iat (Issued At) - 签发时间 + NumericDate issued_at = 6; + + // jti (JWT ID) - JWT ID + string id = 7; +} + +// AuthInfoResp 是认证信息的响应 +message AuthInfoResp { + uint64 user_id = 1; + string type = 2; + RegisteredClaims claims = 3; +} + +message AuthReq { + string token = 1; + google.protobuf.Timestamp time = 2; +} + + +// App 服务定义 +service App { + // 校验token + // group: auth + rpc AuthToken(AuthReq) returns (AuthInfoResp); +} \ No newline at end of file diff --git a/rpc/etc/app.yaml b/rpc/etc/app.yaml index b55b0b3..61fc309 100644 --- a/rpc/etc/app.yaml +++ b/rpc/etc/app.yaml @@ -21,10 +21,10 @@ RedisConf: Db: 0 JWTConf: - access_token_secret: OAMDAascvzcvasdf - refresh_token_secret: ASDZCpajbvasdfasf - access_token_expiry: 24h - refresh_token_expiry: 720h + access_token_secret: KdAj3ZnmHpVvGKBthWXmTNPRcdZaWP7cnbsvmJSYRadN8PebaaAQENVKDD6WCm + refresh_token_secret: J8WRjFcuGpeAnymn5GNvbTJKn2uQsXdjvCFT4dK4dY5TtH88SNwzGH7btJ6ck + access_token_expiry: 30m + refresh_token_expiry: 24h issuer: user-system diff --git a/rpc/internal/logic/auth/auth_token_logic.go b/rpc/internal/logic/auth/auth_token_logic.go new file mode 100644 index 0000000..804715a --- /dev/null +++ b/rpc/internal/logic/auth/auth_token_logic.go @@ -0,0 +1,68 @@ +package auth + +import ( + "context" + "google.golang.org/protobuf/types/known/timestamppb" + "mingyang-admin-app-rpc/internal/jwt_manager" + + "mingyang-admin-app-rpc/internal/svc" + "mingyang-admin-app-rpc/types/app" + + "github.com/zeromicro/go-zero/core/logx" +) + +type TokenLogic struct { + ctx context.Context + svcCtx *svc.ServiceContext + logx.Logger + jwtManager *jwt_manager.JWTManager +} + +func NewAuthTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TokenLogic { + return &TokenLogic{ + ctx: ctx, + svcCtx: svcCtx, + Logger: logx.WithContext(ctx), + jwtManager: jwt_manager.NewJWTManager(&svcCtx.Config.JWTConf), + } +} + +// AuthToken 校验token +func (l *TokenLogic) AuthToken(in *app.AuthReq) (*app.AuthInfoResp, error) { + token, err := l.jwtManager.VerifyAccessToken(in.GetToken()) + if err != nil { + logx.Errorf("verify access token failed: %v", err) + return nil, err + } + + return &app.AuthInfoResp{ + UserId: token.UserID, + Type: token.Type, + Claims: &app.RegisteredClaims{ + IssuedAt: &app.NumericDate{ + Timestamp: ×tamppb.Timestamp{ + Seconds: token.IssuedAt.Unix(), // 转换为 Unix 时间戳 + Nanos: 0, + }, + }, + ExpiresAt: &app.NumericDate{ + Timestamp: ×tamppb.Timestamp{ + Seconds: token.ExpiresAt.Unix(), // 转换为 Unix 时间戳 + Nanos: 0, + }, + }, + Issuer: token.Issuer, + Subject: token.Subject, + Audience: &app.ClaimStrings{ + Values: token.Audience, + }, + NotBefore: &app.NumericDate{ + Timestamp: ×tamppb.Timestamp{ + Seconds: token.NotBefore.Unix(), // 转换为 Unix 时间戳 + Nanos: 0, + }, + }, + Id: token.ID, + }, + }, nil +} diff --git a/rpc/internal/server/app_server.go b/rpc/internal/server/app_server.go index 30f1db5..4f1714c 100644 --- a/rpc/internal/server/app_server.go +++ b/rpc/internal/server/app_server.go @@ -6,6 +6,7 @@ package server import ( "context" + "mingyang-admin-app-rpc/internal/logic/auth" "mingyang-admin-app-rpc/internal/logic/base" "mingyang-admin-app-rpc/internal/logic/code" "mingyang-admin-app-rpc/internal/logic/user" @@ -24,6 +25,12 @@ func NewAppServer(svcCtx *svc.ServiceContext) *AppServer { } } +// 校验token +func (s *AppServer) AuthToken(ctx context.Context, in *app.AuthReq) (*app.AuthInfoResp, error) { + l := auth.NewAuthTokenLogic(ctx, s.svcCtx) + return l.AuthToken(in) +} + // 获取验证码 func (s *AppServer) GetVerifyCode(ctx context.Context, in *app.VerifyCodeReq) (*app.VerifyCodeResp, error) { l := code.NewGetVerifyCodeLogic(ctx, s.svcCtx) diff --git a/rpc/types/app/app.pb.go b/rpc/types/app/app.pb.go index 71bd325..9d2d89e 100644 --- a/rpc/types/app/app.pb.go +++ b/rpc/types/app/app.pb.go @@ -141,6 +141,119 @@ func (VerifyCodeType) EnumDescriptor() ([]byte, []int) { return file_app_proto_rawDescGZIP(), []int{1} } +// AuthInfoResp 是认证信息的响应 +type AuthInfoResp struct { + state protoimpl.MessageState `protogen:"open.v1"` + UserId uint64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id"` + Type string `protobuf:"bytes,2,opt,name=type,proto3" json:"type"` + Claims *RegisteredClaims `protobuf:"bytes,3,opt,name=claims,proto3" json:"claims"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *AuthInfoResp) Reset() { + *x = AuthInfoResp{} + mi := &file_app_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AuthInfoResp) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AuthInfoResp) ProtoMessage() {} + +func (x *AuthInfoResp) ProtoReflect() protoreflect.Message { + mi := &file_app_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AuthInfoResp.ProtoReflect.Descriptor instead. +func (*AuthInfoResp) Descriptor() ([]byte, []int) { + return file_app_proto_rawDescGZIP(), []int{0} +} + +func (x *AuthInfoResp) GetUserId() uint64 { + if x != nil { + return x.UserId + } + return 0 +} + +func (x *AuthInfoResp) GetType() string { + if x != nil { + return x.Type + } + return "" +} + +func (x *AuthInfoResp) GetClaims() *RegisteredClaims { + if x != nil { + return x.Claims + } + return nil +} + +type AuthReq struct { + state protoimpl.MessageState `protogen:"open.v1"` + Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token"` + Time *timestamppb.Timestamp `protobuf:"bytes,2,opt,name=time,proto3" json:"time"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *AuthReq) Reset() { + *x = AuthReq{} + mi := &file_app_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AuthReq) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AuthReq) ProtoMessage() {} + +func (x *AuthReq) ProtoReflect() protoreflect.Message { + mi := &file_app_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AuthReq.ProtoReflect.Descriptor instead. +func (*AuthReq) Descriptor() ([]byte, []int) { + return file_app_proto_rawDescGZIP(), []int{1} +} + +func (x *AuthReq) GetToken() string { + if x != nil { + return x.Token + } + return "" +} + +func (x *AuthReq) GetTime() *timestamppb.Timestamp { + if x != nil { + return x.Time + } + return nil +} + // 认证令牌 type AuthToken struct { state protoimpl.MessageState `protogen:"open.v1"` @@ -155,7 +268,7 @@ type AuthToken struct { func (x *AuthToken) Reset() { *x = AuthToken{} - mi := &file_app_proto_msgTypes[0] + mi := &file_app_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -167,7 +280,7 @@ func (x *AuthToken) String() string { func (*AuthToken) ProtoMessage() {} func (x *AuthToken) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[0] + mi := &file_app_proto_msgTypes[2] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -180,7 +293,7 @@ func (x *AuthToken) ProtoReflect() protoreflect.Message { // Deprecated: Use AuthToken.ProtoReflect.Descriptor instead. func (*AuthToken) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{0} + return file_app_proto_rawDescGZIP(), []int{2} } func (x *AuthToken) GetAccessToken() string { @@ -228,7 +341,7 @@ type BaseIDResp struct { func (x *BaseIDResp) Reset() { *x = BaseIDResp{} - mi := &file_app_proto_msgTypes[1] + mi := &file_app_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -240,7 +353,7 @@ func (x *BaseIDResp) String() string { func (*BaseIDResp) ProtoMessage() {} func (x *BaseIDResp) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[1] + mi := &file_app_proto_msgTypes[3] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -253,7 +366,7 @@ func (x *BaseIDResp) ProtoReflect() protoreflect.Message { // Deprecated: Use BaseIDResp.ProtoReflect.Descriptor instead. func (*BaseIDResp) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{1} + return file_app_proto_rawDescGZIP(), []int{3} } func (x *BaseIDResp) GetId() uint64 { @@ -279,7 +392,7 @@ type BaseMsg struct { func (x *BaseMsg) Reset() { *x = BaseMsg{} - mi := &file_app_proto_msgTypes[2] + mi := &file_app_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -291,7 +404,7 @@ func (x *BaseMsg) String() string { func (*BaseMsg) ProtoMessage() {} func (x *BaseMsg) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[2] + mi := &file_app_proto_msgTypes[4] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -304,7 +417,7 @@ func (x *BaseMsg) ProtoReflect() protoreflect.Message { // Deprecated: Use BaseMsg.ProtoReflect.Descriptor instead. func (*BaseMsg) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{2} + return file_app_proto_rawDescGZIP(), []int{4} } func (x *BaseMsg) GetMsg() string { @@ -323,7 +436,7 @@ type BaseResp struct { func (x *BaseResp) Reset() { *x = BaseResp{} - mi := &file_app_proto_msgTypes[3] + mi := &file_app_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -335,7 +448,7 @@ func (x *BaseResp) String() string { func (*BaseResp) ProtoMessage() {} func (x *BaseResp) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[3] + mi := &file_app_proto_msgTypes[5] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -348,7 +461,7 @@ func (x *BaseResp) ProtoReflect() protoreflect.Message { // Deprecated: Use BaseResp.ProtoReflect.Descriptor instead. func (*BaseResp) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{3} + return file_app_proto_rawDescGZIP(), []int{5} } func (x *BaseResp) GetMsg() string { @@ -368,7 +481,7 @@ type BaseUUIDResp struct { func (x *BaseUUIDResp) Reset() { *x = BaseUUIDResp{} - mi := &file_app_proto_msgTypes[4] + mi := &file_app_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -380,7 +493,7 @@ func (x *BaseUUIDResp) String() string { func (*BaseUUIDResp) ProtoMessage() {} func (x *BaseUUIDResp) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[4] + mi := &file_app_proto_msgTypes[6] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -393,7 +506,7 @@ func (x *BaseUUIDResp) ProtoReflect() protoreflect.Message { // Deprecated: Use BaseUUIDResp.ProtoReflect.Descriptor instead. func (*BaseUUIDResp) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{4} + return file_app_proto_rawDescGZIP(), []int{6} } func (x *BaseUUIDResp) GetId() string { @@ -410,6 +523,51 @@ func (x *BaseUUIDResp) GetMsg() string { return "" } +// ClaimStrings 用于表示 JWT 中的 audience 字段 +type ClaimStrings struct { + state protoimpl.MessageState `protogen:"open.v1"` + Values []string `protobuf:"bytes,1,rep,name=values,proto3" json:"values"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ClaimStrings) Reset() { + *x = ClaimStrings{} + mi := &file_app_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ClaimStrings) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ClaimStrings) ProtoMessage() {} + +func (x *ClaimStrings) ProtoReflect() protoreflect.Message { + mi := &file_app_proto_msgTypes[7] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ClaimStrings.ProtoReflect.Descriptor instead. +func (*ClaimStrings) Descriptor() ([]byte, []int) { + return file_app_proto_rawDescGZIP(), []int{7} +} + +func (x *ClaimStrings) GetValues() []string { + if x != nil { + return x.Values + } + return nil +} + // base message type Empty struct { state protoimpl.MessageState `protogen:"open.v1"` @@ -419,7 +577,7 @@ type Empty struct { func (x *Empty) Reset() { *x = Empty{} - mi := &file_app_proto_msgTypes[5] + mi := &file_app_proto_msgTypes[8] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -431,7 +589,7 @@ func (x *Empty) String() string { func (*Empty) ProtoMessage() {} func (x *Empty) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[5] + mi := &file_app_proto_msgTypes[8] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -444,7 +602,7 @@ func (x *Empty) ProtoReflect() protoreflect.Message { // Deprecated: Use Empty.ProtoReflect.Descriptor instead. func (*Empty) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{5} + return file_app_proto_rawDescGZIP(), []int{8} } type IDReq struct { @@ -456,7 +614,7 @@ type IDReq struct { func (x *IDReq) Reset() { *x = IDReq{} - mi := &file_app_proto_msgTypes[6] + mi := &file_app_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -468,7 +626,7 @@ func (x *IDReq) String() string { func (*IDReq) ProtoMessage() {} func (x *IDReq) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[6] + mi := &file_app_proto_msgTypes[9] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -481,7 +639,7 @@ func (x *IDReq) ProtoReflect() protoreflect.Message { // Deprecated: Use IDReq.ProtoReflect.Descriptor instead. func (*IDReq) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{6} + return file_app_proto_rawDescGZIP(), []int{9} } func (x *IDReq) GetId() uint64 { @@ -500,7 +658,7 @@ type IDsReq struct { func (x *IDsReq) Reset() { *x = IDsReq{} - mi := &file_app_proto_msgTypes[7] + mi := &file_app_proto_msgTypes[10] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -512,7 +670,7 @@ func (x *IDsReq) String() string { func (*IDsReq) ProtoMessage() {} func (x *IDsReq) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[7] + mi := &file_app_proto_msgTypes[10] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -525,7 +683,7 @@ func (x *IDsReq) ProtoReflect() protoreflect.Message { // Deprecated: Use IDsReq.ProtoReflect.Descriptor instead. func (*IDsReq) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{7} + return file_app_proto_rawDescGZIP(), []int{10} } func (x *IDsReq) GetIds() []uint64 { @@ -548,7 +706,7 @@ type LoginRequest struct { func (x *LoginRequest) Reset() { *x = LoginRequest{} - mi := &file_app_proto_msgTypes[8] + mi := &file_app_proto_msgTypes[11] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -560,7 +718,7 @@ func (x *LoginRequest) String() string { func (*LoginRequest) ProtoMessage() {} func (x *LoginRequest) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[8] + mi := &file_app_proto_msgTypes[11] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -573,7 +731,7 @@ func (x *LoginRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use LoginRequest.ProtoReflect.Descriptor instead. func (*LoginRequest) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{8} + return file_app_proto_rawDescGZIP(), []int{11} } func (x *LoginRequest) GetUsername() string { @@ -621,7 +779,7 @@ type LoginResponse struct { func (x *LoginResponse) Reset() { *x = LoginResponse{} - mi := &file_app_proto_msgTypes[9] + mi := &file_app_proto_msgTypes[12] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -633,7 +791,7 @@ func (x *LoginResponse) String() string { func (*LoginResponse) ProtoMessage() {} func (x *LoginResponse) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[9] + mi := &file_app_proto_msgTypes[12] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -646,7 +804,7 @@ func (x *LoginResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use LoginResponse.ProtoReflect.Descriptor instead. func (*LoginResponse) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{9} + return file_app_proto_rawDescGZIP(), []int{12} } func (x *LoginResponse) GetUser() *UserInfo { @@ -663,6 +821,51 @@ func (x *LoginResponse) GetAuthToken() *AuthToken { return nil } +// NumericDate 使用 timestamp 表示 JWT 中的时间字段 +type NumericDate struct { + state protoimpl.MessageState `protogen:"open.v1"` + Timestamp *timestamppb.Timestamp `protobuf:"bytes,1,opt,name=timestamp,proto3" json:"timestamp"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *NumericDate) Reset() { + *x = NumericDate{} + mi := &file_app_proto_msgTypes[13] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *NumericDate) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*NumericDate) ProtoMessage() {} + +func (x *NumericDate) ProtoReflect() protoreflect.Message { + mi := &file_app_proto_msgTypes[13] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use NumericDate.ProtoReflect.Descriptor instead. +func (*NumericDate) Descriptor() ([]byte, []int) { + return file_app_proto_rawDescGZIP(), []int{13} +} + +func (x *NumericDate) GetTimestamp() *timestamppb.Timestamp { + if x != nil { + return x.Timestamp + } + return nil +} + type PageInfoReq struct { state protoimpl.MessageState `protogen:"open.v1"` Page uint64 `protobuf:"varint,1,opt,name=page,proto3" json:"page"` @@ -673,7 +876,7 @@ type PageInfoReq struct { func (x *PageInfoReq) Reset() { *x = PageInfoReq{} - mi := &file_app_proto_msgTypes[10] + mi := &file_app_proto_msgTypes[14] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -685,7 +888,7 @@ func (x *PageInfoReq) String() string { func (*PageInfoReq) ProtoMessage() {} func (x *PageInfoReq) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[10] + mi := &file_app_proto_msgTypes[14] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -698,7 +901,7 @@ func (x *PageInfoReq) ProtoReflect() protoreflect.Message { // Deprecated: Use PageInfoReq.ProtoReflect.Descriptor instead. func (*PageInfoReq) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{10} + return file_app_proto_rawDescGZIP(), []int{14} } func (x *PageInfoReq) GetPage() uint64 { @@ -730,7 +933,7 @@ type PageUserRequest struct { func (x *PageUserRequest) Reset() { *x = PageUserRequest{} - mi := &file_app_proto_msgTypes[11] + mi := &file_app_proto_msgTypes[15] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -742,7 +945,7 @@ func (x *PageUserRequest) String() string { func (*PageUserRequest) ProtoMessage() {} func (x *PageUserRequest) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[11] + mi := &file_app_proto_msgTypes[15] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -755,7 +958,7 @@ func (x *PageUserRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use PageUserRequest.ProtoReflect.Descriptor instead. func (*PageUserRequest) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{11} + return file_app_proto_rawDescGZIP(), []int{15} } func (x *PageUserRequest) GetPage() uint64 { @@ -817,7 +1020,7 @@ type PageUserResponse struct { func (x *PageUserResponse) Reset() { *x = PageUserResponse{} - mi := &file_app_proto_msgTypes[12] + mi := &file_app_proto_msgTypes[16] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -829,7 +1032,7 @@ func (x *PageUserResponse) String() string { func (*PageUserResponse) ProtoMessage() {} func (x *PageUserResponse) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[12] + mi := &file_app_proto_msgTypes[16] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -842,7 +1045,7 @@ func (x *PageUserResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use PageUserResponse.ProtoReflect.Descriptor instead. func (*PageUserResponse) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{12} + return file_app_proto_rawDescGZIP(), []int{16} } func (x *PageUserResponse) GetUsers() []*UserInfo { @@ -877,7 +1080,7 @@ type RegisterUserRequest struct { func (x *RegisterUserRequest) Reset() { *x = RegisterUserRequest{} - mi := &file_app_proto_msgTypes[13] + mi := &file_app_proto_msgTypes[17] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -889,7 +1092,7 @@ func (x *RegisterUserRequest) String() string { func (*RegisterUserRequest) ProtoMessage() {} func (x *RegisterUserRequest) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[13] + mi := &file_app_proto_msgTypes[17] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -902,7 +1105,7 @@ func (x *RegisterUserRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use RegisterUserRequest.ProtoReflect.Descriptor instead. func (*RegisterUserRequest) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{13} + return file_app_proto_rawDescGZIP(), []int{17} } func (x *RegisterUserRequest) GetEmail() string { @@ -987,7 +1190,7 @@ type RegisterUserResponse struct { func (x *RegisterUserResponse) Reset() { *x = RegisterUserResponse{} - mi := &file_app_proto_msgTypes[14] + mi := &file_app_proto_msgTypes[18] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -999,7 +1202,7 @@ func (x *RegisterUserResponse) String() string { func (*RegisterUserResponse) ProtoMessage() {} func (x *RegisterUserResponse) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[14] + mi := &file_app_proto_msgTypes[18] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1012,7 +1215,7 @@ func (x *RegisterUserResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use RegisterUserResponse.ProtoReflect.Descriptor instead. func (*RegisterUserResponse) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{14} + return file_app_proto_rawDescGZIP(), []int{18} } func (x *RegisterUserResponse) GetUser() *UserInfo { @@ -1043,6 +1246,106 @@ func (x *RegisterUserResponse) GetPhoneVerificationRequired() bool { return false } +// RegisteredClaims 是 JWT 声明集的结构化版本 +type RegisteredClaims struct { + state protoimpl.MessageState `protogen:"open.v1"` + // iss (Issuer) - 签发者 + Issuer string `protobuf:"bytes,1,opt,name=issuer,proto3" json:"issuer"` + // sub (Subject) - 主题 + Subject string `protobuf:"bytes,2,opt,name=subject,proto3" json:"subject"` + // aud (Audience) - 受众 + Audience *ClaimStrings `protobuf:"bytes,3,opt,name=audience,proto3" json:"audience"` + // exp (Expiration Time) - 过期时间 + ExpiresAt *NumericDate `protobuf:"bytes,4,opt,name=expires_at,json=expiresAt,proto3" json:"expires_at"` + // nbf (Not Before) - 生效时间 + NotBefore *NumericDate `protobuf:"bytes,5,opt,name=not_before,json=notBefore,proto3" json:"not_before"` + // iat (Issued At) - 签发时间 + IssuedAt *NumericDate `protobuf:"bytes,6,opt,name=issued_at,json=issuedAt,proto3" json:"issued_at"` + // jti (JWT ID) - JWT ID + Id string `protobuf:"bytes,7,opt,name=id,proto3" json:"id"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *RegisteredClaims) Reset() { + *x = RegisteredClaims{} + mi := &file_app_proto_msgTypes[19] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *RegisteredClaims) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RegisteredClaims) ProtoMessage() {} + +func (x *RegisteredClaims) ProtoReflect() protoreflect.Message { + mi := &file_app_proto_msgTypes[19] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RegisteredClaims.ProtoReflect.Descriptor instead. +func (*RegisteredClaims) Descriptor() ([]byte, []int) { + return file_app_proto_rawDescGZIP(), []int{19} +} + +func (x *RegisteredClaims) GetIssuer() string { + if x != nil { + return x.Issuer + } + return "" +} + +func (x *RegisteredClaims) GetSubject() string { + if x != nil { + return x.Subject + } + return "" +} + +func (x *RegisteredClaims) GetAudience() *ClaimStrings { + if x != nil { + return x.Audience + } + return nil +} + +func (x *RegisteredClaims) GetExpiresAt() *NumericDate { + if x != nil { + return x.ExpiresAt + } + return nil +} + +func (x *RegisteredClaims) GetNotBefore() *NumericDate { + if x != nil { + return x.NotBefore + } + return nil +} + +func (x *RegisteredClaims) GetIssuedAt() *NumericDate { + if x != nil { + return x.IssuedAt + } + return nil +} + +func (x *RegisteredClaims) GetId() string { + if x != nil { + return x.Id + } + return "" +} + type UUIDReq struct { state protoimpl.MessageState `protogen:"open.v1"` Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id"` @@ -1052,7 +1355,7 @@ type UUIDReq struct { func (x *UUIDReq) Reset() { *x = UUIDReq{} - mi := &file_app_proto_msgTypes[15] + mi := &file_app_proto_msgTypes[20] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1064,7 +1367,7 @@ func (x *UUIDReq) String() string { func (*UUIDReq) ProtoMessage() {} func (x *UUIDReq) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[15] + mi := &file_app_proto_msgTypes[20] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1077,7 +1380,7 @@ func (x *UUIDReq) ProtoReflect() protoreflect.Message { // Deprecated: Use UUIDReq.ProtoReflect.Descriptor instead. func (*UUIDReq) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{15} + return file_app_proto_rawDescGZIP(), []int{20} } func (x *UUIDReq) GetId() string { @@ -1096,7 +1399,7 @@ type UUIDsReq struct { func (x *UUIDsReq) Reset() { *x = UUIDsReq{} - mi := &file_app_proto_msgTypes[16] + mi := &file_app_proto_msgTypes[21] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1108,7 +1411,7 @@ func (x *UUIDsReq) String() string { func (*UUIDsReq) ProtoMessage() {} func (x *UUIDsReq) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[16] + mi := &file_app_proto_msgTypes[21] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1121,7 +1424,7 @@ func (x *UUIDsReq) ProtoReflect() protoreflect.Message { // Deprecated: Use UUIDsReq.ProtoReflect.Descriptor instead. func (*UUIDsReq) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{16} + return file_app_proto_rawDescGZIP(), []int{21} } func (x *UUIDsReq) GetIds() []string { @@ -1159,7 +1462,7 @@ type UserInfo struct { func (x *UserInfo) Reset() { *x = UserInfo{} - mi := &file_app_proto_msgTypes[17] + mi := &file_app_proto_msgTypes[22] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1171,7 +1474,7 @@ func (x *UserInfo) String() string { func (*UserInfo) ProtoMessage() {} func (x *UserInfo) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[17] + mi := &file_app_proto_msgTypes[22] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1184,7 +1487,7 @@ func (x *UserInfo) ProtoReflect() protoreflect.Message { // Deprecated: Use UserInfo.ProtoReflect.Descriptor instead. func (*UserInfo) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{17} + return file_app_proto_rawDescGZIP(), []int{22} } func (x *UserInfo) GetId() uint64 { @@ -1338,7 +1641,7 @@ type VerifyCodeReq struct { func (x *VerifyCodeReq) Reset() { *x = VerifyCodeReq{} - mi := &file_app_proto_msgTypes[18] + mi := &file_app_proto_msgTypes[23] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1350,7 +1653,7 @@ func (x *VerifyCodeReq) String() string { func (*VerifyCodeReq) ProtoMessage() {} func (x *VerifyCodeReq) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[18] + mi := &file_app_proto_msgTypes[23] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1363,7 +1666,7 @@ func (x *VerifyCodeReq) ProtoReflect() protoreflect.Message { // Deprecated: Use VerifyCodeReq.ProtoReflect.Descriptor instead. func (*VerifyCodeReq) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{18} + return file_app_proto_rawDescGZIP(), []int{23} } func (x *VerifyCodeReq) GetType() VerifyCodeType { @@ -1397,7 +1700,7 @@ type VerifyCodeResp struct { func (x *VerifyCodeResp) Reset() { *x = VerifyCodeResp{} - mi := &file_app_proto_msgTypes[19] + mi := &file_app_proto_msgTypes[24] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1409,7 +1712,7 @@ func (x *VerifyCodeResp) String() string { func (*VerifyCodeResp) ProtoMessage() {} func (x *VerifyCodeResp) ProtoReflect() protoreflect.Message { - mi := &file_app_proto_msgTypes[19] + mi := &file_app_proto_msgTypes[24] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1422,7 +1725,7 @@ func (x *VerifyCodeResp) ProtoReflect() protoreflect.Message { // Deprecated: Use VerifyCodeResp.ProtoReflect.Descriptor instead. func (*VerifyCodeResp) Descriptor() ([]byte, []int) { - return file_app_proto_rawDescGZIP(), []int{19} + return file_app_proto_rawDescGZIP(), []int{24} } func (x *VerifyCodeResp) GetCaptchaCode() string { @@ -1443,7 +1746,14 @@ var File_app_proto protoreflect.FileDescriptor const file_app_proto_rawDesc = "" + "\n" + - "\tapp.proto\x12\x03app\x1a\x1cgoogle/protobuf/struct.proto\x1a\x1fgoogle/protobuf/timestamp.proto\"\x90\x02\n" + + "\tapp.proto\x12\x03app\x1a\x1cgoogle/protobuf/struct.proto\x1a\x1fgoogle/protobuf/timestamp.proto\"j\n" + + "\fAuthInfoResp\x12\x17\n" + + "\auser_id\x18\x01 \x01(\x04R\x06userId\x12\x12\n" + + "\x04type\x18\x02 \x01(\tR\x04type\x12-\n" + + "\x06claims\x18\x03 \x01(\v2\x15.app.RegisteredClaimsR\x06claims\"O\n" + + "\aAuthReq\x12\x14\n" + + "\x05token\x18\x01 \x01(\tR\x05token\x12.\n" + + "\x04time\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\x04time\"\x90\x02\n" + "\tAuthToken\x12!\n" + "\faccess_token\x18\x01 \x01(\tR\vaccessToken\x12#\n" + "\rrefresh_token\x18\x02 \x01(\tR\frefreshToken\x12\x1d\n" + @@ -1461,7 +1771,9 @@ const file_app_proto_rawDesc = "" + "\x03msg\x18\x01 \x01(\tR\x03msg\"0\n" + "\fBaseUUIDResp\x12\x0e\n" + "\x02id\x18\x01 \x01(\tR\x02id\x12\x10\n" + - "\x03msg\x18\x02 \x01(\tR\x03msg\"\a\n" + + "\x03msg\x18\x02 \x01(\tR\x03msg\"&\n" + + "\fClaimStrings\x12\x16\n" + + "\x06values\x18\x01 \x03(\tR\x06values\"\a\n" + "\x05Empty\"\x17\n" + "\x05IDReq\x12\x0e\n" + "\x02id\x18\x01 \x01(\x04R\x02id\"\x1a\n" + @@ -1481,7 +1793,9 @@ const file_app_proto_rawDesc = "" + "\rLoginResponse\x12!\n" + "\x04user\x18\x01 \x01(\v2\r.app.UserInfoR\x04user\x12-\n" + "\n" + - "auth_token\x18\x02 \x01(\v2\x0e.app.AuthTokenR\tauthToken\">\n" + + "auth_token\x18\x02 \x01(\v2\x0e.app.AuthTokenR\tauthToken\"G\n" + + "\vNumericDate\x128\n" + + "\ttimestamp\x18\x01 \x01(\v2\x1a.google.protobuf.TimestampR\ttimestamp\">\n" + "\vPageInfoReq\x12\x12\n" + "\x04page\x18\x01 \x01(\x04R\x04page\x12\x1b\n" + "\tpage_size\x18\x02 \x01(\x04R\bpageSize\"\x93\x02\n" + @@ -1529,7 +1843,17 @@ const file_app_proto_rawDesc = "" + "\n" + "auth_token\x18\x02 \x01(\v2\x0e.app.AuthTokenR\tauthToken\x12>\n" + "\x1bemail_verification_required\x18\x03 \x01(\bR\x19emailVerificationRequired\x12>\n" + - "\x1bphone_verification_required\x18\x04 \x01(\bR\x19phoneVerificationRequired\"\x19\n" + + "\x1bphone_verification_required\x18\x04 \x01(\bR\x19phoneVerificationRequired\"\x94\x02\n" + + "\x10RegisteredClaims\x12\x16\n" + + "\x06issuer\x18\x01 \x01(\tR\x06issuer\x12\x18\n" + + "\asubject\x18\x02 \x01(\tR\asubject\x12-\n" + + "\baudience\x18\x03 \x01(\v2\x11.app.ClaimStringsR\baudience\x12/\n" + + "\n" + + "expires_at\x18\x04 \x01(\v2\x10.app.NumericDateR\texpiresAt\x12/\n" + + "\n" + + "not_before\x18\x05 \x01(\v2\x10.app.NumericDateR\tnotBefore\x12-\n" + + "\tissued_at\x18\x06 \x01(\v2\x10.app.NumericDateR\bissuedAt\x12\x0e\n" + + "\x02id\x18\a \x01(\tR\x02id\"\x19\n" + "\aUUIDReq\x12\x0e\n" + "\x02id\x18\x01 \x01(\tR\x02id\"\x1c\n" + "\bUUIDsReq\x12\x10\n" + @@ -1602,8 +1926,9 @@ const file_app_proto_rawDesc = "" + "\fUPDATE_PHONE\x10\x05\x12\x10\n" + "\fUPDATE_EMAIL\x10\x06\x12\f\n" + "\bWITHDRAW\x10\a\x12\x17\n" + - "\x13CHANGE_PAY_PASSWORD\x10\b2\x9d\x02\n" + - "\x03App\x128\n" + + "\x13CHANGE_PAY_PASSWORD\x10\b2\xcb\x02\n" + + "\x03App\x12,\n" + + "\tAuthToken\x12\f.app.AuthReq\x1a\x11.app.AuthInfoResp\x128\n" + "\rGetVerifyCode\x12\x12.app.VerifyCodeReq\x1a\x13.app.VerifyCodeResp\x12C\n" + "\fRegisterUser\x12\x18.app.RegisterUserRequest\x1a\x19.app.RegisterUserResponse\x122\n" + "\tLoginUser\x12\x11.app.LoginRequest\x1a\x12.app.LoginResponse\x128\n" + @@ -1624,59 +1949,73 @@ func file_app_proto_rawDescGZIP() []byte { } var file_app_proto_enumTypes = make([]protoimpl.EnumInfo, 2) -var file_app_proto_msgTypes = make([]protoimpl.MessageInfo, 20) +var file_app_proto_msgTypes = make([]protoimpl.MessageInfo, 25) var file_app_proto_goTypes = []any{ (AccountType)(0), // 0: app.AccountType (VerifyCodeType)(0), // 1: app.VerifyCodeType - (*AuthToken)(nil), // 2: app.AuthToken - (*BaseIDResp)(nil), // 3: app.BaseIDResp - (*BaseMsg)(nil), // 4: app.BaseMsg - (*BaseResp)(nil), // 5: app.BaseResp - (*BaseUUIDResp)(nil), // 6: app.BaseUUIDResp - (*Empty)(nil), // 7: app.Empty - (*IDReq)(nil), // 8: app.IDReq - (*IDsReq)(nil), // 9: app.IDsReq - (*LoginRequest)(nil), // 10: app.LoginRequest - (*LoginResponse)(nil), // 11: app.LoginResponse - (*PageInfoReq)(nil), // 12: app.PageInfoReq - (*PageUserRequest)(nil), // 13: app.PageUserRequest - (*PageUserResponse)(nil), // 14: app.PageUserResponse - (*RegisterUserRequest)(nil), // 15: app.RegisterUserRequest - (*RegisterUserResponse)(nil), // 16: app.RegisterUserResponse - (*UUIDReq)(nil), // 17: app.UUIDReq - (*UUIDsReq)(nil), // 18: app.UUIDsReq - (*UserInfo)(nil), // 19: app.UserInfo - (*VerifyCodeReq)(nil), // 20: app.VerifyCodeReq - (*VerifyCodeResp)(nil), // 21: app.VerifyCodeResp - (*timestamppb.Timestamp)(nil), // 22: google.protobuf.Timestamp - (*structpb.Struct)(nil), // 23: google.protobuf.Struct + (*AuthInfoResp)(nil), // 2: app.AuthInfoResp + (*AuthReq)(nil), // 3: app.AuthReq + (*AuthToken)(nil), // 4: app.AuthToken + (*BaseIDResp)(nil), // 5: app.BaseIDResp + (*BaseMsg)(nil), // 6: app.BaseMsg + (*BaseResp)(nil), // 7: app.BaseResp + (*BaseUUIDResp)(nil), // 8: app.BaseUUIDResp + (*ClaimStrings)(nil), // 9: app.ClaimStrings + (*Empty)(nil), // 10: app.Empty + (*IDReq)(nil), // 11: app.IDReq + (*IDsReq)(nil), // 12: app.IDsReq + (*LoginRequest)(nil), // 13: app.LoginRequest + (*LoginResponse)(nil), // 14: app.LoginResponse + (*NumericDate)(nil), // 15: app.NumericDate + (*PageInfoReq)(nil), // 16: app.PageInfoReq + (*PageUserRequest)(nil), // 17: app.PageUserRequest + (*PageUserResponse)(nil), // 18: app.PageUserResponse + (*RegisterUserRequest)(nil), // 19: app.RegisterUserRequest + (*RegisterUserResponse)(nil), // 20: app.RegisterUserResponse + (*RegisteredClaims)(nil), // 21: app.RegisteredClaims + (*UUIDReq)(nil), // 22: app.UUIDReq + (*UUIDsReq)(nil), // 23: app.UUIDsReq + (*UserInfo)(nil), // 24: app.UserInfo + (*VerifyCodeReq)(nil), // 25: app.VerifyCodeReq + (*VerifyCodeResp)(nil), // 26: app.VerifyCodeResp + (*timestamppb.Timestamp)(nil), // 27: google.protobuf.Timestamp + (*structpb.Struct)(nil), // 28: google.protobuf.Struct } var file_app_proto_depIdxs = []int32{ - 22, // 0: app.AuthToken.access_token_expires:type_name -> google.protobuf.Timestamp - 22, // 1: app.AuthToken.refresh_token_expires:type_name -> google.protobuf.Timestamp - 19, // 2: app.LoginResponse.user:type_name -> app.UserInfo - 2, // 3: app.LoginResponse.auth_token:type_name -> app.AuthToken - 19, // 4: app.PageUserResponse.users:type_name -> app.UserInfo - 19, // 5: app.RegisterUserResponse.user:type_name -> app.UserInfo - 2, // 6: app.RegisterUserResponse.auth_token:type_name -> app.AuthToken - 23, // 7: app.UserInfo.metadata:type_name -> google.protobuf.Struct - 1, // 8: app.VerifyCodeReq.type:type_name -> app.VerifyCodeType - 0, // 9: app.VerifyCodeReq.account_type:type_name -> app.AccountType - 20, // 10: app.App.GetVerifyCode:input_type -> app.VerifyCodeReq - 15, // 11: app.App.RegisterUser:input_type -> app.RegisterUserRequest - 10, // 12: app.App.LoginUser:input_type -> app.LoginRequest - 13, // 13: app.App.ListUsers:input_type -> app.PageUserRequest - 7, // 14: app.App.InitDatabase:input_type -> app.Empty - 21, // 15: app.App.GetVerifyCode:output_type -> app.VerifyCodeResp - 16, // 16: app.App.RegisterUser:output_type -> app.RegisterUserResponse - 11, // 17: app.App.LoginUser:output_type -> app.LoginResponse - 14, // 18: app.App.ListUsers:output_type -> app.PageUserResponse - 5, // 19: app.App.InitDatabase:output_type -> app.BaseResp - 15, // [15:20] is the sub-list for method output_type - 10, // [10:15] is the sub-list for method input_type - 10, // [10:10] is the sub-list for extension type_name - 10, // [10:10] is the sub-list for extension extendee - 0, // [0:10] is the sub-list for field type_name + 21, // 0: app.AuthInfoResp.claims:type_name -> app.RegisteredClaims + 27, // 1: app.AuthReq.time:type_name -> google.protobuf.Timestamp + 27, // 2: app.AuthToken.access_token_expires:type_name -> google.protobuf.Timestamp + 27, // 3: app.AuthToken.refresh_token_expires:type_name -> google.protobuf.Timestamp + 24, // 4: app.LoginResponse.user:type_name -> app.UserInfo + 4, // 5: app.LoginResponse.auth_token:type_name -> app.AuthToken + 27, // 6: app.NumericDate.timestamp:type_name -> google.protobuf.Timestamp + 24, // 7: app.PageUserResponse.users:type_name -> app.UserInfo + 24, // 8: app.RegisterUserResponse.user:type_name -> app.UserInfo + 4, // 9: app.RegisterUserResponse.auth_token:type_name -> app.AuthToken + 9, // 10: app.RegisteredClaims.audience:type_name -> app.ClaimStrings + 15, // 11: app.RegisteredClaims.expires_at:type_name -> app.NumericDate + 15, // 12: app.RegisteredClaims.not_before:type_name -> app.NumericDate + 15, // 13: app.RegisteredClaims.issued_at:type_name -> app.NumericDate + 28, // 14: app.UserInfo.metadata:type_name -> google.protobuf.Struct + 1, // 15: app.VerifyCodeReq.type:type_name -> app.VerifyCodeType + 0, // 16: app.VerifyCodeReq.account_type:type_name -> app.AccountType + 3, // 17: app.App.AuthToken:input_type -> app.AuthReq + 25, // 18: app.App.GetVerifyCode:input_type -> app.VerifyCodeReq + 19, // 19: app.App.RegisterUser:input_type -> app.RegisterUserRequest + 13, // 20: app.App.LoginUser:input_type -> app.LoginRequest + 17, // 21: app.App.ListUsers:input_type -> app.PageUserRequest + 10, // 22: app.App.InitDatabase:input_type -> app.Empty + 2, // 23: app.App.AuthToken:output_type -> app.AuthInfoResp + 26, // 24: app.App.GetVerifyCode:output_type -> app.VerifyCodeResp + 20, // 25: app.App.RegisterUser:output_type -> app.RegisterUserResponse + 14, // 26: app.App.LoginUser:output_type -> app.LoginResponse + 18, // 27: app.App.ListUsers:output_type -> app.PageUserResponse + 7, // 28: app.App.InitDatabase:output_type -> app.BaseResp + 23, // [23:29] is the sub-list for method output_type + 17, // [17:23] is the sub-list for method input_type + 17, // [17:17] is the sub-list for extension type_name + 17, // [17:17] is the sub-list for extension extendee + 0, // [0:17] is the sub-list for field type_name } func init() { file_app_proto_init() } @@ -1684,17 +2023,17 @@ func file_app_proto_init() { if File_app_proto != nil { return } - file_app_proto_msgTypes[8].OneofWrappers = []any{} file_app_proto_msgTypes[11].OneofWrappers = []any{} - file_app_proto_msgTypes[13].OneofWrappers = []any{} + file_app_proto_msgTypes[15].OneofWrappers = []any{} file_app_proto_msgTypes[17].OneofWrappers = []any{} + file_app_proto_msgTypes[22].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_app_proto_rawDesc), len(file_app_proto_rawDesc)), NumEnums: 2, - NumMessages: 20, + NumMessages: 25, NumExtensions: 0, NumServices: 1, }, diff --git a/rpc/types/app/app_grpc.pb.go b/rpc/types/app/app_grpc.pb.go index 3a5fe10..bc1dd11 100644 --- a/rpc/types/app/app_grpc.pb.go +++ b/rpc/types/app/app_grpc.pb.go @@ -19,6 +19,7 @@ import ( const _ = grpc.SupportPackageIsVersion9 const ( + App_AuthToken_FullMethodName = "/app.App/AuthToken" App_GetVerifyCode_FullMethodName = "/app.App/GetVerifyCode" App_RegisterUser_FullMethodName = "/app.App/RegisterUser" App_LoginUser_FullMethodName = "/app.App/LoginUser" @@ -32,6 +33,9 @@ const ( // // App 服务定义 type AppClient interface { + // 校验token + // group: auth + AuthToken(ctx context.Context, in *AuthReq, opts ...grpc.CallOption) (*AuthInfoResp, error) // 获取验证码 // group: code GetVerifyCode(ctx context.Context, in *VerifyCodeReq, opts ...grpc.CallOption) (*VerifyCodeResp, error) @@ -56,6 +60,16 @@ func NewAppClient(cc grpc.ClientConnInterface) AppClient { return &appClient{cc} } +func (c *appClient) AuthToken(ctx context.Context, in *AuthReq, opts ...grpc.CallOption) (*AuthInfoResp, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(AuthInfoResp) + err := c.cc.Invoke(ctx, App_AuthToken_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *appClient) GetVerifyCode(ctx context.Context, in *VerifyCodeReq, opts ...grpc.CallOption) (*VerifyCodeResp, error) { cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) out := new(VerifyCodeResp) @@ -112,6 +126,9 @@ func (c *appClient) InitDatabase(ctx context.Context, in *Empty, opts ...grpc.Ca // // App 服务定义 type AppServer interface { + // 校验token + // group: auth + AuthToken(context.Context, *AuthReq) (*AuthInfoResp, error) // 获取验证码 // group: code GetVerifyCode(context.Context, *VerifyCodeReq) (*VerifyCodeResp, error) @@ -136,6 +153,9 @@ type AppServer interface { // pointer dereference when methods are called. type UnimplementedAppServer struct{} +func (UnimplementedAppServer) AuthToken(context.Context, *AuthReq) (*AuthInfoResp, error) { + return nil, status.Error(codes.Unimplemented, "method AuthToken not implemented") +} func (UnimplementedAppServer) GetVerifyCode(context.Context, *VerifyCodeReq) (*VerifyCodeResp, error) { return nil, status.Error(codes.Unimplemented, "method GetVerifyCode not implemented") } @@ -172,6 +192,24 @@ func RegisterAppServer(s grpc.ServiceRegistrar, srv AppServer) { s.RegisterService(&App_ServiceDesc, srv) } +func _App_AuthToken_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(AuthReq) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AppServer).AuthToken(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: App_AuthToken_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AppServer).AuthToken(ctx, req.(*AuthReq)) + } + return interceptor(ctx, in, info, handler) +} + func _App_GetVerifyCode_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(VerifyCodeReq) if err := dec(in); err != nil { @@ -269,6 +307,10 @@ var App_ServiceDesc = grpc.ServiceDesc{ ServiceName: "app.App", HandlerType: (*AppServer)(nil), Methods: []grpc.MethodDesc{ + { + MethodName: "AuthToken", + Handler: _App_AuthToken_Handler, + }, { MethodName: "GetVerifyCode", Handler: _App_GetVerifyCode_Handler,