stepancheg / grpc-rust

Rust implementation of gRPC
MIT License
1.37k stars 124 forks source link

Does grpc-rust support call metadata ? #52

Closed tbillington closed 7 years ago

tbillington commented 7 years ago

It was mentioned in https://github.com/grpc/grpc/issues/9316 that it's not supported, just curious about the status now.

stepancheg commented 7 years ago

It is easy to implement call metadata, but I'm not sure what API should look like.

Boscop commented 7 years ago

Then how can one do authentication with tokens? https://github.com/grpc/grpc-go/issues/106#issuecomment-77680241

E.g. with HTTP Auth, the client sends username+password, gets a token (signed user_id), and sends that with every request from then on. I found some info about how to do that with gRPC and they suggest using Metadata for that: https://groups.google.com/d/msg/grpc-io/LP94S3UF8bU/tWCm0ZA_FwAJ

Is it still possible with this lib right now, even though it doesn't support Metadata yet?

Or is there support for the second solution mentioned here? https://groups.google.com/d/msg/grpc-io/iLHgWC8o8UM/2PN4WaA9anMJ

stepancheg commented 7 years ago

I'm working on gRPC metadata currently.

AFAIU, gRPC metadata is a fancy name for request/response custom HTTP headers.

So, I decided (if you have ideas how to do it better, please share) that for method

rpc SayHello (HelloRequest) returns (HelloReply) {}

this code is generated:

fn SayHello(&self, o: GrpcRequestOptions, req: HelloRequest)
    -> GrpcSingleResponse<HelloReply> { ... }

GrpcRequestOptions contains metadata field where you can store whichever headers you want, and GrpcSingleResponse is basically an alias for Future<(Metadata, Future<Reply>)>.

Boscop commented 7 years ago

@stepancheg I'm not an expert on gRPC, but there are some experts in #grpc on Freenode. They helped me with some questions (about using grpc from js), I think it would be helpful to ask them what the best way would be to expose the metadata functionality.

stepancheg commented 7 years ago

OK, initial metadata is implemented in master, trailing metadata not yet.

Boscop commented 7 years ago

Thanks! I installed the grpc-compiler from github, but how can I now get the metadata of a request on the server side, and construct metadata for the reponse? The entries field of GrpcMetadata is private and I don't see any accessors: https://github.com/stepancheg/grpc-rust/blob/master/src/metadata.rs#L71 Also I don't see a way to construct GrpcMetadata (other than from_headers())..

Btw, if I want to respond to a login request with a session token (like a cookie), I should use completed_with_metadata(), right?

stepancheg commented 7 years ago

The entries field of GrpcMetadata is private and I don't see any accessors

I made fields public now, but there should be better API to access metadata.

Btw, if I want to respond to a login request with a session token (like a cookie), I should use completed_with_metadata(), right?

Yes, if your call is synchronous, or GrpcSingleResponse::new or another variable if it is asynchronous.