start hsadmin api

This commit is contained in:
Peter Hormanns 2020-03-06 19:46:05 +01:00
parent 0901802b61
commit 9357384b65
3 changed files with 195 additions and 0 deletions

View File

@ -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<Serializable> xmlRpcParamsList = new ArrayList<Serializable>();
xmlRpcParamsList.add(pac);
xmlRpcParamsList.add(ticketBox.getTicket());
final HashMap<String, Serializable> whereParamsMap = new HashMap<String, Serializable>();
xmlRpcParamsList.add(whereParamsMap);
whereParamsMap.put("name", pac + "-" + localpart);
final Object[] rpcResult = (Object[])rpcClient.execute("user.search", xmlRpcParamsList);
if (rpcResult.length == 1) {
Map<String, Object> userAsMap = (Map<String, Object>) 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<Serializable> xmlRpcParamsList = new ArrayList<Serializable>();
xmlRpcParamsList.add(pac);
xmlRpcParamsList.add(ticketBox.getTicket());
final HashMap<String, Serializable> setParamsMap = new HashMap<String, Serializable>();
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();
}
}
}

View File

@ -0,0 +1,7 @@
package de.jalin.ldapadmin.hsadmin;
public class MailboxNotFound extends Exception {
private static final long serialVersionUID = 1L;
}

View File

@ -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();
}
}
}