Skip to content

Commit 93fc29e

Browse files
Add data structure as input parameter for CredentialProvider
Signed-off-by: gabriel-farache <[email protected]>
1 parent fecd6f1 commit 93fc29e

File tree

8 files changed

+114
-55
lines changed

8 files changed

+114
-55
lines changed

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,24 @@
44
import jakarta.enterprise.context.Dependent;
55
import jakarta.enterprise.inject.Alternative;
66
import jakarta.enterprise.inject.Specializes;
7-
import jakarta.ws.rs.client.ClientRequestContext;
8-
import jakarta.ws.rs.core.HttpHeaders;
97

10-
import org.eclipse.microprofile.config.ConfigProvider;
11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
13-
14-
import io.quarkiverse.openapi.generator.providers.AbstractAuthProvider;
158
import io.quarkiverse.openapi.generator.providers.ConfigCredentialsProvider;
169

1710
@Dependent
1811
@Alternative
1912
@Specializes
2013
@Priority(10)
2114
public class CustomCredentialsProvider extends ConfigCredentialsProvider {
22-
static final String BEARER_TOKEN = "bearer-token";
23-
24-
private static final Logger LOGGER = LoggerFactory.getLogger(CustomCredentialsProvider.class);
25-
2615
public CustomCredentialsProvider() {
2716
}
2817

2918
@Override
30-
public String getBearerToken(ClientRequestContext requestContext, String openApiSpecId, String authName) {
31-
return ConfigProvider.getConfig()
32-
.getOptionalValue(
33-
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(BEARER_TOKEN, openApiSpecId, authName),
34-
String.class)
35-
.orElse("") + "_TEST";
19+
public String getBearerToken(DataInput input) {
20+
return super.getBearerToken(input) + "_TEST";
3621
}
3722

3823
@Override
39-
public String getOauth2BearerToken(ClientRequestContext requestContext) {
40-
return requestContext.getHeaderString(HttpHeaders.AUTHORIZATION) + "_TEST";
24+
public String getOauth2BearerToken(DataInput input) {
25+
return super.getOauth2BearerToken(input) + "_TEST";
4126
}
4227
}

client/integration-tests/auth-provider/src/main/resources/application.properties

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
# Authentication properties
2-
org.acme.openapi.security.auth.basic_auth/username=john
3-
org.acme.openapi.security.auth.basic_auth/password=test
4-
customAuth.additionalApiTypeAnnotations=@org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;@org.eclipse.microprofile.rest.client.annotation.RegisterProvider(io.quarkiverse.openapi.generator.it.security.auth.DummyApiKeyAuthenticationProvider.class)
5-
61
# Note: The property value is the name of an existing securityScheme in the spec file
72
quarkus.openapi-generator.codegen.default-security-scheme=app_id
83

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public void filter(ClientRequestContext requestContext) throws IOException {
3838
bearerToken = this.getTokenForPropagation(requestContext.getHeaders());
3939
} else {
4040
delegate.filter(requestContext);
41-
bearerToken = this.getCredentialsProvider().getOauth2BearerToken(requestContext);
41+
bearerToken = this.getCredentialsProvider().getOauth2BearerToken(CredentialsProvider.DataInput.builder()
42+
.requestContext(requestContext)
43+
.openApiSpecId(getOpenApiSpecId())
44+
.authName(getName())
45+
.build());
4246
}
4347

4448
if (bearerToken != null && !bearerToken.isBlank()) {

client/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
@@ -59,7 +59,11 @@ && isUseAuthorizationHeaderValue()) {
5959
}
6060

6161
private String getApiKey(ClientRequestContext requestContext) {
62-
return credentialsProvider.getApiKey(requestContext, getOpenApiSpecId(), getName());
62+
return credentialsProvider.getApiKey(CredentialsProvider.DataInput.builder()
63+
.requestContext(requestContext)
64+
.openApiSpecId(getOpenApiSpecId())
65+
.authName(getName())
66+
.build());
6367
}
6468

6569
private boolean isUseAuthorizationHeaderValue() {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ public BasicAuthenticationProvider(final String openApiSpecId, String name, List
2727
}
2828

2929
private String getUsername(ClientRequestContext requestContext) {
30-
return credentialsProvider.getBasicUsername(requestContext, getOpenApiSpecId(), getName());
30+
return credentialsProvider.getBasicUsername(CredentialsProvider.DataInput.builder()
31+
.requestContext(requestContext)
32+
.openApiSpecId(getOpenApiSpecId())
33+
.authName(getName())
34+
.build());
3135
}
3236

3337
private String getPassword(ClientRequestContext requestContext) {
34-
return credentialsProvider.getBasicPassword(requestContext, getOpenApiSpecId(), getName());
38+
return credentialsProvider.getBasicPassword(CredentialsProvider.DataInput.builder()
39+
.requestContext(requestContext)
40+
.openApiSpecId(getOpenApiSpecId())
41+
.authName(getName())
42+
.build());
3543
}
3644

3745
@Override

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public void filter(ClientRequestContext requestContext) throws IOException {
4040
}
4141

4242
private String getBearerToken(ClientRequestContext requestContext) {
43-
return credentialsProvider.getBearerToken(requestContext, getOpenApiSpecId(), getName());
43+
return credentialsProvider.getBearerToken(CredentialsProvider.DataInput.builder()
44+
.requestContext(requestContext)
45+
.openApiSpecId(getOpenApiSpecId())
46+
.authName(getName())
47+
.build());
4448
}
4549
}

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import jakarta.annotation.Priority;
44
import jakarta.enterprise.context.Dependent;
55
import jakarta.enterprise.inject.Alternative;
6-
import jakarta.ws.rs.client.ClientRequestContext;
76
import jakarta.ws.rs.core.HttpHeaders;
87

98
import org.eclipse.microprofile.config.ConfigProvider;
@@ -27,45 +26,53 @@ public ConfigCredentialsProvider() {
2726
}
2827

2928
@Override
30-
public String getApiKey(ClientRequestContext requestContext, String openApiSpecId, String authName) {
29+
public String getApiKey(DataInput input) {
3130
final String key = ConfigProvider.getConfig()
32-
.getOptionalValue(AbstractAuthProvider.getCanonicalAuthConfigPropertyName(API_KEY, openApiSpecId, authName),
31+
.getOptionalValue(
32+
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(API_KEY, input.getOpenApiSpecId(),
33+
input.getAuthName()),
3334
String.class)
3435
.orElse("");
3536
if (key.isEmpty()) {
3637
LOGGER.warn("configured {} property (see application.properties) is empty. hint: configure it.",
37-
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(API_KEY, openApiSpecId, authName));
38+
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(API_KEY, input.getOpenApiSpecId(),
39+
input.getAuthName()));
3840
}
3941
return key;
4042
}
4143

4244
@Override
43-
public String getBasicUsername(ClientRequestContext requestContext, String openApiSpecId, String authName) {
45+
public String getBasicUsername(DataInput input) {
4446
return ConfigProvider.getConfig()
45-
.getOptionalValue(AbstractAuthProvider.getCanonicalAuthConfigPropertyName(USER_NAME, openApiSpecId, authName),
47+
.getOptionalValue(
48+
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(USER_NAME, input.getOpenApiSpecId(),
49+
input.getAuthName()),
4650
String.class)
4751
.orElse("");
4852
}
4953

5054
@Override
51-
public String getBasicPassword(ClientRequestContext requestContext, String openApiSpecId, String authName) {
55+
public String getBasicPassword(DataInput input) {
5256
return ConfigProvider.getConfig()
53-
.getOptionalValue(AbstractAuthProvider.getCanonicalAuthConfigPropertyName(PASSWORD, openApiSpecId, authName),
57+
.getOptionalValue(
58+
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(PASSWORD, input.getOpenApiSpecId(),
59+
input.getAuthName()),
5460
String.class)
5561
.orElse("");
5662
}
5763

5864
@Override
59-
public String getBearerToken(ClientRequestContext requestContext, String openApiSpecId, String authName) {
65+
public String getBearerToken(DataInput input) {
6066
return ConfigProvider.getConfig()
6167
.getOptionalValue(
62-
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(BEARER_TOKEN, openApiSpecId, authName),
68+
AbstractAuthProvider.getCanonicalAuthConfigPropertyName(BEARER_TOKEN, input.getOpenApiSpecId(),
69+
input.getAuthName()),
6370
String.class)
6471
.orElse("");
6572
}
6673

6774
@Override
68-
public String getOauth2BearerToken(ClientRequestContext requestContext) {
69-
return requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
75+
public String getOauth2BearerToken(DataInput input) {
76+
return input.getRequestContext().getHeaderString(HttpHeaders.AUTHORIZATION);
7077
}
7178
}

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

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,96 @@ public interface CredentialsProvider {
1212
/**
1313
* Gets the API Key given the OpenAPI definition and security schema
1414
*
15-
* @param openApiSpecId the OpenAPI Spec identification as defined by the OpenAPI Extension
16-
* @param authName The security schema for this API Key definition
15+
* @param input the input data available to the method
1716
* @return the API Key to use when filtering the request
1817
*/
19-
String getApiKey(ClientRequestContext requestContext, String openApiSpecId, String authName);
18+
String getApiKey(DataInput input);
2019

2120
/**
2221
* Gets the username given the OpenAPI definition and security schema
2322
*
24-
* @param openApiSpecId the OpenAPI Spec identification as defined by the OpenAPI Extension
25-
* @param authName The security schema for this Basic Authorization definition
23+
* @param input the input data available to the method
2624
* @return the username to use when filtering the request
2725
*/
28-
String getBasicUsername(ClientRequestContext requestContext, String openApiSpecId, String authName);
26+
String getBasicUsername(DataInput input);
2927

3028
/**
3129
* Gets the password given the OpenAPI definition and security schema
3230
*
33-
* @param openApiSpecId the OpenAPI Spec identification as defined by the OpenAPI Extension
34-
* @param authName The security schema for this Basic Authorization definition
31+
* @param input the input data available to the method
3532
* @return the password to use when filtering the request
3633
*/
37-
String getBasicPassword(ClientRequestContext requestContext, String openApiSpecId, String authName);
34+
String getBasicPassword(DataInput input);
3835

3936
/**
4037
* Gets the Bearer Token given the OpenAPI definition and security schema
4138
*
42-
* @param openApiSpecId the OpenAPI Spec identification as defined by the OpenAPI Extension
43-
* @param authName The security schema for this Bearer Token definition
39+
* @param input the input data available to the method
4440
* @return the Bearer Token to use when filtering the request
4541
*/
46-
String getBearerToken(ClientRequestContext requestContext, String openApiSpecId, String authName);
42+
String getBearerToken(DataInput input);
4743

4844
/**
4945
* Gets the OAuth2 Bearer Token given the OpenAPI definition and security schema
5046
*
51-
* @param requestContext The current request context in which set the authorization header token
47+
* @param input the input data available to the method
5248
* @return the Bearer Token to use when filtering the request
5349
*/
54-
String getOauth2BearerToken(ClientRequestContext requestContext);
50+
String getOauth2BearerToken(DataInput input);
51+
52+
class DataInput {
53+
// requestContext The current request context in which set the authorization header token
54+
private ClientRequestContext requestContext;
55+
// openApiSpecId the OpenAPI Spec identification as defined by the OpenAPI Extension
56+
private String openApiSpecId;
57+
// authName The security schema for this Bearer Token definition
58+
private String authName;
59+
60+
public DataInput(ClientRequestContext requestContext, String openApiSpecId, String authName) {
61+
this.requestContext = requestContext;
62+
this.openApiSpecId = openApiSpecId;
63+
this.authName = authName;
64+
}
65+
66+
public ClientRequestContext getRequestContext() {
67+
return requestContext;
68+
}
69+
70+
public String getOpenApiSpecId() {
71+
return openApiSpecId;
72+
}
73+
74+
public String getAuthName() {
75+
return authName;
76+
}
77+
78+
public static DataInputBuilder builder() {
79+
return new DataInputBuilder();
80+
}
81+
82+
public static class DataInputBuilder {
83+
private ClientRequestContext requestContext;
84+
private String openApiSpecId;
85+
private String authName;
86+
87+
public DataInputBuilder requestContext(ClientRequestContext requestContext) {
88+
this.requestContext = requestContext;
89+
return this;
90+
}
91+
92+
public DataInputBuilder openApiSpecId(String openApiSpecId) {
93+
this.openApiSpecId = openApiSpecId;
94+
return this;
95+
}
96+
97+
public DataInputBuilder authName(String authName) {
98+
this.authName = authName;
99+
return this;
100+
}
101+
102+
public CredentialsProvider.DataInput build() {
103+
return new CredentialsProvider.DataInput(requestContext, openApiSpecId, authName);
104+
}
105+
}
106+
}
55107
}

0 commit comments

Comments
 (0)