2010-10-01 21:42:49 +02:00
|
|
|
package de.hsadmin.remote;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|