hs.hsadmin/hsarback/test/de/hsadmin/remote/RemoteCASHelper.java

111 lines
3.3 KiB
Java
Raw Normal View History

2010-10-01 21:42:49 +02:00
package de.hsadmin.remote;
2012-06-12 15:27:10 +02:00
import static org.junit.Assert.fail;
2010-10-01 21:42:49 +02:00
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
2012-06-12 15:27:10 +02:00
import java.util.HashMap;
import java.util.Map;
2010-10-01 21:42:49 +02:00
import javax.net.ssl.HttpsURLConnection;
2012-06-12 15:27:10 +02:00
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
2010-10-05 21:42:07 +02:00
import de.hsadmin.core.util.Config;
2010-10-01 21:42:49 +02:00
public class RemoteCASHelper {
private static String LOGIN_URL = "https://login.hostsharing.net:443/cas/v1/tickets";
private String loginURL;
public RemoteCASHelper() {
initConfig();
}
private void initConfig() {
2010-10-05 21:42:07 +02:00
loginURL = Config.getInstance().getProperty("loginURL", LOGIN_URL);
2010-10-01 21:42:49 +02:00
}
2010-10-04 19:44:49 +02:00
public String getGrantingTicketURL(String user) {
2010-10-05 21:42:07 +02:00
String pw = Config.getInstance().getProperty(user + ".passWord", "-");
2010-10-01 21:42:49 +02:00
try {
String encodedParams = URLEncoder.encode("username", "UTF-8")
+ "=" + URLEncoder.encode(user, "UTF-8")
+ "&" + URLEncoder.encode("password", "UTF-8")
+ "=" + URLEncoder.encode(pw, "UTF-8");
return doHttpPost(loginURL, encodedParams);
} catch (UnsupportedEncodingException e) {
System.err.println(e.getMessage());
return null;
}
}
public String getServiceTicket(String location, String service) {
try {
String encodedParams = URLEncoder.encode("service", "UTF-8")
+ "=" + URLEncoder.encode(service, "UTF-8");
return doHttpPost(location, encodedParams);
} catch (UnsupportedEncodingException e) {
System.err.println(e.getMessage());
return null;
}
}
private String doHttpPost(String urlString, String encodedParams) {
String result = null;
try {
URL url = new URL(urlString);
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);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write(encodedParams);
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String ticket = reader.readLine();
String readLine = null;
do {
readLine = reader.readLine();
} while (readLine != null);
result = connection.getHeaderField("Location");
if (ticket != null && ticket.startsWith("ST-")) {
result = ticket;
}
} catch (IOException e) {
System.err.println(e.getMessage());
return null;
}
return result;
}
2012-06-12 15:27:10 +02:00
public void setPassword(XmlRpcClient client, String user) {
try {
String admin = "ad";
String grantingTicketURL = getGrantingTicketURL(admin);
Map<String, String> whereParams = new HashMap<String, String>();
Map<String, String> setParams = new HashMap<String, String>();
whereParams.put("name", user);
setParams.put("password", "test123");
Object[] params = new Object[] { user,
getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()),
setParams, whereParams };
client.execute("user.update", params);
} catch (XmlRpcException e) {
fail(e.getMessage());
}
}
2010-10-01 21:42:49 +02:00
}