Skip to content

Commit 10aa5ab

Browse files
gabriel-farachericardozanini
authored andcommitted
Fix #1191 - refactor CredentialsProvider return type (#1192)
* Fix #1191 - refactor CredentialsProvider return type * Apply feedback Signed-off-by: gabriel-farache <[email protected]> * Better log message for oauth2 provider Signed-off-by: gabriel-farache <[email protected]> --------- Signed-off-by: gabriel-farache <[email protected]>
1 parent c55c7c3 commit 10aa5ab

File tree

8 files changed

+76
-52
lines changed

8 files changed

+76
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkiverse.openapi.generator.it.auth.provider;
22

3+
import java.util.Optional;
4+
35
import jakarta.annotation.Priority;
46
import jakarta.enterprise.context.Dependent;
57
import jakarta.enterprise.inject.Alternative;
@@ -11,18 +13,18 @@
1113
@Dependent
1214
@Alternative
1315
@Specializes
14-
@Priority(200)
16+
@Priority(201)
1517
public class CustomCredentialsProvider extends ConfigCredentialsProvider {
1618
public CustomCredentialsProvider() {
1719
}
1820

1921
@Override
20-
public String getBearerToken(CredentialsContext input) {
21-
return super.getBearerToken(input) + "_TEST";
22+
public Optional<String> getBearerToken(CredentialsContext input) {
23+
return Optional.of("BEARER_TOKEN_TEST");
2224
}
2325

2426
@Override
25-
public String getOauth2BearerToken(CredentialsContext input) {
26-
return super.getOauth2BearerToken(input) + "_TEST";
27+
public Optional<String> getOauth2BearerToken(CredentialsContext input) {
28+
return Optional.of("KEYCLOAK_ACCESS_TOKEN_TEST");
2729
}
2830
}

client/integration-tests/override-credential-provider/src/main/java/io/quarkiverse/openapi/generator/it/creds/CustomCredentialsProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkiverse.openapi.generator.it.creds;
22

3+
import java.util.Optional;
4+
35
import jakarta.annotation.Priority;
46
import jakarta.enterprise.context.Dependent;
57
import jakarta.enterprise.inject.Alternative;
@@ -19,8 +21,8 @@ public class CustomCredentialsProvider extends ConfigCredentialsProvider {
1921
public static String TOKEN = "FIXED_TEST_TOKEN";
2022

2123
@Override
22-
public String getBearerToken(CredentialsContext input) {
24+
public Optional<String> getBearerToken(CredentialsContext input) {
2325
LOGGER.info("========> getBearerToken from CustomCredentialsProvider");
24-
return TOKEN;
26+
return Optional.of(TOKEN);
2527
}
26-
}
28+
}

client/oidc/src/main/java/io/quarkiverse/openapi/generator/oidc/providers/OAuth2AuthenticationProvider.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
import java.io.IOException;
66
import java.util.List;
7+
import java.util.Optional;
78

89
import jakarta.ws.rs.client.ClientRequestContext;
10+
import jakarta.ws.rs.core.HttpHeaders;
911

1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
@@ -32,27 +34,38 @@ public OAuth2AuthenticationProvider(String name,
3234

3335
@Override
3436
public void filter(ClientRequestContext requestContext) throws IOException {
35-
String bearerToken;
37+
String bearerToken = "";
3638

3739
if (this.isTokenPropagation()) {
3840
bearerToken = this.getTokenForPropagation(requestContext.getHeaders());
41+
if (isEmptyOrBlank(bearerToken)) {
42+
LOGGER.debug(
43+
"Token propagation for OAUTH2 is enabled but the configured propagation header defined by {} is not present",
44+
getHeaderForPropagation(getOpenApiSpecId(), getName()));
45+
}
3946
} else {
40-
delegate.filter(requestContext);
41-
bearerToken = this.getCredentialsProvider().getOauth2BearerToken(CredentialsContext.builder()
42-
.requestContext(requestContext)
43-
.openApiSpecId(getOpenApiSpecId())
44-
.authName(getName())
45-
.build());
47+
Optional<String> optionalBearerToken = this.getCredentialsProvider()
48+
.getOauth2BearerToken(CredentialsContext.builder()
49+
.requestContext(requestContext)
50+
.openApiSpecId(getOpenApiSpecId())
51+
.authName(getName())
52+
.build());
53+
if (optionalBearerToken.isPresent()) {
54+
bearerToken = optionalBearerToken.get();
55+
if (isEmptyOrBlank(bearerToken)) {
56+
LOGGER.debug("The CredentialProvider implementation returned an empty OAUTH2 bearer");
57+
}
58+
} else {
59+
LOGGER.debug(
60+
"There is no custom CredentialProvider implementation, the {} header will be set using delegate's filter. ",
61+
HttpHeaders.AUTHORIZATION);
62+
delegate.filter(requestContext);
63+
}
4664
}
4765

4866
if (!isEmptyOrBlank(bearerToken)) {
4967
addAuthorizationHeader(requestContext.getHeaders(),
5068
AuthUtils.authTokenOrBearer("Bearer", AbstractAuthProvider.sanitizeBearerToken(bearerToken)));
51-
} else {
52-
LOGGER.debug("No bearer token was found for the oauth2 security scheme: {}." +
53-
" You must verify that a Quarkus OIDC Client with the name: {} is properly configured," +
54-
" or the request header: {} is set when the token propagation is enabled.",
55-
getName(), getName(), getHeaderForPropagation(getOpenApiSpecId(), getName()));
5669
}
5770
}
5871

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import jakarta.ws.rs.core.UriBuilder;
1212

1313
import org.eclipse.microprofile.config.ConfigProvider;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
1416

1517
import io.quarkiverse.openapi.generator.OpenApiGeneratorException;
1618

@@ -23,6 +25,8 @@ public class ApiKeyAuthenticationProvider extends AbstractAuthProvider {
2325
private final ApiKeyIn apiKeyIn;
2426
private final String apiKeyName;
2527

28+
private static final Logger LOGGER = LoggerFactory.getLogger(ApiKeyAuthenticationProvider.class);
29+
2630
public ApiKeyAuthenticationProvider(final String openApiSpecId, final String name, final ApiKeyIn apiKeyIn,
2731
final String apiKeyName, List<OperationAuthInfo> operations, CredentialsProvider credentialsProvider) {
2832
super(name, openApiSpecId, operations, credentialsProvider);
@@ -54,11 +58,20 @@ && isUseAuthorizationHeaderValue()) {
5458
}
5559

5660
private String getApiKey(ClientRequestContext requestContext) {
57-
return credentialsProvider.getApiKey(CredentialsContext.builder()
61+
final String key = credentialsProvider.getApiKey(CredentialsContext.builder()
5862
.requestContext(requestContext)
5963
.openApiSpecId(getOpenApiSpecId())
6064
.authName(getName())
61-
.build());
65+
.build()).orElse("");
66+
67+
if (key.isEmpty()) {
68+
LOGGER.warn("configured {} property (see application.properties) is empty. hint: configure it.",
69+
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(ConfigCredentialsProvider.API_KEY,
70+
getOpenApiSpecId(),
71+
getName()));
72+
}
73+
74+
return key;
6275
}
6376

6477
private boolean isUseAuthorizationHeaderValue() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ private String getUsername(ClientRequestContext requestContext) {
2929
.requestContext(requestContext)
3030
.openApiSpecId(getOpenApiSpecId())
3131
.authName(getName())
32-
.build());
32+
.build()).orElse("");
3333
}
3434

3535
private String getPassword(ClientRequestContext requestContext) {
3636
return credentialsProvider.getBasicPassword(CredentialsContext.builder()
3737
.requestContext(requestContext)
3838
.openApiSpecId(getOpenApiSpecId())
3939
.authName(getName())
40-
.build());
40+
.build()).orElse("");
4141
}
4242

4343
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ private String getBearerToken(ClientRequestContext requestContext) {
5151
.requestContext(requestContext)
5252
.openApiSpecId(getOpenApiSpecId())
5353
.authName(getName())
54-
.build());
54+
.build()).orElse("");
5555
}
5656
}
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.quarkiverse.openapi.generator.providers;
22

3+
import java.util.Optional;
4+
35
import jakarta.annotation.Priority;
46
import jakarta.enterprise.context.Dependent;
57
import jakarta.enterprise.inject.Alternative;
6-
import jakarta.ws.rs.core.HttpHeaders;
78

89
import org.eclipse.microprofile.config.ConfigProvider;
910
import org.slf4j.Logger;
@@ -26,53 +27,44 @@ public ConfigCredentialsProvider() {
2627
}
2728

2829
@Override
29-
public String getApiKey(CredentialsContext input) {
30-
final String key = ConfigProvider.getConfig()
30+
public Optional<String> getApiKey(CredentialsContext input) {
31+
return ConfigProvider.getConfig()
3132
.getOptionalValue(
3233
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(API_KEY, input.getOpenApiSpecId(),
3334
input.getAuthName()),
34-
String.class)
35-
.orElse("");
36-
if (key.isEmpty()) {
37-
LOGGER.warn("configured {} property (see application.properties) is empty. hint: configure it.",
38-
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(API_KEY, input.getOpenApiSpecId(),
39-
input.getAuthName()));
40-
}
41-
return key;
35+
String.class);
36+
4237
}
4338

4439
@Override
45-
public String getBasicUsername(CredentialsContext input) {
40+
public Optional<String> getBasicUsername(CredentialsContext input) {
4641
return ConfigProvider.getConfig()
4742
.getOptionalValue(
4843
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(USER_NAME, input.getOpenApiSpecId(),
4944
input.getAuthName()),
50-
String.class)
51-
.orElse("");
45+
String.class);
5246
}
5347

5448
@Override
55-
public String getBasicPassword(CredentialsContext input) {
49+
public Optional<String> getBasicPassword(CredentialsContext input) {
5650
return ConfigProvider.getConfig()
5751
.getOptionalValue(
5852
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(PASSWORD, input.getOpenApiSpecId(),
5953
input.getAuthName()),
60-
String.class)
61-
.orElse("");
54+
String.class);
6255
}
6356

6457
@Override
65-
public String getBearerToken(CredentialsContext input) {
58+
public Optional<String> getBearerToken(CredentialsContext input) {
6659
return ConfigProvider.getConfig()
6760
.getOptionalValue(
6861
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(BEARER_TOKEN, input.getOpenApiSpecId(),
6962
input.getAuthName()),
70-
String.class)
71-
.orElse("");
63+
String.class);
7264
}
7365

7466
@Override
75-
public String getOauth2BearerToken(CredentialsContext input) {
76-
return input.getRequestContext().getHeaderString(HttpHeaders.AUTHORIZATION);
67+
public Optional<String> getOauth2BearerToken(CredentialsContext input) {
68+
return Optional.empty();
7769
}
7870
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkiverse.openapi.generator.providers;
22

3+
import java.util.Optional;
4+
35
/**
46
* Provider for security credentials. Clients can implement this interface to control how to provide security credentials in
57
* runtime.
@@ -13,37 +15,37 @@ public interface CredentialsProvider {
1315
* @param input the input data available to the method
1416
* @return the API Key to use when filtering the request
1517
*/
16-
String getApiKey(CredentialsContext input);
18+
Optional<String> getApiKey(CredentialsContext input);
1719

1820
/**
1921
* Gets the username given the OpenAPI definition and security schema
2022
*
2123
* @param input the input data available to the method
2224
* @return the username to use when filtering the request
2325
*/
24-
String getBasicUsername(CredentialsContext input);
26+
Optional<String> getBasicUsername(CredentialsContext input);
2527

2628
/**
2729
* Gets the password given the OpenAPI definition and security schema
2830
*
2931
* @param input the input data available to the method
3032
* @return the password to use when filtering the request
3133
*/
32-
String getBasicPassword(CredentialsContext input);
34+
Optional<String> getBasicPassword(CredentialsContext input);
3335

3436
/**
3537
* Gets the Bearer Token given the OpenAPI definition and security schema
3638
*
3739
* @param input the input data available to the method
3840
* @return the Bearer Token to use when filtering the request
3941
*/
40-
String getBearerToken(CredentialsContext input);
42+
Optional<String> getBearerToken(CredentialsContext input);
4143

4244
/**
4345
* Gets the OAuth2 Bearer Token given the OpenAPI definition and security schema
4446
*
4547
* @param input the input data available to the method
4648
* @return the Bearer Token to use when filtering the request
4749
*/
48-
String getOauth2BearerToken(CredentialsContext input);
50+
Optional<String> getOauth2BearerToken(CredentialsContext input);
4951
}

0 commit comments

Comments
 (0)