Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Jan 9, 2020
1 parent bfedd50 commit 05ca120
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,135 +4,27 @@
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.util.JsonSerialization;

import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;

/**
* @author <a href="mailto:[email protected]">Pedro Igor</a>
*/
@QuarkusTest
@QuarkusTestResource(KeycloakRealmResourceManager.class)
public class CodeFlowTest {

private static final String KEYCLOAK_SERVER_URL = System.getProperty("keycloak.url", "http://localhost:8180/auth");
private static final String KEYCLOAK_REALM = "quarkus";

@BeforeAll
public static void configureKeycloakRealm() throws IOException {
RealmRepresentation realm = createRealm(KEYCLOAK_REALM);

realm.getClients().add(createClient("quarkus-app"));
realm.getUsers().add(createUser("alice", "user"));
realm.getUsers().add(createUser("admin", "user", "admin"));
realm.getUsers().add(createUser("jdoe", "user", "confidential"));

RestAssured
.given()
.auth().oauth2(getAdminAccessToken())
.contentType("application/json")
.body(JsonSerialization.writeValueAsBytes(realm))
.when()
.post(KEYCLOAK_SERVER_URL + "/admin/realms").then()
.statusCode(201);
}

@AfterAll
public static void removeKeycloakRealm() {
RestAssured
.given()
.auth().oauth2(getAdminAccessToken())
.when()
.delete(KEYCLOAK_SERVER_URL + "/admin/realms/" + KEYCLOAK_REALM).thenReturn().prettyPrint();
}

private static String getAdminAccessToken() {
return RestAssured
.given()
.param("grant_type", "password")
.param("username", "admin")
.param("password", "admin")
.param("client_id", "admin-cli")
.when()
.post(KEYCLOAK_SERVER_URL + "/realms/master/protocol/openid-connect/token")
.as(AccessTokenResponse.class).getToken();
}

private static RealmRepresentation createRealm(String name) {
RealmRepresentation realm = new RealmRepresentation();

realm.setRealm(name);
realm.setEnabled(true);
realm.setUsers(new ArrayList<>());
realm.setClients(new ArrayList<>());
realm.setSsoSessionMaxLifespan(2); // sec
realm.setAccessTokenLifespan(3); // 3 seconds

RolesRepresentation roles = new RolesRepresentation();
List<RoleRepresentation> realmRoles = new ArrayList<>();

roles.setRealm(realmRoles);
realm.setRoles(roles);

realm.getRoles().getRealm().add(new RoleRepresentation("user", null, false));
realm.getRoles().getRealm().add(new RoleRepresentation("admin", null, false));
realm.getRoles().getRealm().add(new RoleRepresentation("confidential", null, false));

return realm;
}

private static ClientRepresentation createClient(String clientId) {
ClientRepresentation client = new ClientRepresentation();

client.setClientId(clientId);
client.setPublicClient(true);
client.setDirectAccessGrantsEnabled(true);
client.setEnabled(true);
client.setRedirectUris(Arrays.asList("*"));

return client;
}

private static UserRepresentation createUser(String username, String... realmRoles) {
UserRepresentation user = new UserRepresentation();

user.setUsername(username);
user.setEnabled(true);
user.setCredentials(new ArrayList<>());
user.setRealmRoles(Arrays.asList(realmRoles));

CredentialRepresentation credential = new CredentialRepresentation();

credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue(username);
credential.setTemporary(false);

user.getCredentials().add(credential);

return user;
}

@Test
public void testCodeFlowNoConsent() throws IOException {
try (final WebClient webClient = createWebClient()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package io.quarkus.it.keycloak;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.keycloak.representations.AccessTokenResponse;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.util.JsonSerialization;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import io.restassured.RestAssured;

public class KeycloakRealmResourceManager implements QuarkusTestResourceLifecycleManager {

private static final String KEYCLOAK_SERVER_URL = System.getProperty("keycloak.url", "http://localhost:8180/auth");
private static final String KEYCLOAK_REALM = "quarkus";

@Override
public Map<String, String> start() {

try {

RealmRepresentation realm = createRealm(KEYCLOAK_REALM);

realm.getClients().add(createClient("quarkus-app"));
realm.getUsers().add(createUser("alice", "user"));
realm.getUsers().add(createUser("admin", "user", "admin"));
realm.getUsers().add(createUser("jdoe", "user", "confidential"));

RestAssured
.given()
.auth().oauth2(getAdminAccessToken())
.contentType("application/json")
.body(JsonSerialization.writeValueAsBytes(realm))
.when()
.post(KEYCLOAK_SERVER_URL + "/admin/realms").then()
.statusCode(201);
} catch (IOException e) {
throw new RuntimeException(e);
}
return Collections.emptyMap();
}

private static String getAdminAccessToken() {
return RestAssured
.given()
.param("grant_type", "password")
.param("username", "admin")
.param("password", "admin")
.param("client_id", "admin-cli")
.when()
.post(KEYCLOAK_SERVER_URL + "/realms/master/protocol/openid-connect/token")
.as(AccessTokenResponse.class).getToken();
}

private static RealmRepresentation createRealm(String name) {
RealmRepresentation realm = new RealmRepresentation();

realm.setRealm(name);
realm.setEnabled(true);
realm.setUsers(new ArrayList<>());
realm.setClients(new ArrayList<>());
realm.setSsoSessionMaxLifespan(2); // sec
realm.setAccessTokenLifespan(3); // 3 seconds

RolesRepresentation roles = new RolesRepresentation();
List<RoleRepresentation> realmRoles = new ArrayList<>();

roles.setRealm(realmRoles);
realm.setRoles(roles);

realm.getRoles().getRealm().add(new RoleRepresentation("user", null, false));
realm.getRoles().getRealm().add(new RoleRepresentation("admin", null, false));
realm.getRoles().getRealm().add(new RoleRepresentation("confidential", null, false));

return realm;
}

private static ClientRepresentation createClient(String clientId) {
ClientRepresentation client = new ClientRepresentation();

client.setClientId(clientId);
client.setPublicClient(true);
client.setDirectAccessGrantsEnabled(true);
client.setEnabled(true);
client.setRedirectUris(Arrays.asList("*"));

return client;
}

private static UserRepresentation createUser(String username, String... realmRoles) {
UserRepresentation user = new UserRepresentation();

user.setUsername(username);
user.setEnabled(true);
user.setCredentials(new ArrayList<>());
user.setRealmRoles(Arrays.asList(realmRoles));

CredentialRepresentation credential = new CredentialRepresentation();

credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue(username);
credential.setTemporary(false);

user.getCredentials().add(credential);

return user;
}

@Override
public void stop() {

RestAssured
.given()
.auth().oauth2(getAdminAccessToken())
.when()
.delete(KEYCLOAK_SERVER_URL + "/admin/realms/" + KEYCLOAK_REALM).thenReturn().prettyPrint();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.jboss.logging.Logger;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;

/**
* Activates request context before test runs and shuts it down afterwards
Expand All @@ -18,18 +17,30 @@ public class RequestContextLifecycle {
private static final int DEFAULT_PRECEDENCE = 100;

public void on(@Observes(precedence = DEFAULT_PRECEDENCE) Before event) throws Throwable {
ArcContainer container = Arc.container();
if (container != null && container.isRunning()) {
container.requestContext().activate();
LOGGER.debug("RequestContextLifecycle activating CDI Request context.");
//we are outside the runtime class loader, so we don't have direct access to the container
Class<?> arcClz = Thread.currentThread().getContextClassLoader().loadClass(Arc.class.getName());
Object container = arcClz.getMethod("container").invoke(null);
if (container != null) {
boolean running = (boolean) container.getClass().getMethod("isRunning").invoke(container);
if (running) {
Object context = container.getClass().getMethod("requestContext").invoke(container);
context.getClass().getMethod("activate").invoke(context);
LOGGER.debug("RequestContextLifecycle activating CDI Request context.");
}
}
}

public void on(@Observes(precedence = DEFAULT_PRECEDENCE) After event) throws Throwable {
ArcContainer container = Arc.container();
if (container != null && container.isRunning()) {
container.requestContext().terminate();
LOGGER.debug("RequestContextLifecycle shutting down CDI Request context.");
//we are outside the runtime class loader, so we don't have direct access to the container
Class<?> arcClz = Thread.currentThread().getContextClassLoader().loadClass(Arc.class.getName());
Object container = arcClz.getMethod("container").invoke(null);
if (container != null) {
boolean running = (boolean) container.getClass().getMethod("isRunning").invoke(container);
if (running) {
Object context = container.getClass().getMethod("requestContext").invoke(container);
context.getClass().getMethod("terminate").invoke(context);
LOGGER.debug("RequestContextLifecycle activating CDI Request context.");
}
}
}
}

0 comments on commit 05ca120

Please sign in to comment.