From 3f1b7a0335968c763bf579f31f577e47fabee213 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Thu, 13 Mar 2014 21:47:46 -0400 Subject: [PATCH] adapter testing --- .../adapters/OAuthRequestAuthenticator.java | 3 +- .../undertow/KeycloakServletExtension.java | 28 +++- .../undertow/ServletKeycloakAuthMech.java | 4 +- testsuite/integration/pom.xml | 11 ++ .../testsuite/adapter/AdapterTest.java | 105 +++++++++++++++ .../adapter/CustomerDatabaseServlet.java | 29 +++++ .../testsuite/adapter/CustomerServlet.java | 48 +++++++ .../testsuite/adapter/ProductServlet.java | 29 +++++ .../testsuite/rule/AbstractKeycloakRule.java | 24 +++- .../adapter-test/cust-app-keycloak.json | 11 ++ .../adapter-test/customer-db-keycloak.json | 8 ++ .../resources/adapter-test/demorealm.json | 121 ++++++++++++++++++ .../adapter-test/product-keycloak.json | 10 ++ 13 files changed, 423 insertions(+), 8 deletions(-) create mode 100755 testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java create mode 100755 testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CustomerDatabaseServlet.java create mode 100755 testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CustomerServlet.java create mode 100755 testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/ProductServlet.java create mode 100755 testsuite/integration/src/test/resources/adapter-test/cust-app-keycloak.json create mode 100755 testsuite/integration/src/test/resources/adapter-test/customer-db-keycloak.json create mode 100755 testsuite/integration/src/test/resources/adapter-test/demorealm.json create mode 100755 testsuite/integration/src/test/resources/adapter-test/product-keycloak.json diff --git a/integration/adapter-core/src/main/java/org/keycloak/adapters/OAuthRequestAuthenticator.java b/integration/adapter-core/src/main/java/org/keycloak/adapters/OAuthRequestAuthenticator.java index e39602e01e..a948b03a7c 100755 --- a/integration/adapter-core/src/main/java/org/keycloak/adapters/OAuthRequestAuthenticator.java +++ b/integration/adapter-core/src/main/java/org/keycloak/adapters/OAuthRequestAuthenticator.java @@ -108,7 +108,7 @@ public abstract class OAuthRequestAuthenticator { protected String getRedirectUri(String state) { String url = getRequestUrl(); - log.infof("sending redirect uri: %s", url); + log.infof("callback uri: %s", url); if (!isRequestSecure() && deployment.isSslRequired()) { int port = sslRedirectPort(); if (port < 0) { @@ -147,6 +147,7 @@ public abstract class OAuthRequestAuthenticator { exchange.getResponse().setStatus(403); return true; } + log.info("Sending redirect to login page: " + redirect); exchange.getResponse().setStatus(302); exchange.getResponse().setCookie(deployment.getStateCookieName(), state, /* need to set path? */ null, null, -1, deployment.isSslRequired(), false); exchange.getResponse().setHeader("Location", redirect); diff --git a/integration/undertow/src/main/java/org/keycloak/adapters/undertow/KeycloakServletExtension.java b/integration/undertow/src/main/java/org/keycloak/adapters/undertow/KeycloakServletExtension.java index 4c074807d8..68f53bac08 100755 --- a/integration/undertow/src/main/java/org/keycloak/adapters/undertow/KeycloakServletExtension.java +++ b/integration/undertow/src/main/java/org/keycloak/adapters/undertow/KeycloakServletExtension.java @@ -19,6 +19,8 @@ import org.keycloak.adapters.KeycloakDeployment; import org.keycloak.adapters.KeycloakDeploymentBuilder; import javax.servlet.ServletContext; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Map; @@ -43,7 +45,7 @@ public class KeycloakServletExtension implements ServletExtension { return false; } - private InputStream getJSONFromServletContext(ServletContext servletContext) { + private static InputStream getJSONFromServletContext(ServletContext servletContext) { String json = servletContext.getInitParameter(AdapterConstants.AUTH_DATA_PARAM_NAME); if (json == null) { return null; @@ -51,6 +53,25 @@ public class KeycloakServletExtension implements ServletExtension { return new ByteArrayInputStream(json.getBytes()); } + private static InputStream getConfigInputStream(ServletContext context) { + InputStream is = getJSONFromServletContext(context); + if (is == null) { + String path = context.getInitParameter("keycloak.config.file"); + if (path == null) { + log.info("**** using /WEB-INF/keycloak.json"); + is = context.getResourceAsStream("/WEB-INF/keycloak.json"); + } else { + try { + is = new FileInputStream(path); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + } + return is; + } + + @Override public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) { if (!isAuthenticationMechanismPresent(deploymentInfo, "KEYCLOAK")) { @@ -58,10 +79,7 @@ public class KeycloakServletExtension implements ServletExtension { return; } log.info("KeycloakServletException initialization"); - InputStream is = getJSONFromServletContext(servletContext); - if (is == null) { - is = servletContext.getResourceAsStream("/WEB-INF/keycloak.json"); - } + InputStream is = getConfigInputStream(servletContext); if (is == null) throw new RuntimeException("Unable to find realm config in /WEB-INF/keycloak.json or in keycloak subsystem."); KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(is); UndertowUserSessionManagement userSessionManagement = new UndertowUserSessionManagement(deployment); diff --git a/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletKeycloakAuthMech.java b/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletKeycloakAuthMech.java index 42b9237170..d72033f752 100755 --- a/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletKeycloakAuthMech.java +++ b/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletKeycloakAuthMech.java @@ -46,8 +46,10 @@ public class ServletKeycloakAuthMech implements AuthenticationMechanism { } protected ServletRequestAuthenticator createRequestAuthenticator(HttpServerExchange exchange, SecurityContext securityContext, UndertowHttpFacade facade) { + int confidentialPort = 8443; + if (portManager != null) confidentialPort = portManager.getConfidentialPort(exchange); return new ServletRequestAuthenticator(facade, deployment, - portManager.getConfidentialPort(exchange), securityContext, exchange, userSessionManagement); + confidentialPort, securityContext, exchange, userSessionManagement); } @Override diff --git a/testsuite/integration/pom.xml b/testsuite/integration/pom.xml index 7b80860392..ea61388294 100755 --- a/testsuite/integration/pom.xml +++ b/testsuite/integration/pom.xml @@ -66,6 +66,17 @@ keycloak-js-adapter ${project.version} + + org.keycloak + keycloak-undertow-adapter + ${project.version} + + + org.apache.httpcomponents + httpclient + 4.2.1 + +