Skip to content

Add smtp auth to the model config #101

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

Merged
merged 8 commits into from
Aug 13, 2024
Merged
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
8 changes: 7 additions & 1 deletion Model/lib/rng/wdkModel-config.rng
Original file line number Diff line number Diff line change
@@ -8,7 +8,13 @@
<attribute name="smtpServer" />
<attribute name="supportEmail" />
<optional>
<attribute name="webAppUrl" />
<attribute name="smtpUsername" />
</optional>
<optional>
<attribute name="smtpPassword" />
</optional>
<optional>
<attribute name="webAppUrl" />
</optional>
<optional>
<attribute name="assetsUrl" />
29 changes: 24 additions & 5 deletions Model/src/main/java/org/gusdb/wdk/model/Utilities.java
Original file line number Diff line number Diff line change
@@ -18,9 +18,11 @@

import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
@@ -215,13 +217,18 @@ public static void sendEmail(String smtpServer, String sendTos, String reply,
String subject, String content, String ccAddresses, Attachment[] attachments)
throws WdkModelException {
// call the 8 parameter one
sendEmail(smtpServer, sendTos, reply, subject, content, ccAddresses, null, attachments);
sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, null, attachments);
}

// sendEmail() all 8 parameters
public static void sendEmail(String smtpServer, String sendTos, String reply,
String subject, String content, String ccAddresses, String bccAddresses,
Attachment[] attachments) throws WdkModelException {
String subject, String content, String ccAddresses, String bccAddresses,
Attachment[] attachments) throws WdkModelException {
sendEmail(smtpServer, null, null, sendTos, reply, subject, content, ccAddresses, bccAddresses, attachments);
}

// sendEmail() all 10 parameters
public static void sendEmail(String smtpServer, String username, String password, String sendTos, String reply,
String subject, String content, String ccAddresses, String bccAddresses, Attachment[] attachments) throws WdkModelException {

LOG.debug("Sending message to: " + sendTos + ", bcc to: " + bccAddresses +
",reply: " + reply + ", using SMPT: " + smtpServer);
@@ -230,7 +237,19 @@ public static void sendEmail(String smtpServer, String sendTos, String reply,
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
Authenticator auth = null;

if (username != null && password != null) {
props.put("mail.smtp.auth", "true");
auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
}

Session session = Session.getInstance(props, auth);

// instantiate a message
Message message = new MimeMessage(session);
24 changes: 23 additions & 1 deletion Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java
Original file line number Diff line number Diff line change
@@ -41,6 +41,18 @@ public String getName() {
*/
private final String _smtpServer;

/**
* the SMTP username used to authenticate with smtpServer.
*/
private final Optional<String> _smtpUserName;


/**
* the SMTP password used to authenticate with smtpServer.
*/
private final Optional<String> _smtpPassword;


/**
* the reply of the registration & recover password emails.
*/
@@ -143,7 +155,7 @@ public String getName() {

public ModelConfig(String modelName, String projectId, Path gusHome, boolean caching, boolean useWeights,
String paramRegex, Optional<Path> secretKeyFile, String secretKey, Path wdkTempDir, String webServiceUrl, String assetsUrl,
String smtpServer, String supportEmail, List<String> adminEmails, String emailSubject,
String smtpServer, Optional<String> smtpUser, Optional<String> smtpPassword, String supportEmail, List<String> adminEmails, String emailSubject,
String emailContent, ModelConfigUserDB userDB, ModelConfigAppDB appDB,
ModelConfigUserDatasetStore userDatasetStoreConfig, QueryMonitor queryMonitor,
boolean monitorBlockedThreads, int blockedThreshold, AuthenticationMethod authenticationMethod,
@@ -171,6 +183,8 @@ public ModelConfig(String modelName, String projectId, Path gusHome, boolean cac

// email setup
_smtpServer = smtpServer;
_smtpUserName = smtpUser;
_smtpPassword = smtpPassword;
_supportEmail = supportEmail;
_adminEmails = adminEmails;
_emailSubject = emailSubject;
@@ -255,6 +269,14 @@ public String getSmtpServer() {
return _smtpServer;
}

public Optional<String> getSmtpUserName() {
return _smtpUserName;
}

public Optional<String> getSmtpPassword() {
return _smtpPassword;
}

/**
* @return Returns the emailContent.
*/
Original file line number Diff line number Diff line change
@@ -37,7 +37,9 @@ public class ModelConfigBuilder {
private String _assetsUrl;

// email setup
private String _smtpServer = "localhost";
private String _smtpServer;
private String _smtpUsername;
private String _smtpPassword;
private String _supportEmail;
private List<String> _adminEmails = Collections.emptyList();
private String _emailSubject = "";
@@ -93,6 +95,9 @@ public ModelConfig build() throws WdkModelException {
assertNonNull("appDb", _appDB);
// TODO: should probably have a default stub for this to avoid NPEs
//assertNonNull("userDatasetStoreConfig", _userDatasetStoreConfig);
Optional<String> smtpUsername = Optional.ofNullable(_smtpUsername).filter(s -> !s.isBlank());
Optional<String> smtpPassword = Optional.ofNullable(_smtpPassword).filter(s -> !s.isBlank());
String smtpServer = _smtpServer == null ? "localhost" : _smtpServer;

return new ModelConfig(

@@ -116,7 +121,9 @@ public ModelConfig build() throws WdkModelException {
_assetsUrl,

// email setup
_smtpServer,
smtpServer,
smtpUsername,
smtpPassword,
_supportEmail,
_adminEmails,
_emailSubject,
@@ -199,6 +206,14 @@ public void setSmtpServer(String smtpServer) {
_smtpServer = smtpServer;
}

public void setSmtpUsername(String smtpUsername) {
_smtpUsername = smtpUsername;
}

public void setSmtpPassword(String smtpPassword) {
_smtpPassword = smtpPassword;
}

/**
* @param emailContent
* The emailContent to set.
@@ -256,7 +271,7 @@ public void setSecretKeyFile(String secretKeyFile) {
}

/**
* @param secretKeyFile
* @param secretKey
* the secretKeyFile to set
*/
public void setSecretKey(String secretKey) {
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.gusdb.wdk.model.user;

import java.util.Optional;
import java.util.regex.Matcher;

import org.gusdb.wdk.model.Attachment;
import org.gusdb.wdk.model.Utilities;
import org.gusdb.wdk.model.WdkModel;
import org.gusdb.wdk.model.WdkModelException;
@@ -37,6 +39,10 @@ public void emailTemporaryPassword(User user, String password) throws WdkModelEx
String supportEmail = wdkModelConfig.getSupportEmail();
String emailSubject = wdkModelConfig.getEmailSubject();

// Unwrap optionals here to maintain consistency with nullable arguments in Utilities.sendEmail method.
String smtpUser = wdkModelConfig.getSmtpUserName().orElse(null);
String smtpPass = wdkModelConfig.getSmtpPassword().orElse(null);

// populate email content macros with user data
String emailContent = wdkModelConfig.getEmailContent()
.replaceAll("\\$\\$" + EMAIL_MACRO_USER_NAME + "\\$\\$",
@@ -46,6 +52,6 @@ public void emailTemporaryPassword(User user, String password) throws WdkModelEx
.replaceAll("\\$\\$" + EMAIL_MACRO_PASSWORD + "\\$\\$",
Matcher.quoteReplacement(password));

Utilities.sendEmail(smtpServer, user.getEmail(), supportEmail, emailSubject, emailContent);
Utilities.sendEmail(smtpServer, smtpUser, smtpPass, user.getEmail(), supportEmail, emailSubject, emailContent, null, null, new Attachment[]{});
}
}