This commit is contained in:
parent
1b76f27e52
commit
d53401cde1
31
src/main/java/de/mattv/fileserver/services/Mailer.java
Normal file
31
src/main/java/de/mattv/fileserver/services/Mailer.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -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) {
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user