Custom channel based authentication with gRPC

妖精的绣舞 提交于 2019-12-06 05:09:10

Auth Data in Metadata

Passing them in metadata one good solution. Take a look at hpack. Your header will be compressed, will take only a few bytes.

You cannot bind your auth data to the channel, as it is not guaranteed in HTTP/2, that the same TCP channel will be used for subsequent calls.

That said, I am still waiting for a proper example form the GRPC java team on Metadata based custom authentication.

Stream-based authentication

Stream-based authentication is also an option in case you want to save auth data between subsequent calls of the same API. In my interpretation this it means, that you have to pass authentication data only in the beginning of a stream. Your StreamObserver can then save the authentication data and reuse it in subsequent onNext() calls. I use this approach, it works really well.

Example

service MyService {
  rpc myFunction(stream MyMessage) returns (stream MyResponse)
}
message MyMessage {
  string user = 1;
  string password = 2;

  int32 myMessageVariable = 3;
}

user / password should only be set in the first onNext(myMessage) call on the requestObserever. This is also really efficient, because on the wire the stream is represented by the StreamId which is a single byte (depending on how many streams you have open at the same time).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!