8000 BACKPORT: Share theme-resources.ftl via ThemeResourceProvider (#55518) by edewit · Pull Request #55583 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading 10BC8
Diff view
Diff view
Original file line number Diff line number Diff line change
< 10BC8 /td> @@ -1,4 +1,4 @@
<#-- Keep synchronized with the other theme-resources.ftl copies under themes/. -->
<#-- Shared FreeMarker macros for stylesheets, scripts, and favicons. Loaded via ThemeResourceProvider for all theme types. -->
<#macro renderStyles resources pathPrefix>
<#list resources as resource>
<link href="${pathPrefix}/${resource.path}" rel="stylesheet"<#if resource.media?has_content> media="${resource.media}"</#if><#if resource.integrity?has_content> integrity="${resource.integrity}"</#if><#if resource.crossorigin?has_content> crossorigin="${resource.crossorigin}"</#if> />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void deferAndAsyncAreNotInterchangeable() {
private String renderScripts(List<ThemeResourceDescriptor> scripts) throws Exception {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_32);
cfg.setOutputFormat(HTMLOutputFormat.INSTANCE);
cfg.setClassLoaderForTemplateLoading(getClass().getClassLoader(), "theme/base/login");
cfg.setClassLoaderForTemplateLoading(getClass().getClassLoader(), "theme-resources/templates");

Template template = new Template("wrapper",
"<#import \"theme-resources.ftl\" as tags><@tags.renderScripts scripts pathPrefix defaultScriptType />",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2026 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.keycloak.tests.theme;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;

import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.remote.runonserver.InjectRunOnServer;
import org.keycloak.testframework.remote.runonserver.RunOnServerClient;
import org.keycloak.theme.Theme;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Regression coverage for shared {@code theme-resources.ftl} resolution via ThemeResourceProvider
* (see #55514). Built-in themes no longer ship per-type copies of that template.
*/
@KeycloakIntegrationTest
public class ThemeResourcesSharedTemplateTest {

private static final String TEMPLATE = "theme-resources.ftl";
private static final String SHARED_PATH = "theme-resources/templates/" + TEMPLATE;

@InjectRunOnServer(permittedPackages = "org.keycloak.tests.theme")
RunOnServerClient runOnServer;

@Test
public void sharedTemplateResolvedForBuiltinThemeTypes() {
runOnServer.run(session -> {
try {
assertSharedTemplate(session.theme().getTheme("base", Theme.Type.LOGIN));
assertSharedTemplate(session.theme().getTheme("keycloak.v2", Theme.Type.ADMIN));
assertSharedTemplate(session.theme().getTheme("keycloak.v3", Theme.Type.ACCOUNT));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

@Test
public void customThemeTemplateTakesPrecedenceOverShared() {
String themeName = "theme-resources-override";
String marker = "<#-- custom-theme-resources-override -->";

runOnServer.run(session -> {
try {
Path themeDir = Paths.get(System.getProperty("kc.home.dir"), "themes", themeName, "login");
Files.createDirectories(themeDir);
Files.writeString(themeDir.resolve("theme.properties"), "parent=base\n");
Files.writeString(themeDir.resolve(TEMPLATE), marker + "\n");
session.theme().clearCache();
} catch (IOException e) {
throw new RuntimeException(e);
}
});

try {
runOnServer.run(session -> {
try {
Theme theme = session.theme().getTheme(themeName, Theme.Type.LOGIN);
URL template = theme.getTemplate(TEMPLATE);
assertNotNull(template, "custom theme should resolve " + TEMPLATE);
assertTrue(template.toString().contains("/themes/" + themeName + "/login/" + TEMPLATE)
|| template.toString().endsWith(themeName + "/login/" + TEMPLATE),
"expected theme-local template, got: " + template);
try (InputStream in = template.openStream()) {
String content = new String(in.readAllBytes(), StandardCharsets.UTF_8);
assertTrue(content.contains(marker), "custom theme template content should win over shared copy");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} finally {
deleteThemeDir(themeName);
runOnServer.run(session -> session.theme().clearCache());
}
}

private static void assertSharedTemplate(Theme theme) throws IOException {
URL template = theme.getTemplate(TEMPLATE);
assertNotNull(template, theme.getType() + "/" + theme.getName() + " should resolve " + TEMPLATE);
assertTrue(template.toString().contains(SHARED_PATH),
theme.getType() + "/" + theme.getName() + " should use shared " + SHARED_PATH + ", got: " + template);
}

private void deleteThemeDir(String themeName) {
runOnServer.run(session -> {
try {
Path themeRoot = Paths.get(System.getProperty("kc.home.dir"), "themes", themeName);
if (Files.exists(themeRoot)) {
try (var walk = Files.walk(themeRoot)) {
walk.sorted(Comparator.reverseOrder()).forEach(path -> {
try {
Files.delete(path);
} catch (IOException ignored) {
}
});
}
}
} catch (IOException ignored) {
}
});
}
}

This file was deleted.

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions themes/src/main/resources/theme/base/login/theme-resources.ftl
6921

This file was deleted.

Loading
0