diff --git a/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java b/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java index 0e1d46eac0..a7d2f68ff6 100644 --- a/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java @@ -292,12 +292,9 @@ public class AdminConsole { URI baseUri = uriInfo.getBaseUri(); - String authUrl = baseUri.toString(); - authUrl = authUrl.substring(0, authUrl.length() - 1); - - map.put("authUrl", authUrl); - map.put("consoleBaseUrl", Urls.adminConsoleRoot(baseUri, realm.getName())); - map.put("resourceUrl", Urls.themeRoot(baseUri) + "/admin/" + theme.getName()); + map.put("authUrl", session.getContext().getContextPath()); + map.put("consoleBaseUrl", Urls.adminConsoleRoot(baseUri, realm.getName()).getPath()); + map.put("resourceUrl", Urls.themeRoot(baseUri).getPath() + "/admin/" + theme.getName()); map.put("masterRealm", Config.getAdminRealm()); map.put("resourceVersion", Version.RESOURCES_VERSION); map.put("properties", theme.getProperties()); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleLandingPageTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleLandingPageTest.java new file mode 100644 index 0000000000..4f6c691291 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/AdminConsoleLandingPageTest.java @@ -0,0 +1,77 @@ +package org.keycloak.testsuite.admin; + +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.keycloak.broker.provider.util.SimpleHttp; +import org.keycloak.representations.idm.RealmRepresentation; +import org.keycloak.testsuite.AbstractKeycloakTest; + +import java.io.IOException; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class AdminConsoleLandingPageTest extends AbstractKeycloakTest { + + private CloseableHttpClient client; + + @Before + public void before() { + client = HttpClientBuilder.create().build(); + } + + @After + public void after() { + try { + client.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void addTestRealms(List testRealms) { + } + + @Test + public void landingPage() throws IOException { + String body = SimpleHttp.doGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/admin/master/console", client).asString(); + + String authUrl = body.substring(body.indexOf("var authUrl = '") + 15); + authUrl = authUrl.substring(0, authUrl.indexOf("'")); + Assert.assertEquals("/auth", authUrl); + + String resourceUrl = body.substring(body.indexOf("var resourceUrl = '") + 19); + resourceUrl = resourceUrl.substring(0, resourceUrl.indexOf("'")); + Assert.assertTrue(resourceUrl.matches("/auth/resources/[^/]*/admin/([a-z]*|[a-z]*-[a-z]*)")); + + String consoleBaseUrl = body.substring(body.indexOf("var consoleBaseUrl = '") + 22); + consoleBaseUrl = consoleBaseUrl.substring(0, consoleBaseUrl.indexOf("'")); + Assert.assertEquals(consoleBaseUrl, "/auth/admin/master/console/"); + + Pattern p = Pattern.compile("link href=\"([^\"]*)\""); + Matcher m = p.matcher(body); + + while(m.find()) { + String url = m.group(1); + Assert.assertTrue(url.startsWith("/auth/resources/")); + } + + p = Pattern.compile("script src=\"([^\"]*)\""); + m = p.matcher(body); + + while(m.find()) { + String url = m.group(1); + if (url.contains("keycloak.js")) { + Assert.assertTrue(url, url.startsWith("/auth/js/")); + } else { + Assert.assertTrue(url, url.startsWith("/auth/resources/")); + } + } + } + +}