diff --git a/api/user.http.go b/api/user.http.go index c31ed74..83b2528 100644 --- a/api/user.http.go +++ b/api/user.http.go @@ -1,16 +1,16 @@ package user import ( - "github.com/gin-gonic/gin" "context" "git.diulo.com/mogfee/kit/middleware" "git.diulo.com/mogfee/kit/errors" "git.diulo.com/mogfee/kit/response" + "github.com/gin-gonic/gin" ) func RegisterUserHandler(app *gin.Engine, srv UserServer, m ...middleware.Middleware) { app.GET("/api/v1/user/list", httpListHandler(srv, m...)) - app.POST("/api/v1/user/login", httpLoginHandler(srv, m...)) + app.GET("/api/v1/user/login", httpLoginHandler(srv, m...)) } func httpListHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { return func(c *gin.Context) { @@ -39,7 +39,7 @@ func httpLoginHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Co return func(c *gin.Context) { var post LoginRequest resp := response.New(c) - if err := resp.BindJSON(&post); err != nil { + if err := resp.BindQuery(&post); err != nil { resp.Error(err) return } diff --git a/api/user.pb.go b/api/user.pb.go index 356c804..52cda45 100644 --- a/api/user.pb.go +++ b/api/user.pb.go @@ -157,26 +157,26 @@ var file_user_proto_rawDesc = []byte{ 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x66, 0x69, 0x72, 0x73, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x25, 0x0a, 0x0d, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x32, 0x9a, 0x02, 0x0a, 0x04, 0x75, 0x73, - 0x65, 0x72, 0x12, 0x6a, 0x0a, 0x04, 0x6c, 0x69, 0x73, 0x74, 0x12, 0x1b, 0x2e, 0x63, 0x6f, 0x6d, + 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x32, 0x9c, 0x02, 0x0a, 0x04, 0x75, 0x73, + 0x65, 0x72, 0x12, 0x68, 0x0a, 0x04, 0x6c, 0x69, 0x73, 0x74, 0x12, 0x1b, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x27, 0xba, 0x45, 0x0b, 0x75, 0x73, 0x65, 0x72, 0x3a, 0x73, - 0x65, 0x61, 0x72, 0x63, 0x68, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, - 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x6c, 0x69, 0x73, 0x74, 0x12, 0x61, - 0x0a, 0x05, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x1b, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, - 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, - 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x22, 0x1d, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x17, 0x22, 0x12, 0x2f, 0x61, 0x70, 0x69, - 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x3a, 0x01, - 0x2a, 0x12, 0x43, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x1b, 0x2e, 0x63, 0x6f, - 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, - 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, - 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x3b, 0x75, 0x73, 0x65, - 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x25, 0xba, 0x45, 0x09, 0x75, 0x73, 0x65, 0x72, 0x3a, 0x6c, + 0x69, 0x73, 0x74, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, + 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x6c, 0x69, 0x73, 0x74, 0x12, 0x65, 0x0a, 0x05, + 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x1b, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, + 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, + 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x22, 0x21, 0xba, 0x45, 0x04, 0x6e, 0x6f, 0x6e, 0x65, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x14, 0x12, + 0x12, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x6c, 0x6f, + 0x67, 0x69, 0x6e, 0x12, 0x43, 0x0a, 0x06, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x1b, 0x2e, + 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, + 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x63, 0x6f, 0x6d, + 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x6c, 0x6f, 0x67, 0x69, 0x6e, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x2f, 0x3b, 0x75, + 0x73, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/api/user.ts b/api/user.ts index 102215e..cb8f911 100644 --- a/api/user.ts +++ b/api/user.ts @@ -26,7 +26,7 @@ export class userService{ return http('/api/v1/user/login', { ...param, data: data, - method:'POST' + method:'GET' }) } } diff --git a/api/user_http.pb.go b/api/user_http.pb.go index 2deaf5c..14849ac 100644 --- a/api/user_http.pb.go +++ b/api/user_http.pb.go @@ -15,14 +15,14 @@ type UserHTTPServer interface { func RegisterUserHTTPServer(s *http.Server, srv UserServer) { r := s.Route("/") r.GET("/api/v1/user/list", _User_List0_HTTP_Handler(srv)) - r.POST("/api/v1/user/login", _User_Login0_HTTP_Handler(srv)) + r.GET("/api/v1/user/login", _User_Login0_HTTP_Handler(srv)) } func _User_List0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { return func(ctx http.Context) error { var in LoginRequest - //user:search + //user:list var newCtx context.Context = ctx - newCtx = jwt.SetAuthKeyContext(ctx, "user:search") + newCtx = jwt.SetAuthKeyContext(ctx, "user:list") if err := ctx.BindQuery(&in); err != nil { return err } @@ -41,8 +41,10 @@ func _User_List0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { func _User_Login0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { return func(ctx http.Context) error { var in LoginRequest + // none var newCtx context.Context = ctx - if err := ctx.Bind(&in); err != nil { + newCtx = jwt.SetAuthKeyContext(ctx, "none") + if err := ctx.BindQuery(&in); err != nil { return err } http.SetOperation(ctx, "/com.diulo.api.user/login") diff --git a/cmd/kit/main.go b/cmd/kit/main.go index d70b782..dc43329 100644 --- a/cmd/kit/main.go +++ b/cmd/kit/main.go @@ -98,7 +98,7 @@ func (u *Kit) Generate(plugin *protogen.Plugin) error { } func (u *Kit) genGet(f *protogen.File, s *protogen.Service, t *protogen.GeneratedFile, m *protogen.Method) { - autkKey := protogen2.GetAuthKey(m) + authKey := protogen2.GetAuthKey(m) method, path := protogen2.GetProtoMethod(m) if method == "" { return @@ -108,10 +108,10 @@ func (u *Kit) genGet(f *protogen.File, s *protogen.Service, t *protogen.Generate return func(ctx http.Context) error { var in `, m.Input.GoIdent.GoName) - t.P("//", protogen2.GetAuthKey(m)) + t.P("//", authKey) t.P(`var newCtx context.Context=ctx`) - if autkKey != "" { - t.P(`newCtx = jwt.SetAuthKeyContext(ctx, "`, autkKey, `")`) + if authKey != "" { + t.P(`newCtx = jwt.SetAuthKeyContext(ctx, "`, authKey, `")`) } if method == protogen2.METHOD_GET { t.P(`if err := ctx.BindQuery(&in); err != nil { diff --git a/encoding/encoding.go b/encoding/encoding.go index 6d536fc..f9defe6 100644 --- a/encoding/encoding.go +++ b/encoding/encoding.go @@ -1,7 +1,6 @@ package encoding import ( - "fmt" "strings" ) @@ -31,7 +30,6 @@ func RegisterCodec(codec Codec) { panic("cannot register Codec with empty string result for Name()") } contentSubtype := strings.ToLower(codec.Name()) - fmt.Println("reg", contentSubtype) registeredCodecs[contentSubtype] = codec } diff --git a/example/main.go b/example/main.go index d2b6e87..5f5842e 100644 --- a/example/main.go +++ b/example/main.go @@ -10,9 +10,7 @@ import ( "git.diulo.com/mogfee/kit/middleware/jwt" "git.diulo.com/mogfee/kit/middleware/logging" "git.diulo.com/mogfee/kit/middleware/validate" - "git.diulo.com/mogfee/kit/registry/etcd" "git.diulo.com/mogfee/kit/transport/http" - clientv3 "go.etcd.io/etcd/client/v3" ) var host string @@ -53,18 +51,18 @@ func runApp(host string) { //}) user.RegisterUserHTTPServer(hs, &service.UserService{}) - client, err := clientv3.New(clientv3.Config{ - Endpoints: []string{"127.0.0.1:2379"}, - }) - if err != nil { - panic(err) - } + //client, err := clientv3.New(clientv3.Config{ + // Endpoints: []string{"127.0.0.1:2379"}, + //}) + //if err != nil { + // panic(err) + //} app := kit.New( kit.Name("kit-server"), kit.Version("v1.0"), kit.ID(host), kit.Server(hs), - kit.Registrar(etcd.New(client)), + //kit.Registrar(etcd.New(client)), ) fmt.Println(app.Run()) fmt.Println(app.Stop()) diff --git a/middleware/jwt/jwt.go b/middleware/jwt/jwt.go index c2c62e8..f0ab65a 100644 --- a/middleware/jwt/jwt.go +++ b/middleware/jwt/jwt.go @@ -8,6 +8,10 @@ import ( "git.diulo.com/mogfee/kit/transport" ) +const ( + permissionNoCheck = "none" +) + type userIdKey struct{} type authKey struct { } @@ -48,6 +52,12 @@ func JWT(opts ...JwtOption) middleware.Middleware { return func(handler middleware.Handler) middleware.Handler { return func(ctx context.Context, a any) (any, error) { + permission := FromAuthKeyContext(ctx) + + if permission == permissionNoCheck { + return handler(ctx, a) + } + var tokenStr string if tr, ok := transport.FromServerContext(ctx); ok { tokenStr = tr.RequestHeader().Get("token") @@ -56,7 +66,6 @@ func JWT(opts ...JwtOption) middleware.Middleware { if err != nil { return nil, err } - permission := FromAuthKeyContext(ctx) if permission != "" { if !cfg.validatePermission(userInfo.Permissions, permission) { return nil, errors.Unauthorized("TOKEN_PERMISSION_BAD", "") diff --git a/proto/user.proto b/proto/user.proto index bbacc7b..9b56679 100644 --- a/proto/user.proto +++ b/proto/user.proto @@ -12,16 +12,16 @@ import "google/protobuf/descriptor.proto"; service user{ //列表 rpc list(loginRequest)returns(loginResponse){ - option(auth.auth_key) = "user:search"; + option(auth.auth_key) = "user:list"; option(google.api.http) = { get:"/api/v1/user/list" }; } //等了 rpc login(loginRequest)returns(loginResponse){ + option(auth.auth_key) = "none"; option (google.api.http) = { - post: "/api/v1/user/login", - body:"*" + get: "/api/v1/user/login", }; } rpc delete(loginRequest)returns(loginResponse); diff --git a/third_party/auth/auth.pb.go b/third_party/auth/auth.pb.go index 33b2138..c50e4c9 100644 --- a/third_party/auth/auth.pb.go +++ b/third_party/auth/auth.pb.go @@ -2,7 +2,7 @@ // versions: // protoc-gen-go v1.28.1 // protoc v3.17.3 -// source: third_party/auth/auth.proto +// source: auth.proto package auth @@ -20,43 +20,43 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -var file_third_party_auth_auth_proto_extTypes = []protoimpl.ExtensionInfo{ +var file_auth_proto_extTypes = []protoimpl.ExtensionInfo{ { ExtendedType: (*descriptorpb.MethodOptions)(nil), ExtensionType: (*string)(nil), Field: 1111, Name: "auth.auth_key", Tag: "bytes,1111,opt,name=auth_key", - Filename: "third_party/auth/auth.proto", + Filename: "auth.proto", }, } // Extension fields to descriptorpb.MethodOptions. var ( // optional string auth_key = 1111; - E_AuthKey = &file_third_party_auth_auth_proto_extTypes[0] + E_AuthKey = &file_auth_proto_extTypes[0] ) -var File_third_party_auth_auth_proto protoreflect.FileDescriptor +var File_auth_proto protoreflect.FileDescriptor -var file_third_party_auth_auth_proto_rawDesc = []byte{ - 0x0a, 0x1b, 0x74, 0x68, 0x69, 0x72, 0x64, 0x5f, 0x70, 0x61, 0x72, 0x74, 0x79, 0x2f, 0x61, 0x75, - 0x74, 0x68, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x61, - 0x75, 0x74, 0x68, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3a, 0x3a, 0x0a, 0x08, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x6b, 0x65, - 0x79, 0x12, 0x1e, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, - 0x73, 0x18, 0xd7, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x75, 0x74, 0x68, 0x4b, 0x65, - 0x79, 0x42, 0x17, 0x5a, 0x15, 0x74, 0x68, 0x69, 0x72, 0x64, 0x5f, 0x70, 0x61, 0x72, 0x74, 0x79, - 0x2f, 0x61, 0x75, 0x74, 0x68, 0x3b, 0x61, 0x75, 0x74, 0x68, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, +var file_auth_proto_rawDesc = []byte{ + 0x0a, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x61, 0x75, + 0x74, 0x68, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x3a, 0x3a, 0x0a, 0x08, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x6b, 0x65, 0x79, + 0x12, 0x1e, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x18, 0xd7, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x75, 0x74, 0x68, 0x4b, 0x65, 0x79, + 0x42, 0x2b, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x6d, 0x6f, 0x67, 0x66, 0x65, 0x65, 0x2f, 0x6b, 0x69, 0x74, 0x2f, 0x74, 0x68, 0x69, + 0x72, 0x64, 0x5f, 0x70, 0x61, 0x72, 0x74, 0x79, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } -var file_third_party_auth_auth_proto_goTypes = []interface{}{ +var file_auth_proto_goTypes = []interface{}{ (*descriptorpb.MethodOptions)(nil), // 0: google.protobuf.MethodOptions } -var file_third_party_auth_auth_proto_depIdxs = []int32{ +var file_auth_proto_depIdxs = []int32{ 0, // 0: auth.auth_key:extendee -> google.protobuf.MethodOptions 1, // [1:1] is the sub-list for method output_type 1, // [1:1] is the sub-list for method input_type @@ -65,27 +65,27 @@ var file_third_party_auth_auth_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for field type_name } -func init() { file_third_party_auth_auth_proto_init() } -func file_third_party_auth_auth_proto_init() { - if File_third_party_auth_auth_proto != nil { +func init() { file_auth_proto_init() } +func file_auth_proto_init() { + if File_auth_proto != nil { return } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_third_party_auth_auth_proto_rawDesc, + RawDescriptor: file_auth_proto_rawDesc, NumEnums: 0, NumMessages: 0, NumExtensions: 1, NumServices: 0, }, - GoTypes: file_third_party_auth_auth_proto_goTypes, - DependencyIndexes: file_third_party_auth_auth_proto_depIdxs, - ExtensionInfos: file_third_party_auth_auth_proto_extTypes, + GoTypes: file_auth_proto_goTypes, + DependencyIndexes: file_auth_proto_depIdxs, + ExtensionInfos: file_auth_proto_extTypes, }.Build() - File_third_party_auth_auth_proto = out.File - file_third_party_auth_auth_proto_rawDesc = nil - file_third_party_auth_auth_proto_goTypes = nil - file_third_party_auth_auth_proto_depIdxs = nil + File_auth_proto = out.File + file_auth_proto_rawDesc = nil + file_auth_proto_goTypes = nil + file_auth_proto_depIdxs = nil }