diff --git a/go.mod b/go.mod index ffe7276..c9df209 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,13 @@ module github.com/dapperlabs/protoc-gen-go-grpc-scopes go 1.20 require ( - github.com/golang/protobuf v1.5.3 github.com/stretchr/testify v1.8.4 google.golang.org/grpc v1.56.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/main.go b/main.go deleted file mode 100644 index 8a93374..0000000 --- a/main.go +++ /dev/null @@ -1,59 +0,0 @@ -package main - -import ( - "flag" - "fmt" - - "github.com/dapperlabs/protoc-gen-go-grpc-scopes/scopesproto" - "github.com/golang/protobuf/proto" - "google.golang.org/protobuf/compiler/protogen" - "google.golang.org/protobuf/types/descriptorpb" - "google.golang.org/protobuf/types/pluginpb" -) - -func main() { - var ( - flags flag.FlagSet - ) - - protogen.Options{ - ParamFunc: flags.Set, - }.Run(func(gen *protogen.Plugin) error { - for _, f := range gen.Files { - if !f.Generate { - continue - } - msgScopesInfo := map[string][]string{} - for _, msg := range f.Messages { - msgName := msg.GoIdent.GoName - msgOptions := msg.Desc.Options() - if msgOptions == nil { - continue - } - msgOptionsPB := msgOptions.(*descriptorpb.MessageOptions) - ext, err := proto.GetExtension(msgOptionsPB, scopesproto.E_RequiredReqScopes) - if err != nil { - // not a scope extension - continue - } - scopesExt := ext.(*scopesproto.RequiredScopesOption) - msgScopesInfo[msgName] = scopesExt.Scopes - } - - if len(msgScopesInfo) > 0 { - // generate scopes file - filename := f.GeneratedFilenamePrefix + "_scopes.pb.go" - g := gen.NewGeneratedFile(filename, f.GoImportPath) - g.P("// Code generated by protoc-gen-go-scopes. DO NOT EDIT.") - g.P("package ", f.GoPackageName) - for msg, requiredScopes := range msgScopesInfo { - g.P(fmt.Sprintf("func (*%s) RequiredScopes() []string {", msg)) - g.P(fmt.Sprintf("return %#v", requiredScopes)) - g.P("}") - } - } - } - gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) - return nil - }) -} diff --git a/proto/scopes.proto b/proto/scopes.proto index 6017774..da31124 100644 --- a/proto/scopes.proto +++ b/proto/scopes.proto @@ -11,6 +11,6 @@ message RequiredScopesOption { repeated string scopes = 1; } -extend google.protobuf.MessageOptions { - RequiredScopesOption required_req_scopes = 50000; // 50000 is an example, replace it with your desired field number +extend google.protobuf.MethodOptions { + RequiredScopesOption required_method_scopes = 70000; } diff --git a/scopes/grpc.go b/scopes/grpc.go index 6cdcf05..5580ec1 100644 --- a/scopes/grpc.go +++ b/scopes/grpc.go @@ -2,20 +2,78 @@ package scopes import ( "context" + "fmt" + "strings" + "github.com/dapperlabs/protoc-gen-go-grpc-scopes/scopesproto" "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/reflect/protoregistry" + "google.golang.org/protobuf/types/descriptorpb" ) type ScopeValidator func(ctx context.Context, scopes []string) error +func getMethodDescriptor(info *grpc.UnaryServerInfo) (protoreflect.MethodDescriptor, error) { + // /package.Service/Method + fullMethodSplit := strings.Split(info.FullMethod, "/") + + if len(fullMethodSplit) != 3 { + return nil, fmt.Errorf("invalid full method: %s", info.FullMethod) + } + + svcFullName := protoreflect.FullName(fullMethodSplit[1]) + + descriptorByService, err := protoregistry.GlobalFiles.FindDescriptorByName(svcFullName) + if err != nil { + return nil, fmt.Errorf("failed to find service descriptor: %s", err) + } + + svc, ok := descriptorByService.(protoreflect.ServiceDescriptor) + if !ok { + return nil, fmt.Errorf("descriptor is not of a service: %s", descriptorByService.FullName()) + } + + serviceMethod := protoreflect.FullName(info.FullMethod).Name() + + // Service/Method + serviceMethodSplit := strings.Split(string(serviceMethod), "/") + if len(serviceMethodSplit) != 2 { + return nil, fmt.Errorf("invalid service method: %s", serviceMethod) + } + + methodName := protoreflect.Name(serviceMethodSplit[1]) + + method := svc.Methods().ByName(methodName) + if method == nil { + return nil, fmt.Errorf("method not found: %s", methodName) + } + + return method, nil +} + // ScopeValidationInterceptor validates that the request has the required scopes func ScopeValidationInterceptor(v ScopeValidator) grpc.UnaryServerInterceptor { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { - if scopeFetcher, ok := req.(HasScopeRequirements); ok { - if err := v(ctx, scopeFetcher.RequiredScopes()); err != nil { + method, err := getMethodDescriptor(info) + if err != nil { + return nil, status.Errorf(codes.Internal, "failed to get method descriptor: %s", err) + } + + methodOptionsPB := method.Options().(*descriptorpb.MethodOptions) + + methodExt := proto.GetExtension(methodOptionsPB, scopesproto.E_RequiredMethodScopes) + if methodExt != nil { + scopesExt := methodExt.(*scopesproto.RequiredScopesOption) + + if err := v(ctx, scopesExt.GetScopes()); err != nil { return nil, err } } + return handler(ctx, req) } } diff --git a/scopes/grpc_test.go b/scopes/grpc_test.go index 0c5d678..56f1265 100644 --- a/scopes/grpc_test.go +++ b/scopes/grpc_test.go @@ -18,6 +18,9 @@ import ( func TestScopeValidationInterceptor(t *testing.T) { server := grpc.NewServer( grpc.UnaryInterceptor(scopes.ScopeValidationInterceptor(func(ctx context.Context, scopes []string) error { + if len(scopes) == 0 { + return nil + } md, ok := metadata.FromIncomingContext(ctx) if !ok { return status.Error(codes.Unauthenticated, "missing metadata") @@ -53,7 +56,7 @@ func TestScopeValidationInterceptor(t *testing.T) { ctx := metadata.NewOutgoingContext(context.Background(), header) res, err := client.Ping(ctx, &testgen.PingRequest{}) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "pong", res.GetPong()) res, err = client.Ping(context.Background(), &testgen.PingRequest{}) @@ -63,11 +66,19 @@ func TestScopeValidationInterceptor(t *testing.T) { ctx = metadata.NewOutgoingContext(context.Background(), header) res, err = client.Ping(ctx, &testgen.PingRequest{}) assert.Equal(t, codes.PermissionDenied, status.Code(err)) + + res, err = client.FreePing(ctx, &testgen.PingRequest{}) + require.NoError(t, err) + assert.Equal(t, "free pong", res.GetPong()) } type ScopeValidatorServer struct { } +func (s ScopeValidatorServer) FreePing(ctx context.Context, request *testgen.PingRequest) (*testgen.PingResponse, error) { + return &testgen.PingResponse{Pong: "free pong"}, nil +} + func (s ScopeValidatorServer) Ping(ctx context.Context, request *testgen.PingRequest) (*testgen.PingResponse, error) { return &testgen.PingResponse{Pong: "pong"}, nil } diff --git a/scopes/hasscope.go b/scopes/hasscope.go deleted file mode 100644 index df5bbc5..0000000 --- a/scopes/hasscope.go +++ /dev/null @@ -1,5 +0,0 @@ -package scopes - -type HasScopeRequirements interface { - RequiredScopes() []string -} diff --git a/scopesproto/scopes.pb.go b/scopesproto/scopes.pb.go index 9d3eb66..76ed88c 100644 --- a/scopesproto/scopes.pb.go +++ b/scopesproto/scopes.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.31.0 +// protoc-gen-go v1.26.0 // protoc v4.23.3 // source: scopes.proto @@ -71,19 +71,19 @@ func (x *RequiredScopesOption) GetScopes() []string { var file_scopes_proto_extTypes = []protoimpl.ExtensionInfo{ { - ExtendedType: (*descriptorpb.MessageOptions)(nil), + ExtendedType: (*descriptorpb.MethodOptions)(nil), ExtensionType: (*RequiredScopesOption)(nil), - Field: 50000, - Name: "scopes.required_req_scopes", - Tag: "bytes,50000,opt,name=required_req_scopes", + Field: 70000, + Name: "scopes.required_method_scopes", + Tag: "bytes,70000,opt,name=required_method_scopes", Filename: "scopes.proto", }, } -// Extension fields to descriptorpb.MessageOptions. +// Extension fields to descriptorpb.MethodOptions. var ( - // optional scopes.RequiredScopesOption required_req_scopes = 50000; - E_RequiredReqScopes = &file_scopes_proto_extTypes[0] // 50000 is an example, replace it with your desired field number + // optional scopes.RequiredScopesOption required_method_scopes = 70000; + E_RequiredMethodScopes = &file_scopes_proto_extTypes[0] ) var File_scopes_proto protoreflect.FileDescriptor @@ -92,21 +92,22 @@ var file_scopes_proto_rawDesc = []byte{ 0x0a, 0x0c, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 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, 0x22, 0x33, 0x0a, 0x14, 0x52, 0x65, 0x71, 0x75, + 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2e, 0x0a, 0x14, 0x52, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x53, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, - 0x12, 0x1b, 0x0a, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, - 0x42, 0x03, 0x98, 0x01, 0x03, 0x52, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x3a, 0x6f, 0x0a, - 0x13, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x71, 0x5f, 0x73, 0x63, - 0x6f, 0x70, 0x65, 0x73, 0x12, 0x1f, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x4f, 0x70, - 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd0, 0x86, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, - 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x53, - 0x63, 0x6f, 0x70, 0x65, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x11, 0x72, 0x65, 0x71, - 0x75, 0x69, 0x72, 0x65, 0x64, 0x52, 0x65, 0x71, 0x53, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x42, 0x32, - 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x61, 0x70, - 0x70, 0x65, 0x72, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x67, 0x6f, 0x2d, 0x67, 0x72, 0x70, 0x63, 0x2d, - 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2f, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x12, 0x16, 0x0a, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, + 0x52, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x3a, 0x74, 0x0a, 0x16, 0x72, 0x65, 0x71, 0x75, + 0x69, 0x72, 0x65, 0x64, 0x5f, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x5f, 0x73, 0x63, 0x6f, 0x70, + 0x65, 0x73, 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, 0xf0, 0xa2, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1c, 0x2e, 0x73, 0x63, 0x6f, + 0x70, 0x65, 0x73, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x64, 0x53, 0x63, 0x6f, 0x70, + 0x65, 0x73, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x14, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, + 0x65, 0x64, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x53, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x42, 0x3d, + 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x61, 0x70, + 0x70, 0x65, 0x72, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, + 0x65, 0x6e, 0x2d, 0x67, 0x6f, 0x2d, 0x67, 0x72, 0x70, 0x63, 0x2d, 0x73, 0x63, 0x6f, 0x70, 0x65, + 0x73, 0x2f, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -123,12 +124,12 @@ func file_scopes_proto_rawDescGZIP() []byte { var file_scopes_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_scopes_proto_goTypes = []interface{}{ - (*RequiredScopesOption)(nil), // 0: scopes.RequiredScopesOption - (*descriptorpb.MessageOptions)(nil), // 1: google.protobuf.MessageOptions + (*RequiredScopesOption)(nil), // 0: scopes.RequiredScopesOption + (*descriptorpb.MethodOptions)(nil), // 1: google.protobuf.MethodOptions } var file_scopes_proto_depIdxs = []int32{ - 1, // 0: scopes.required_req_scopes:extendee -> google.protobuf.MessageOptions - 0, // 1: scopes.required_req_scopes:type_name -> scopes.RequiredScopesOption + 1, // 0: scopes.required_method_scopes:extendee -> google.protobuf.MethodOptions + 0, // 1: scopes.required_method_scopes:type_name -> scopes.RequiredScopesOption 2, // [2:2] is the sub-list for method output_type 2, // [2:2] is the sub-list for method input_type 1, // [1:2] is the sub-list for extension type_name diff --git a/test/buf.gen.yaml b/test/buf.gen.yaml index 225de31..5eff7a9 100644 --- a/test/buf.gen.yaml +++ b/test/buf.gen.yaml @@ -9,8 +9,4 @@ plugins: out: ./testgen opt: - paths=source_relative - - require_unimplemented_servers=false - - name: go-grpc-scopes - out: ./testgen - opt: - - paths=source_relative \ No newline at end of file + - require_unimplemented_servers=false \ No newline at end of file diff --git a/test/buf.lock b/test/buf.lock index dcc0d66..854b714 100644 --- a/test/buf.lock +++ b/test/buf.lock @@ -8,4 +8,4 @@ deps: - remote: buf.build owner: rrrkren repository: protoc-gen-go-grpc-scopes - commit: 1ca8d5541c154b60afd6d65d7f63acee + commit: 65b5b0c776414d3c8f89b8310b77ec1c diff --git a/test/test.proto b/test/test.proto index 0ef7a08..524b294 100644 --- a/test/test.proto +++ b/test/test.proto @@ -4,14 +4,7 @@ package testscopes; option go_package = "github.com/dapperlabs/protoc-gen-go-grpc-scopes/test/testgen"; -import "scopes.proto"; -service PingPong { - rpc Ping (PingRequest) returns (PingResponse); -} - message PingRequest { - option (scopes.required_req_scopes).scopes = "scope1"; - option (scopes.required_req_scopes).scopes = "scope2"; string ping = 1; } diff --git a/test/testgen/test.pb.go b/test/testgen/test.pb.go index e03a0b5..0d19b43 100644 --- a/test/testgen/test.pb.go +++ b/test/testgen/test.pb.go @@ -7,7 +7,6 @@ package testgen import ( - _ "github.com/dapperlabs/protoc-gen-go-grpc-scopes/scopesproto" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" @@ -119,29 +118,22 @@ var File_test_proto protoreflect.FileDescriptor var file_test_proto_rawDesc = []byte{ 0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x74, 0x65, - 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x1a, 0x0c, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x37, 0x0a, 0x0b, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x69, 0x6e, 0x67, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x69, 0x6e, 0x67, 0x3a, 0x14, 0x82, 0xb5, 0x18, 0x10, 0x0a, - 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x31, 0x0a, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x32, 0x22, - 0x22, 0x0a, 0x0c, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, - 0x12, 0x0a, 0x04, 0x70, 0x6f, 0x6e, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, - 0x6f, 0x6e, 0x67, 0x32, 0x45, 0x0a, 0x08, 0x50, 0x69, 0x6e, 0x67, 0x50, 0x6f, 0x6e, 0x67, 0x12, - 0x39, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x17, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, 0x63, - 0x6f, 0x70, 0x65, 0x73, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x18, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2e, 0x50, 0x69, - 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0xa1, 0x01, 0x0a, 0x0e, 0x63, - 0x6f, 0x6d, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x42, 0x09, 0x54, - 0x65, 0x73, 0x74, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x3c, 0x67, 0x69, 0x74, 0x68, - 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x61, 0x70, 0x70, 0x65, 0x72, 0x6c, 0x61, 0x62, - 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x67, 0x6f, 0x2d, - 0x67, 0x72, 0x70, 0x63, 0x2d, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74, - 0x2f, 0x74, 0x65, 0x73, 0x74, 0x67, 0x65, 0x6e, 0xa2, 0x02, 0x03, 0x54, 0x58, 0x58, 0xaa, 0x02, - 0x0a, 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0xca, 0x02, 0x0a, 0x54, 0x65, - 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0xe2, 0x02, 0x16, 0x54, 0x65, 0x73, 0x74, 0x73, - 0x63, 0x6f, 0x70, 0x65, 0x73, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, - 0x61, 0xea, 0x02, 0x0a, 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x22, 0x21, 0x0a, 0x0b, 0x50, 0x69, 0x6e, 0x67, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x69, 0x6e, 0x67, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x69, 0x6e, 0x67, 0x22, 0x22, 0x0a, 0x0c, 0x50, + 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x70, + 0x6f, 0x6e, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x6f, 0x6e, 0x67, 0x42, + 0xa1, 0x01, 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, + 0x65, 0x73, 0x42, 0x09, 0x54, 0x65, 0x73, 0x74, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, + 0x3c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x64, 0x61, 0x70, 0x70, + 0x65, 0x72, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, + 0x6e, 0x2d, 0x67, 0x6f, 0x2d, 0x67, 0x72, 0x70, 0x63, 0x2d, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, + 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x67, 0x65, 0x6e, 0xa2, 0x02, 0x03, + 0x54, 0x58, 0x58, 0xaa, 0x02, 0x0a, 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, + 0xca, 0x02, 0x0a, 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0xe2, 0x02, 0x16, + 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, + 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0a, 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, + 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -162,10 +154,8 @@ var file_test_proto_goTypes = []interface{}{ (*PingResponse)(nil), // 1: testscopes.PingResponse } var file_test_proto_depIdxs = []int32{ - 0, // 0: testscopes.PingPong.Ping:input_type -> testscopes.PingRequest - 1, // 1: testscopes.PingPong.Ping:output_type -> testscopes.PingResponse - 1, // [1:2] is the sub-list for method output_type - 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -210,7 +200,7 @@ func file_test_proto_init() { NumEnums: 0, NumMessages: 2, NumExtensions: 0, - NumServices: 1, + NumServices: 0, }, GoTypes: file_test_proto_goTypes, DependencyIndexes: file_test_proto_depIdxs, diff --git a/test/testgen/test_scopes.pb.go b/test/testgen/test_scopes.pb.go deleted file mode 100644 index b637e28..0000000 --- a/test/testgen/test_scopes.pb.go +++ /dev/null @@ -1,6 +0,0 @@ -// Code generated by protoc-gen-go-scopes. DO NOT EDIT. -package testgen - -func (*PingRequest) RequiredScopes() []string { - return []string{"scope1", "scope2"} -} diff --git a/test/testgen/testrpc.pb.go b/test/testgen/testrpc.pb.go new file mode 100644 index 0000000..bd20d88 --- /dev/null +++ b/test/testgen/testrpc.pb.go @@ -0,0 +1,92 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc (unknown) +// source: testrpc.proto + +package testgen + +import ( + _ "github.com/dapperlabs/protoc-gen-go-grpc-scopes/scopesproto" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +var File_testrpc_proto protoreflect.FileDescriptor + +var file_testrpc_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x72, 0x70, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x0a, 0x74, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x1a, 0x0c, 0x73, 0x63, 0x6f, + 0x70, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x32, 0x9a, 0x01, 0x0a, 0x08, 0x50, 0x69, 0x6e, 0x67, 0x50, 0x6f, + 0x6e, 0x67, 0x12, 0x4f, 0x0a, 0x04, 0x50, 0x69, 0x6e, 0x67, 0x12, 0x17, 0x2e, 0x74, 0x65, 0x73, + 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, + 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x14, 0x82, + 0x97, 0x22, 0x10, 0x0a, 0x06, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x31, 0x0a, 0x06, 0x73, 0x63, 0x6f, + 0x70, 0x65, 0x32, 0x12, 0x3d, 0x0a, 0x08, 0x46, 0x72, 0x65, 0x65, 0x50, 0x69, 0x6e, 0x67, 0x12, + 0x17, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2e, 0x50, 0x69, 0x6e, + 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, + 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x42, 0xa4, 0x01, 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x73, + 0x63, 0x6f, 0x70, 0x65, 0x73, 0x42, 0x0c, 0x54, 0x65, 0x73, 0x74, 0x72, 0x70, 0x63, 0x50, 0x72, + 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x3c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x64, 0x61, 0x70, 0x70, 0x65, 0x72, 0x6c, 0x61, 0x62, 0x73, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x67, 0x6f, 0x2d, 0x67, 0x72, 0x70, 0x63, 0x2d, + 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x74, 0x65, 0x73, 0x74, + 0x67, 0x65, 0x6e, 0xa2, 0x02, 0x03, 0x54, 0x58, 0x58, 0xaa, 0x02, 0x0a, 0x54, 0x65, 0x73, 0x74, + 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0xca, 0x02, 0x0a, 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, + 0x70, 0x65, 0x73, 0xe2, 0x02, 0x16, 0x54, 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, + 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0a, 0x54, + 0x65, 0x73, 0x74, 0x73, 0x63, 0x6f, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, +} + +var file_testrpc_proto_goTypes = []interface{}{ + (*PingRequest)(nil), // 0: testscopes.PingRequest + (*PingResponse)(nil), // 1: testscopes.PingResponse +} +var file_testrpc_proto_depIdxs = []int32{ + 0, // 0: testscopes.PingPong.Ping:input_type -> testscopes.PingRequest + 0, // 1: testscopes.PingPong.FreePing:input_type -> testscopes.PingRequest + 1, // 2: testscopes.PingPong.Ping:output_type -> testscopes.PingResponse + 1, // 3: testscopes.PingPong.FreePing:output_type -> testscopes.PingResponse + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_testrpc_proto_init() } +func file_testrpc_proto_init() { + if File_testrpc_proto != nil { + return + } + file_test_proto_init() + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_testrpc_proto_rawDesc, + NumEnums: 0, + NumMessages: 0, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_testrpc_proto_goTypes, + DependencyIndexes: file_testrpc_proto_depIdxs, + }.Build() + File_testrpc_proto = out.File + file_testrpc_proto_rawDesc = nil + file_testrpc_proto_goTypes = nil + file_testrpc_proto_depIdxs = nil +} diff --git a/test/testgen/test_grpc.pb.go b/test/testgen/testrpc_grpc.pb.go similarity index 70% rename from test/testgen/test_grpc.pb.go rename to test/testgen/testrpc_grpc.pb.go index 429d7ff..b46cc36 100644 --- a/test/testgen/test_grpc.pb.go +++ b/test/testgen/testrpc_grpc.pb.go @@ -2,7 +2,7 @@ // versions: // - protoc-gen-go-grpc v1.2.0 // - protoc (unknown) -// source: test.proto +// source: testrpc.proto package testgen @@ -23,6 +23,7 @@ const _ = grpc.SupportPackageIsVersion7 // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type PingPongClient interface { Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error) + FreePing(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error) } type pingPongClient struct { @@ -42,11 +43,21 @@ func (c *pingPongClient) Ping(ctx context.Context, in *PingRequest, opts ...grpc return out, nil } +func (c *pingPongClient) FreePing(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error) { + out := new(PingResponse) + err := c.cc.Invoke(ctx, "/testscopes.PingPong/FreePing", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // PingPongServer is the server API for PingPong service. // All implementations should embed UnimplementedPingPongServer // for forward compatibility type PingPongServer interface { Ping(context.Context, *PingRequest) (*PingResponse, error) + FreePing(context.Context, *PingRequest) (*PingResponse, error) } // UnimplementedPingPongServer should be embedded to have forward compatible implementations. @@ -56,6 +67,9 @@ type UnimplementedPingPongServer struct { func (UnimplementedPingPongServer) Ping(context.Context, *PingRequest) (*PingResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented") } +func (UnimplementedPingPongServer) FreePing(context.Context, *PingRequest) (*PingResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method FreePing not implemented") +} // UnsafePingPongServer may be embedded to opt out of forward compatibility for this service. // Use of this interface is not recommended, as added methods to PingPongServer will @@ -86,6 +100,24 @@ func _PingPong_Ping_Handler(srv interface{}, ctx context.Context, dec func(inter return interceptor(ctx, in, info, handler) } +func _PingPong_FreePing_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(PingRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(PingPongServer).FreePing(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/testscopes.PingPong/FreePing", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(PingPongServer).FreePing(ctx, req.(*PingRequest)) + } + return interceptor(ctx, in, info, handler) +} + // PingPong_ServiceDesc is the grpc.ServiceDesc for PingPong service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -97,7 +129,11 @@ var PingPong_ServiceDesc = grpc.ServiceDesc{ MethodName: "Ping", Handler: _PingPong_Ping_Handler, }, + { + MethodName: "FreePing", + Handler: _PingPong_FreePing_Handler, + }, }, Streams: []grpc.StreamDesc{}, - Metadata: "test.proto", + Metadata: "testrpc.proto", } diff --git a/test/testrpc.proto b/test/testrpc.proto new file mode 100644 index 0000000..c87aceb --- /dev/null +++ b/test/testrpc.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package testscopes; + +option go_package = "github.com/dapperlabs/protoc-gen-go-grpc-scopes/test/testgen"; + +import "scopes.proto"; +import "test.proto"; + +service PingPong { + rpc Ping (PingRequest) returns (PingResponse) { + option (scopes.required_method_scopes).scopes = "scope1"; + option (scopes.required_method_scopes).scopes = "scope2"; + } + rpc FreePing (PingRequest) returns (PingResponse); +} \ No newline at end of file