protobuf
protobuf copied to clipboard
how to use custom MethodOptions
extend google.protobuf.MethodOptions {
bool authentication = 1332;
}
service Echo {
rpc Send(Message) returns (google.protobuf.Empty) {}
rpc Get(Message) returns (Message) {
option (authentication) = true;
}
}
message Message {
int64 id = 1;
string message = 2;
}
I want to check if there is an authentication option in the middleware to execute the corresponding process.
Hey.
MethodOptions
Is part of the protocol descriptor.proto and not a well known type. Here is a list of all the well known types:
https://developers.google.com/protocol-buffers/docs/reference/google.protobuf
Did you want to use the Method
well known type?
With regard to your option checking. I am not sure that you can specific logic in your grpc service. @johanbrandhorst Do you have a suggestion for @ckeyer to solve his problem? I am not that familiar with grpc.
This kind of things isn't really possible or encouraged - in gRPC authentication is decided by the service not the protobuf interface. There are various tools to make this easier https://github.com/grpc-ecosystem/go-grpc-middleware/tree/master/auth, but none which allow you to specify this in the proto file.
Having said that, it is possible to extend the method options with your own type, like what gogoproto has done to Messages. However, this is really really not something you as a user of protobuf should be getting in the habit of doing, it's for library authors to use.
I would also be really interested by doing this.
I need to have some authenticated routes, and some not.
So your advice would be to :
- Create 2 services For example
service Api {}
service AuthenticatedApi {}
- Condition in the middleware
In my code, do a middleware and make conditions for authenticated routes
I really think it's cleaner to setup that in the service rpc