Skip to content

Commit 8563e87

Browse files
[quarkus2] Issue 437: Configurable API key header with Authorization header value (#516)
1 parent cfbd03d commit 8563e87

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ Similarly to bearer token, the API Key Authentication also has the token entry k
290290

291291
The API Key scheme has an additional property that requires where to add the API key in the request token: header, cookie or query. The inner provider takes care of that for you.
292292

293+
If an `Authorization` header is present, then the value of this header is used as API Key. This behaviour can be changed by setting the property `use-authorization-header-value` to `false`.
294+
293295
### OAuth2 Authentication
294296

295297
The extension will generate a `ClientRequestFilter` capable to add OAuth2 authentication capabilities to the OpenAPI operations that require it. This means that you can use

runtime/src/main/java/io/quarkiverse/openapi/generator/providers/ApiKeyAuthenticationProvider.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class ApiKeyAuthenticationProvider extends AbstractAuthProvider {
2222
private static final Logger LOGGER = LoggerFactory.getLogger(ApiKeyAuthenticationProvider.class);
2323

2424
static final String API_KEY = "api-key";
25+
static final String USE_AUTHORIZATION_HEADER_VALUE = "use-authorization-header-value";
2526

2627
private final ApiKeyIn apiKeyIn;
2728
private final String apiKeyName;
@@ -47,7 +48,8 @@ public void filter(ClientRequestContext requestContext) throws IOException {
4748
break;
4849
case header:
4950
if (requestContext.getHeaderString("Authorization") != null
50-
&& !requestContext.getHeaderString("Authorization").isEmpty()) {
51+
&& !requestContext.getHeaderString("Authorization").isEmpty()
52+
&& isUseAuthorizationHeaderValue()) {
5153
requestContext.getHeaders().putSingle(apiKeyName, requestContext.getHeaderString("Authorization"));
5254
} else
5355
requestContext.getHeaders().putSingle(apiKeyName, getApiKey());
@@ -63,6 +65,11 @@ private String getApiKey() {
6365
return key;
6466
}
6567

68+
private boolean isUseAuthorizationHeaderValue() {
69+
final String value = getAuthConfigParam(USE_AUTHORIZATION_HEADER_VALUE, "true");
70+
return "true".equals(value);
71+
}
72+
6673
private void validateConfig() {
6774
if (isTokenPropagation()) {
6875
throw new OpenApiGeneratorException(

runtime/src/test/java/io/quarkiverse/openapi/generator/providers/ApiKeyAuthenticationProviderTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ApiKeyAuthenticationProviderTest extends AbstractAuthenticationProviderTes
2323

2424
private static final String API_KEY_NAME = "API_KEY_NAME";
2525
private static final String API_KEY_VALUE = "API_KEY_VALUE";
26+
private static final String API_KEY_AUTH_HEADER_VALUE = "API_KEY_AUTH_HEADER_VALUE";
2627

2728
private static final URI INVOKED_URI = URI.create("https://example.com/my-service");
2829

@@ -42,6 +43,31 @@ protected ApiKeyAuthenticationProvider createProvider(String openApiSpecId, Stri
4243
openApiGeneratorConfig);
4344
}
4445

46+
@Test
47+
void filterHeaderFromAuthorizationHeaderDefaultCase() throws IOException {
48+
doReturn(API_KEY_AUTH_HEADER_VALUE).when(requestContext).getHeaderString("Authorization");
49+
provider.filter(requestContext);
50+
assertHeader(headers, API_KEY_NAME, API_KEY_AUTH_HEADER_VALUE);
51+
}
52+
53+
@Test
54+
void filterHeaderFromAuthorizationHeaderCase() throws IOException {
55+
authConfig.authConfigParams.put(ApiKeyAuthenticationProvider.USE_AUTHORIZATION_HEADER_VALUE, "true");
56+
doReturn(API_KEY_AUTH_HEADER_VALUE).when(requestContext).getHeaderString("Authorization");
57+
provider.filter(requestContext);
58+
assertHeader(headers, API_KEY_NAME, API_KEY_AUTH_HEADER_VALUE);
59+
authConfig.authConfigParams.remove(ApiKeyAuthenticationProvider.USE_AUTHORIZATION_HEADER_VALUE);
60+
}
61+
62+
@Test
63+
void filterHeaderNotFromAuthorizationHeaderCase() throws IOException {
64+
authConfig.authConfigParams.put(ApiKeyAuthenticationProvider.USE_AUTHORIZATION_HEADER_VALUE, "false");
65+
doReturn(API_KEY_AUTH_HEADER_VALUE).when(requestContext).getHeaderString("Authorization");
66+
provider.filter(requestContext);
67+
assertHeader(headers, API_KEY_NAME, API_KEY_VALUE);
68+
authConfig.authConfigParams.remove(ApiKeyAuthenticationProvider.USE_AUTHORIZATION_HEADER_VALUE);
69+
}
70+
4571
@Test
4672
void filterHeaderCase() throws IOException {
4773
provider.filter(requestContext);

0 commit comments

Comments
 (0)