diff --git a/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/ping/filters/CustomTokenRequestFilter.java b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/ping/filters/CustomTokenRequestFilter.java new file mode 100644 index 000000000..5334997ca --- /dev/null +++ b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/ping/filters/CustomTokenRequestFilter.java @@ -0,0 +1,15 @@ +package io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.ping.filters; + +import io.quarkus.oidc.token.propagation.AccessTokenRequestFilter; + +public class CustomTokenRequestFilter extends AccessTokenRequestFilter { + @Override + protected String getClientName() { + return "exchange-token"; + } + + @Override + protected boolean isExchangeToken() { + return true; + } +} diff --git a/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/FilteredTokenResource.java b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/FilteredTokenResource.java new file mode 100644 index 000000000..b867bf85c --- /dev/null +++ b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/FilteredTokenResource.java @@ -0,0 +1,22 @@ +package io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.principal; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +import org.eclipse.microprofile.rest.client.inject.RestClient; + +import io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.principal.clients.TokenPropagationFilteredClient; + +@Path("/token-propagation-filter") +public class FilteredTokenResource { + + @Inject + @RestClient + TokenPropagationFilteredClient tokenPropagationFilterClient; + + @GET + public String getUserName() { + return tokenPropagationFilterClient.getUserName(); + } +} diff --git a/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/PrincipalResource.java b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/PrincipalResource.java new file mode 100644 index 000000000..196147e7d --- /dev/null +++ b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/PrincipalResource.java @@ -0,0 +1,22 @@ +package io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.principal; + +import java.security.Principal; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +import io.quarkus.security.Authenticated; + +@Path("/principal") +@Authenticated +public class PrincipalResource { + + @Inject + Principal principal; + + @GET + public String principalName() { + return principal.getName(); + } +} diff --git a/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/clients/TokenPropagationFilteredClient.java b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/clients/TokenPropagationFilteredClient.java new file mode 100644 index 000000000..e95cecd55 --- /dev/null +++ b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/clients/TokenPropagationFilteredClient.java @@ -0,0 +1,20 @@ +package io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.principal.clients; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders; +import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; + +import io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.ping.filters.CustomTokenRequestFilter; + +@RegisterRestClient +@RegisterClientHeaders +@Path("/principal") +@RegisterProvider(CustomTokenRequestFilter.class) +public interface TokenPropagationFilteredClient { + + @GET + String getUserName(); +} diff --git a/security/keycloak-oidc-client-extended/src/main/resources/application.properties b/security/keycloak-oidc-client-extended/src/main/resources/application.properties index cffc8f631..08a07d7d9 100644 --- a/security/keycloak-oidc-client-extended/src/main/resources/application.properties +++ b/security/keycloak-oidc-client-extended/src/main/resources/application.properties @@ -24,6 +24,13 @@ quarkus.oidc-client.test-user.grant.type=password quarkus.oidc-client.test-user.grant-options.password.username=test-user quarkus.oidc-client.test-user.grant-options.password.password=test-user + +## Exchange token client +quarkus.oidc-client.exchange-token.auth-server-url=${quarkus.oidc.auth-server-url} +quarkus.oidc-client.exchange-token.client-id=test-application-client +quarkus.oidc-client.exchange-token.credentials.secret=test-application-client-secret +quarkus.oidc-client.exchange-token.grant.type=exchange + # RestClient io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.ping.clients.PongClient/mp-rest/url=http://localhost:${quarkus.http.port} io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.ping.clients.PongClient/mp-rest/scope=jakarta.inject.Singleton @@ -38,5 +45,7 @@ io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.ping.clients.Auto io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.ping.clients.TokenPropagationPongClient/mp-rest/url=http://localhost:${quarkus.http.port} +io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.principal.clients.TokenPropagationFilteredClient/mp-rest/url=http://localhost:${quarkus.http.port} + #OpenAPI quarkus.smallrye-openapi.store-schema-directory=target/generated/jakarta-rest/ diff --git a/security/keycloak-oidc-client-extended/src/test/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/TokenPropagationFilterIT.java b/security/keycloak-oidc-client-extended/src/test/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/TokenPropagationFilterIT.java new file mode 100644 index 000000000..d3a751d7a --- /dev/null +++ b/security/keycloak-oidc-client-extended/src/test/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/TokenPropagationFilterIT.java @@ -0,0 +1,22 @@ +package io.quarkus.ts.security.keycloak.oidcclient.extended.restclient; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.containsString; + +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Test; + +import io.quarkus.test.scenarios.QuarkusScenario; + +@QuarkusScenario +public class TokenPropagationFilterIT extends BaseOidcIT { + + @Test + public void usernameTest() { + given() + .auth().oauth2(createToken()) + .when().get("/token-propagation-filter") + .then().statusCode(HttpStatus.SC_OK) + .body(containsString(USER)); + } +}