Skip to content

Instantly share code, notes, and snippets.

@maxanier
Created Dec 21, 2016
Embed
What would you like to do?
Dirtiest solution of all time. Logs into the CampusSachsen Website
import org.apache.commons.cli.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.xml.sax.SAXException;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
private static String JSESSION;
private static String relayState="";
private static String samlResponse="";
private static String username,password;
private static CookieManager cookieManager=new CookieManager(null,CookiePolicy.ACCEPT_ALL);
public static void main(String[] args) throws IOException, URISyntaxException, ParseException {
CommandLine commandLine;
// Option optionUsername = Option.builder("u").longOpt("username").required().build();
// Option optionPassword = Option.builder("p").longOpt("password").required().build();
Options options=new Options();
CommandLineParser parser = new DefaultParser();
// options.addOption(optionPassword);
// options.addOption(optionUsername);
options.addOption("u","username",true,"Username");
options.addOption("p","password",true,"Password");
commandLine = parser.parse(options,args);
username= commandLine.getOptionValue("u");
password = commandLine.getOptionValue("p");
System.out.println("username "+ username);
System.out.println("password "+ password);
if(password==null||username==null){
System.err.println("Wir brauchen password und nutzernamen");
}
CookieHandler.setDefault(cookieManager);
String startUrl=startLogin();
System.out.println(startUrl);
System.out.println("SELECTING DRESDEN --------------------------------------------");
selectDresden(startUrl);
System.out.println("LOGIN STEP 1 -----------------------------------------------");
performLogin1();
System.out.println("LOGIN STEP 2 -----------------------------------------------");
performLogin2();
System.out.println("FINISH LOGIN -----------------------------------------------");
finishLogin();
System.out.println("CHECKING STATUS ---------------------------------------------");
preLogout();
System.out.println("LOGGING OUT ------------------------------------------------");
logout();
}
public static String startLogin() throws IOException {
String url="https://campussachsen.tu-dresden.de/o365/login.php";
URLConnection connection = new URL(url).openConnection();
InputStream response = connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Scanner scanner = new Scanner(response) ;
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
//PHPCOOKIE=connection.getHeaderField("Set-Cookie").substring(10,62);
//System.out.println("Cookie "+PHPCOOKIE+"/"+connection.getHeaderField("Set-Cookie"));
return connection.getURL().toString();
}
public static String selectDresden(String loc) throws UnsupportedEncodingException, IOException {
String charset = StandardCharsets.UTF_8.name();
String ret = "https://campussachsen.tu-dresden.de/Shibboleth.sso/Login?SAMLDS=1";
String entityID = "https://idp2.tu-dresden.de/idp/shibboleth";
String query = String.format("return=%s&entityID=%s",
URLEncoder.encode(ret, charset),
URLEncoder.encode(entityID, charset));
URLConnection connection = new URL(loc).openConnection();
connection.setDoOutput(true);//POST
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//connection.setRequestProperty("Cookie","PHPSESSID="+PHPCOOKIE);
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream response=connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Scanner scanner = new Scanner(response) ;
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
return "";
}
public static String performLogin1() throws URISyntaxException, IOException {
List<String> list=cookieManager.get(new URI("https://idp2.tu-dresden.de/idp/profile/SAML2/Redirect/SSO;"),new HashMap<>()).get("Cookie");
JSESSION=list.get(0).substring(11);
System.out.println("Cookies: "+list);
System.out.println(JSESSION);
String charset = StandardCharsets.UTF_8.name();
String event = "Login";
String query = String.format("j_username=%s&j_password=%s&_eventId_proceed=%s",
URLEncoder.encode(username, charset),
URLEncoder.encode(password, charset),
URLEncoder.encode(event,charset));
URLConnection connection = new URL("https://idp2.tu-dresden.de/idp/profile/SAML2/Redirect/SSO;jsessionid="+JSESSION+"?execution=e1s1").openConnection();
connection.setDoOutput(true);//POST
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream response=connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Scanner scanner = new Scanner(response) ;
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
return "";
}
public static String performLogin2() throws URISyntaxException, IOException {
String charset = StandardCharsets.UTF_8.name();
String event = "Accept";
String query = String.format("_shib_idp_consentIds=uid&_shib_idp_consentIds=mail&_shib_idp_consentIds=eduPersonOrgUnitDN&_shib_idp_consentIds=eduPersonScopedAffiliation&_shib_idp_consentIds=eduPersonEntitlement&_eventId_proceed=%s",
URLEncoder.encode(event,charset));
URLConnection connection = new URL("https://idp2.tu-dresden.de/idp/profile/SAML2/Redirect/SSO;jsessionid="+JSESSION+"?execution=e1s2").openConnection();
connection.setDoOutput(true);//POST
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream response=connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Scanner scanner = new Scanner(response) ;
String responseBody = scanner.useDelimiter("\\A").next();
Document doc=Jsoup.parse(responseBody);
System.out.println(responseBody);
for(Element e:doc.getElementsByTag("input")){
if("RelayState".equals(e.attr("name"))){
relayState=e.attr("value");
}
else if("SAMLResponse".equals(e.attr("name"))){
samlResponse=e.attr("value");
}
}
System.out.println("RelayState: "+relayState);
System.out.println("SAMLResponse: "+samlResponse);
return "";
}
public static String finishLogin() throws URISyntaxException, IOException {
String charset = StandardCharsets.UTF_8.name();
String query = String.format("RelayState=%s&SAMLResponse=%s",
URLEncoder.encode(relayState, charset),
URLEncoder.encode(samlResponse, charset));
URLConnection connection = new URL("https://campussachsen.tu-dresden.de/Shibboleth.sso/SAML2/POST").openConnection();
connection.setDoOutput(true);//POST
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream response=connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Scanner scanner = new Scanner(response) ;
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
return "";
}
public static void preLogout() throws IOException {
String url="https://campussachsen.tu-dresden.de/o365/login.php";
URLConnection connection = new URL(url).openConnection();
InputStream response = connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Scanner scanner = new Scanner(response) ;
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
}
public static void logout() throws IOException {
String url="https://campussachsen.tu-dresden.de/Shibboleth.sso/Logout?return=https://campussachsen.tu-dresden.de/o365/logout.php";
URLConnection connection = new URL(url).openConnection();
InputStream response = connection.getInputStream();
for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
Scanner scanner = new Scanner(response) ;
String responseBody = scanner.useDelimiter("\\A").next();
System.out.println(responseBody);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment