From 9357384b65f029675e9e6e03cf78d9585929010d Mon Sep 17 00:00:00 2001 From: Peter Hormanns Date: Fri, 6 Mar 2020 19:46:05 +0100 Subject: [PATCH] start hsadmin api --- .../jalin/ldapadmin/hsadmin/MailboxDAO.java | 95 +++++++++++++++++++ .../ldapadmin/hsadmin/MailboxNotFound.java | 7 ++ .../ldapadmin/hsadmin/TicketProvider.java | 93 ++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java create mode 100644 src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java create mode 100644 src/main/java/de/jalin/ldapadmin/hsadmin/TicketProvider.java diff --git a/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java new file mode 100644 index 0000000..18c60db --- /dev/null +++ b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxDAO.java @@ -0,0 +1,95 @@ +package de.jalin.ldapadmin.hsadmin; + +import java.io.IOException; +import java.io.Serializable; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClient; +import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; + +public class MailboxDAO { + + private final TicketProvider ticketBox; + private final XmlRpcClient rpcClient; + + public MailboxDAO(final TicketProvider ticketBox) throws IOException { + this.ticketBox = ticketBox; + final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); + config.setServerURL(new URL("https://config.hostsharing.net:443/hsar/xmlrpc/hsadmin")); + config.setEnabledForExtensions(true); + this.rpcClient = new XmlRpcClient(); + this.rpcClient.setConfig(config); + } + + public void assertMailboxExists(final String emailAddress, final String comment) throws IOException, XmlRpcException { + final String[] parts = emailAddress.split("@"); + if (parts.length == 2) { + final String localpart = parts[0]; + final String domain = parts[1]; + final String pac = ticketBox.getAdminLogin(); + try { + getMailbox(pac, localpart); + } catch (MailboxNotFound e) { + try { + createMailbox(pac, localpart, comment); + } catch (MailboxNotFound e1) { + + } + } + } + } + + private String getMailbox(String pac, String localpart) throws IOException, XmlRpcException, MailboxNotFound { + final List xmlRpcParamsList = new ArrayList(); + xmlRpcParamsList.add(pac); + xmlRpcParamsList.add(ticketBox.getTicket()); + final HashMap whereParamsMap = new HashMap(); + xmlRpcParamsList.add(whereParamsMap); + whereParamsMap.put("name", pac + "-" + localpart); + final Object[] rpcResult = (Object[])rpcClient.execute("user.search", xmlRpcParamsList); + if (rpcResult.length == 1) { + Map userAsMap = (Map) rpcResult[0]; + return (String) userAsMap.get("name"); + } + throw new MailboxNotFound(); + } + + private void createMailbox(String pac, String localpart, String comment) throws IOException, XmlRpcException, MailboxNotFound { + final List xmlRpcParamsList = new ArrayList(); + xmlRpcParamsList.add(pac); + xmlRpcParamsList.add(ticketBox.getTicket()); + final HashMap setParamsMap = new HashMap(); + xmlRpcParamsList.add(setParamsMap); + setParamsMap.put("name", pac + "-" + localpart); + setParamsMap.put("password", pwGen()); + setParamsMap.put("comment", comment); + final Object rpcResult = rpcClient.execute("user.add", xmlRpcParamsList); + } + + private String pwGen() { + final String pwCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.,_:;?!#+&%$="; + final StringBuffer buffer = new StringBuffer(); + for (int loop=0; loop<64; loop++) { + final int length = pwCharacters.length() - 1; + double rand = Math.random() * length; + int idx = (new Double(rand)).intValue(); + buffer.append(pwCharacters.charAt(idx)); + } + return buffer.toString(); + } + + public static void main(String[] args) throws XmlRpcException { + try { + MailboxDAO mailboxDAO = new MailboxDAO(new TicketProvider("peh00", "SMid13A+liT")); + mailboxDAO.assertMailboxExists("peter.hormanns@ostwall195.de", "Peter Hormanns"); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java new file mode 100644 index 0000000..6a87205 --- /dev/null +++ b/src/main/java/de/jalin/ldapadmin/hsadmin/MailboxNotFound.java @@ -0,0 +1,7 @@ +package de.jalin.ldapadmin.hsadmin; + +public class MailboxNotFound extends Exception { + + private static final long serialVersionUID = 1L; + +} diff --git a/src/main/java/de/jalin/ldapadmin/hsadmin/TicketProvider.java b/src/main/java/de/jalin/ldapadmin/hsadmin/TicketProvider.java new file mode 100644 index 0000000..468a321 --- /dev/null +++ b/src/main/java/de/jalin/ldapadmin/hsadmin/TicketProvider.java @@ -0,0 +1,93 @@ +package de.jalin.ldapadmin.hsadmin; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.net.URLEncoder; + +import javax.net.ssl.HttpsURLConnection; + +public class TicketProvider { + + private final String adminLogin; + private final String adminPassword; + + private String grantingTicket = null; + + public TicketProvider(final String login, final String password) { + this.adminLogin = login; + this.adminPassword = password; + } + + public String getAdminLogin() { + return adminLogin; + } + + public String getTicket() throws IOException { + if (grantingTicket == null) { + grantingTicket = getGrantingTicket(); + } + String ticket = null; + while (ticket == null) { + final String serviceParam = "service=" + URLEncoder.encode("https://config.hostsharing.net:443/hsar/backend", "UTF-8"); + final URL url = new URL(grantingTicket); + final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setUseCaches(false); + connection.setAllowUserInteraction(false); + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); + writer.write(serviceParam); + writer.close(); + connection.connect(); + int httpResponseCode = connection.getResponseCode(); + if (200 == httpResponseCode) { + final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + ticket = reader.readLine(); + String readLine = reader.readLine(); + do { + readLine = reader.readLine(); + } while (readLine != null); + } + if (httpResponseCode >= 400) { + grantingTicket = getGrantingTicket(); + } + } + return ticket; + } + + private String getGrantingTicket() throws IOException { + final String userParam = "username=" + URLEncoder.encode(adminLogin, "UTF-8"); + final String passwordParam = "password=" + URLEncoder.encode(adminPassword, "UTF-8"); + final String encodedData = userParam + "&" + passwordParam; + final URL url = new URL("https://login.hostsharing.net/cas/v1/tickets"); + final HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); + connection.setDoInput(true); + connection.setDoOutput(true); + connection.setUseCaches(false); + connection.setAllowUserInteraction(false); + final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream())); + writer.write(encodedData); + writer.close(); + connection.connect(); + return connection.getHeaderField("Location"); + } + + public static void main(String[] args) { + try { + final TicketProvider provider = new TicketProvider("peh00", "SMid13A+liT"); + String ticket = provider.getTicket(); + System.out.println(ticket); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +}