Skip to content

Commit a96c561

Browse files
committed
Document How to retry with new auth token using builtin retry?
Fixes #415
1 parent e27bd6f commit a96c561

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

docs/en/client/security.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ This page describes how you connect to a grpc server and authenticate yourself.
1212
- [Trusting a Server](#trusting-a-server)
1313
- [Mutual Certificate Authentication](#mutual-certificate-authentication)
1414
- [Authentication](#authentication)
15+
- [Creating CallCredentials](#creating-callcredentials)
16+
- [Using CallCredentials](#using-callcredentials)
17+
- [Retry with new Authentication](#retry-with-new-authentication)
1518

1619
## Additional Topics <!-- omit in toc -->
1720

@@ -97,6 +100,8 @@ grpc.client.__name__.security.privateKey=file:certificates/client.key
97100
98101
## Authentication
99102
103+
### Creating CallCredentials
104+
100105
In addition to mutual certificate authentication, there are several other ways to authenticate yourself, such as
101106
`BasicAuth`.
102107
@@ -106,6 +111,20 @@ various libraries out there that provide implementations for grpc's
106111
`CallCredentials` are potentially active components because they can authenticate the request using a (third party)
107112
service and can manage and renew session tokens themselves.
108113

114+
````java
115+
@Bean
116+
CallCredentials basicAuthCredentials() {
117+
return CallCredentialsHelper.basicAuth("user", "password");
118+
}
119+
120+
@Bean
121+
CallCredentials bearerAuthForwardingCredentials() {
122+
return CallCredentialsHelper.bearerAuth(() -> KeycloakSecurityContext.getTokenString());
123+
}
124+
````
125+
126+
### Using CallCredentials
127+
109128
If you have exactly one `CallCredentials` in your application context, we'll automatically create a `StubTransformer`
110129
for you and configure all `Stub`s to use it. If you wish to configure different credentials per stub, then you use our
111130
helper methods in the
@@ -122,6 +141,45 @@ MyServiceBlockingStub myServiceForUser = myService.withCallCredentials(userCrede
122141
return myServiceForUser.send(request);
123142
````
124143
144+
### Retry with new Authentication
145+
146+
If you want to retry calls that failed due to an expired token (using grpc's built-in retry mechanism), you can use the
147+
following example `ClientInterceptor` as a guide to automatically report the failure to the token store.
148+
Please note that many popular token-based authentication systems (such as OAuth) also provide a token TTL that can be
149+
used to automatically update the token before the call is even sent for the first time, rendering this obsolete.
150+
151+
````java
152+
@Override
153+
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
154+
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
155+
156+
callOptions = callOptions
157+
.withCallCredentials(this.credentials)
158+
.withStreamTracerFactory(new ClientStreamTracer.Factory() {
159+
160+
@Override
161+
public ClientStreamTracer newClientStreamTracer(
162+
ClientStreamTracer.StreamInfo info, Metadata headers) {
163+
164+
// Make sure your implementations do _not_ block and return _immediately_
165+
final Object authToken = headers.get(AUTH_TOKEN_KEY);
166+
return new ClientStreamTracer() {
167+
168+
@Override
169+
public void streamClosed(final Status status) {
170+
this.credentials.invalidate(authToken);
171+
}
172+
};
173+
174+
}
175+
});
176+
177+
return next.newCall(method, callOptions);
178+
}
179+
````
180+
181+
For more details refer to [How to retry with new auth token using builtin retry?](https://github.com/grpc/grpc-java/issues/7345#issuecomment-679295003)
182+
125183
## Additional Topics <!-- omit in toc -->
126184

127185
- [Getting Started](getting-started.md)

0 commit comments

Comments
 (0)