forked from Archives/Athou_commafeed
use quarkus mailer for password recovery
This commit is contained in:
@@ -49,6 +49,14 @@ public interface CommaFeedConfiguration {
|
||||
@WithDefault("false")
|
||||
boolean imageProxyEnabled();
|
||||
|
||||
/**
|
||||
* Enable password recovery via email.
|
||||
*
|
||||
* Quarkus mailer will need to be configured.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean passwordRecoveryEnabled();
|
||||
|
||||
/**
|
||||
* Message displayed in a notification at the bottom of the page.
|
||||
*/
|
||||
@@ -84,11 +92,6 @@ public interface CommaFeedConfiguration {
|
||||
*/
|
||||
Websocket websocket();
|
||||
|
||||
/**
|
||||
* SMTP settings for password recovery.
|
||||
*/
|
||||
Optional<Smtp> smtp();
|
||||
|
||||
/**
|
||||
* Redis settings to enable caching. This is only really useful on instances with a lot of users.
|
||||
*/
|
||||
@@ -207,20 +210,6 @@ public interface CommaFeedConfiguration {
|
||||
boolean createDemoAccount();
|
||||
}
|
||||
|
||||
interface Smtp {
|
||||
String host();
|
||||
|
||||
int port();
|
||||
|
||||
boolean tls();
|
||||
|
||||
String userName();
|
||||
|
||||
String password();
|
||||
|
||||
String fromAddress();
|
||||
}
|
||||
|
||||
interface Websocket {
|
||||
/**
|
||||
* Enable websocket connection so the server can notify the web client that there are new entries for your feeds.
|
||||
|
||||
@@ -1,64 +1,20 @@
|
||||
package com.commafeed.backend.service;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.commafeed.CommaFeedConfiguration;
|
||||
import com.commafeed.CommaFeedConfiguration.Smtp;
|
||||
import com.commafeed.backend.model.User;
|
||||
|
||||
import io.quarkus.mailer.Mail;
|
||||
import io.quarkus.mailer.Mailer;
|
||||
import jakarta.inject.Singleton;
|
||||
import jakarta.mail.Authenticator;
|
||||
import jakarta.mail.Message;
|
||||
import jakarta.mail.PasswordAuthentication;
|
||||
import jakarta.mail.Session;
|
||||
import jakarta.mail.Transport;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Mailing service
|
||||
*
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Singleton
|
||||
public class MailService {
|
||||
|
||||
private final CommaFeedConfiguration config;
|
||||
|
||||
public void sendMail(User user, String subject, String content) throws Exception {
|
||||
Optional<Smtp> settings = config.smtp();
|
||||
if (settings.isEmpty()) {
|
||||
throw new IllegalArgumentException("SMTP settings not configured");
|
||||
}
|
||||
|
||||
final String username = settings.get().userName();
|
||||
final String password = settings.get().password();
|
||||
final String fromAddress = Optional.ofNullable(settings.get().fromAddress()).orElse(settings.get().userName());
|
||||
|
||||
String dest = user.getEmail();
|
||||
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", String.valueOf(settings.get().tls()));
|
||||
props.put("mail.smtp.host", settings.get().host());
|
||||
props.put("mail.smtp.port", String.valueOf(settings.get().port()));
|
||||
|
||||
Session session = Session.getInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
|
||||
Message message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(fromAddress, "CommaFeed"));
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(dest));
|
||||
message.setSubject("CommaFeed - " + subject);
|
||||
message.setContent(content, "text/html; charset=utf-8");
|
||||
|
||||
Transport.send(message);
|
||||
private final Mailer mailer;
|
||||
|
||||
public void sendMail(User user, String subject, String content) {
|
||||
Mail mail = Mail.withHtml(user.getEmail(), "CommaFeed - " + subject, content);
|
||||
mailer.send(mail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ServerREST {
|
||||
infos.setGitCommit(version.getGitCommit());
|
||||
infos.setAllowRegistrations(config.users().allowRegistrations());
|
||||
infos.setGoogleAnalyticsCode(config.googleAnalyticsTrackingCode().orElse(null));
|
||||
infos.setSmtpEnabled(config.smtp().isPresent());
|
||||
infos.setSmtpEnabled(config.passwordRecoveryEnabled());
|
||||
infos.setDemoAccountEnabled(config.users().createDemoAccount());
|
||||
infos.setWebsocketEnabled(config.websocket().enabled());
|
||||
infos.setWebsocketPingInterval(config.websocket().pingInterval().toMillis());
|
||||
|
||||
@@ -267,6 +267,10 @@ public class UserREST {
|
||||
@Transactional
|
||||
@Operation(summary = "send a password reset email")
|
||||
public Response sendPasswordReset(@Valid @Parameter(required = true) PasswordResetRequest req) {
|
||||
if (!config.passwordRecoveryEnabled()) {
|
||||
throw new IllegalArgumentException("Password recovery is not enabled on this CommaFeed instance");
|
||||
}
|
||||
|
||||
User user = userDAO.findByEmail(req.getEmail());
|
||||
if (user == null) {
|
||||
return Response.ok().build();
|
||||
|
||||
@@ -14,18 +14,19 @@ import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.core.NewCookie;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@Path("/logout")
|
||||
@PermitAll
|
||||
@RequiredArgsConstructor
|
||||
@Singleton
|
||||
public class LogoutServlet {
|
||||
|
||||
@ConfigProperty(name = "quarkus.http.auth.form.cookie-name")
|
||||
String cookieName;
|
||||
|
||||
private final CommaFeedConfiguration config;
|
||||
private final String cookieName;
|
||||
|
||||
public LogoutServlet(CommaFeedConfiguration config, @ConfigProperty(name = "quarkus.http.auth.form.cookie-name") String cookieName) {
|
||||
this.config = config;
|
||||
this.cookieName = cookieName;
|
||||
}
|
||||
|
||||
@GET
|
||||
public Response get() {
|
||||
|
||||
Reference in New Issue
Block a user