diff --git a/security-openid-connect-client-quickstart/src/main/java/org/acme/security/openid/connect/client/FrontendResource.java b/security-openid-connect-client-quickstart/src/main/java/org/acme/security/openid/connect/client/FrontendResource.java index 14fea8aee9..cf87223f61 100644 --- a/security-openid-connect-client-quickstart/src/main/java/org/acme/security/openid/connect/client/FrontendResource.java +++ b/security-openid-connect-client-quickstart/src/main/java/org/acme/security/openid/connect/client/FrontendResource.java @@ -1,15 +1,9 @@ package org.acme.security.openid.connect.client; -import java.util.Map; - -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.rest.client.inject.RestClient; -import io.quarkus.oidc.client.OidcClient; -import io.quarkus.oidc.client.OidcClientConfig; -import io.quarkus.oidc.client.OidcClientConfig.Grant.Type; -import io.quarkus.oidc.client.OidcClients; import io.quarkus.oidc.client.Tokens; +import io.quarkus.oidc.client.runtime.TokensHelper; import io.smallrye.mutiny.Uni; import jakarta.inject.Inject; import jakarta.ws.rs.GET; @@ -19,11 +13,9 @@ @Path("/frontend") public class FrontendResource { @Inject - OidcClients oidcClients; - - @ConfigProperty(name = "quarkus.oidc.auth-server-url") - String oidcProviderAddress; - + OidcClientCreator oidcClientCreator; + TokensHelper tokenHelper = new TokensHelper(); + @Inject @RestClient RestClientWithOidcClientFilter restClientWithOidcClientFilter; @@ -54,29 +46,23 @@ public Uni getAdminNameWithOidcClientToken() { @Path("user-name-with-oidc-client-token-header-param") @Produces("text/plain") public Uni getUserNameWithOidcClientTokenHeaderParam() { - Uni oidcClient = createOidcClientDynamically(); - return oidcClient.onItem() - .transformToUni(client -> client.getTokens().onItem() - .transformToUni(tokens -> restClientWithTokenHeaderParam.getUserName("Bearer " + tokens.getAccessToken()))); + return tokenHelper.getTokens(oidcClientCreator.getOidcClient()).onItem() + .transformToUni(tokens -> restClientWithTokenHeaderParam.getUserName("Bearer " + tokens.getAccessToken())); } @GET @Path("admin-name-with-oidc-client-token-header-param") @Produces("text/plain") public Uni getAdminNameWithOidcClientTokenHeaderParam() { - Uni oidcClient = createOidcClientDynamically(); - return oidcClient.onItem() - .transformToUni(client -> client.getTokens().onItem() - .transformToUni(tokens -> restClientWithTokenHeaderParam.getAdminName("Bearer " + tokens.getAccessToken()))); + return tokenHelper.getTokens(oidcClientCreator.getOidcClient()).onItem() + .transformToUni(tokens -> restClientWithTokenHeaderParam.getAdminName("Bearer " + tokens.getAccessToken())); } @GET @Path("user-name-with-oidc-client-token-header-param-blocking") @Produces("text/plain") public String getUserNameWithOidcClientTokenHeaderParamBlocking() { - Uni oidcClient = createOidcClientDynamically(); - OidcClient client = oidcClient.await().indefinitely(); - Tokens tokens = client.getTokens().await().indefinitely(); + Tokens tokens = tokenHelper.getTokens(oidcClientCreator.getOidcClient()).await().indefinitely(); return restClientWithTokenHeaderParam.getUserName("Bearer " + tokens.getAccessToken()).await().indefinitely(); } @@ -84,24 +70,10 @@ public String getUserNameWithOidcClientTokenHeaderParamBlocking() { @Path("admin-name-with-oidc-client-token-header-param-blocking") @Produces("text/plain") public String getAdminNameWithOidcClientTokenHeaderParamBlocking() { - Uni oidcClient = createOidcClientDynamically(); - OidcClient client = oidcClient.await().indefinitely(); - Tokens tokens = client.getTokens().await().indefinitely(); + Tokens tokens = tokenHelper.getTokens(oidcClientCreator.getOidcClient()).await().indefinitely(); return restClientWithTokenHeaderParam.getAdminName("Bearer " + tokens.getAccessToken()).await().indefinitely(); } - private Uni createOidcClientDynamically() { - OidcClientConfig cfg = new OidcClientConfig(); - cfg.setId("myclient"); - cfg.setAuthServerUrl(oidcProviderAddress); - cfg.setClientId("backend-service"); - cfg.getCredentials().setSecret("secret"); - cfg.getGrant().setType(Type.PASSWORD); - cfg.setGrantOptions(Map.of("password", - Map.of("username", "alice", "password", "alice"))); - return oidcClients.newClient(cfg); - } - @GET @Path("user-name-with-propagated-token") @Produces("text/plain") diff --git a/security-openid-connect-client-quickstart/src/main/java/org/acme/security/openid/connect/client/OidcClientCreator.java b/security-openid-connect-client-quickstart/src/main/java/org/acme/security/openid/connect/client/OidcClientCreator.java new file mode 100644 index 0000000000..775ecf81c8 --- /dev/null +++ b/security-openid-connect-client-quickstart/src/main/java/org/acme/security/openid/connect/client/OidcClientCreator.java @@ -0,0 +1,46 @@ +package org.acme.security.openid.connect.client; + +import java.util.Map; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import io.quarkus.oidc.client.OidcClient; +import io.quarkus.oidc.client.OidcClientConfig; +import io.quarkus.oidc.client.OidcClientConfig.Grant.Type; +import io.quarkus.oidc.client.OidcClients; +import io.quarkus.runtime.StartupEvent; +import io.smallrye.mutiny.Uni; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; + +@ApplicationScoped +public class OidcClientCreator { + + @Inject + OidcClients oidcClients; + @ConfigProperty(name = "quarkus.oidc.auth-server-url") + String oidcProviderAddress; + + private volatile OidcClient oidcClient; + + public void startup(@Observes StartupEvent event) { + createOidcClient().subscribe().with(client -> {oidcClient = client;}); + } + + public OidcClient getOidcClient() { + return oidcClient; + } + + private Uni createOidcClient() { + OidcClientConfig cfg = new OidcClientConfig(); + cfg.setId("myclient"); + cfg.setAuthServerUrl(oidcProviderAddress); + cfg.setClientId("backend-service"); + cfg.getCredentials().setSecret("secret"); + cfg.getGrant().setType(Type.PASSWORD); + cfg.setGrantOptions(Map.of("password", + Map.of("username", "alice", "password", "alice"))); + return oidcClients.newClient(cfg); + } +} \ No newline at end of file