Thastertyn 4464cd2840 LoginDialog and source target
Source target changed to 1.8 to allow a few things
2023-02-23 16:18:21 +01:00

122 lines
2.8 KiB
Java

package xyz.thastertyn.Jecna;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
public class Login {
private String Jsessionid = null;
// Check for session expiration
private long start;
private long lastCheck;
/**
* Zkontroluje zda jiz neexistuji ulozene udaje na systemu
* @return <ul>
* <li> {@code true} soubor existuje a neni prazdny </li>
* <li> {@code false} neexistuje, nebo jsou prazdne </li>
* </ul>
*/
public boolean getCredentials()
{
File credentials = null;
if(System.getProperty("os.name").equals("Linux"))
{
credentials = new File("~/.local/share/jecnak/credentials.json");
}else if(System.getProperty("os.name").contains("Windows"))
{
credentials = new File(System.getenv("APPDATA\\jecnak\\"));
}
if(!credentials.exists() || credentials.length() == 0)
{
return false;
}
return true;
}
public void saveCredentials(String user, String pass)
{
File credentials = null;
if(System.getProperty("os.name").equals("Linux"))
{
credentials = new File("~/.local/share/jecnak/credentials.json");
}else if(System.getProperty("os.name").contains("Windows"))
{
credentials = new File(System.getenv("APPDATA\\jecnak\\"));
}
if(!credentials.exists())
{
}
}
public String getJSESSIONID()
{
return Jsessionid;
}
public byte login(String user, String pass)
{
try {
//#region JSESSIONID
Connection.Response response = Jsoup.connect("https://www.spsejecna.cz")
.header("Connection", "keep-alive")
.method(Method.HEAD)
.execute();
Jsessionid = response.cookie("JSESSIONID");
//#endregion
//#region Token3
String token3 = Jsoup.connect("https://www.spsejecna.cz/user/role?role=student")
.header("Connection", "keep-alive")
.cookie("JSESSIONID", Jsessionid)
.get()
.select("input[name=token3]")
.attr("value");
//#endregion
//#region Login
Connection.Response login = Jsoup.connect("https://www.spsejecna.cz/user/login")
.method(Connection.Method.POST)
.header("Content-Type", "application/x-www-form-urlencoded")
//.header("Content-Length", "71") Adds 10 seconds to total request time
.header("Origin", "https://www.spsejecna.cz")
.header("Connection", "keep-alive")
.cookie("JSESSIONID", Jsessionid)
.cookie("role", "student")
.data("token3", token3)
.data("user", user)
.data("pass", pass)
.data("submit", "P%C5%99ihl%C3%A1sit+se")
.followRedirects(true)
.execute();
//#endregion
start = System.currentTimeMillis() / 1000L;
lastCheck = start;
return 0;
} catch(UnknownHostException e)
{
// Not connected to internet
return 127;
} catch(IOException e)
{
return 126;
}
}
}