I have a grpc-java server and need to make an async call to an auth service, before processing a request. I think this should be done in an interceptor, but it requires to synchronously return a Listener from interceptCall()
class AuthInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
Metadata headers,
ServerCallHandler<ReqT, RespT> next
) {
String token = ""; //get token from headers
authService.authorize(token).subscribe(
ok -> // process the request
error -> call.close(Status.UNAUTHENTICATED, headers)
);
// Here we need to return a Listener, but we haven't started a call yet
}
}
So the questions are: how to make an async call from a ServerInterceptor, and if it can't be done, what is the right way to asynchronously authenticate requests it grpc? I know it can be done directly in grpc services with StreamObservers, but request authorization is a cross cutting concern and interceptors seem to be a perfect place for it.
DelayedListener
? It's unclear to me how to connect the dots. – Abhijit Sarkar