Skip to content

Commit 20e31c2

Browse files
Use CredentialProvider in OAUTH2 provider
Signed-off-by: gabriel-farache <[email protected]>
1 parent f468379 commit 20e31c2

File tree

9 files changed

+76
-24
lines changed

9 files changed

+76
-24
lines changed

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/GeneratorProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ void produceOauthAuthentication(CombinedIndexBuildItem beanArchiveBuildItem,
162162
.annotation(OpenApiSpec.class)
163163
.addValue("openApiSpecId", openApiSpecId)
164164
.done()
165+
.addInjectionPoint(ClassType.create(DotName.createSimple(CredentialsProvider.class)))
165166
.addInjectionPoint(ClassType.create(OAuth2AuthenticationProvider.OidcClientRequestFilterDelegate.class),
166167
AnnotationInstance.builder(OidcClient.class).add("name", sanitizeAuthName(name)).build())
167168
.createWith(oidcRecorder.recordOauthAuthProvider(sanitizeAuthName(name), openApiSpecId, operations))

client/oidc/src/main/java/io/quarkiverse/openapi/generator/oidc/ClassicOidcClientRequestFilterDelegate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import jakarta.ws.rs.Priorities;
88
import jakarta.ws.rs.client.ClientRequestContext;
99
import jakarta.ws.rs.client.ClientRequestFilter;
10+
import jakarta.ws.rs.core.HttpHeaders;
1011

1112
import org.jboss.logging.Logger;
1213

@@ -41,7 +42,7 @@ protected java.util.Optional<String> clientId() {
4142
public void filter(ClientRequestContext requestContext) throws IOException {
4243
try {
4344
String accessToken = this.getAccessToken();
44-
requestContext.getHeaders().add("Authorization", "Bearer " + accessToken);
45+
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
4546
} catch (DisabledOidcClientException ex) {
4647
LOG.debug("Client is disabled, acquiring and propagating the token is not necessary");
4748
} catch (RuntimeException ex) {

client/oidc/src/main/java/io/quarkiverse/openapi/generator/oidc/OidcAuthenticationRecorder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.quarkiverse.openapi.generator.OidcClient;
77
import io.quarkiverse.openapi.generator.oidc.providers.OAuth2AuthenticationProvider;
88
import io.quarkiverse.openapi.generator.providers.AuthProvider;
9+
import io.quarkiverse.openapi.generator.providers.CredentialsProvider;
910
import io.quarkiverse.openapi.generator.providers.OperationAuthInfo;
1011
import io.quarkus.arc.SyntheticCreationalContext;
1112
import io.quarkus.runtime.annotations.Recorder;
@@ -20,6 +21,6 @@ public Function<SyntheticCreationalContext<AuthProvider>, AuthProvider> recordOa
2021
return context -> new OAuth2AuthenticationProvider(name, openApiSpecId,
2122
context.getInjectedReference(OAuth2AuthenticationProvider.OidcClientRequestFilterDelegate.class,
2223
new OidcClient.Literal(name)),
23-
operations);
24+
operations, context.getInjectedReference(CredentialsProvider.class));
2425
}
2526
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
import org.slf4j.LoggerFactory;
1313

1414
import io.quarkiverse.openapi.generator.providers.AbstractAuthProvider;
15+
import io.quarkiverse.openapi.generator.providers.AuthUtils;
1516
import io.quarkiverse.openapi.generator.providers.ConfigCredentialsProvider;
17+
import io.quarkiverse.openapi.generator.providers.CredentialsProvider;
1618
import io.quarkiverse.openapi.generator.providers.OperationAuthInfo;
17-
import io.quarkus.oidc.common.runtime.OidcConstants;
1819

1920
public class OAuth2AuthenticationProvider extends AbstractAuthProvider {
2021

@@ -24,19 +25,32 @@ public class OAuth2AuthenticationProvider extends AbstractAuthProvider {
2425

2526
public OAuth2AuthenticationProvider(String name,
2627
String openApiSpecId, OidcClientRequestFilterDelegate delegate, List<OperationAuthInfo> operations) {
27-
super(name, openApiSpecId, operations, new ConfigCredentialsProvider());
28+
this(name, openApiSpecId, delegate, operations, new ConfigCredentialsProvider());
29+
}
30+
31+
public OAuth2AuthenticationProvider(String name,
32+
String openApiSpecId, OidcClientRequestFilterDelegate delegate, List<OperationAuthInfo> operations,
33+
CredentialsProvider credentialsProvider) {
34+
super(name, openApiSpecId, operations, credentialsProvider);
2835
this.delegate = delegate;
2936
validateConfig();
3037
}
3138

3239
@Override
3340
public void filter(ClientRequestContext requestContext) throws IOException {
34-
if (isTokenPropagation()) {
35-
String bearerToken = getTokenForPropagation(requestContext.getHeaders());
36-
bearerToken = sanitizeBearerToken(bearerToken);
37-
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, OidcConstants.BEARER_SCHEME + " " + bearerToken);
41+
String bearerToken;
42+
43+
if (this.isTokenPropagation()) {
44+
bearerToken = this.getTokenForPropagation(requestContext.getHeaders());
3845
} else {
3946
delegate.filter(requestContext);
47+
bearerToken = this.getCredentialsProvider().getOauth2BearerToken(requestContext);
48+
}
49+
50+
if (bearerToken != null && !bearerToken.isBlank()) {
51+
requestContext.getHeaders().remove(HttpHeaders.AUTHORIZATION);
52+
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION,
53+
AuthUtils.authTokenOrBearer("Bearer", AbstractAuthProvider.sanitizeBearerToken(bearerToken)));
4054
}
4155
}
4256

client/oidc/src/test/java/io/quarkiverse/openapi/generator/oidc/OAuth2AuthenticationProviderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import java.util.Optional;
99
import java.util.stream.Stream;
1010

11+
import jakarta.ws.rs.client.ClientRequestContext;
12+
import jakarta.ws.rs.core.HttpHeaders;
13+
import jakarta.ws.rs.core.MultivaluedHashMap;
14+
import jakarta.ws.rs.core.MultivaluedMap;
15+
1116
import org.assertj.core.api.Assertions;
1217
import org.eclipse.microprofile.config.Config;
1318
import org.eclipse.microprofile.config.ConfigProvider;
@@ -26,11 +31,6 @@
2631
import io.quarkiverse.openapi.generator.oidc.providers.OAuth2AuthenticationProvider;
2732
import io.quarkus.oidc.client.Tokens;
2833

29-
import jakarta.ws.rs.client.ClientRequestContext;
30-
import jakarta.ws.rs.core.HttpHeaders;
31-
import jakarta.ws.rs.core.MultivaluedHashMap;
32-
import jakarta.ws.rs.core.MultivaluedMap;
33-
3434
@ExtendWith(MockitoExtension.class)
3535
public class OAuth2AuthenticationProviderTest {
3636
private static final String OPEN_API_FILE_SPEC_ID = "open_api_file_spec_id_json";

client/oidc/src/test/java/io/quarkiverse/openapi/generator/oidc/ReactiveOAuth2AuthenticationProviderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import java.util.Optional;
99
import java.util.stream.Stream;
1010

11+
import jakarta.ws.rs.client.ClientRequestContext;
12+
import jakarta.ws.rs.core.HttpHeaders;
13+
import jakarta.ws.rs.core.MultivaluedHashMap;
14+
import jakarta.ws.rs.core.MultivaluedMap;
15+
1116
import org.assertj.core.api.Assertions;
1217
import org.eclipse.microprofile.config.Config;
1318
import org.eclipse.microprofile.config.ConfigProvider;
@@ -28,11 +33,6 @@
2833
import io.quarkus.oidc.client.Tokens;
2934
import io.smallrye.mutiny.Uni;
3035

31-
import jakarta.ws.rs.client.ClientRequestContext;
32-
import jakarta.ws.rs.core.HttpHeaders;
33-
import jakarta.ws.rs.core.MultivaluedHashMap;
34-
import jakarta.ws.rs.core.MultivaluedMap;
35-
3636
@ExtendWith(MockitoExtension.class)
3737
public class ReactiveOAuth2AuthenticationProviderTest {
3838
private static final String OPEN_API_FILE_SPEC_ID = "open_api_file_spec_id_json";

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,33 @@ public String getName() {
6060
}
6161

6262
public boolean isTokenPropagation() {
63-
return ConfigProvider.getConfig()
64-
.getOptionalValue(getCanonicalAuthConfigPropertyName(AuthConfig.TOKEN_PROPAGATION), Boolean.class)
65-
.orElse(false);
63+
return isTokenPropagation(getOpenApiSpecId(), getName());
6664
}
6765

68-
public String getTokenForPropagation(MultivaluedMap<String, Object> httpHeaders) {
69-
String headerName = getHeaderName() != null ? getHeaderName() : HttpHeaders.AUTHORIZATION;
70-
String propagatedHeaderName = propagationHeaderName(getOpenApiSpecId(), getName(), headerName);
66+
public static String getTokenForPropagation(MultivaluedMap<String, Object> httpHeaders, String openApiSpecId,
67+
String authName) {
68+
String headerName = getHeaderName(openApiSpecId, authName) != null ? getHeaderName(openApiSpecId, authName)
69+
: HttpHeaders.AUTHORIZATION;
70+
String propagatedHeaderName = propagationHeaderName(openApiSpecId, authName, headerName);
7171
return Objects.toString(httpHeaders.getFirst(propagatedHeaderName));
7272
}
7373

74+
public String getTokenForPropagation(MultivaluedMap<String, Object> httpHeaders) {
75+
return getTokenForPropagation(httpHeaders, getOpenApiSpecId(), getName());
76+
}
77+
7478
public String getHeaderName() {
7579
return ConfigProvider.getConfig()
7680
.getOptionalValue(getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME), String.class).orElse(null);
7781
}
7882

83+
public static String getHeaderName(String openApiSpecId, String authName) {
84+
return ConfigProvider.getConfig()
85+
.getOptionalValue(getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME, openApiSpecId, authName),
86+
String.class)
87+
.orElse(null);
88+
}
89+
7990
@Override
8091
public List<OperationAuthInfo> operationsToFilter() {
8192
return applyToOperations;
@@ -88,4 +99,15 @@ public final String getCanonicalAuthConfigPropertyName(String authPropertyName)
8899
public static String getCanonicalAuthConfigPropertyName(String authPropertyName, String openApiSpecId, String authName) {
89100
return String.format(CANONICAL_AUTH_CONFIG_PROPERTY_NAME, openApiSpecId, authName, authPropertyName);
90101
}
102+
103+
public static boolean isTokenPropagation(String openApiSpecId, String authName) {
104+
return ConfigProvider.getConfig()
105+
.getOptionalValue(getCanonicalAuthConfigPropertyName(AuthConfig.TOKEN_PROPAGATION, openApiSpecId, authName),
106+
Boolean.class)
107+
.orElse(false);
108+
}
109+
110+
public CredentialsProvider getCredentialsProvider() {
111+
return credentialsProvider;
112+
}
91113
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.enterprise.context.Dependent;
55
import jakarta.enterprise.inject.Alternative;
66
import jakarta.ws.rs.client.ClientRequestContext;
7+
import jakarta.ws.rs.core.HttpHeaders;
78

89
import org.eclipse.microprofile.config.ConfigProvider;
910
import org.slf4j.Logger;
@@ -63,4 +64,8 @@ public String getBearerToken(ClientRequestContext requestContext, String openApi
6364
.orElse("");
6465
}
6566

67+
@Override
68+
public String getOauth2BearerToken(ClientRequestContext requestContext) {
69+
return requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
70+
}
6671
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public interface CredentialsProvider {
4444
* @return the Bearer Token to use when filtering the request
4545
*/
4646
String getBearerToken(ClientRequestContext requestContext, String openApiSpecId, String authName);
47+
48+
/**
49+
* Gets the OAuth2 Bearer Token given the OpenAPI definition and security schema
50+
*
51+
* @param requestContext The current request context in which set the authorization header token
52+
* @return the Bearer Token to use when filtering the request
53+
*/
54+
String getOauth2BearerToken(ClientRequestContext requestContext);
4755
}

0 commit comments

Comments
 (0)