use of org.collectionspace.chain.csp.schema.EmailData in project application by collectionspace.
the class UserDetailsReset method doEmail.
private Boolean doEmail(String csid, String emailparam, Request in, JSONObject userdetails) throws UIException, JSONException {
String token = createToken(csid);
EmailData ed = spec.getEmailData();
String[] recipients = new String[1];
/* ABSTRACT EMAIL STUFF : WHERE do we get the content of emails from? cspace-config.xml */
String messagebase = ed.getPasswordResetMessage();
String link = ed.getBaseURL() + ed.getLoginUrl() + "?token=" + token + "&email=" + emailparam;
String message = messagebase.replaceAll("\\{\\{link\\}\\}", link);
String greeting = userdetails.getJSONObject("fields").getString("screenName");
message = message.replaceAll("\\{\\{greeting\\}\\}", greeting);
message = message.replaceAll("\\\\n", "\\\n");
message = message.replaceAll("\\\\r", "\\\r");
String SMTP_HOST_NAME = ed.getSMTPHost();
String SMTP_PORT = ed.getSMTPPort();
String subject = ed.getPasswordResetSubject();
String from = ed.getFromAddress();
if (ed.getToAddress().isEmpty()) {
recipients[0] = emailparam;
} else {
recipients[0] = ed.getToAddress();
}
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
boolean debug = false;
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", ed.doSMTPAuth());
props.put("mail.debug", ed.doSMTPDebug());
props.put("mail.smtp.port", SMTP_PORT);
Session session = Session.getDefaultInstance(props);
// XXX fix to allow authpassword /username
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom;
try {
addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setText(message);
Transport.send(msg);
} catch (AddressException e) {
throw new UIException("AddressException: " + e.getMessage());
} catch (MessagingException e) {
throw new UIException("MessagingException: " + e.getMessage());
}
return true;
}
use of org.collectionspace.chain.csp.schema.EmailData in project application by collectionspace.
the class UserDetailsReset method tokenExpired.
private boolean tokenExpired(String token, String match) throws UIException {
EmailData ed = spec.getEmailData();
Integer lengthTokenIsValidFor = ed.getTokenExpirationDays();
// token is what we got from the user, match is what we created
// Get the date from when the token was created by dividing token by match
Long dateFromToken = (Long.parseLong(token) * 100000) / Long.parseLong(match, 16);
Date oldDate = new Date(dateFromToken);
// Date oldDate = new Date(dateFromToken - (8*24*60*60*1000));
// String old = dateFormat.format(oldDate);
// Get the current date
Date date = new Date();
// lengthTokenIsValidFor defaults to 7 days
if (daysBetween(oldDate, date) > lengthTokenIsValidFor)
return true;
else
return false;
}
use of org.collectionspace.chain.csp.schema.EmailData in project application by collectionspace.
the class ServiceBindingsGeneration method makeEmailBindings.
/*
* Builds something like the following XML:
* <tenant:emailConfig>
<tenant:baseurl>http://qa.collectionspace.org</tenant:baseurl>
<tenant:from>admin@collectionspace.org</tenant:from>
<tenant:smtpConfig>
<tenant:host>smtp.gmail.com</tenant:host>
<tenant:port>587</tenant:port>
<tenant:debug>true</tenant:debug>
<tenant:smtpAuth enabled="true">
<tenant:username>tom@example.com</tenant:username>
<tenant:password>password123</tenant:password>
</tenant:smtpAuth>
</tenant:smtpConfig>
<tenant:passwordResetConfig>
<tenant:tokenExpirationDays>3600</tenant:tokenExpirationSeconds>
<tenant:loginpage>/collectionspace/ui/core/html/index.html</tenant:loginpage>
<tenant:subject>CollectionSpace Password Reset</tenant:subject>
<tenant:message>You've started the process to reset your CollectionSpace account password.</tenant:message>
</tenant:passwordResetConfig>
</tenant:emailConfig>
*/
private void makeEmailBindings(Element tenantBindingElement) {
EmailData emailData = spec.getEmailData();
if (emailData != null) {
Element emailConfigElement = tenantBindingElement.addElement(new QName("emailConfig", nstenant));
if (emailData.getBaseURL() != null) {
Element ele = emailConfigElement.addElement(new QName("baseurl", nstenant));
ele.addText(emailData.getBaseURL());
}
if (emailData.getFromAddress() != null) {
Element ele = emailConfigElement.addElement(new QName("from", nstenant));
ele.addText(emailData.getFromAddress());
}
//
// Build <tenant:smtpConfig>
//
Element smtpConfigElement = emailConfigElement.addElement(new QName("smtpConfig", nstenant));
if (emailData.getSMTPHost() != null) {
Element ele = smtpConfigElement.addElement(new QName("host", nstenant));
ele.addText(emailData.getSMTPHost());
}
if (emailData.getSMTPPort() != null) {
Element ele = smtpConfigElement.addElement(new QName("port", nstenant));
ele.addText(emailData.getSMTPPort());
}
if (emailData.doSMTPDebug() != null) {
Element ele = smtpConfigElement.addElement(new QName("debug", nstenant));
ele.addText(emailData.doSMTPDebug().toString());
}
//
// Build <tenant:smtpAuth enabled="false">
//
Element smtpAuthElement = smtpConfigElement.addElement(new QName("smtpAuth", nstenant));
if (emailData.doSMTPAuth() != null) {
smtpAuthElement.addAttribute("enabled", emailData.doSMTPAuth().toString());
}
if (emailData.getSMTPAuthUsername() != null) {
Element ele = smtpAuthElement.addElement(new QName("username", nstenant));
ele.addText(emailData.getSMTPAuthUsername());
}
if (emailData.getSMTPAuthPassword() != null) {
Element ele = smtpAuthElement.addElement(new QName("password", nstenant));
ele.addText(emailData.getSMTPAuthPassword());
}
//
// Build <tenant:passwordResetConfig>
//
Element passwordResetElement = emailConfigElement.addElement(new QName("passwordResetConfig", nstenant));
if (emailData.getTokenExpirationSeconds() != null) {
Element ele = passwordResetElement.addElement(new QName("tokenExpirationSeconds", nstenant));
ele.addText(emailData.getTokenExpirationSeconds().toString());
} else if (emailData.getTokenExpirationDays() != null) {
//
// If the new 'tokenExpirationSeconds' config field is not set, then convert the 'daysvalid' days value
// into seconds; otherwise, ignore the 'daysvalid' field and use the configured 'tokenExpirationSeconds' value.
//
Element ele = passwordResetElement.addElement(new QName("tokenExpirationSeconds", nstenant));
// Convert days into seconds
Integer secondsValid = emailData.getTokenExpirationDays() * 60 * 60 * 24;
ele.addText(emailData.getTokenExpirationSeconds().toString());
}
if (emailData.getLoginUrl() != null) {
Element ele = passwordResetElement.addElement(new QName("loginpage", nstenant));
ele.addText(emailData.getLoginUrl());
}
if (emailData.getPasswordResetSubject() != null) {
Element ele = passwordResetElement.addElement(new QName("subject", nstenant));
ele.addText(emailData.getPasswordResetSubject());
}
if (emailData.getPasswordResetMessage() != null) {
Element ele = passwordResetElement.addElement(new QName("message", nstenant));
ele.addText(emailData.getPasswordResetMessage());
}
}
}
Aggregations