Skip to content

Commit

Permalink
Refactor the way the dynamic OidcClient is used
Browse files Browse the repository at this point in the history
  • Loading branch information
sberyozkin committed Mar 8, 2024
1 parent 4e45d5a commit 8dae175
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -54,54 +46,34 @@ public Uni<String> getAdminNameWithOidcClientToken() {
@Path("user-name-with-oidc-client-token-header-param")
@Produces("text/plain")
public Uni<String> getUserNameWithOidcClientTokenHeaderParam() {
Uni<OidcClient> 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<String> getAdminNameWithOidcClientTokenHeaderParam() {
Uni<OidcClient> 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> 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();
}

@GET
@Path("admin-name-with-oidc-client-token-header-param-blocking")
@Produces("text/plain")
public String getAdminNameWithOidcClientTokenHeaderParamBlocking() {
Uni<OidcClient> 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<OidcClient> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<OidcClient> 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);
}
}

0 comments on commit 8dae175

Please sign in to comment.