Skip to content

Commit 798c2fb

Browse files
Refactor based on feedback and update tests acordingly
Co-authored-by: Francisco Javier Tirado Sarti <[email protected]>
1 parent e5b6cf1 commit 798c2fb

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

client/runtime/src/main/java/io/quarkiverse/openapi/generator/providers/BasicAuthenticationProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ private String getPassword() {
3737

3838
@Override
3939
public void filter(ClientRequestContext requestContext) throws IOException {
40-
String basicToken;
40+
String basicToken = AuthUtils.basicAuthAccessTokenWithoutPrefix(getUsername(), getPassword());
41+
4142
if (isTokenPropagation()) {
4243
LOGGER.warn("Token propagation enabled for BasicAuthentication");
43-
basicToken = getTokenForPropagation(requestContext.getHeaders());
44-
basicToken = sanitizeBasicToken(basicToken);
45-
} else {
46-
basicToken = AuthUtils.basicAuthAccessTokenWithoutPrefix(getUsername(), getPassword());
44+
basicToken = sanitizeBasicToken(getTokenForPropagation(requestContext.getHeaders()));
4745
}
46+
4847
if (!basicToken.isBlank()) {
4948
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION,
5049
AuthUtils.basicAuthAccessToken(basicToken));

client/runtime/src/main/java/io/quarkiverse/openapi/generator/providers/BearerAuthenticationProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ public BearerAuthenticationProvider(final String openApiSpecId, final String nam
2727

2828
@Override
2929
public void filter(ClientRequestContext requestContext) throws IOException {
30-
String bearerToken;
30+
String bearerToken = getBearerToken();
31+
3132
if (isTokenPropagation()) {
32-
bearerToken = getTokenForPropagation(requestContext.getHeaders());
33-
bearerToken = sanitizeBearerToken(bearerToken);
34-
} else {
35-
bearerToken = getBearerToken();
33+
bearerToken = sanitizeBearerToken(getTokenForPropagation(requestContext.getHeaders()));
3634
}
35+
3736
if (!bearerToken.isBlank()) {
3837
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION,
3938
AuthUtils.authTokenOrBearer(this.scheme, bearerToken));

client/runtime/src/test/java/io/quarkiverse/openapi/generator/providers/BasicOpenApiSpecProviderTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class BasicOpenApiSpecProviderTest extends AbstractOpenApiSpecProviderTest<Basic
2828
private static final String USER = "USER";
2929
private static final String PASSWORD = "PASSWORD";
3030

31+
private static final String USER_PROP = "username";
32+
private static final String PASSWORD_PROP = "password";
33+
3134
private static final String CUSTOM_SCHEMA = "custom_scheme";
3235
private static final String HEADER_NAME = "HEADER_NAME";
3336

@@ -53,14 +56,9 @@ private void filter(String expectedAuthorizationHeader) throws IOException {
5356
@MethodSource("filterWithPropagationTestValues")
5457
void filterWithPropagation(String headerName,
5558
String expectedAuthorizationHeader) throws IOException {
56-
String propagatedHeaderName;
57-
if (headerName == null) {
58-
propagatedHeaderName = propagationHeaderName(OPEN_API_FILE_SPEC_ID, AUTH_SCHEME_NAME,
59-
HttpHeaders.AUTHORIZATION);
60-
} else {
61-
propagatedHeaderName = propagationHeaderName(OPEN_API_FILE_SPEC_ID, AUTH_SCHEME_NAME,
62-
HEADER_NAME);
63-
}
59+
String propagatedHeaderName = headerName == null
60+
? propagationHeaderName(OPEN_API_FILE_SPEC_ID, AUTH_SCHEME_NAME, HttpHeaders.AUTHORIZATION)
61+
: propagationHeaderName(OPEN_API_FILE_SPEC_ID, AUTH_SCHEME_NAME, HEADER_NAME);
6462
try (MockedStatic<ConfigProvider> configProviderMocked = Mockito.mockStatic(ConfigProvider.class)) {
6563
Config mockedConfig = Mockito.mock(Config.class);
6664
configProviderMocked.when(ConfigProvider::getConfig).thenReturn(mockedConfig);
@@ -69,7 +67,10 @@ void filterWithPropagation(String headerName,
6967
Boolean.class)).thenReturn(Optional.of(true));
7068
when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME),
7169
String.class)).thenReturn(Optional.of(headerName == null ? HttpHeaders.AUTHORIZATION : headerName));
72-
70+
when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(USER_PROP),
71+
String.class)).thenReturn(Optional.of(USER));
72+
when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(PASSWORD_PROP),
73+
String.class)).thenReturn(Optional.of(PASSWORD));
7374
headers.putSingle(propagatedHeaderName, PROPAGATED_TOKEN);
7475
filter(expectedAuthorizationHeader);
7576
}

client/runtime/src/test/java/io/quarkiverse/openapi/generator/providers/BearerOpenApiSpecProviderTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class BearerOpenApiSpecProviderTest extends AbstractOpenApiSpecProviderTest<Bear
2828
private static final String PROPAGATED_TOKEN = "PROPAGATED_TOKEN";
2929

3030
private static final String BEARER_SCHEMA = "bearer";
31+
32+
private static final String BEARER_TOKEN = "bearer-token";
3133
private static final String CUSTOM_SCHEMA = "custom_scheme";
3234
private static final String HEADER_NAME = "HEADER_NAME";
3335

@@ -80,6 +82,8 @@ void filterWithPropagation(String headerName,
8082
Boolean.class)).thenReturn(Optional.of(true));
8183
when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME),
8284
String.class)).thenReturn(Optional.of(headerName == null ? HttpHeaders.AUTHORIZATION : headerName));
85+
when(mockedConfig.getOptionalValue(provider.getCanonicalAuthConfigPropertyName(BEARER_TOKEN),
86+
String.class)).thenReturn(Optional.of(INCOMING_TOKEN));
8387

8488
headers.putSingle(propagatedHeaderName, PROPAGATED_TOKEN);
8589
filter(bearerScheme, expectedAuthorizationHeader);

0 commit comments

Comments
 (0)