Fixed mail sending
All checks were successful
/ Build the server (push) Successful in 2m25s

This commit is contained in:
Mutzi 2024-09-28 16:10:56 +02:00
parent 1b76f27e52
commit d53401cde1
Signed by: root
GPG Key ID: 2437494E09F13876
3 changed files with 39 additions and 21 deletions

View File

@ -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);
}
}

View File

@ -8,8 +8,6 @@ import de.mattv.fileserver.util.NonNull;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.tuple.Pair; 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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -26,21 +24,17 @@ public class RecoveryMailer {
private static final Duration LIFETIME = Duration.ofMinutes(5); private static final Duration LIFETIME = Duration.ofMinutes(5);
private static final int CODE_LENGTH = 16; private static final int CODE_LENGTH = 16;
private final MailSender mailSender; private final Mailer mailer;
public RecoveryMailer(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @NonNull MailSender mailSender) { public RecoveryMailer(@NonNull Mailer mailer) {
this.mailSender = mailSender; this.mailer = mailer;
} }
public void sendMail(@NonNull User user) { public void sendMail(@NonNull User user) {
log.info("Sending recovery mail to: {}", user.name); log.info("Sending recovery mail to: {}", user.name);
String code = RandomStringUtils.random(CODE_LENGTH, false, true); String code = RandomStringUtils.random(CODE_LENGTH, false, true);
RECOVERY_KEYS.put(code, Pair.of(user.id, Instant.now().plus(LIFETIME))); RECOVERY_KEYS.put(code, Pair.of(user.id, Instant.now().plus(LIFETIME)));
SimpleMailMessage msg = new SimpleMailMessage(); mailer.send(user.name, "Password recovery", "Your recovery key is: " + code + "\nIt is valid for " + LIFETIME.toMinutes() + " minutes.");
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);
} }
public @Nullable User checkCode(@NonNull String code) { public @Nullable User checkCode(@NonNull String code) {

View File

@ -6,8 +6,6 @@ import de.mattv.fileserver.util.NonNull;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.tuple.Pair; 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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -24,22 +22,17 @@ public class TFAMailer {
private static final Duration LIFETIME = Duration.ofMinutes(5); private static final Duration LIFETIME = Duration.ofMinutes(5);
private static final int CODE_LENGTH = 10; private static final int CODE_LENGTH = 10;
private final MailSender mailSender; private final Mailer mailer;
public TFAMailer(@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @NonNull MailSender mailSender) { public TFAMailer(@NonNull Mailer mailer) {
this.mailSender = mailSender; this.mailer = mailer;
} }
public void sendMail(@NonNull User user) { public void sendMail(@NonNull User user) {
log.info("Sending tfa mail to: {}", user.name); log.info("Sending tfa mail to: {}", user.name);
String code = RandomStringUtils.random(CODE_LENGTH, false, true); String code = RandomStringUtils.random(CODE_LENGTH, false, true);
MAIL_OTP.put(code, Pair.of(user.id, Instant.now().plus(LIFETIME))); MAIL_OTP.put(code, Pair.of(user.id, Instant.now().plus(LIFETIME)));
SimpleMailMessage msg = new SimpleMailMessage(); mailer.send(user.name, "TFA Code", "Your code is: " + code + "\nIt is valid for " + LIFETIME.toMinutes() + " minutes.");
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);
} }
public boolean checkCode(long userId, @NonNull String code) { public boolean checkCode(long userId, @NonNull String code) {