Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve bug with empty strings and enhance the sent email's body #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/main/java/io/phasetwo/keycloak/magic/MagicLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -255,15 +257,14 @@ public static boolean sendMagicLinkEmail(KeycloakSession session, UserModel user
try {
EmailTemplateProvider emailTemplateProvider =
session.getProvider(EmailTemplateProvider.class);
String realmName = getRealmName(realm);
List<Object> subjAttr = ImmutableList.of(realmName);
String loginSubjectName = getLoginSubjectName(session.getContext());
List<Object> subjAttr = ImmutableList.of(loginSubjectName);
Map<String, Object> bodyAttr = Maps.newHashMap();
bodyAttr.put("realmName", realmName);
bodyAttr.put("loginSubjectName", loginSubjectName);
bodyAttr.put("magicLink", link);
emailTemplateProvider
.setRealm(realm)
.setUser(user)
.setAttribute("realmName", realmName)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this deletion, but according to my quick review noone uses this.

.send("magicLinkSubject", subjAttr, "magic-link-email.ftl", bodyAttr);
return true;
} catch (EmailException e) {
Expand All @@ -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<Object> subjAttr = ImmutableList.of(realmName);
Map<String, Object> bodyAttr = Maps.newHashMap();
bodyAttr.put("realmName", realmName);
Expand All @@ -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<Object> subjAttr = ImmutableList.of(realmName);
Map<String, Object> bodyAttr = Maps.newHashMap();
bodyAttr.put("code", code);
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
</@layout.emailLayout>
Original file line number Diff line number Diff line change
@@ -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}
</@layout.emailLayout>
Original file line number Diff line number Diff line change
@@ -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}
</@layout.emailLayout>
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<#ftl output_format="plainText">
${msg("magicLinkContinuationBody", realmName, magicLink)}
${msg("magicLinkContinuationBody", loginSubjectName, magicLink)}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<#ftl output_format="plainText">
${msg("magicLinkBody", realmName, magicLink)}
${msg("magicLinkBody", loginSubjectName, magicLink)}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<#ftl output_format="plainText">
${msg("otpBody", realmName, code)}
${msg("otpBody", loginSubjectName, code)}