Stephan Richter
4 months ago
11 changed files with 307 additions and 57 deletions
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* © SRSoftware 2024 */ |
||||
package de.srsoftware.oidc.api; |
||||
|
||||
import static de.srsoftware.oidc.api.Constants.*; |
||||
|
||||
import java.util.Map; |
||||
import java.util.Properties; |
||||
|
||||
public interface MailConfig { |
||||
public String smtpHost(); |
||||
public MailConfig smtpHost(String newValue); |
||||
|
||||
public int smtpPort(); |
||||
public MailConfig smtpPort(int newValue); |
||||
|
||||
public String senderAddress(); |
||||
public MailConfig senderAddress(String newValue); |
||||
|
||||
public String senderPassword(); |
||||
public MailConfig senderPassword(String newValue); |
||||
|
||||
default boolean startTls() { |
||||
return true; |
||||
} |
||||
MailConfig startTls(boolean newValue); |
||||
|
||||
default boolean smtpAuth() { |
||||
return true; |
||||
} |
||||
MailConfig smtpAuth(boolean newValue); |
||||
|
||||
public default Properties props() { |
||||
Properties props = new Properties(); |
||||
props.put("mail.smtp.host", smtpHost()); |
||||
props.put("mail.smtp.port", smtpPort()); |
||||
props.put("mail.smtp.auth", smtpAuth() ? "true" : "false"); |
||||
props.put("mail.smtp.starttls.enable", startTls() ? "true" : "false"); |
||||
return props; |
||||
} |
||||
|
||||
default Map<String, Object> map() { |
||||
return Map.of( //
|
||||
SMTP_HOST, smtpHost(), //
|
||||
SMTP_PORT, smtpPort(), //
|
||||
SMTP_AUTH, smtpAuth(), //
|
||||
SENDER_ADDRESS, senderAddress(), //
|
||||
START_TLS, startTls()); |
||||
} |
||||
} |
@ -1,4 +1,4 @@
@@ -1,4 +1,4 @@
|
||||
/* © SRSoftware 2024 */ |
||||
package de.srsoftware.oidc.api.data; |
||||
|
||||
public enum Permission { MANAGE_CLIENTS, MANAGE_USERS } |
||||
public enum Permission { MANAGE_CLIENTS, MANAGE_SMTP, MANAGE_USERS } |
||||
|
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
/* © SRSoftware 2024 */ |
||||
package de.srsoftware.oidc.backend; |
||||
|
||||
import static de.srsoftware.oidc.api.data.Permission.MANAGE_SMTP; |
||||
import static java.net.HttpURLConnection.HTTP_FORBIDDEN; |
||||
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; |
||||
|
||||
import com.sun.net.httpserver.HttpExchange; |
||||
import de.srsoftware.oidc.api.MailConfig; |
||||
import de.srsoftware.oidc.api.SessionService; |
||||
import de.srsoftware.oidc.api.data.User; |
||||
import java.io.IOException; |
||||
|
||||
public class EmailController extends Controller { |
||||
private final MailConfig mailConfig; |
||||
|
||||
public EmailController(MailConfig mailConfig, SessionService sessionService) { |
||||
super(sessionService); |
||||
this.mailConfig = mailConfig; |
||||
} |
||||
|
||||
@Override |
||||
public boolean doGet(String path, HttpExchange ex) throws IOException { |
||||
var optSession = getSession(ex); |
||||
if (optSession.isEmpty()) return sendEmptyResponse(HTTP_UNAUTHORIZED, ex); |
||||
var user = optSession.get().user(); |
||||
switch (path) { |
||||
case "/settings": |
||||
return provideSettings(ex, user); |
||||
} |
||||
return notFound(ex); |
||||
} |
||||
|
||||
private boolean provideSettings(HttpExchange ex, User user) throws IOException { |
||||
if (!user.hasPermission(MANAGE_SMTP)) return sendEmptyResponse(HTTP_FORBIDDEN, ex); |
||||
return sendContent(ex, mailConfig.map()); |
||||
} |
||||
} |
Loading…
Reference in new issue