diff --git a/src/main/java/de/mattv/fileserver/services/Mailer.java b/src/main/java/de/mattv/fileserver/services/Mailer.java new file mode 100644 index 0000000..a7c2a1c --- /dev/null +++ b/src/main/java/de/mattv/fileserver/services/Mailer.java @@ -0,0 +1,31 @@ +package de.mattv.fileserver.services; + +import de.mattv.fileserver.util.NonNull; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mail.MailSender; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class Mailer { + + @Value("${mail.from}") + private String from; + + private final MailSender mailSender; + + public Mailer(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @NonNull MailSender mailSender) { + this.mailSender = mailSender; + } + + public void send(@NonNull String to, @NonNull String subject, @NonNull String body) { + SimpleMailMessage msg = new SimpleMailMessage(); + msg.setTo(to); + msg.setFrom(from); + msg.setSubject("MFileserver - " + subject); + msg.setText(body); + mailSender.send(msg); + } +} diff --git a/src/main/java/de/mattv/fileserver/services/RecoveryMailer.java b/src/main/java/de/mattv/fileserver/services/RecoveryMailer.java index f3d43da..7bfb2f1 100644 --- a/src/main/java/de/mattv/fileserver/services/RecoveryMailer.java +++ b/src/main/java/de/mattv/fileserver/services/RecoveryMailer.java @@ -8,8 +8,6 @@ import de.mattv.fileserver.util.NonNull; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.tuple.Pair; -import org.springframework.mail.MailSender; -import org.springframework.mail.SimpleMailMessage; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -26,21 +24,17 @@ public class RecoveryMailer { private static final Duration LIFETIME = Duration.ofMinutes(5); private static final int CODE_LENGTH = 16; - private final MailSender mailSender; + private final Mailer mailer; - public RecoveryMailer(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @NonNull MailSender mailSender) { - this.mailSender = mailSender; + public RecoveryMailer(@NonNull Mailer mailer) { + this.mailer = mailer; } public void sendMail(@NonNull User user) { log.info("Sending recovery mail to: {}", user.name); String code = RandomStringUtils.random(CODE_LENGTH, false, true); RECOVERY_KEYS.put(code, Pair.of(user.id, Instant.now().plus(LIFETIME))); - SimpleMailMessage msg = new SimpleMailMessage(); - msg.setTo(user.name); - msg.setSubject("MFileserver - Password recovery"); - msg.setText("Your recovery key is: " + code + "\nIt is valid for " + LIFETIME.toMinutes() + " minutes."); - mailSender.send(msg); + mailer.send(user.name, "Password recovery", "Your recovery key is: " + code + "\nIt is valid for " + LIFETIME.toMinutes() + " minutes."); } public @Nullable User checkCode(@NonNull String code) { diff --git a/src/main/java/de/mattv/fileserver/services/TFAMailer.java b/src/main/java/de/mattv/fileserver/services/TFAMailer.java index fda2830..ef75723 100644 --- a/src/main/java/de/mattv/fileserver/services/TFAMailer.java +++ b/src/main/java/de/mattv/fileserver/services/TFAMailer.java @@ -6,8 +6,6 @@ import de.mattv.fileserver.util.NonNull; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.tuple.Pair; -import org.springframework.mail.MailSender; -import org.springframework.mail.SimpleMailMessage; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -24,22 +22,17 @@ public class TFAMailer { private static final Duration LIFETIME = Duration.ofMinutes(5); private static final int CODE_LENGTH = 10; - private final MailSender mailSender; + private final Mailer mailer; - public TFAMailer(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @NonNull MailSender mailSender) { - this.mailSender = mailSender; + public TFAMailer(@NonNull Mailer mailer) { + this.mailer = mailer; } public void sendMail(@NonNull User user) { log.info("Sending tfa mail to: {}", user.name); String code = RandomStringUtils.random(CODE_LENGTH, false, true); MAIL_OTP.put(code, Pair.of(user.id, Instant.now().plus(LIFETIME))); - SimpleMailMessage msg = new SimpleMailMessage(); - msg.setTo(user.name); - msg.setSubject("MFileserver - TFA code"); - msg.setText("Your code is: " + code + "\nIt is valid for " + LIFETIME.toMinutes() + " minutes."); - mailSender.send(msg); - + mailer.send(user.name, "TFA Code", "Your code is: " + code + "\nIt is valid for " + LIFETIME.toMinutes() + " minutes."); } public boolean checkCode(long userId, @NonNull String code) {