/** * Check if the user has been logged in when he retrieved the data. * * @param page * @return <code>true</code> if user is logged in, <code>false</code> otherwise */ public static boolean getLoginStatus(final String page) { if (StringUtils.isBlank(page)) { Log.e("Login.checkLogin: No page given"); return false; } setActualStatus(cgeoapplication.getInstance().getString(R.string.init_login_popup_ok)); // on every page except login page setActualLoginStatus(BaseUtils.matches(page, GCConstants.PATTERN_LOGIN_NAME)); if (isActualLoginStatus()) { setActualUserName(BaseUtils.getMatch(page, GCConstants.PATTERN_LOGIN_NAME, true, "???")); setActualCachesFound( Integer.parseInt( BaseUtils.getMatch(page, GCConstants.PATTERN_CACHES_FOUND, true, "0") .replaceAll("[,.]", ""))); Settings.setMemberStatus( BaseUtils.getMatch(page, GCConstants.PATTERN_MEMBER_STATUS, true, null)); if (page.contains(GCConstants.MEMBER_STATUS_RENEW)) { Settings.setMemberStatus(GCConstants.MEMBER_STATUS_PM); } return true; } // login page setActualLoginStatus(BaseUtils.matches(page, GCConstants.PATTERN_LOGIN_NAME_LOGIN_PAGE)); if (isActualLoginStatus()) { setActualUserName(Settings.getUsername()); // number of caches found is not part of this page return true; } setActualStatus(cgeoapplication.getInstance().getString(R.string.init_login_popup_failed)); return false; }
public static StatusCode logout() { HttpResponse logoutResponse = Network.getRequest( "https://www.geocaching.com/login/default.aspx?RESET=Y&redir=http%3a%2f%2fwww.geocaching.com%2fdefault.aspx%3f"); String logoutData = Network.getResponseData(logoutResponse); if (logoutResponse != null && logoutResponse.getStatusLine().getStatusCode() == 503 && BaseUtils.matches(logoutData, GCConstants.PATTERN_MAINTENANCE)) { return StatusCode.MAINTENANCE; } Cookies.clearCookies(); Settings.setCookieStore(null); return StatusCode.NO_ERROR; }
private static StatusCode login(boolean retry) { final ImmutablePair<String, String> login = Settings.getLogin(); if (login == null || StringUtils.isEmpty(login.left) || StringUtils.isEmpty(login.right)) { Login.setActualStatus(cgeoapplication.getInstance().getString(R.string.err_login)); Log.e("Login.login: No login information stored"); return StatusCode.NO_LOGIN_INFO_STORED; } Login.setActualStatus( cgeoapplication.getInstance().getString(R.string.init_login_popup_working)); HttpResponse loginResponse = Network.getRequest("https://www.geocaching.com/login/default.aspx"); String loginData = Network.getResponseData(loginResponse); if (loginResponse != null && loginResponse.getStatusLine().getStatusCode() == 503 && BaseUtils.matches(loginData, GCConstants.PATTERN_MAINTENANCE)) { return StatusCode.MAINTENANCE; } if (StringUtils.isBlank(loginData)) { Log.e("Login.login: Failed to retrieve login page (1st)"); return StatusCode.CONNECTION_FAILED; // no loginpage } if (Login.getLoginStatus(loginData)) { Log.i( "Already logged in Geocaching.com as " + login.left + " (" + Settings.getMemberStatus() + ')'); Login.switchToEnglish(loginData); return StatusCode.NO_ERROR; // logged in } Cookies.clearCookies(); Settings.setCookieStore(null); final Parameters params = new Parameters( "__EVENTTARGET", "", "__EVENTARGUMENT", "", "ctl00$ContentBody$tbUsername", login.left, "ctl00$ContentBody$tbPassword", login.right, "ctl00$ContentBody$cbRememberMe", "on", "ctl00$ContentBody$btnSignIn", "Login"); final String[] viewstates = Login.getViewstates(loginData); if (isEmpty(viewstates)) { Log.e("Login.login: Failed to find viewstates"); return StatusCode.LOGIN_PARSE_ERROR; // no viewstates } Login.putViewstates(params, viewstates); loginResponse = Network.postRequest("https://www.geocaching.com/login/default.aspx", params); loginData = Network.getResponseData(loginResponse); if (StringUtils.isBlank(loginData)) { Log.e("Login.login: Failed to retrieve login page (2nd)"); // FIXME: should it be CONNECTION_FAILED to match the first attempt? return StatusCode.COMMUNICATION_ERROR; // no login page } if (Login.getLoginStatus(loginData)) { Log.i( "Successfully logged in Geocaching.com as " + login.left + " (" + Settings.getMemberStatus() + ')'); Login.switchToEnglish(loginData); Settings.setCookieStore(Cookies.dumpCookieStore()); return StatusCode.NO_ERROR; // logged in } if (loginData.contains("Your username/password combination does not match.")) { Log.i( "Failed to log in Geocaching.com as " + login.left + " because of wrong username/password"); return StatusCode.WRONG_LOGIN_DATA; // wrong login } Log.i("Failed to log in Geocaching.com as " + login.left + " for some unknown reason"); if (retry) { Login.switchToEnglish(loginData); return login(false); } return StatusCode.UNKNOWN_ERROR; // can't login }