Skip to content

Commit 7f74c60

Browse files
committed
Add more utility methods to CallCredentialsHelper (Fixes #385)
1 parent b56a9bf commit 7f74c60

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/security/CallCredentialsHelper.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,23 @@ public static StubTransformer mappedCredentialsStubTransformer(
158158
}
159159

160160
/**
161-
* Creates a new call credential with the given token for bearer auth.
161+
* Creates new call credentials with the given token for bearer auth.
162162
*
163163
* <p>
164164
* <b>Note:</b> This method uses experimental grpc-java-API features.
165165
* </p>
166166
*
167167
* @param token the bearer token to use
168168
* @return The newly created bearer auth credentials.
169+
* @see #BEARER_AUTH_PREFIX
170+
* @see #authorizationHeader(String)
169171
*/
170172
public static CallCredentials bearerAuth(final String token) {
171-
final Metadata extraHeaders = new Metadata();
172-
extraHeaders.put(AUTHORIZATION_HEADER, BEARER_AUTH_PREFIX + token);
173-
return new StaticSecurityHeaderCallCredentials(extraHeaders);
173+
return authorizationHeader(BEARER_AUTH_PREFIX + token);
174174
}
175175

176176
/**
177-
* Creates a new call credential with the given username and password for basic auth.
177+
* Creates new call credentials with the given username and password for basic auth.
178178
*
179179
* <p>
180180
* <b>Note:</b> This method uses experimental grpc-java-API features.
@@ -183,11 +183,12 @@ public static CallCredentials bearerAuth(final String token) {
183183
* @param username The username to use.
184184
* @param password The password to use.
185185
* @return The newly created basic auth credentials.
186+
* @see #BASIC_AUTH_PREFIX
187+
* @see #encodeBasicAuth(String, String)
188+
* @see #authorizationHeader(String)
186189
*/
187190
public static CallCredentials basicAuth(final String username, final String password) {
188-
final Metadata extraHeaders = new Metadata();
189-
extraHeaders.put(AUTHORIZATION_HEADER, encodeBasicAuth(username, password));
190-
return new StaticSecurityHeaderCallCredentials(extraHeaders);
191+
return authorizationHeader(encodeBasicAuth(username, password));
191192
}
192193

193194
/**
@@ -197,6 +198,7 @@ public static CallCredentials basicAuth(final String username, final String pass
197198
* @param username The username to use.
198199
* @param password The password to use.
199200
* @return The encoded basic auth header value.
201+
* @see #BASIC_AUTH_PREFIX
200202
*/
201203
public static String encodeBasicAuth(final String username, final String password) {
202204
requireNonNull(username, "username");
@@ -211,6 +213,36 @@ public static String encodeBasicAuth(final String username, final String passwor
211213
return BASIC_AUTH_PREFIX + new String(encoded, UTF_8);
212214
}
213215

216+
/**
217+
* Creates new call credentials with the given static authorization information.
218+
*
219+
* <p>
220+
* <b>Note:</b> This method uses experimental grpc-java-API features.
221+
* </p>
222+
*
223+
* @param authorization The authorization to use. The authorization usually starts with the scheme such as as
224+
* {@code "Basic "} or {@code "Bearer "} followed by the actual authentication information.
225+
* @return The newly created call credentials.
226+
* @see #AUTHORIZATION_HEADER
227+
* @see #authorizationHeaders(Metadata)
228+
*/
229+
public static CallCredentials authorizationHeader(final String authorization) {
230+
requireNonNull(authorization);
231+
final Metadata extraHeaders = new Metadata();
232+
extraHeaders.put(AUTHORIZATION_HEADER, authorization);
233+
return authorizationHeaders(extraHeaders);
234+
}
235+
236+
/**
237+
* Creates new call credentials with the given static authorization headers.
238+
*
239+
* @param authorizationHeaders The authorization headers to use.
240+
* @return The newly created call credentials.
241+
*/
242+
public static CallCredentials authorizationHeaders(final Metadata authorizationHeaders) {
243+
return new StaticSecurityHeaderCallCredentials(requireNonNull(authorizationHeaders));
244+
}
245+
214246
/**
215247
* The static security header {@link CallCredentials} simply add a set of predefined headers to the call. Their
216248
* specific meaning is server specific. This implementation can be used, for example, for BasicAuth.

0 commit comments

Comments
 (0)