From a4445a8a9738d02808f8b21638a508354c30a67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 19 May 2018 23:02:54 +0200 Subject: [PATCH] Fix api login error (#115) --- .../java/io/github/wulkanowy/api/Client.java | 8 ++-- .../io/github/wulkanowy/api/login/Login.java | 48 ++++++++----------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/api/src/main/java/io/github/wulkanowy/api/Client.java b/api/src/main/java/io/github/wulkanowy/api/Client.java index 34e71398b..53300788a 100644 --- a/api/src/main/java/io/github/wulkanowy/api/Client.java +++ b/api/src/main/java/io/github/wulkanowy/api/Client.java @@ -63,7 +63,7 @@ public class Client { private boolean isLoggedIn() { 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()); - response.bufferUp(); // fixes cert parsing issues + response.bufferUp(); // fixes cert parsing issues #109 return checkForErrors(response.parse()); } @@ -195,8 +195,8 @@ public class Client { throw new NotLoggedInErrorException(singIn); } - if ("Błąd strony".equals(title)) { - throw new VulcanException("Nieznany błąd"); + if (title.startsWith("Błąd")) { + throw new NotLoggedInErrorException(title + " " + doc.selectFirst("p, body")); } return doc; diff --git a/api/src/main/java/io/github/wulkanowy/api/login/Login.java b/api/src/main/java/io/github/wulkanowy/api/login/Login.java index 3ecfb7b6c..a734501ea 100644 --- a/api/src/main/java/io/github/wulkanowy/api/login/Login.java +++ b/api/src/main/java/io/github/wulkanowy/api/login/Login.java @@ -36,29 +36,29 @@ public class Login { {"Password", password} }; - String nextUrl = LOGIN_PAGE_URL; - Document loginPage = client.getPageByUrl(nextUrl, false); + Document nextDoc = sendCredentialsData(credentials, LOGIN_PAGE_URL); - Element formFirst = loginPage.select("#form1").first(); - 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(); + Element errorMessage = nextDoc.selectFirst(".ErrorMessage, #ErrorTextLabel"); if (null != errorMessage) { 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) { @@ -77,13 +77,7 @@ public class Login { } String sendCertificate(Document doc, String defaultSymbol) throws IOException, VulcanException { - String certificate = 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)); + client.setSymbol(findSymbol(defaultSymbol, doc.select("input[name=wresult]").val())); Document targetDoc = sendCertData(doc); String title = targetDoc.title(); @@ -106,10 +100,6 @@ public class Login { private Document sendCertData(Document doc) throws IOException, VulcanException { 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[][]{ {"wa", "wsignin1.0"}, {"wresult", doc.select("input[name=wresult]").val()},