From a2eb399584f29286a7177ce707b042cc84e16fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Wed, 31 Dec 2014 17:54:15 +0100 Subject: HTTP 401 = Logged out correctly --- .../java/se/leap/bitmaskclient/ProviderAPI.java | 57 +++++++++++++++------- 1 file changed, 39 insertions(+), 18 deletions(-) (limited to 'app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index ce13d322..d63d641a 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -232,8 +232,8 @@ public class ProviderAPI extends IntentService { Bundle result = new Bundle(); int progress = 0; - String username = (String) task.get(SessionDialog.USERNAME); - String password = (String) task.get(SessionDialog.PASSWORD); + String username = task.getString(SessionDialog.USERNAME); + String password = task.getString(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { result = authenticate(username, password); broadcast_progress(progress++); @@ -243,7 +243,7 @@ public class ProviderAPI extends IntentService { result.putString(SessionDialog.USERNAME, username); result.putBoolean(SessionDialog.PASSWORD_INVALID_LENGTH, true); } - if(username.isEmpty()) { + if(!validUsername(username)) { result.putBoolean(RESULT_KEY, false); result.putBoolean(SessionDialog.USERNAME_MISSING, true); } @@ -327,21 +327,25 @@ public class ProviderAPI extends IntentService { /** * Validates parameters entered by the user to log in - * @param entered_username - * @param entered_password + * @param username + * @param password * @return true if both parameters are present and the entered password length is greater or equal to eight (8). */ - private boolean validUserLoginData(String entered_username, String entered_password) { - return !(entered_username.isEmpty()) && wellFormedPassword(entered_password); + private boolean validUserLoginData(String username, String password) { + return validUsername(username) && wellFormedPassword(password); } + private boolean validUsername(String username) { + return username != null && !username.isEmpty(); + } + /** * Validates a password - * @param entered_password + * @param password * @return true if the entered password length is greater or equal to eight (8). */ - private boolean wellFormedPassword(String entered_password) { - return entered_password.length() >= 8; + private boolean wellFormedPassword(String password) { + return password != null && password.length() >= 8; } /** @@ -826,20 +830,23 @@ public class ProviderAPI extends IntentService { return string; } - /** - * Logs out from the api url retrieved from the task. - * @return true if there were no exceptions - */ + /** + * Logs out from the api url retrieved from the task. + * @return true if there were no exceptions + */ private boolean logOut() { + String delete_url = provider_api_url + "/logout"; + + HttpsURLConnection urlConnection = null; + int responseCode = 0; + int progress = 0; try { - String delete_url = provider_api_url + "/logout"; - int progress = 0; - HttpsURLConnection urlConnection = (HttpsURLConnection)new URL(delete_url).openConnection(); + urlConnection = (HttpsURLConnection)new URL(delete_url).openConnection(); urlConnection.setRequestMethod("DELETE"); urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); - int responseCode = urlConnection.getResponseCode(); + responseCode = urlConnection.getResponseCode(); broadcast_progress(progress++); LeapSRPSession.setToken(""); Log.d(TAG, Integer.toString(responseCode)); @@ -853,6 +860,20 @@ public class ProviderAPI extends IntentService { return false; } catch (IOException e) { // TODO Auto-generated catch block + try { + if(urlConnection != null) { + responseCode = urlConnection.getResponseCode(); + if(responseCode == 401) { + broadcast_progress(progress++); + LeapSRPSession.setToken(""); + Log.d(TAG, Integer.toString(responseCode)); + return true; + } + } + } catch (IOException e1) { + e1.printStackTrace(); + } + e.printStackTrace(); return false; } catch (KeyManagementException e) { -- cgit v1.2.3 From b3ea137781af2eacd0a5b75f24030cfbc3c9d962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 13 Jan 2015 13:48:34 +0100 Subject: Reusing methods to download certificate. This makes sure that if a a certificate is not valid, a new one is downloaded. Unit tests don't apply to this, I've already added one to the certificate validator, whose result will determine if a new certificate will be downloaded. --- .../debug/java/se/leap/bitmaskclient/ProviderAPI.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java') diff --git a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java index d63d641a..1dbe11d3 100644 --- a/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java +++ b/app/src/debug/java/se/leap/bitmaskclient/ProviderAPI.java @@ -187,7 +187,7 @@ public class ProviderAPI extends IntentService { if(validUserLoginData(username, password)) { session_id_bundle = register(username, password); - broadcast_progress(progress++); + broadcastProgress(progress++); } else { if(!wellFormedPassword(password)) { session_id_bundle.putBoolean(RESULT_KEY, false); @@ -236,7 +236,7 @@ public class ProviderAPI extends IntentService { String password = task.getString(SessionDialog.PASSWORD); if(validUserLoginData(username, password)) { result = authenticate(username, password); - broadcast_progress(progress++); + broadcastProgress(progress++); } else { if(!wellFormedPassword(password)) { result.putBoolean(RESULT_KEY, false); @@ -317,7 +317,7 @@ public class ProviderAPI extends IntentService { * and sends it as a broadcast. * @param progress */ - private void broadcast_progress(int progress) { + private void broadcastProgress(int progress) { Intent intentUpdate = new Intent(); intentUpdate.setAction(UPDATE_PROGRESSBAR); intentUpdate.addCategory(Intent.CATEGORY_DEFAULT); @@ -507,16 +507,16 @@ public class ProviderAPI extends IntentService { if(!PROVIDER_JSON_DOWNLOADED) current_download = getAndSetProviderJson(last_provider_main_url, last_danger_on); if(PROVIDER_JSON_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { - broadcast_progress(progress++); + broadcastProgress(progress++); PROVIDER_JSON_DOWNLOADED = true; current_download = downloadCACert(last_danger_on); if(CA_CERT_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { - broadcast_progress(progress++); + broadcastProgress(progress++); CA_CERT_DOWNLOADED = true; current_download = getAndSetEipServiceJson(); if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { - broadcast_progress(progress++); + broadcastProgress(progress++); EIP_SERVICE_JSON_DOWNLOADED = true; } } @@ -847,7 +847,7 @@ public class ProviderAPI extends IntentService { urlConnection.setSSLSocketFactory(getProviderSSLSocketFactory()); responseCode = urlConnection.getResponseCode(); - broadcast_progress(progress++); + broadcastProgress(progress++); LeapSRPSession.setToken(""); Log.d(TAG, Integer.toString(responseCode)); } catch (ClientProtocolException e) { @@ -864,7 +864,7 @@ public class ProviderAPI extends IntentService { if(urlConnection != null) { responseCode = urlConnection.getResponseCode(); if(responseCode == 401) { - broadcast_progress(progress++); + broadcastProgress(progress++); LeapSRPSession.setToken(""); Log.d(TAG, Integer.toString(responseCode)); return true; -- cgit v1.2.3