summaryrefslogtreecommitdiff
path: root/src/se/leap/leapclient/ProviderAPI.java
diff options
context:
space:
mode:
authorParménides GV <parmegv@sdf.org>2013-04-15 16:30:13 +0200
committerParménides GV <parmegv@sdf.org>2013-04-15 16:30:13 +0200
commitbb4e2e8f16f79c59806a017d8f37e2bdd10a4e87 (patch)
tree6e1c13f8b23774277e427953215143fa7d51dbb5 /src/se/leap/leapclient/ProviderAPI.java
parent60e5f181c1a9f4f1851aafd059971ba5d05748f3 (diff)
parent5f66e5765f52aab1907939018e40578e939dbc5f (diff)
Merge branch 'wizard' into feature/wizard
Conflicts: src/se/leap/leapclient/ConfigHelper.java src/se/leap/leapclient/ConfigurationWizard.java src/se/leap/leapclient/ProviderAPI.java
Diffstat (limited to 'src/se/leap/leapclient/ProviderAPI.java')
-rw-r--r--src/se/leap/leapclient/ProviderAPI.java112
1 files changed, 80 insertions, 32 deletions
diff --git a/src/se/leap/leapclient/ProviderAPI.java b/src/se/leap/leapclient/ProviderAPI.java
index ea1410ad..6b09eb9d 100644
--- a/src/se/leap/leapclient/ProviderAPI.java
+++ b/src/se/leap/leapclient/ProviderAPI.java
@@ -4,6 +4,8 @@ import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.List;
+import java.net.MalformedURLException;
+import java.net.URL;
import java.util.Scanner;
import org.apache.http.HttpEntity;
@@ -17,7 +19,6 @@ import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
-import org.jboss.security.srp.SRPClientSession;
import org.jboss.security.srp.SRPParameters;
import org.json.JSONException;
import org.json.JSONObject;
@@ -42,43 +43,15 @@ public class ProviderAPI extends IntentService {
final ResultReceiver receiver = task_for.getParcelableExtra("receiver");
Bundle task;
- System.out.println("onHandleIntent called");
if((task = task_for.getBundleExtra(ConfigHelper.downloadJsonFilesBundleExtra)) != null) {
if(!downloadJsonFiles(task))
receiver.send(ConfigHelper.INCORRECTLY_DOWNLOADED_JSON_FILES, Bundle.EMPTY);
else
receiver.send(ConfigHelper.CORRECTLY_DOWNLOADED_JSON_FILES, Bundle.EMPTY);
- }
- else if ((task = task_for.getBundleExtra(ConfigHelper.downloadNewProviderDotJSON)) != null) {
- boolean custom = true;
- String provider_main_url = (String) task.get(ConfigHelper.provider_key_url);
- String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_");
- String provider_json_url = guessURL(provider_main_url);
- try {
- JSONObject provider_json = getJSONFromProvider(provider_json_url);
- String filename = provider_name + "_provider.json".replaceFirst("__", "_");
- ConfigHelper.saveFile(filename, provider_json.toString());
- ProviderListContent.addItem(new ProviderItem(provider_name, ConfigHelper.openFileInputStream(filename), custom));
- receiver.send(ConfigHelper.CUSTOM_PROVIDER_ADDED, Bundle.EMPTY);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- else if ((task = task_for.getBundleExtra(ConfigHelper.srpRegister)) != null) {
- if(!registerWithSRP(task))
- receiver.send(ConfigHelper.SRP_REGISTRATION_FAILED, Bundle.EMPTY);
- else
- receiver.send(ConfigHelper.SRP_REGISTRATION_SUCCESSFUL, Bundle.EMPTY);
- }
- else if ((task = task_for.getBundleExtra(ConfigHelper.srpAuth)) != null) {
- if(!authenticateBySRP(task))
- receiver.send(ConfigHelper.SRP_AUTHENTICATION_FAILED, Bundle.EMPTY);
+ if(downloadJsonFilesBundleExtra(task))
+ receiver.send(ConfigHelper.CORRECTLY_DOWNLOADED_JSON_FILES, Bundle.EMPTY);
else
- receiver.send(ConfigHelper.SRP_AUTHENTICATION_SUCCESSFUL, Bundle.EMPTY);
+ receiver.send(ConfigHelper.INCORRECTLY_DOWNLOADED_JSON_FILES, Bundle.EMPTY);
}
}
@@ -202,6 +175,81 @@ public class ProviderAPI extends IntentService {
return json_response.getString("M2").getBytes();
}
+ private boolean downloadNewProviderDotJSON(Bundle task) {
+ boolean custom = true;
+ String provider_main_url = (String) task.get(ConfigHelper.provider_key_url);
+ String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_");
+ String provider_json_url = guessURL(provider_main_url);
+ JSONObject provider_json = null;
+ try {
+ provider_json = getJSONFromProvider(provider_json_url);
+ } catch (IOException e) {
+ // It could happen that an https site used a certificate not trusted.
+ provider_json = downloadNewProviderDotJsonWithoutCert(provider_json_url);
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return false;
+ }
+
+ if(provider_json == null) {
+ return false;
+ } else {
+ String filename = provider_name + "_provider.json".replaceFirst("__", "_");
+ ConfigHelper.saveFile(filename, provider_json.toString());
+
+ ProviderListContent.addItem(new ProviderItem(provider_name, ConfigHelper.openFileInputStream(filename), custom));
+ return true;
+ }
+ }
+
+ private boolean downloadJsonFilesBundleExtra(Bundle task) {
+ String provider_name = (String) task.get(ConfigHelper.provider_key);
+ String cert_url = (String) task.get(ConfigHelper.cert_key);
+ String eip_service_json_url = (String) task.get(ConfigHelper.eip_service_key);
+ try {
+ //JSONObject provider_json = new JSONObject("{ \"provider\" : \"" + provider_name + "\"}");
+ //ConfigHelper.saveSharedPref(ConfigHelper.provider_key, provider_json);
+
+ /*String cert_string = getStringFromProvider(cert_url);
+ JSONObject cert_json = new JSONObject("{ \"certificate\" : \"" + cert_string + "\"}");
+ ConfigHelper.saveSharedPref(ConfigHelper.cert_key, cert_json);
+ ConfigHelper.addTrustedCertificate(provider_name, cert_string);*/
+ URL cacert = new URL(cert_url);
+ ConfigHelper.addTrustedCertificate(provider_name, cacert.openStream());
+ JSONObject eip_service_json = getJSONFromProvider(eip_service_json_url);
+ ConfigHelper.saveSharedPref(ConfigHelper.eip_service_key, eip_service_json);
+ return true;
+ } catch (IOException e) {
+ // It could happen that an https site used a certificate not trusted: solved above using URL
+ e.printStackTrace();
+ return false;
+ } catch (JSONException e) {
+ ConfigHelper.rescueJSONException(e);
+ return false;
+ } catch(Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ private JSONObject downloadNewProviderDotJsonWithoutCert(
+ String provider_json_url) {
+ JSONObject provider_json = null;
+ try {
+ URL provider_url = new URL(provider_json_url);
+ String provider_json_string = new Scanner(provider_url.openStream()).useDelimiter("\\A").next();
+ provider_json = new JSONObject(provider_json_string);
+ } catch (MalformedURLException e1) {
+ e1.printStackTrace();
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ } catch (JSONException e1) {
+ e1.printStackTrace();
+ }
+ return provider_json;
+ }
+
private String guessURL(String provider_main_url) {
return provider_main_url + "/provider.json";
}