From cfdbf5ec0dec5b2b8993e3bb9af380181cb2ee6e Mon Sep 17 00:00:00 2001 From: Vilmos Nagy Date: Tue, 26 Nov 2024 16:11:00 +0100 Subject: [PATCH] #97 resolve bug with empty strings and enhance the sent email's body --- .../io/phasetwo/keycloak/magic/MagicLink.java | 23 ++++++++++++------- .../html/magic-link-continuation-email.ftl | 2 +- .../templates/html/magic-link-email.ftl | 2 +- .../templates/html/otp-email.ftl | 2 +- .../text/magic-link-continuation-email.ftl | 2 +- .../templates/text/magic-link-email.ftl | 2 +- .../templates/text/otp-email.ftl | 2 +- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/phasetwo/keycloak/magic/MagicLink.java b/src/main/java/io/phasetwo/keycloak/magic/MagicLink.java index e32bb9c..b8a632e 100644 --- a/src/main/java/io/phasetwo/keycloak/magic/MagicLink.java +++ b/src/main/java/io/phasetwo/keycloak/magic/MagicLink.java @@ -15,6 +15,7 @@ import java.net.URI; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.OptionalInt; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -33,6 +34,7 @@ import org.keycloak.models.AuthenticationFlowModel; import org.keycloak.models.ClientModel; import org.keycloak.models.Constants; +import org.keycloak.models.KeycloakContext; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionTask; @@ -255,15 +257,14 @@ public static boolean sendMagicLinkEmail(KeycloakSession session, UserModel user try { EmailTemplateProvider emailTemplateProvider = session.getProvider(EmailTemplateProvider.class); - String realmName = getRealmName(realm); - List subjAttr = ImmutableList.of(realmName); + String loginSubjectName = getLoginSubjectName(session.getContext()); + List subjAttr = ImmutableList.of(loginSubjectName); Map bodyAttr = Maps.newHashMap(); - bodyAttr.put("realmName", realmName); + bodyAttr.put("loginSubjectName", loginSubjectName); bodyAttr.put("magicLink", link); emailTemplateProvider .setRealm(realm) .setUser(user) - .setAttribute("realmName", realmName) .send("magicLinkSubject", subjAttr, "magic-link-email.ftl", bodyAttr); return true; } catch (EmailException e) { @@ -278,7 +279,7 @@ public static boolean sendMagicLinkContinuationEmail( try { EmailTemplateProvider emailTemplateProvider = session.getProvider(EmailTemplateProvider.class); - String realmName = getRealmName(realm); + String realmName = getLoginSubjectName(session.getContext()); List subjAttr = ImmutableList.of(realmName); Map bodyAttr = Maps.newHashMap(); bodyAttr.put("realmName", realmName); @@ -304,7 +305,7 @@ public static boolean sendOtpEmail(KeycloakSession session, UserModel user, Stri try { EmailTemplateProvider emailTemplateProvider = session.getProvider(EmailTemplateProvider.class); - String realmName = getRealmName(realm); + String realmName = getLoginSubjectName(session.getContext()); List subjAttr = ImmutableList.of(realmName); Map bodyAttr = Maps.newHashMap(); bodyAttr.put("code", code); @@ -320,8 +321,14 @@ public static boolean sendOtpEmail(KeycloakSession session, UserModel user, Stri return false; } - public static String getRealmName(RealmModel realm) { - return Strings.isNullOrEmpty(realm.getDisplayName()) ? realm.getName() : realm.getDisplayName(); + public static String getLoginSubjectName(KeycloakContext context) { + return Optional + .ofNullable(context.getClient()) + .map(ClientModel::getName) + .orElseGet(() -> { + final var realm = context.getRealm(); + return Strings.isNullOrEmpty(realm.getDisplayName()) ? realm.getName() : realm.getDisplayName(); + }); } public static final String MAGIC_LINK_AUTH_FLOW_ALIAS = "magic link"; diff --git a/src/main/resources/theme-resources/templates/html/magic-link-continuation-email.ftl b/src/main/resources/theme-resources/templates/html/magic-link-continuation-email.ftl index e2703ec..d500554 100644 --- a/src/main/resources/theme-resources/templates/html/magic-link-continuation-email.ftl +++ b/src/main/resources/theme-resources/templates/html/magic-link-continuation-email.ftl @@ -1,4 +1,4 @@ <#import "template.ftl" as layout> <@layout.emailLayout> -${kcSanitize(msg("magicLinkContinuationBodyHtml", realmName, magicLink))?no_esc} +${kcSanitize(msg("magicLinkContinuationBodyHtml", loginSubjectName, magicLink))?no_esc} diff --git a/src/main/resources/theme-resources/templates/html/magic-link-email.ftl b/src/main/resources/theme-resources/templates/html/magic-link-email.ftl index cc94807..29777d3 100644 --- a/src/main/resources/theme-resources/templates/html/magic-link-email.ftl +++ b/src/main/resources/theme-resources/templates/html/magic-link-email.ftl @@ -1,4 +1,4 @@ <#import "template.ftl" as layout> <@layout.emailLayout> -${kcSanitize(msg("magicLinkBodyHtml", realmName, magicLink))?no_esc} +${kcSanitize(msg("magicLinkBodyHtml", loginSubjectName, magicLink))?no_esc} diff --git a/src/main/resources/theme-resources/templates/html/otp-email.ftl b/src/main/resources/theme-resources/templates/html/otp-email.ftl index 0087358..087498f 100644 --- a/src/main/resources/theme-resources/templates/html/otp-email.ftl +++ b/src/main/resources/theme-resources/templates/html/otp-email.ftl @@ -1,4 +1,4 @@ <#import "template.ftl" as layout> <@layout.emailLayout> -${kcSanitize(msg("otpBodyHtml", realmName, code))?no_esc} +${kcSanitize(msg("otpBodyHtml", loginSubjectName, code))?no_esc} diff --git a/src/main/resources/theme-resources/templates/text/magic-link-continuation-email.ftl b/src/main/resources/theme-resources/templates/text/magic-link-continuation-email.ftl index 60848a5..2c08ee8 100644 --- a/src/main/resources/theme-resources/templates/text/magic-link-continuation-email.ftl +++ b/src/main/resources/theme-resources/templates/text/magic-link-continuation-email.ftl @@ -1,2 +1,2 @@ <#ftl output_format="plainText"> -${msg("magicLinkContinuationBody", realmName, magicLink)} +${msg("magicLinkContinuationBody", loginSubjectName, magicLink)} diff --git a/src/main/resources/theme-resources/templates/text/magic-link-email.ftl b/src/main/resources/theme-resources/templates/text/magic-link-email.ftl index 26f5c61..a314792 100644 --- a/src/main/resources/theme-resources/templates/text/magic-link-email.ftl +++ b/src/main/resources/theme-resources/templates/text/magic-link-email.ftl @@ -1,2 +1,2 @@ <#ftl output_format="plainText"> -${msg("magicLinkBody", realmName, magicLink)} +${msg("magicLinkBody", loginSubjectName, magicLink)} diff --git a/src/main/resources/theme-resources/templates/text/otp-email.ftl b/src/main/resources/theme-resources/templates/text/otp-email.ftl index 0fdcb30..7768d79 100644 --- a/src/main/resources/theme-resources/templates/text/otp-email.ftl +++ b/src/main/resources/theme-resources/templates/text/otp-email.ftl @@ -1,2 +1,2 @@ <#ftl output_format="plainText"> -${msg("otpBody", realmName, code)} +${msg("otpBody", loginSubjectName, code)}