Skip to content

Commit

Permalink
feat(core): allow customization of replyTo field of emails
Browse files Browse the repository at this point in the history
* methods using `sendEmail` (account activation, email validation) now fill in the `replyTo` field from config value
* `from` field still gets filled from the same config property
* registrar now uses default replyTo address as well

DEPLOYMENT NOTE: edit new config property perun.mailchange.replyTo and existing perun.mailchange.backupFrom to customize the respective fields of sent emails (from core API).
 `replyTo` (and `replyToName`) can be defined in `perun-registrar-lib.properties` to achieve the same for registrar
  • Loading branch information
xflord committed Oct 18, 2023
1 parent 2d9c33b commit 1f20e82
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void initBeansUtils() {
private String instanceId;
private String instanceName;
private String mailchangeBackupFrom;
private String mailchangeReplyTo;
private String mailchangeSecretKey;
private String nativeLanguage;
private String passwordManagerProgram;
Expand Down Expand Up @@ -325,6 +326,15 @@ public void setMailchangeBackupFrom(String mailchangeBackupFrom) {
this.mailchangeBackupFrom = mailchangeBackupFrom;
}

public String getMailchangeReplyTo() {
return mailchangeReplyTo;
}

public void setMailchangeReplyTo(String mailchangeReplyTo) {
this.mailchangeReplyTo = mailchangeReplyTo;
}


public String getMailchangeSecretKey() {
return mailchangeSecretKey;
}
Expand Down
2 changes: 2 additions & 0 deletions perun-base/src/main/resources/perun-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<property name="instanceId" value="${perun.instanceId}"/>
<property name="instanceName" value="${perun.instanceName}"/>
<property name="mailchangeBackupFrom" value="${perun.mailchange.backupFrom}"/>
<property name="mailchangeReplyTo" value="${perun.mailchange.replyTo}"/>
<property name="mailchangeSecretKey" value="${perun.mailchange.secretKey}"/>
<property name="mailchangeValidationWindow" value="${perun.mailchange.validationWindow}"/>
<property name="nativeLanguage" value="${perun.native.language}"/>
Expand Down Expand Up @@ -134,6 +135,7 @@
<prop key="perun.recaptcha.privatekey"/>
<prop key="perun.mailchange.secretKey"/>
<prop key="perun.mailchange.backupFrom"/>
<prop key="perun.mailchange.replyTo"/>
<prop key="perun.mailchange.validationWindow">6</prop>
<prop key="perun.pwdreset.secretKey">jda3ufK92DKs2335af</prop>
<prop key="perun.pwdreset.initVector">2Akd14k3o9s1d2G5</prop>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import cz.metacentrum.perun.core.bl.PerunBl;
import cz.metacentrum.perun.core.blImpl.ModulesUtilsBlImpl;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
Expand Down Expand Up @@ -1274,7 +1275,7 @@ public static void sendAccountActivationConfirmationEmail(User user, String emai
String defaultSubject = "["+instanceName+"] Account activation in namespace: "+namespace;
String defaultText = "Dear "+user.getDisplayName()+",\n\nyour account in namespace \""+namespace+"\" was successfully activated."+
"\n\nThis message is automatically sent to all your email addresses registered in "+instanceName+" in order to prevent malicious account activation without your knowledge.\n\n" +
"If you didn't request / perform account activation, please notify your administrators and support at "+BeansUtils.getCoreConfig().getMailchangeBackupFrom()+" to resolve this security issue.\n\n" +
"If you didn't request / perform account activation, please notify your administrators and support at "+(StringUtils.isNotEmpty(BeansUtils.getCoreConfig().getMailchangeReplyTo())? BeansUtils.getCoreConfig().getMailchangeReplyTo() : BeansUtils.getCoreConfig().getMailchangeBackupFrom())+" to resolve this security issue.\n\n" +
"Message is automatically generated." +
"\n----------------------------------------------------------------" +
"\nPerun - Identity & Access Management System";
Expand Down Expand Up @@ -1310,7 +1311,7 @@ public static void sendPasswordResetConfirmationEmail(User user, String email, S
String defaultSubject = "["+instanceName+"] Password reset in namespace: "+namespace;
String defaultText = "Dear "+user.getDisplayName()+",\n\nyour password in namespace \""+namespace+"\" was successfully reset."+
"\n\nThis message is automatically sent to all your email addresses registered in "+instanceName+" in order to prevent malicious password reset without your knowledge.\n\n" +
"If you didn't request / perform password reset, please notify your administrators and support at "+BeansUtils.getCoreConfig().getMailchangeBackupFrom()+" to resolve this security issue.\n\n" +
"If you didn't request / perform password reset, please notify your administrators and support at "+ (StringUtils.isNotEmpty(BeansUtils.getCoreConfig().getMailchangeReplyTo()) ? BeansUtils.getCoreConfig().getMailchangeReplyTo() : BeansUtils.getCoreConfig().getMailchangeBackupFrom())+" to resolve this security issue.\n\n" +
"Message is automatically generated." +
"\n----------------------------------------------------------------" +
"\nPerun - Identity & Access Management System";
Expand Down Expand Up @@ -1407,6 +1408,9 @@ private static void sendEmail(String subjectOfEmail, String bodyOfEmail, String
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(email);
message.setFrom(BeansUtils.getCoreConfig().getMailchangeBackupFrom());
if (StringUtils.isNotEmpty(BeansUtils.getCoreConfig().getMailchangeReplyTo())) {
message.setReplyTo(BeansUtils.getCoreConfig().getMailchangeReplyTo());
}

// set subject and body
message.setSubject(subjectOfEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -1096,6 +1097,11 @@ private void setFromMailAddress(MimeMessage message, Application app) {
String replyToMail = fromMail;
String replyToName = fromName;

if (!Objects.equals(getPropertyFromConfiguration("replyTo"), EMPTY_STRING)) {
replyToMail = getPropertyFromConfiguration("replyTo");
replyToName = getPropertyFromConfiguration("replyToName");
}

// get proper value from attribute
try {
Attribute attrSenderName = getMailFromVoAndGroupAttrs(app, URN_VO_FROM_NAME_EMAIL, URN_GROUP_FROM_NAME_EMAIL);
Expand Down

0 comments on commit 1f20e82

Please sign in to comment.