Skip to content

Commit c783a6b

Browse files
#352 enable api key override for Authorization header (#355) (#366)
Co-authored-by: Laurent Perez <[email protected]> (cherry picked from commit 432a8a0) Co-authored-by: Laurent Perez <[email protected]>
1 parent 6a5f3e3 commit c783a6b

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

integration-tests/security/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ quarkus.openapi-generator.codegen.spec.open_weather_yaml.base-package=org.acme.o
88
quarkus.openapi-generator.open_weather_yaml.auth.app_id.api-key=12345
99

1010
quarkus.openapi-generator.codegen.spec.fooopenapi_json.base-package=org.acme.openapi.foo
11-
quarkus.openapi-generator.fooopenapi_json.auth.JWT.api-key=fooapikey
11+
quarkus.openapi-generator.fooopenapi_json.auth.JWT.api-key=staticapikey
1212

1313
# Authentication properties
1414
quarkus.openapi-generator.codegen.spec.open_weather_no_security_yaml.base-package=org.acme.openapi.weathernosec

integration-tests/security/src/test/java/io/quarkiverse/openapi/generator/it/security/AuthorizationHeaderApiKeyCanFilterWithoutDuplicateAuthorizationTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,35 @@ public class AuthorizationHeaderApiKeyCanFilterWithoutDuplicateAuthorizationTest
3535

3636
@Test
3737
public void testNoMultipleAuthorizationHeadersAreSent() {
38-
List<FooDTO> foos = fooResourceApi.getFoosUsingGET("not the fooapikey",
38+
List<FooDTO> foos = fooResourceApi.getFoosUsingGET("dynamicapikey",
3939
123465L);
4040
assertNotNull(foos);
4141

4242
RequestPatternBuilder builder = getRequestedFor(
4343
urlEqualTo("/api/foo/v2.0/foo?something=123465"))
44-
.withHeader("Authorization", equalTo("fooapikey"));
44+
.withHeader("Authorization", equalTo("dynamicapikey"));
4545
List<LoggedRequest> requestsWithAuthHeader = fooServer.findAll(builder);
46-
assertEquals(1, requestsWithAuthHeader.size(), "more than one request");
46+
assertEquals(1, requestsWithAuthHeader.size(), "unexpected Authorization header in request");
4747

4848
LoggedRequest loggedRequest = requestsWithAuthHeader.get(0);
4949
HttpHeaders httpHeaders = loggedRequest.getHeaders();
5050
long authHeaderCount = httpHeaders.all().stream().filter(
5151
httpHeader -> httpHeader.keyEquals("Authorization")).count();
5252
assertEquals(1, authHeaderCount, "multiple Authorization headers found");
5353
}
54+
55+
@Test
56+
public void testStaticApiKeyAsPrecedenceOverDynamicApiKey() {
57+
List<FooDTO> foos = fooResourceApi.getFoosUsingGET("",
58+
123465L);
59+
assertNotNull(foos);
60+
61+
RequestPatternBuilder builder = getRequestedFor(
62+
urlEqualTo("/api/foo/v2.0/foo?something=123465"))
63+
.withHeader("Authorization", equalTo("staticapikey"));
64+
List<LoggedRequest> requestsWithAuthHeader = fooServer.findAll(builder);
65+
assertEquals(1, requestsWithAuthHeader.size(),
66+
"did not use staticapikey from application.properties for Authorization header");
67+
68+
}
5469
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public void filter(ClientRequestContext requestContext) throws IOException {
4646
requestContext.getCookies().put(apiKeyName, new Cookie(apiKeyName, getApiKey()));
4747
break;
4848
case header:
49-
requestContext.getHeaders().putSingle(apiKeyName, getApiKey());
49+
if (requestContext.getHeaderString("Authorization") != null
50+
&& !requestContext.getHeaderString("Authorization").isEmpty()) {
51+
requestContext.getHeaders().putSingle(apiKeyName, requestContext.getHeaderString("Authorization"));
52+
} else
53+
requestContext.getHeaders().putSingle(apiKeyName, getApiKey());
5054
break;
5155
}
5256
}

0 commit comments

Comments
 (0)