Skip to content

Commit

Permalink
Update processTemplate method lookup (#156)
Browse files Browse the repository at this point in the history
* change reflection for getting FreeMarkerLoginFormsProvider processTemplate method to descend class hierarchy.

* including 26 upgrade and some deprecation updates
  • Loading branch information
xgp authored Oct 26, 2024
1 parent 6670669 commit 79f0dc8
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 340 deletions.
2 changes: 2 additions & 0 deletions ext/main/java/io/phasetwo/portal/PortalFeatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public static PortalFeatures fromSession(KeycloakSession session, Auth auth) {
// deviceActivityEnabled
config.deviceActivityEnabled(CONFIG_ENABLED(realm, "profile.activity.enabled", true));
// linkedAccountsEnabled
// TODO update to `IdentityProviderStorageProvider.html#isIdentityFederationEnabled()` because
// of deprecation
config.linkedAccountsEnabled(
realm.isIdentityFederationEnabled()
&& CONFIG_ENABLED(realm, "profile.linked.enabled", true));
Expand Down
28 changes: 22 additions & 6 deletions ext/main/java/io/phasetwo/portal/PortalResourceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ public Response portal() {
String portalResources =
Urls.themeRoot(authUrl).getPath() + "/" + "admin-portal" + "/" + theme.getName();

Locale locale = new Locale("en");
Locale locale = Locale.of("en");
try {
if (auth != null && auth.getUser() != null) {
locale = session.getContext().resolveLocale(auth.getUser());
} else {
locale = new Locale(realm.getDefaultLocale() != null ? realm.getDefaultLocale() : "en");
locale = Locale.of(realm.getDefaultLocale() != null ? realm.getDefaultLocale() : "en");
}
} catch (Exception e) {
log.warn("Unable to determine locale.");
Expand Down Expand Up @@ -235,7 +235,7 @@ public Response portal() {

String envStr =
new String(JsonStringEncoder.getInstance().quoteAsString(mapper.writeValueAsString(env)));
log.infof("set environment string to %s", envStr);
log.debugf("set environment string to %s", envStr);
String authUrlAttribute =
authUrl.getPath().endsWith("/")
? authUrl.toString().substring(0, authUrl.toString().length() - 1)
Expand Down Expand Up @@ -263,9 +263,7 @@ public Response portal() {
.setAttribute("displayName", realmName)
.setAttribute("realmName", realm.getName());
FreeMarkerLoginFormsProvider fm = (FreeMarkerLoginFormsProvider) form;
Method processTemplateMethod =
fm.getClass()
.getDeclaredMethod("processTemplate", Theme.class, String.class, Locale.class);
Method processTemplateMethod = getProcessTemplateMethod(fm);
processTemplateMethod.setAccessible(true);
Response resp =
(Response)
Expand All @@ -278,6 +276,24 @@ public Response portal() {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}

Method getProcessTemplateMethod(FreeMarkerLoginFormsProvider provider)
throws NoSuchMethodException {
Class<?> clazz = provider.getClass();
StringBuilder o = new StringBuilder();
while (clazz != null) {
o.append(clazz.getSimpleName()).append("->");
try {
Method method =
clazz.getDeclaredMethod("processTemplate", Theme.class, String.class, Locale.class);
return method;
} catch (NoSuchMethodException ignore) {
}
clazz = clazz.getSuperclass();
}
throw new NoSuchMethodException(
String.format("Unable to find processTemplate method in hierarchy %s", o.toString()));
}

Map<String, String> getSupportedLocales(RealmModel realm, Locale locale) {
Properties messages = new Properties();
if (!Strings.isNullOrEmpty(realm.getDefaultLocale())) {
Expand Down
12 changes: 2 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<main.java.package>io.phasetwo.portal</main.java.package>
<keycloak.version>25.0.0</keycloak.version>
<keycloak.version>26.0.2</keycloak.version>
<guava.version>33.0.0-jre</guava.version>
<commons-lang3.version>3.11</commons-lang3.version>
<lombok.version>1.18.32</lombok.version>
<lombok.version>1.18.34</lombok.version>
<auto-service.version>1.1.1</auto-service.version>
<ossrh.url>https://s01.oss.sonatype.org</ossrh.url>
</properties>
Expand Down Expand Up @@ -72,14 +72,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifestEntries>
<Dependencies>
<![CDATA[org.keycloak.keycloak-common,org.keycloak.keycloak-core,org.keycloak.keycloak-server-spi,org.keycloak.keycloak-server-spi-private,org.keycloak.keycloak-services,com.google.guava]]></Dependencies>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
Expand Down
Loading

0 comments on commit 79f0dc8

Please sign in to comment.