From 1ab2b9054c7c35b52d6500c59de4b57dd8a44232 Mon Sep 17 00:00:00 2001 From: Fedor Dudinsky Date: Wed, 29 May 2024 15:28:40 +0200 Subject: [PATCH] Coverage for JSON tokens https://issues.redhat.com/browse/QUARKUS-4179 --- .../principal/JsonTokenResource.java | 22 +++++++++++++++++++ .../principal/clients/JsonTokenClient.java | 20 +++++++++++++++++ .../src/main/resources/application.properties | 1 + .../restclient/TokenPropagationFilterIT.java | 11 ++++++++++ 4 files changed, 54 insertions(+) create mode 100644 security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/JsonTokenResource.java create mode 100644 security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/clients/JsonTokenClient.java diff --git a/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/JsonTokenResource.java b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/JsonTokenResource.java new file mode 100644 index 000000000..92c4d1a6f --- /dev/null +++ b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/JsonTokenResource.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.JsonTokenClient; + +@Path("/json-propagation-filter") +public class JsonTokenResource { + + @Inject + @RestClient + JsonTokenClient jsonClient; + + @GET + public String getUserNameThroughJson() { + return jsonClient.getUserName(); + } +} diff --git a/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/clients/JsonTokenClient.java b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/clients/JsonTokenClient.java new file mode 100644 index 000000000..6815e09b4 --- /dev/null +++ b/security/keycloak-oidc-client-extended/src/main/java/io/quarkus/ts/security/keycloak/oidcclient/extended/restclient/principal/clients/JsonTokenClient.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.oidc.token.propagation.JsonWebTokenRequestFilter; + +@RegisterRestClient +@RegisterClientHeaders +@Path("/principal") +@RegisterProvider(JsonWebTokenRequestFilter.class) +public interface JsonTokenClient { + + @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 08a07d7d9..1d2f47928 100644 --- a/security/keycloak-oidc-client-extended/src/main/resources/application.properties +++ b/security/keycloak-oidc-client-extended/src/main/resources/application.properties @@ -46,6 +46,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} +io.quarkus.ts.security.keycloak.oidcclient.extended.restclient.principal.clients.JsonTokenClient/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 index 29b69a965..754e91522 100644 --- 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 @@ -4,10 +4,12 @@ import static org.hamcrest.CoreMatchers.containsString; import org.apache.http.HttpStatus; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import io.quarkus.test.scenarios.QuarkusScenario; +import io.restassured.response.Response; @Tag("QUARKUS-3680") @QuarkusScenario @@ -21,4 +23,13 @@ public void usernameTest() { .then().statusCode(HttpStatus.SC_OK) .body(containsString(USER)); } + + @Test + public void jsonUsernameTest() { + Response response = given() + .auth().oauth2(createToken()) + .when().get("/json-propagation-filter"); + Assertions.assertEquals(HttpStatus.SC_OK, response.statusCode()); + Assertions.assertEquals(USER, response.body().asString()); + } }