How to add CallCredentails to grpc requests #52531
Replies: 2 comments 3 replies
|
The cleanest centralized solution here is a dedicated reactive wrapper around your gRPC client — essentially a thin factory that prepends the token fetch before every call, without touching business logic. Approach: Token-aware client wrapper @ApplicationScoped
public class SecureTaskClient {
@Inject
OidcClient oidcClient;
@GrpcClient("task-service")
MutinyTaskServiceGrpc.MutinyTaskServiceStub taskStub;
private Uni<MutinyTaskServiceGrpc.MutinyTaskServiceStub> authenticatedStub() {
return oidcClient.getTokens()
.map(tokens -> taskStub.withInterceptors(
new ClientInterceptor() {
@Override
public <Q, R> ClientCall<Q, R> interceptCall(
MethodDescriptor<Q, R> method, CallOptions options, Channel next) {
return new ForwardingClientCall.SimpleForwardingClientCall<>(next.newCall(method, options)) {
@Override
public void start(Listener<R> listener, Metadata headers) {
headers.put(
Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER),
"Bearer " + tokens.getAccessToken()
);
super.start(listener, headers);
}
};
}
}
));
}
public Uni<TaskResponse> readPendingTasks(TaskRequest request) {
return authenticatedStub()
.flatMap(stub -> stub.readPendingTasks(request));
}
}Inject Why The Vert.x gRPC client does not implement the Token caching If you are hitting a high-traffic endpoint, add token caching to avoid fetching a new token per call: @ApplicationScoped
public class CachedTokenProvider {
@Inject OidcClient oidcClient;
private volatile Tokens cached;
public Uni<String> getAccessToken() {
if (cached != null && !cached.isAccessTokenExpired()) {
return Uni.createFrom().item(cached.getAccessToken());
}
return oidcClient.getTokens()
.invoke(t -> this.cached = t)
.map(Tokens::getAccessToken);
}
}Then use |
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
I'm facing a challenge with Quarkus gRPC (Vert.x gRPC client) and OIDC token integration.
I need to attach a Bearer Token to every outgoing gRPC call, but the token needs to be fetched via the OidcClient.
The Setup:
The Problem:
I need to fetch the token per request. However, I'm hitting a wall regarding the execution context:
What I've tried:
Question:
Thanks in advance,
Tobias
All reactions