From 476da4f276a6cc996d83a9a4aa8dde6f070f12e8 Mon Sep 17 00:00:00 2001 From: Domenico Briganti Date: Fri, 20 Dec 2019 11:32:16 +0100 Subject: [PATCH] KEYCLOAK-9837 Not hide exception in email templating --- .../src/main/java/org/keycloak/email/EmailException.java | 4 ++++ .../email/freemarker/FreeMarkerEmailTemplateProvider.java | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/server-spi-private/src/main/java/org/keycloak/email/EmailException.java b/server-spi-private/src/main/java/org/keycloak/email/EmailException.java index 7f7a76f711..f9e2d903d1 100644 --- a/server-spi-private/src/main/java/org/keycloak/email/EmailException.java +++ b/server-spi-private/src/main/java/org/keycloak/email/EmailException.java @@ -26,6 +26,10 @@ public class EmailException extends Exception { super(cause); } + public EmailException(String message) { + super(message); + } + public EmailException(String message, Throwable cause) { super(message, cause); } diff --git a/services/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailTemplateProvider.java b/services/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailTemplateProvider.java index d57036698c..d12f4d786d 100755 --- a/services/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailTemplateProvider.java +++ b/services/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailTemplateProvider.java @@ -28,6 +28,7 @@ import java.util.Locale; import java.util.Map; import java.util.Properties; +import org.jboss.logging.Logger; import org.keycloak.broker.provider.BrokeredIdentityContext; import org.keycloak.common.util.ObjectUtil; import org.keycloak.email.EmailException; @@ -54,6 +55,7 @@ import org.keycloak.theme.beans.MessageFormatterMethod; */ public class FreeMarkerEmailTemplateProvider implements EmailTemplateProvider { + private static final Logger logger = Logger.getLogger(FreeMarkerEmailTemplateProvider.class); protected KeycloakSession session; /** * authenticationSession can be null for some email sendings, it is filled only for email sendings performed as part of the authentication session (email verification, password reset, broker link @@ -217,6 +219,7 @@ public class FreeMarkerEmailTemplateProvider implements EmailTemplateProvider { textBody = freeMarker.processTemplate(attributes, textTemplate, theme); } catch (final FreeMarkerException e) { textBody = null; + logger.warn("Failed to template plain text email.", e); } String htmlTemplate = String.format("html/%s", template); String htmlBody; @@ -224,6 +227,11 @@ public class FreeMarkerEmailTemplateProvider implements EmailTemplateProvider { htmlBody = freeMarker.processTemplate(attributes, htmlTemplate, theme); } catch (final FreeMarkerException e) { htmlBody = null; + logger.warn("Failed to template html email.", e); + } + + if (textBody == null && htmlBody == null) { + throw new EmailException("Both plain text and html are empty!"); } return new EmailTemplate(subject, textBody, htmlBody);