Skip to content

Commit

Permalink
Migrate 013-quarkus-oidc-restlcient to Keycloak Dev Svc
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik committed Sep 27, 2024
1 parent 7d242e7 commit 8456834
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 181 deletions.
4 changes: 2 additions & 2 deletions 013-quarkus-oidc-restclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
<artifactId>keycloak-authz-client</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-keycloak-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ default String lookupAuth() {
Collections.singletonMap("secret", clientSecret),
HttpClients.createDefault()));

return "Bearer " + authzClient.obtainAccessToken("test-user", "test-user").getToken();
return "Bearer " + authzClient.obtainAccessToken("alice", "alice").getToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
quarkus.http.port=8081

# Security
quarkus.oidc.auth-server-url=http://localhost:8180/auth/realms/test-realm
quarkus.oidc.client-id=test-application-client
quarkus.oidc.credentials.secret=test-application-client-secret

quarkus.http.auth.permission.unsecured.paths=/generate-token/*
quarkus.http.auth.permission.unsecured.policy=permit
Expand All @@ -15,16 +12,16 @@ org.eclipse.microprofile.rest.client.propagateHeaders=Authorization

# OIDC Client Configuration
quarkus.oidc-client.auth-server-url=${quarkus.oidc.auth-server-url}
quarkus.oidc-client.client-id=test-application-client
quarkus.oidc-client.credentials.secret=test-application-client-secret
quarkus.oidc-client.client-id=${quarkus.oidc.client-id}
quarkus.oidc-client.credentials.secret=${quarkus.oidc.credentials.secret}

## Normal User Password
quarkus.oidc-client.test-user.auth-server-url=${quarkus.oidc.auth-server-url}
quarkus.oidc-client.test-user.client-id=test-application-client
quarkus.oidc-client.test-user.credentials.secret=test-application-client-secret
quarkus.oidc-client.test-user.client-id=${quarkus.oidc.client-id}
quarkus.oidc-client.test-user.credentials.secret=${quarkus.oidc.credentials.secret}
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
quarkus.oidc-client.test-user.grant-options.password.username=alice
quarkus.oidc-client.test-user.grant-options.password.password=alice

# RestClient
io.quarkus.qe.ping.clients.PongClient/mp-rest/url=http://localhost:8081
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
package io.quarkus.qe;

import static io.restassured.RestAssured.given;
import static io.restassured.config.HttpClientConfig.httpClientConfig;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;

import java.util.UUID;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.keycloak.authorization.client.AuthzClient;

import io.quarkus.qe.containers.KeycloakTestResource;
import io.quarkus.qe.model.Score;
import io.quarkus.test.common.TestResourceScope;
import io.quarkus.test.common.WithTestResource;
import io.restassured.RestAssured;
import io.restassured.config.RestAssuredConfig;
import io.quarkus.test.keycloak.client.KeycloakTestClient;
import io.restassured.http.ContentType;

@WithTestResource(value = KeycloakTestResource.class, scope = TestResourceScope.MATCHING_RESOURCES)
public abstract class AbstractPingPongResourceTest {

private static final String PING_ENDPOINT = "/%s-ping";
private static final String PONG_ENDPOINT = "/%s-pong";
private static final String USER = "test-user";
private static final String WRONG_TOKEN = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

private static final String HTTP_SOCKET_TIMEOUT_PROPERTY = "http.socket.timeout";
private static final String HTTP_CONNECTION_TIMEOUT_PROPERTY = "http.connection.timeout";
private static final int TIMEOUT_IN_SECONDS = 1000;

AuthzClient authzClient;

@BeforeEach
public void setup() {
RestAssured.config = RestAssuredConfig.config()
.httpClient(httpClientConfig()
.setParam(HTTP_SOCKET_TIMEOUT_PROPERTY, TIMEOUT_IN_SECONDS)
.setParam(HTTP_CONNECTION_TIMEOUT_PROPERTY, TIMEOUT_IN_SECONDS));
}
private KeycloakTestClient keycloakTestClient = new KeycloakTestClient();

@Test
public void testPingUnauthorized() {
Expand Down Expand Up @@ -152,6 +131,6 @@ protected String pongEndpoint() {
}

private String createToken() {
return authzClient.obtainAccessToken(USER, USER).getToken();
return keycloakTestClient.getAccessToken("alice");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.keycloak.client.KeycloakTestClient;

@QuarkusTest
public class SecuredResourceTest {
Expand All @@ -15,6 +16,8 @@ public class SecuredResourceTest {
private static final String CLAIMS_FROM_BEANS_PATH = "/getClaimsFromBeans";
private static final String CLAIMS_FROM_TOKEN_PATH = "/getClaimsFromToken";

private KeycloakTestClient keycloakClient = new KeycloakTestClient();

@Test
public void verifySecuredEndpointIsProtected() {
given().get(SECURED_PATH + CLAIMS_FROM_BEANS_PATH)
Expand All @@ -38,7 +41,7 @@ private String getClaimsFromToken() {
}

private String getClaimsInstancesFromPath(String path) {
String token = given().when().get("/generate-token/test-user").then().statusCode(200).extract().asString();
String token = keycloakClient.getAccessToken("alice");

return given()
.auth().preemptive().oauth2(token)
Expand Down
43 changes: 0 additions & 43 deletions 013-quarkus-oidc-restclient/src/test/resources/test-realm.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.smallrye.mutiny.Uni;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;

@RouteBase(path = "/validate")
public class ValidationOnResponseRouteHandler {
Expand Down

0 comments on commit 8456834

Please sign in to comment.