Skip to content

Commit 54644c4

Browse files
authored
issue-237 OAuth2AuthenticationProvider is now managed by CDI (#239) (#244)
Signed-off-by: Helber Belmiro <[email protected]>
1 parent 14fe27b commit 54644c4

File tree

10 files changed

+64
-114
lines changed

10 files changed

+64
-114
lines changed

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/template/QuteTemplatingEngineAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class QuteTemplatingEngineAdapter extends AbstractTemplatingEngineAdapter
2828
"queryParams.qute",
2929
"auth/compositeAuthenticationProvider.qute",
3030
"auth/headersFactory.qute",
31+
"auth/oauth2AuthenticationProvider.qute",
3132
"multipartFormdataPojo.qute"
3233
};
3334
public final Engine engine;

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getSanitizedFileName;
44
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.resolveApiPackage;
55
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.resolveModelPackage;
6+
import static io.quarkiverse.openapi.generator.deployment.wrapper.QuarkusJavaClientCodegen.QUARKUS_GENERATOR_NAME;
67
import static java.lang.Boolean.FALSE;
78
import static java.lang.Boolean.TRUE;
89
import static java.util.Objects.requireNonNull;
@@ -64,7 +65,7 @@ public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputD
6465
this.configurator = new QuarkusCodegenConfigurator();
6566
this.configurator.setInputSpec(specFilePath.toString());
6667
this.configurator.setOutputDir(outputDir.toString());
67-
this.configurator.addAdditionalProperty("quarkus-generator",
68+
this.configurator.addAdditionalProperty(QUARKUS_GENERATOR_NAME,
6869
Collections.singletonMap("openApiSpecId", getSanitizedFileName(specFilePath)));
6970
this.configurator.addAdditionalProperty("openApiNullable", false);
7071
this.configurator.setValidateSpec(validateSpec);

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/QuarkusJavaClientCodegen.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
public class QuarkusJavaClientCodegen extends JavaClientCodegen {
1919

20+
public static final String QUARKUS_GENERATOR_NAME = "quarkus-generator";
21+
2022
private static final String AUTH_PACKAGE = "auth";
2123
/*
2224
* Default server URL (the first one in the OpenAPI spec file servers definition.
@@ -62,6 +64,18 @@ private void replaceWithQuarkusTemplateFiles() {
6264
authFileFolder(),
6365
"AuthenticationPropagationHeadersFactory.java"));
6466

67+
Object quarkusGeneratorProperties = additionalProperties.get(QUARKUS_GENERATOR_NAME);
68+
69+
if (!(quarkusGeneratorProperties instanceof Map)) {
70+
throw new IllegalStateException("Quarkus generator properties not found.");
71+
}
72+
73+
Object openApiSpecId = ((Map<?, ?>) quarkusGeneratorProperties).get("openApiSpecId");
74+
75+
supportingFiles.add(
76+
new SupportingFile("auth/oauth2AuthenticationProvider.qute",
77+
authFileFolder(),
78+
openApiSpecId + "OAuth2AuthenticationProvider.java"));
6579
}
6680

6781
apiTemplateFiles.clear();

deployment/src/main/resources/templates/auth/compositeAuthenticationProvider.qute

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ import io.quarkiverse.openapi.generator.providers.ApiKeyIn;
1818
{#if hasHttpBearerMethods}
1919
import io.quarkiverse.openapi.generator.providers.BearerAuthenticationProvider;
2020
{/if}
21-
{#if hasOAuthMethods}
22-
import io.quarkiverse.openapi.generator.providers.OAuth2AuthenticationProvider;
23-
{/if}
2421
import io.quarkiverse.openapi.generator.providers.AbstractCompositeAuthenticationProvider;
2522
import io.quarkiverse.openapi.generator.providers.OperationAuthInfo;
2623

2724
@Priority(Priorities.AUTHENTICATION)
2825
public class CompositeAuthenticationProvider extends AbstractCompositeAuthenticationProvider {
2926

30-
@Inject
27+
@jakarta.inject.Inject
3128
OpenApiGeneratorConfig generatorConfig;
3229

30+
{#for auth in oauthMethods.orEmpty}
31+
@jakarta.inject.Inject
32+
{quarkus-generator.openApiSpecId}OAuth2AuthenticationProvider oAuth2Provider{auth_index};
33+
{/for}
34+
3335
@PostConstruct
3436
public void init() {
3537
{#for auth in httpBasicMethods.orEmpty}
@@ -58,7 +60,6 @@ public class CompositeAuthenticationProvider extends AbstractCompositeAuthentica
5860
{/for}
5961
{/for}
6062
{#for auth in oauthMethods.orEmpty}
61-
OAuth2AuthenticationProvider oAuth2Provider{auth_index} = new OAuth2AuthenticationProvider("{quarkus-generator.openApiSpecId}", sanitizeAuthName("{auth.name}"), generatorConfig);
6263
this.addAuthenticationProvider(oAuth2Provider{auth_index});
6364
{#for api in apiInfo.apis}
6465
{#for op in api.operations.operation}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.quarkiverse.openapi.generator.providers;
1+
package {apiPackage}.auth;
22

33
import static io.quarkiverse.openapi.generator.AuthConfig.TOKEN_PROPAGATION;
44

@@ -12,21 +12,30 @@
1212
import org.slf4j.LoggerFactory;
1313

1414
import io.quarkiverse.openapi.generator.OpenApiGeneratorConfig;
15+
import io.quarkiverse.openapi.generator.providers.AbstractAuthProvider;
1516
import io.quarkus.oidc.client.filter.OidcClientRequestFilter;
1617
import io.quarkus.oidc.common.runtime.OidcConstants;
1718

18-
public class OAuth2AuthenticationProvider extends AbstractAuthProvider {
19+
@io.quarkus.arc.Priority(jakarta.ws.rs.Priorities.AUTHENTICATION)
20+
@jakarta.enterprise.context.ApplicationScoped
21+
public class {quarkus-generator.openApiSpecId}OAuth2AuthenticationProvider extends AbstractAuthProvider {
1922

20-
private static final Logger LOGGER = LoggerFactory.getLogger(OAuth2AuthenticationProvider.class);
23+
private static final Logger LOGGER = LoggerFactory.getLogger({quarkus-generator.openApiSpecId}OAuth2AuthenticationProvider.class);
2124

22-
private final OidcClientRequestFilter delegate;
25+
private static final String SCHEMA_NAME = "oauth";
2326

24-
public OAuth2AuthenticationProvider(final String openApiSpecId, final String name,
25-
final OpenApiGeneratorConfig generatorConfig) {
26-
super(openApiSpecId, name, generatorConfig);
27-
this.delegate = createDelegate();
28-
// it's fine calling it here since this class will be created on `init()` method of the generated CompositeAuthenticationProvider
29-
delegate.init();
27+
private final OidcClientRequestFilterDelegate delegate;
28+
29+
{quarkus-generator.openApiSpecId}OAuth2AuthenticationProvider() {
30+
// Required by CDI. Not supposed to be used.
31+
delegate = null;
32+
}
33+
34+
@jakarta.inject.Inject
35+
public {quarkus-generator.openApiSpecId}OAuth2AuthenticationProvider(
36+
final OpenApiGeneratorConfig generatorConfig, final OidcClientRequestFilterDelegate delegate) {
37+
super("{quarkus-generator.openApiSpecId}", SCHEMA_NAME, generatorConfig);
38+
this.delegate = delegate;
3039
validateConfig();
3140
}
3241

@@ -49,21 +58,12 @@ private void validateConfig() {
4958
}
5059
}
5160

52-
OidcClientRequestFilterDelegate createDelegate() {
53-
return new OidcClientRequestFilterDelegate(getName());
54-
}
55-
61+
@jakarta.enterprise.context.ApplicationScoped
5662
static class OidcClientRequestFilterDelegate extends OidcClientRequestFilter {
5763

58-
private final String clientId;
59-
60-
public OidcClientRequestFilterDelegate(String clientId) {
61-
this.clientId = clientId;
62-
}
63-
6464
@Override
6565
protected Optional<String> clientId() {
66-
return Optional.of(this.clientId);
66+
return Optional.of(SCHEMA_NAME);
6767
}
6868
}
6969
}

integration-tests/register-provider/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<groupId>io.quarkiverse.openapi.generator</groupId>
1616
<artifactId>quarkus-openapi-generator</artifactId>
1717
</dependency>
18+
<dependency>
19+
<groupId>io.quarkus</groupId>
20+
<artifactId>quarkus-oidc-client-filter</artifactId>
21+
</dependency>
1822
</dependencies>
1923

2024
<build>

integration-tests/security/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<groupId>io.quarkiverse.openapi.generator</groupId>
1717
<artifactId>quarkus-openapi-generator</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>io.quarkus</groupId>
21+
<artifactId>quarkus-oidc-client-filter</artifactId>
22+
</dependency>
1923
<dependency>
2024
<groupId>io.quarkus</groupId>
2125
<artifactId>quarkus-junit5</artifactId>

integration-tests/skip-validation/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<groupId>io.quarkiverse.openapi.generator</groupId>
1717
<artifactId>quarkus-openapi-generator</artifactId>
1818
</dependency>
19+
<dependency>
20+
<groupId>io.quarkus</groupId>
21+
<artifactId>quarkus-oidc-client-filter</artifactId>
22+
<scope>provided</scope>
23+
</dependency>
1924
<dependency>
2025
<groupId>io.quarkus</groupId>
2126
<artifactId>quarkus-junit5</artifactId>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public abstract class AbstractAuthProvider implements AuthProvider {
2828
private AuthConfig authConfig;
2929
private final List<OperationAuthInfo> applyToOperations = new ArrayList<>();
3030

31+
protected AbstractAuthProvider() {
32+
// Required by CDI. Not supposed to be used.
33+
openApiSpecId = null;
34+
name = null;
35+
generatorConfig = null;
36+
}
37+
3138
protected AbstractAuthProvider(String openApiSpecId, String name, OpenApiGeneratorConfig generatorConfig) {
3239
this.openApiSpecId = openApiSpecId;
3340
this.name = name;

runtime/src/test/java/io/quarkiverse/openapi/generator/providers/OAuth2AuthenticationProviderTest.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)