1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-20 04:19:08 -05:00

Fix api login error (#115)

This commit is contained in:
Mikołaj Pich 2018-05-19 23:02:54 +02:00 committed by Rafał Borcz
parent 3f54d13b6b
commit a4445a8a97
2 changed files with 23 additions and 33 deletions

View File

@ -63,7 +63,7 @@ public class Client {
private boolean isLoggedIn() { private boolean isLoggedIn() {
return getCookies().size() > 0 && lastSuccessRequest != null && return getCookies().size() > 0 && lastSuccessRequest != null &&
29 > TimeUnit.MILLISECONDS.toMinutes(new Date().getTime() - lastSuccessRequest.getTime()); 5 > TimeUnit.MILLISECONDS.toMinutes(new Date().getTime() - lastSuccessRequest.getTime());
} }
@ -142,7 +142,7 @@ public class Client {
this.cookies.addItems(response.cookies()); this.cookies.addItems(response.cookies());
response.bufferUp(); // fixes cert parsing issues response.bufferUp(); // fixes cert parsing issues #109
return checkForErrors(response.parse()); return checkForErrors(response.parse());
} }
@ -195,8 +195,8 @@ public class Client {
throw new NotLoggedInErrorException(singIn); throw new NotLoggedInErrorException(singIn);
} }
if ("Błąd strony".equals(title)) { if (title.startsWith("Błąd")) {
throw new VulcanException("Nieznany błąd"); throw new NotLoggedInErrorException(title + " " + doc.selectFirst("p, body"));
} }
return doc; return doc;

View File

@ -36,29 +36,29 @@ public class Login {
{"Password", password} {"Password", password}
}; };
String nextUrl = LOGIN_PAGE_URL; Document nextDoc = sendCredentialsData(credentials, LOGIN_PAGE_URL);
Document loginPage = client.getPageByUrl(nextUrl, false);
Element formFirst = loginPage.select("#form1").first(); Element errorMessage = nextDoc.selectFirst(".ErrorMessage, #ErrorTextLabel");
if (null != formFirst) { // on adfs login
Document formSecond = client.postPageByUrl(
formFirst.attr("abs:action"),
getFormStateParams(formFirst, "", "")
);
credentials = getFormStateParams(formSecond, email, password);
nextUrl = formSecond.select("#form1").first().attr("abs:action");
} else if (!"Logowanie".equals(loginPage.select("#h1Default").text())) {
throw new VulcanException("Expected login page, got page with title: " + loginPage.title());
}
Document html = client.postPageByUrl(nextUrl, credentials);
Element errorMessage = html.select(".ErrorMessage, #ErrorTextLabel").first();
if (null != errorMessage) { if (null != errorMessage) {
throw new BadCredentialsException(errorMessage.text()); throw new BadCredentialsException(errorMessage.text());
} }
return html; return nextDoc;
}
private Document sendCredentialsData(String[][] credentials, String nextUrl) throws IOException, VulcanException {
Element formFirst = client.getPageByUrl(nextUrl, false).selectFirst("#form1");
if (null != formFirst) { // only on adfs login
Document formSecond = client.postPageByUrl(
formFirst.attr("abs:action"),
getFormStateParams(formFirst, "", "")
);
credentials = getFormStateParams(formSecond, credentials[0][1], credentials[1][1]);
nextUrl = formSecond.selectFirst("#form1").attr("abs:action");
}
return client.postPageByUrl(nextUrl, credentials);
} }
private String[][] getFormStateParams(Element form, String email, String password) { private String[][] getFormStateParams(Element form, String email, String password) {
@ -77,13 +77,7 @@ public class Login {
} }
String sendCertificate(Document doc, String defaultSymbol) throws IOException, VulcanException { String sendCertificate(Document doc, String defaultSymbol) throws IOException, VulcanException {
String certificate = doc.select("input[name=wresult]").val(); client.setSymbol(findSymbol(defaultSymbol, doc.select("input[name=wresult]").val()));
if ("".equals(certificate)) {
throw new VulcanException("Expected certificate, got empty string. Page title: " + doc.title());
}
client.setSymbol(findSymbol(defaultSymbol, certificate));
Document targetDoc = sendCertData(doc); Document targetDoc = sendCertData(doc);
String title = targetDoc.title(); String title = targetDoc.title();
@ -106,10 +100,6 @@ public class Login {
private Document sendCertData(Document doc) throws IOException, VulcanException { private Document sendCertData(Document doc) throws IOException, VulcanException {
String url = doc.select("form[name=hiddenform]").attr("action"); String url = doc.select("form[name=hiddenform]").attr("action");
if (!doc.title().equals("Working...")) {
throw new VulcanException("Expected certificate page, got page with title: " + doc.title());
}
return client.postPageByUrl(url.replaceFirst("Default", "{symbol}"), new String[][]{ return client.postPageByUrl(url.replaceFirst("Default", "{symbol}"), new String[][]{
{"wa", "wsignin1.0"}, {"wa", "wsignin1.0"},
{"wresult", doc.select("input[name=wresult]").val()}, {"wresult", doc.select("input[name=wresult]").val()},