Skip to content

Commit

Permalink
refactor : mail
Browse files Browse the repository at this point in the history
  • Loading branch information
YGwan committed Aug 12, 2023
1 parent 7a0beac commit 19adc0b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/main/java/se/ton/t210/service/AuthCodeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static String createAuthNumberCode(int length) {
}
return builder.toString();
} catch (Exception e) {
throw new IllegalArgumentException("email authentication code create error");
throw new IllegalArgumentException("create auth code error");
}
}
}
15 changes: 4 additions & 11 deletions src/main/java/se/ton/t210/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.springframework.stereotype.Service;
import se.ton.t210.cache.EmailAuthMailCache;
import se.ton.t210.cache.EmailAuthMailCacheRepository;
import se.ton.t210.dto.Email;
import se.ton.t210.exception.AuthException;
import se.ton.t210.service.mail.MailServiceInterface;
import se.ton.t210.service.mail.form.SignUpAuthMailForm;
import se.ton.t210.service.token.AuthTokenService;
import se.ton.t210.utils.http.CookieUtils;

Expand All @@ -27,12 +27,6 @@ public class AuthService {
@Value("${auth.jwt.token.email.cookie.key:emailAuthToken}")
private String authTokenCookieKey;

@Value("${auth.mail.title}")
private String emailAuthMailTitle;

@Value("${auth.mail.content.header}")
private String emailAuthMailContentHeader;

private final EmailAuthMailCacheRepository emailAuthMailCacheRepository;
private final AuthTokenService authTokenService;
private final MailServiceInterface mailServiceInterface;
Expand All @@ -43,11 +37,10 @@ public AuthService(EmailAuthMailCacheRepository emailAuthMailCacheRepository, Au
this.mailServiceInterface = mailServiceInterface;
}

public void sendEmailAuthMail(String userEmail) {
public void sendEmailAuthMail(String userEmailAddress) {
String authCode = AuthCodeUtils.createAuthNumberCode(authCodeLength);
Email email = new Email(emailAuthMailTitle, emailAuthMailContentHeader + authCode, userEmail);
mailServiceInterface.sendMail(email);
saveAuthInfoFromEmailAuthMailCache(userEmail, authCode);
mailServiceInterface.sendMail(userEmailAddress, new SignUpAuthMailForm(authCode));
saveAuthInfoFromEmailAuthMailCache(userEmailAddress, authCode);
}

private void saveAuthInfoFromEmailAuthMailCache(String userEmail, String authCode) {
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/se/ton/t210/service/mail/JavaMailServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import se.ton.t210.service.mail.form.Email;
import se.ton.t210.service.mail.form.MailForm;

@ConditionalOnProperty(value = "auth.mail.enable.mode", havingValue = "true")
@Async
@Component
public class JavaMailServiceImpl implements MailServiceInterface {

@Value("${auth.mail.fromAddress:[email protected]}")
private String fromAddress;
@Value("${auth.mail.fromAddr:[email protected]}")
private String fromAddr;

private final JavaMailSender mailSender;

Expand All @@ -23,19 +23,19 @@ public JavaMailServiceImpl(JavaMailSender mailSender) {
}

@Override
public void sendMail(Email email) {
sendMail(email, fromAddress);
}

public void sendMail(Email email, String fromAddress) {
public void sendMail(String userAddr, MailForm form, String fromAddr) {
final SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(email.getToAddress());
mailMessage.setSubject(email.getTitle());
mailMessage.setText(email.getContent());
mailMessage.setFrom(fromAddress);
mailMessage.setTo(userAddr);
mailMessage.setSubject(form.title());
mailMessage.setText(form.body());
mailMessage.setFrom(fromAddr);
sendMail(mailMessage);
}

public void sendMail(String userAddr, MailForm form) {
sendMail(userAddr, form, fromAddr);
}

public void sendMail(SimpleMailMessage mailMessage) {
mailSender.send(mailMessage);
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/se/ton/t210/service/mail/LogMailServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import se.ton.t210.service.mail.form.Email;
import se.ton.t210.service.mail.form.MailForm;

@ConditionalOnProperty(value = "auth.mail.enable.mode", havingValue = "false", matchIfMissing = true)
@Component
public class LogMailServiceImpl implements MailServiceInterface {

private static final Logger LOGGER = LoggerFactory.getLogger(LogMailServiceImpl.class);

@Value("${auth.mail.fromAddress:[email protected]}")
private String fromAddress;
@Value("${auth.mail.fromAddr:[email protected]}")
private String fromAddr;

@Override
public void sendMail(Email email) {
sendMail(email, fromAddress);
public void sendMail(String userAddr, MailForm form, String fromAddr) {
LOGGER.info("from : " + fromAddr + "\n");
LOGGER.info("title : " + form.title() + "\n");
LOGGER.info("content : " + form.body() + "\n");
LOGGER.info("to : " + userAddr);
}

@Override
public void sendMail(Email email, String fromAddress) {
LOGGER.info("from : " + fromAddress + "\n");
LOGGER.info("title : " + email.getTitle() + "\n");
LOGGER.info("content : " + email.getContent() + "\n");
LOGGER.info("to : " + email.getToAddress());
public void sendMail(String userAddr, MailForm form) {
sendMail(userAddr, form, fromAddr);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package se.ton.t210.service.mail;

import se.ton.t210.service.mail.form.Email;
import se.ton.t210.service.mail.form.MailForm;

public interface MailServiceInterface {

void sendMail(Email email);
void sendMail(String userAddr, MailForm mailForm, String fromAddr);

void sendMail(Email email, String fromAddress);
void sendMail(String userAddr, MailForm mailForm);
}
18 changes: 0 additions & 18 deletions src/main/java/se/ton/t210/service/mail/form/AuthEmailForm.java

This file was deleted.

19 changes: 0 additions & 19 deletions src/main/java/se/ton/t210/service/mail/form/Email.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/java/se/ton/t210/service/mail/form/MailForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package se.ton.t210.service.mail.form;

public interface MailForm {

String title();

String body();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package se.ton.t210.service.mail.form;

public class SignUpAuthMailForm implements MailForm {

private final String authCode;

public SignUpAuthMailForm(String authCode) {
this.authCode = authCode;
}

@Override
public String title() {
return "CSPFT Email Authentication";
}

@Override
public String body() {
return "Email Authentication code is : " + authCode;
}
}
6 changes: 2 additions & 4 deletions src/main/resources/properties/prod/mail.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ spring.mail.properties.mail.smtp.starttls.enable=true
## mail enable mode
auth.mail.enable.mode=true

## mail phrase
auth.mail.title=CSPFT Email Authentication
auth.mail.content.header=Email Authentication code is :
auth.mail.fromAddress=[email protected]
## mail fromAddr
auth.mail.fromAddr=[email protected]

# valid time - 3 min
auth.mail.valid.time=180
Expand Down

0 comments on commit 19adc0b

Please sign in to comment.