working on oidc auth

This commit is contained in:
2022-04-14 11:58:21 +02:00
parent b2d9a115b9
commit b251e4e4cb
13 changed files with 371 additions and 19 deletions

View File

@@ -0,0 +1,34 @@
package de.srsoftware.widerhall.mail;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.Message;
import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
public class Forwarder implements MessageHandler {
private static final Logger LOG = LoggerFactory.getLogger(Forwarder.class);
private final SmtpClient smtp;
private final JSONObject config;
public Forwarder(JSONObject config) {
this.config = config;
SmtpClient smtp = new SmtpClient(config);
this.smtp = smtp;
}
@Override
public void onMessageReceived(Message message) throws MessagingException {
LOG.debug("forwarding {}",message.getSubject());
String testSender = (String) config.get("sender");
String testReceiver = (String) config.get("receiver");
try {
smtp.send(config,testSender,"Stephan Richter",testReceiver,"Info: "+message.getSubject(),"Neue Mail eingegangen!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,124 @@
package de.srsoftware.widerhall.mail;
import com.sun.mail.imap.IMAPFolder;
import de.srsoftware.widerhall.Constants;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import java.util.HashSet;
import java.util.Properties;
public class ImapClient {
private static final Logger LOG = LoggerFactory.getLogger(ImapClient.class);
private final JSONObject config;
private boolean stopped = false;
private IMAPFolder inbox;
private HashSet<MessageHandler> listeners = new HashSet<>();
private class ListeningThread extends Thread {
private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class);
@Override
public void run() {
while (!stopped) {
try {
openInbox();
} catch (MessagingException e){
LOG.warn("Connection problem:",e);
try {
sleep(1000);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
}
private void openInbox() throws MessagingException {
String host = (String) config.get(Constants.HOST);
String username = (String) config.get(Constants.USER);
String password = (String) config.get(Constants.PASSWORD);
String folderName = (String) config.get(Constants.INBOX);
LOG.debug("Connecting and logging in…");
Properties props = imapProps();
Session session = Session.getInstance(props);
Store store = session.getStore(Constants.IMAPS);
store.connect(host,username,password);
LOG.debug("Connected, opening {}:",folderName);
inbox = (IMAPFolder)store.getFolder(folderName);
inbox.open(IMAPFolder.READ_WRITE);
while (!stopped){
handleMessages();
LOG.debug("Idling.");
inbox.idle(true);
}
}
private void handleMessages() throws MessagingException {
LOG.debug("reading email:");
if (!inbox.isOpen()){
inbox.open(IMAPFolder.READ_WRITE);
}
for (Message message : inbox.getMessages()){
if (message.isSet(Flags.Flag.SEEN)) continue;
handle(message);
message.setFlag(Flags.Flag.SEEN,true);
}
}
private void handle(Message message) throws MessagingException {
LOG.debug("Handling {}",message.getSubject());
for (MessageHandler listener : listeners) listener.onMessageReceived(message);
}
private Properties imapProps() {
Properties props = new Properties();
props.put(Constants.PROTOCOL,Constants.IMAPS);
return props;
}
}
private class Heartbeat extends Thread{
private static final Logger LOG = LoggerFactory.getLogger(Heartbeat.class);
@Override
public void run() {
while (!stopped){
try {
sleep(300034);
if (inbox != null) continue;
LOG.debug("sending NOOP");
inbox.doCommand(protocol -> {
protocol.simpleCommand("NOOP",null);
return null;
});
} catch (InterruptedException | MessagingException e) {
e.printStackTrace();
}
}
}
}
public ImapClient(JSONObject config){
this.config = config;
}
public void start() {
LOG.debug("Creating ListeningThread…");
new ListeningThread().start();
LOG.debug("Creating Heartbeat…");
new Heartbeat().start();
}
public ImapClient addListener(MessageHandler messageHandler) {
listeners.add(messageHandler);
return this;
}
}

View File

@@ -0,0 +1,8 @@
package de.srsoftware.widerhall.mail;
import javax.mail.Message;
import javax.mail.MessagingException;
public interface MessageHandler {
public void onMessageReceived(Message message) throws MessagingException;
}

View File

@@ -0,0 +1,59 @@
package de.srsoftware.widerhall.mail;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
public class SmtpClient {
private static final Logger LOG = LoggerFactory.getLogger(SmtpClient.class);
private static final String HOST = "mail.smtp.host";
private static final String PORT = "mail.smtp.port";
private static final String AUTH = "mail.smtp.auth";
private static final String SSL = "mail.smtp.ssl.enable";
private static final String UTF8 = "UTF-8";
private Session session;
public SmtpClient(Map<String,Object> config){
String host = (String) config.get("host");
long port = (long) config.get("port");
Properties props = new Properties();
props.put(HOST,host);
props.put(PORT,port);
props.put(AUTH,true);
props.put(SSL,true);
session = Session.getInstance(props);
LOG.debug("Created new {}: {}", getClass().getSimpleName(),session);
}
public void send(JSONObject config, String senderAdress, String senderName, String receivers, String subject, String content) throws MessagingException, UnsupportedEncodingException {
MimeMessage message = new MimeMessage(session);
message.addHeader("Content-Type","text/plain; charset="+UTF8);
message.addHeader("format","flowed");
message.addHeader("Content-Transfer-Encoding","8bit");
message.setFrom(new InternetAddress(senderAdress,senderName));
message.setReplyTo(InternetAddress.parse(senderAdress,false));
message.setSubject(subject,UTF8);
message.setText(content,UTF8);
message.setSentDate(new Date());
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receivers,false));
String username = (String) config.get("user");
String password = (String) config.get("password");
LOG.debug("Versende Mail…");
Transport.send(message,username,password);
LOG.debug("…versendet");
}
}