27
27
import java .util .Base64 ;
28
28
import java .util .Map ;
29
29
import java .util .concurrent .Executor ;
30
+ import java .util .function .Supplier ;
30
31
31
32
import javax .annotation .Nullable ;
32
33
57
58
* </p>
58
59
* <ul>
59
60
* <li>{@link #basicAuth(String, String) Basic-Auth}</li>
61
+ * <li>{@link #bearerAuth(Supplier) Bearer-Auth}</li>
62
+ * <li>Other variants using static or dynamic headers</li>
60
63
* <li>{@link #requirePrivacy(CallCredentials) Require privacy for the connection} (Wrapper)</li>
61
64
* <li>{@link #includeWhenPrivate(CallCredentials) Include credentials only if connection is private} (Wrapper)</li>
62
65
* </ul>
72
75
* <pre>
73
76
* <code>@Bean
74
77
* CallCredentials myCallCredentials() {
75
- * return CallCredentialsHelper# basicAuth("user", "password")}
78
+ * return CallCredentialsHelper. basicAuth("user", "password");
76
79
* }</code>
77
80
* </pre>
78
81
*
84
87
* <pre>
85
88
* <code>@Bean
86
89
* StubTransformer myCallCredentialsTransformer() {
87
- * return CallCredentialsHelper# mappedCredentialsStubTransformer(Map.of(
90
+ * return CallCredentialsHelper. mappedCredentialsStubTransformer(Map.of(
88
91
* "myService1", basicAuth("user1", "password1"),
89
92
* "theService2", basicAuth("foo", "bar"),
90
93
* "publicApi", null // No credentials needed
96
99
* <li>If you need different CallCredentials for each call, then you have to define it in the method yourself.
97
100
*
98
101
* <pre>
99
- * <code>stub.withCallCredentials(CallCredentialsHelper# basicAuth("user", "password")).doStuff(request);</code>
102
+ * <code>stub.withCallCredentials(CallCredentialsHelper. basicAuth("user", "password")).doStuff(request);</code>
100
103
* </pre>
101
104
*
102
105
* </li>
@@ -159,7 +162,8 @@ public static StubTransformer mappedCredentialsStubTransformer(
159
162
}
160
163
161
164
/**
162
- * Creates new call credentials with the given token for bearer auth.
165
+ * Creates new call credentials with the given token for bearer auth. Use this method if you have a permanent token
166
+ * or only use the call credentials for a single call/while the token is valid.
163
167
*
164
168
* <p>
165
169
* <b>Note:</b> This method uses experimental grpc-java-API features.
@@ -174,6 +178,23 @@ public static CallCredentials bearerAuth(final String token) {
174
178
return authorizationHeader (BEARER_AUTH_PREFIX + token );
175
179
}
176
180
181
+ /**
182
+ * Creates new call credentials with the given token source for bearer auth. Use this method if you derive the token
183
+ * from the active context (e.g. currently logged in user) or dynamically obtain it from the authentication server.
184
+ *
185
+ * <p>
186
+ * <b>Note:</b> This method uses experimental grpc-java-API features.
187
+ * </p>
188
+ *
189
+ * @param tokenSource the bearer token source to use
190
+ * @return The newly created bearer auth credentials.
191
+ * @see SecurityConstants#BEARER_AUTH_PREFIX
192
+ * @see #authorizationHeader(Supplier)
193
+ */
194
+ public static CallCredentials bearerAuth (final Supplier <String > tokenSource ) {
195
+ return authorizationHeader (() -> BEARER_AUTH_PREFIX + tokenSource );
196
+ }
197
+
177
198
/**
178
199
* Creates new call credentials with the given username and password for basic auth.
179
200
*
@@ -228,32 +249,55 @@ public static String encodeBasicAuth(final String username, final String passwor
228
249
* @see #authorizationHeaders(Metadata)
229
250
*/
230
251
public static CallCredentials authorizationHeader (final String authorization ) {
231
- requireNonNull (authorization );
252
+ requireNonNull (authorization , "authorization" );
232
253
final Metadata extraHeaders = new Metadata ();
233
254
extraHeaders .put (AUTHORIZATION_HEADER , authorization );
234
255
return authorizationHeaders (extraHeaders );
235
256
}
236
257
258
+ /**
259
+ * Creates new call credentials with the given authorization information source.
260
+ *
261
+ * <p>
262
+ * <b>Note:</b> This method uses experimental grpc-java-API features.
263
+ * </p>
264
+ *
265
+ * @param authorizationSource The authorization source to use. The authorization usually starts with the scheme such
266
+ * as as {@code "Basic "} or {@code "Bearer "} followed by the actual authentication information.
267
+ * @return The newly created call credentials.
268
+ * @see SecurityConstants#AUTHORIZATION_HEADER
269
+ * @see #authorizationHeaders(Supplier)
270
+ */
271
+ public static CallCredentials authorizationHeader (final Supplier <String > authorizationSource ) {
272
+ requireNonNull (authorizationSource , "authorizationSource" );
273
+
274
+ return authorizationHeaders (() -> {
275
+ final Metadata extraHeaders = new Metadata ();
276
+ extraHeaders .put (AUTHORIZATION_HEADER , authorizationSource .get ());
277
+ return extraHeaders ;
278
+ });
279
+ }
280
+
237
281
/**
238
282
* Creates new call credentials with the given static authorization headers.
239
283
*
240
284
* @param authorizationHeaders The authorization headers to use.
241
285
* @return The newly created call credentials.
242
286
*/
243
287
public static CallCredentials authorizationHeaders (final Metadata authorizationHeaders ) {
244
- return new StaticSecurityHeaderCallCredentials (requireNonNull ( authorizationHeaders ) );
288
+ return new StaticSecurityHeaderCallCredentials (authorizationHeaders );
245
289
}
246
290
247
291
/**
248
- * The static security header {@link CallCredentials} simply add a set of predefined headers to the call. Their
292
+ * The static security header {@link CallCredentials} simply adds a set of predefined headers to the call. Their
249
293
* specific meaning is server specific. This implementation can be used, for example, for BasicAuth.
250
294
*/
251
295
private static final class StaticSecurityHeaderCallCredentials extends CallCredentials {
252
296
253
297
private final Metadata extraHeaders ;
254
298
255
- StaticSecurityHeaderCallCredentials (final Metadata extraHeaders ) {
256
- this .extraHeaders = requireNonNull (extraHeaders , "extraHeaders " );
299
+ StaticSecurityHeaderCallCredentials (final Metadata authorizationHeaders ) {
300
+ this .extraHeaders = requireNonNull (authorizationHeaders , "authorizationHeaders " );
257
301
}
258
302
259
303
@ Override
@@ -272,6 +316,44 @@ public String toString() {
272
316
273
317
}
274
318
319
+ /**
320
+ * Creates new call credentials with the given authorization headers source.
321
+ *
322
+ * @param authorizationHeadersSupplier The authorization headers source to use.
323
+ * @return The newly created call credentials.
324
+ */
325
+ public static CallCredentials authorizationHeaders (final Supplier <Metadata > authorizationHeadersSupplier ) {
326
+ return new DynamicSecurityHeaderCallCredentials (authorizationHeadersSupplier );
327
+ }
328
+
329
+ /**
330
+ * The dynamic security header {@link CallCredentials} simply adds a set of dynamic headers to the call. Their
331
+ * specific meaning is server specific. This implementation can be used, for example, for BasicAuth.
332
+ */
333
+ private static final class DynamicSecurityHeaderCallCredentials extends CallCredentials {
334
+
335
+ private final Supplier <Metadata > extraHeadersSupplier ;
336
+
337
+ DynamicSecurityHeaderCallCredentials (final Supplier <Metadata > authorizationHeadersSupplier ) {
338
+ this .extraHeadersSupplier = requireNonNull (authorizationHeadersSupplier , "authorizationHeadersSupplier" );
339
+ }
340
+
341
+ @ Override
342
+ public void applyRequestMetadata (final RequestInfo requestInfo , final Executor appExecutor ,
343
+ final MetadataApplier applier ) {
344
+ applier .apply (this .extraHeadersSupplier .get ());
345
+ }
346
+
347
+ @ Override
348
+ public void thisUsesUnstableApi () {} // API evolution in progress
349
+
350
+ @ Override
351
+ public String toString () {
352
+ return "DynamicSecurityHeaderCallCredentials [extraHeadersSupplier=" + this .extraHeadersSupplier + "]" ;
353
+ }
354
+
355
+ }
356
+
275
357
/**
276
358
* Checks whether the given security level provides privacy for all data being send on the connection.
277
359
*
0 commit comments