Skip to content

Commit 0602297

Browse files
Use CredentialProvider in OAUTH2 provider
Signed-off-by: gabriel-farache <[email protected]>
1 parent 81f08e2 commit 0602297

File tree

4 files changed

+69
-24
lines changed

4 files changed

+69
-24
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
import java.io.IOException;
66
import java.util.List;
77

8-
import jakarta.ws.rs.client.ClientRequestContext;
9-
import jakarta.ws.rs.core.HttpHeaders;
10-
118
import org.slf4j.Logger;
129
import org.slf4j.LoggerFactory;
1310

1411
import io.quarkiverse.openapi.generator.providers.AbstractAuthProvider;
1512
import io.quarkiverse.openapi.generator.providers.ConfigCredentialsProvider;
1613
import io.quarkiverse.openapi.generator.providers.OperationAuthInfo;
17-
import io.quarkus.oidc.common.runtime.OidcConstants;
14+
import jakarta.ws.rs.client.ClientRequestContext;
1815

1916
public class OAuth2AuthenticationProvider extends AbstractAuthProvider {
2017

@@ -31,13 +28,7 @@ public OAuth2AuthenticationProvider(String name,
3128

3229
@Override
3330
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);
38-
} else {
39-
delegate.filter(requestContext);
40-
}
31+
getCredentialsProvider().setOauth2BearerToken(requestContext, getOpenApiSpecId(), getName(), delegate::filter);
4132
}
4233

4334
private void validateConfig() {

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
import java.util.List;
88
import java.util.Objects;
99

10-
import jakarta.ws.rs.core.HttpHeaders;
11-
import jakarta.ws.rs.core.MultivaluedMap;
12-
1310
import org.eclipse.microprofile.config.ConfigProvider;
1411

1512
import io.quarkiverse.openapi.generator.AuthConfig;
13+
import jakarta.ws.rs.core.HttpHeaders;
14+
import jakarta.ws.rs.core.MultivaluedMap;
1615

1716
public abstract class AbstractAuthProvider implements AuthProvider {
1817

@@ -60,22 +59,33 @@ public String getName() {
6059
}
6160

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

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

73+
public String getTokenForPropagation(MultivaluedMap<String, Object> httpHeaders) {
74+
return getTokenForPropagation(httpHeaders, getOpenApiSpecId(), getName());
75+
}
76+
7477
public String getHeaderName() {
7578
return ConfigProvider.getConfig()
7679
.getOptionalValue(getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME), String.class).orElse(null);
7780
}
7881

82+
public static String getHeaderName(String openApiSpecId, String authName) {
83+
return ConfigProvider.getConfig()
84+
.getOptionalValue(getCanonicalAuthConfigPropertyName(AuthConfig.HEADER_NAME, openApiSpecId, authName),
85+
String.class)
86+
.orElse(null);
87+
}
88+
7989
@Override
8090
public List<OperationAuthInfo> operationsToFilter() {
8191
return applyToOperations;
@@ -88,4 +98,15 @@ public final String getCanonicalAuthConfigPropertyName(String authPropertyName)
8898
public static String getCanonicalAuthConfigPropertyName(String authPropertyName, String openApiSpecId, String authName) {
8999
return String.format(CANONICAL_AUTH_CONFIG_PROPERTY_NAME, openApiSpecId, authName, authPropertyName);
90100
}
101+
102+
public static boolean isTokenPropagation(String openApiSpecId, String authName) {
103+
return ConfigProvider.getConfig()
104+
.getOptionalValue(getCanonicalAuthConfigPropertyName(AuthConfig.TOKEN_PROPAGATION, openApiSpecId, authName),
105+
Boolean.class)
106+
.orElse(false);
107+
}
108+
109+
public CredentialsProvider getCredentialsProvider() {
110+
return credentialsProvider;
111+
}
91112
}

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

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

3-
import jakarta.annotation.Priority;
4-
import jakarta.enterprise.context.Dependent;
5-
import jakarta.enterprise.inject.Alternative;
6-
import jakarta.ws.rs.client.ClientRequestContext;
3+
import java.io.IOException;
74

85
import org.eclipse.microprofile.config.ConfigProvider;
96
import org.slf4j.Logger;
107
import org.slf4j.LoggerFactory;
118

9+
import io.quarkus.oidc.common.runtime.OidcConstants;
10+
import jakarta.annotation.Priority;
11+
import jakarta.enterprise.context.Dependent;
12+
import jakarta.enterprise.inject.Alternative;
13+
import jakarta.ws.rs.client.ClientRequestContext;
14+
import jakarta.ws.rs.core.HttpHeaders;
15+
1216
@Dependent
1317
@Alternative
1418
@Priority(100)
@@ -63,4 +67,22 @@ public String getBearerToken(ClientRequestContext requestContext, String openApi
6367
.orElse("");
6468
}
6569

70+
@Override
71+
public void setOauth2BearerToken(ClientRequestContext requestContext, String openApiSpecId, String authName,
72+
ThrowingConsumer<ClientRequestContext, IOException> filter) throws IOException {
73+
if (AbstractAuthProvider.isTokenPropagation(openApiSpecId, authName)) {
74+
String bearerToken = AbstractAuthProvider.getTokenForPropagation(requestContext.getHeaders(), openApiSpecId,
75+
authName);
76+
requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION,
77+
OidcConstants.BEARER_SCHEME + " " + AbstractAuthProvider.sanitizeBearerToken(bearerToken));
78+
} else {
79+
filter.accept(requestContext);
80+
}
81+
}
82+
83+
@FunctionalInterface
84+
public interface ThrowingConsumer<T, E extends Exception> {
85+
void accept(T t) throws E;
86+
}
87+
6688
}

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

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

3+
import java.io.IOException;
4+
35
import jakarta.ws.rs.client.ClientRequestContext;
46

57
/**
@@ -44,4 +46,13 @@ public interface CredentialsProvider {
4446
* @return the Bearer Token to use when filtering the request
4547
*/
4648
String getBearerToken(ClientRequestContext requestContext, String openApiSpecId, String authName);
49+
50+
/**
51+
* Gets the Bearer Token given the OpenAPI definition and security schema
52+
*
53+
* @param openApiSpecId the OpenAPI Spec identification as defined by the OpenAPI Extension
54+
* @param authName The security schema for this Bearer Token definition
55+
*/
56+
void setOauth2BearerToken(ClientRequestContext requestContext, String openApiSpecId, String authName,
57+
ConfigCredentialsProvider.ThrowingConsumer<ClientRequestContext, IOException> filter) throws IOException;
4758
}

0 commit comments

Comments
 (0)