Skip to content

Commit

Permalink
create SMTPService with proper configuration for sending magic link
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Jul 9, 2024
1 parent 803706b commit a098761
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
45 changes: 45 additions & 0 deletions vcell-rest/src/main/java/org/vcell/restq/db/SMTPService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.vcell.restq.db;

import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@ApplicationScoped
public class SMTPService {

@ConfigProperty(name = "vcell.smtp.hostName")
String smtpHostName;

@ConfigProperty(name = "vcell.smtp.port")
String smtpPost;

@ConfigProperty(name = "vcell.smtp.emailAddress")
String smtpEmail;


public void sendEmail(String emailAddress, String subject, String body) throws MessagingException {
// Create a mail session
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", smtpHostName);
props.put("mail.smtp.port", "" + Integer.parseInt(smtpPost));
Session session = Session.getDefaultInstance(props, null);

// Construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(smtpEmail));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
msg.setSubject(subject);
msg.setText(body);

// Send the message
Transport.send(msg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import cbit.vcell.modeldb.ApiAccessToken;
import cbit.vcell.modeldb.UserIdentity;
import cbit.vcell.resource.PropertyLoader;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.annotation.security.RolesAllowed;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriInfo;
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.eclipse.microprofile.openapi.annotations.Operation;
Expand All @@ -22,14 +20,13 @@
import org.jose4j.lang.JoseException;
import org.vcell.auth.JWTUtils;
import org.vcell.restq.auth.CustomSecurityIdentityAugmentor;
import org.vcell.restq.db.SMTPService;
import org.vcell.restq.db.UserRestDB;
import org.vcell.util.BeanUtils;
import org.vcell.util.DataAccessException;
import org.vcell.util.UseridIDExistsException;
import org.vcell.util.document.User;
import org.vcell.util.document.UserInfo;

import javax.mail.MessagingException;
import java.math.BigDecimal;
import java.net.URI;
import java.sql.SQLException;
Expand All @@ -47,6 +44,9 @@ public class UsersResource {
@Inject
UriInfo uriInfo;

@Inject
SMTPService smtpService;

private final UserRestDB userRestDB;

@Inject
Expand Down Expand Up @@ -133,15 +133,8 @@ email, requestorSubject, requestorIssuer, new User(userInfo.userid, userInfo.id)
"\n" +
"Please note that this link will expire in 24 hours, and can only be used once.";

//Send new password to user
BeanUtils.sendSMTP(
PropertyLoader.getRequiredProperty(PropertyLoader.vcellSMTPHostName),
Integer.parseInt(PropertyLoader.getRequiredProperty(PropertyLoader.vcellSMTPPort)),
PropertyLoader.getRequiredProperty(PropertyLoader.vcellSMTPEmailAddress),
userInfo.email,
subject,
content
);
//Send magic link to user
smtpService.sendEmail(userInfo.email, subject, content);
} catch (Exception e) {
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
Expand Down
5 changes: 4 additions & 1 deletion vcell-rest/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,8 @@ quarkus.swagger-ui.always-include=true
%dev.quarkus.swagger-ui.oauth-use-pkce-with-authorization-code-grant=true
%test.quarkus.swagger-ui.oauth-use-pkce-with-authorization-code-grant=false

## VCell tests
## VCell properties
%dev,test.vcell.softwareVersion=8.0.0
vcell.smtp.hostName=smtp.cam.uchc.edu
vcell.smtp.port=25
vcell.smtp.emailAddress=[email protected]

0 comments on commit a098761

Please sign in to comment.