summaryrefslogtreecommitdiff
path: root/src/se/leap/bitmaskclient/ProviderAPI.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/se/leap/bitmaskclient/ProviderAPI.java')
-rw-r--r--src/se/leap/bitmaskclient/ProviderAPI.java162
1 files changed, 130 insertions, 32 deletions
diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java
index cc94edff..ff6dd852 100644
--- a/src/se/leap/bitmaskclient/ProviderAPI.java
+++ b/src/se/leap/bitmaskclient/ProviderAPI.java
@@ -59,7 +59,6 @@ import org.apache.http.client.ClientProtocolException;
import org.jboss.security.srp.SRPParameters;
import org.json.JSONException;
import org.json.JSONObject;
-import org.json.JSONStringer;
import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.ProviderListContent.ProviderItem;
@@ -70,7 +69,7 @@ import android.os.Handler;
import android.os.ResultReceiver;
import android.util.Base64;
import android.util.Log;
-import android.widget.Toast;
+
/**
* Implements HTTP api methods used to manage communications with the provider server.
@@ -98,8 +97,8 @@ public class ProviderAPI extends IntentService {
SESSION_ID_COOKIE_KEY = "session_id_cookie_key",
SESSION_ID_KEY = "session_id",
ERRORS = "errors",
- UPDATE_PROGRESSBAR = "update_action",
- CURRENT_PROGRESS = "update data",
+ UPDATE_PROGRESSBAR = "update_progressbar",
+ CURRENT_PROGRESS = "current_progress",
TAG = "provider_api_tag"
;
@@ -119,6 +118,15 @@ public class ProviderAPI extends IntentService {
INCORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 14
;
+ private static boolean
+ CA_CERT_DOWNLOADED = false,
+ PROVIDER_JSON_DOWNLOADED = false,
+ EIP_SERVICE_JSON_DOWNLOADED = false
+ ;
+
+ private static String last_provider_main_url;
+ private static boolean last_danger_on = false;
+
public ProviderAPI() {
super("ProviderAPI");
Log.v("ClassName", "Provider API");
@@ -131,22 +139,30 @@ public class ProviderAPI extends IntentService {
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER) );
}
+ public static String lastProviderMainUrl() {
+ return last_provider_main_url;
+ }
+
+ public static boolean lastDangerOn() {
+ return last_danger_on;
+ }
+
private String formatErrorMessage(final int toast_string_id) {
return "{ \"" + ERRORS + "\" : \""+getResources().getString(toast_string_id)+"\" }";
}
@Override
protected void onHandleIntent(Intent command) {
- final ResultReceiver receiver = command.getParcelableExtra("receiver");
+ final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY);
String action = command.getAction();
Bundle parameters = command.getBundleExtra(PARAMETERS);
if(action.equalsIgnoreCase(SET_UP_PROVIDER)) {
Bundle result = setUpProvider(parameters);
if(result.getBoolean(RESULT_KEY)) {
- receiver.send(PROVIDER_OK, result);
+ receiver.send(PROVIDER_OK, Bundle.EMPTY);
} else {
- receiver.send(PROVIDER_NOK, Bundle.EMPTY);
+ receiver.send(PROVIDER_NOK, result);
}
} else if (action.equalsIgnoreCase(SRP_AUTH)) {
Bundle session_id_bundle = authenticateBySRP(parameters);
@@ -418,38 +434,85 @@ public class ProviderAPI extends IntentService {
return result.toString();
}
+
+
+
+
/**
* Downloads a provider.json from a given URL, adding a new provider using the given name.
* @param task containing a boolean meaning if the provider is custom or not, another boolean meaning if the user completely trusts this provider, the provider name and its provider.json url.
* @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if the update was successful.
*/
private Bundle setUpProvider(Bundle task) {
- Bundle result = new Bundle();
+//<<<<<<< HEAD
+// Bundle result = new Bundle();
+// int progress = 0;
+// boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON);
+// String provider_main_url = task.getString(Provider.MAIN_URL);
+// if(downloadCACert(provider_main_url, danger_on)) {
+// broadcast_progress(progress++);
+// result.putBoolean(RESULT_KEY, true);
+// if(getAndSetProviderJson(provider_main_url)) {
+// broadcast_progress(progress++);
+// if(getAndSetEipServiceJson())
+// broadcast_progress(progress++);
+// }
+// }
+// return result;
+// }
+//
+//
+//=======
int progress = 0;
- boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON);
- String provider_main_url = task.getString(Provider.MAIN_URL);
- if(downloadCACert(provider_main_url, danger_on)) {
+ Bundle current_download = new Bundle();
+
+ if(task != null && task.containsKey(ProviderItem.DANGER_ON) && task.containsKey(Provider.MAIN_URL)) {
+ last_danger_on = task.getBoolean(ProviderItem.DANGER_ON);
+ last_provider_main_url = task.getString(Provider.MAIN_URL);
+ CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false;
+ }
+
+ if(!CA_CERT_DOWNLOADED)
+ current_download = downloadCACert(last_provider_main_url, last_danger_on);
+ if(CA_CERT_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) {
broadcast_progress(progress++);
- result.putBoolean(RESULT_KEY, true);
- if(getAndSetProviderJson(provider_main_url)) {
+ CA_CERT_DOWNLOADED = true;
+ if(!PROVIDER_JSON_DOWNLOADED)
+ current_download = getAndSetProviderJson(last_provider_main_url);
+ if(PROVIDER_JSON_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) {
broadcast_progress(progress++);
- if(getAndSetEipServiceJson())
+ PROVIDER_JSON_DOWNLOADED = true;
+ current_download = getAndSetEipServiceJson();
+ if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) {
broadcast_progress(progress++);
+ EIP_SERVICE_JSON_DOWNLOADED = true;
+ }
}
}
- return result;
+
+ return current_download;
}
- private boolean downloadCACert(String provider_main_url, boolean danger_on) {
+ private Bundle downloadCACert(String provider_main_url, boolean danger_on) {
+ Bundle result = new Bundle();
String cert_string = downloadWithCommercialCA(provider_main_url + "/ca.crt", danger_on);
- if(validCertificate(cert_string))
+ if(validCertificate(cert_string)) {
ConfigHelper.saveSharedPref(Provider.CA_CERT, cert_string);
- else
- return false;
+ result.putBoolean(RESULT_KEY, true);
+ } else {
+ String reason_to_fail = pickErrorMessage(cert_string);
+ result.putString(ERRORS, reason_to_fail);
+ result.putBoolean(RESULT_KEY, false);
+ }
- return true;
+ return result;
}
+
+ public static boolean caCertDownloaded() {
+ return CA_CERT_DOWNLOADED;
+ }
+
private boolean validCertificate(String cert_string) {
boolean result = false;
if(!ConfigHelper.checkErroneousDownload(cert_string)) {
@@ -465,8 +528,8 @@ public class ProviderAPI extends IntentService {
return result;
}
- private boolean getAndSetProviderJson(String provider_main_url) {
- boolean result = false;
+ private Bundle getAndSetProviderJson(String provider_main_url) {
+ Bundle result = new Bundle();
String provider_dot_json_string = downloadWithProviderCA(provider_main_url + "/provider.json", true);
@@ -477,31 +540,67 @@ public class ProviderAPI extends IntentService {
ConfigHelper.saveSharedPref(Provider.KEY, provider_json);
ConfigHelper.saveSharedPref(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON));
- ProviderItem added_provider = new ProviderItem(name, provider_main_url);
-
- result = true;
+
+ result.putBoolean(RESULT_KEY, true);
} catch (JSONException e) {
+ //TODO Error message should be contained in that provider_dot_json_string
+ String reason_to_fail = pickErrorMessage(provider_dot_json_string);
+ result.putString(ERRORS, reason_to_fail);
+ result.putBoolean(RESULT_KEY, false);
}
return result;
}
+
- private boolean getAndSetEipServiceJson() {
- boolean result = false;
+ public static boolean providerJsonDownloaded() {
+ return PROVIDER_JSON_DOWNLOADED;
+ }
+ private Bundle getAndSetEipServiceJson() {
+ Bundle result = new Bundle();
+ String eip_service_json_string = "";
try {
JSONObject provider_json = ConfigHelper.getJsonFromSharedPref(Provider.KEY);
String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH;
- String eip_service_json_string = downloadWithProviderCA(eip_service_url, true);
+ eip_service_json_string = downloadWithProviderCA(eip_service_url, true);
JSONObject eip_service_json = new JSONObject(eip_service_json_string);
eip_service_json.getInt(Provider.API_RETURN_SERIAL);
ConfigHelper.saveSharedPref(EIP.KEY, eip_service_json);
- result = true;
+
+ result.putBoolean(RESULT_KEY, true);
} catch (JSONException e) {
+ String reason_to_fail = pickErrorMessage(eip_service_json_string);
+ result.putString(ERRORS, reason_to_fail);
+ result.putBoolean(RESULT_KEY, false);
}
return result;
}
+
+ public static boolean eipServiceDownloaded() {
+ return EIP_SERVICE_JSON_DOWNLOADED;
+ }
+
+ /**
+ * Interprets the error message as a JSON object and extract the "errors" keyword pair.
+ * If the error message is not a JSON object, then it is returned untouched.
+ * @param string_json_error_message
+ * @return final error message
+ */
+ private String pickErrorMessage(String string_json_error_message) {
+ String error_message = "";
+ try {
+ JSONObject json_error_message = new JSONObject(string_json_error_message);
+ error_message = json_error_message.getString(ERRORS);
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ error_message = string_json_error_message;
+ }
+
+ return error_message;
+ }
+
/**
* Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider.
*
@@ -723,10 +822,9 @@ public class ProviderAPI extends IntentService {
URL new_cert_string_url = new URL(provider_main_url + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.CERTIFICATE);
boolean danger_on = ConfigHelper.getBoolFromSharedPref(ProviderItem.DANGER_ON);
-//<<<<<<< HEAD
-// String cert_string = downloadWithProviderCA(new_cert_string_url, true);
-//=======
+
String cert_string = downloadWithProviderCA(new_cert_string_url.toString(), danger_on);
+
if(!cert_string.isEmpty()) {
if(ConfigHelper.checkErroneousDownload(cert_string)) {
String reason_to_fail = provider_json.getString(ERRORS);