From a0987613233b4f5d9f2dcdc2a78bb685fbb269a2 Mon Sep 17 00:00:00 2001 From: jcschaff Date: Tue, 9 Jul 2024 08:03:10 -0400 Subject: [PATCH] create SMTPService with proper configuration for sending magic link --- .../java/org/vcell/restq/db/SMTPService.java | 45 +++++++++++++++++++ .../vcell/restq/handlers/UsersResource.java | 19 +++----- .../src/main/resources/application.properties | 5 ++- 3 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 vcell-rest/src/main/java/org/vcell/restq/db/SMTPService.java diff --git a/vcell-rest/src/main/java/org/vcell/restq/db/SMTPService.java b/vcell-rest/src/main/java/org/vcell/restq/db/SMTPService.java new file mode 100644 index 0000000000..c86edcb158 --- /dev/null +++ b/vcell-rest/src/main/java/org/vcell/restq/db/SMTPService.java @@ -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); + } + +} diff --git a/vcell-rest/src/main/java/org/vcell/restq/handlers/UsersResource.java b/vcell-rest/src/main/java/org/vcell/restq/handlers/UsersResource.java index 28af985039..88adcc3155 100644 --- a/vcell-rest/src/main/java/org/vcell/restq/handlers/UsersResource.java +++ b/vcell-rest/src/main/java/org/vcell/restq/handlers/UsersResource.java @@ -2,7 +2,6 @@ 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; @@ -10,7 +9,6 @@ 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; @@ -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; @@ -47,6 +44,9 @@ public class UsersResource { @Inject UriInfo uriInfo; + @Inject + SMTPService smtpService; + private final UserRestDB userRestDB; @Inject @@ -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); } diff --git a/vcell-rest/src/main/resources/application.properties b/vcell-rest/src/main/resources/application.properties index ee1039bd2c..75ad736abe 100644 --- a/vcell-rest/src/main/resources/application.properties +++ b/vcell-rest/src/main/resources/application.properties @@ -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=VCell_Support@uchc.edu