-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the way the dynamic OidcClient is used
- Loading branch information
1 parent
4e45d5a
commit 8dae175
Showing
2 changed files
with
56 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...t-quickstart/src/main/java/org/acme/security/openid/connect/client/OidcClientCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |