diff options
| -rw-r--r-- | src/se/leap/leapclient/ProviderAPI.java | 54 | ||||
| -rw-r--r-- | tests/.project | 34 | 
2 files changed, 78 insertions, 10 deletions
diff --git a/src/se/leap/leapclient/ProviderAPI.java b/src/se/leap/leapclient/ProviderAPI.java index a81cc788..d4402aa5 100644 --- a/src/se/leap/leapclient/ProviderAPI.java +++ b/src/se/leap/leapclient/ProviderAPI.java @@ -16,15 +16,13 @@   */   package se.leap.leapclient; +import java.io.BufferedInputStream;  import java.io.FileNotFoundException;  import java.io.IOException; +import java.io.InputStream;  import java.math.BigInteger; -import java.net.CookieHandler; -import java.net.CookieManager; -import java.net.HttpCookie;  import java.net.MalformedURLException;  import java.net.SocketTimeoutException; -import java.net.URISyntaxException;  import java.net.URL;  import java.net.URLConnection;  import java.net.UnknownHostException; @@ -36,7 +34,10 @@ import java.security.SecureRandom;  import java.security.cert.CertificateException;  import java.security.cert.X509Certificate;  import java.security.interfaces.RSAPrivateKey; +import java.util.HashMap; +import java.util.Iterator;  import java.util.List; +import java.util.Map;  import java.util.Scanner;  import javax.net.ssl.HostnameVerifier; @@ -52,14 +53,10 @@ import org.apache.http.HttpEntity;  import org.apache.http.HttpResponse;  import org.apache.http.client.ClientProtocolException;  import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpPost;  import org.apache.http.client.methods.HttpPut;  import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.protocol.ClientContext;  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.SRPParameters;  import org.json.JSONException;  import org.json.JSONObject; @@ -279,8 +276,10 @@ public class ProviderAPI extends IntentService {  	 * @throws JSONException  	 */  	private JSONObject sendAToSRPServer(String server_url, String username, String clientA) throws ClientProtocolException, IOException, JSONException { -		HttpPost post = new HttpPost(server_url + "/sessions.json" + "?" + "login=" + username + "&&" + "A=" + clientA); -		return sendToServer(post); +		Map<String, String> parameters = new HashMap<String, String>(); +		parameters.put("login", username); +		parameters.put("A", clientA); +		return sendToServer(server_url + "/sessions.json", "POST", parameters);  	}  	/** @@ -331,6 +330,41 @@ public class ProviderAPI extends IntentService {  		return json_response;  	} +	 +	/** +	 * Executes an HTTP request expecting a JSON response. +	 * @param url +	 * @param request_method +	 * @param parameters +	 * @return response from authentication server +	 * @throws IOException +	 * @throws JSONException +	 * @throws MalformedURLException  +	 */ +	private JSONObject sendToServer(String url, String request_method, Map<String, String> parameters) throws JSONException, MalformedURLException, IOException { +		JSONObject json_response; +		HttpsURLConnection urlConnection = (HttpsURLConnection)new URL(url).openConnection(); +		try { +			urlConnection.setRequestMethod(request_method); +			Iterator<String> parameter_iterator = parameters.keySet().iterator(); +			while(parameter_iterator.hasNext()) { +				String key = parameter_iterator.next(); +				urlConnection.addRequestProperty(key, parameters.get(key)); +			} +			//urlConnection.setChunkedStreamingMode(0); + +			InputStream in = new BufferedInputStream(urlConnection.getInputStream()); +			String plain_response = new Scanner(in).useDelimiter("\\A").next(); +			json_response = new JSONObject(plain_response); +			if(!json_response.isNull(ConfigHelper.ERRORS_KEY) || json_response.has(ConfigHelper.ERRORS_KEY)) { +				return new JSONObject(); +			} +		} finally { +			urlConnection.disconnect(); +		} + +		return json_response; +	}  	/**  	 * Downloads a provider.json from a given URL, adding a new provider using the given name.   diff --git a/tests/.project b/tests/.project new file mode 100644 index 00000000..851db452 --- /dev/null +++ b/tests/.project @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> +	<name>leap_androidTest</name> +	<comment></comment> +	<projects> +		<project>leap_android</project> +	</projects> +	<buildSpec> +		<buildCommand> +			<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name> +			<arguments> +			</arguments> +		</buildCommand> +		<buildCommand> +			<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name> +			<arguments> +			</arguments> +		</buildCommand> +		<buildCommand> +			<name>org.eclipse.jdt.core.javabuilder</name> +			<arguments> +			</arguments> +		</buildCommand> +		<buildCommand> +			<name>com.android.ide.eclipse.adt.ApkBuilder</name> +			<arguments> +			</arguments> +		</buildCommand> +	</buildSpec> +	<natures> +		<nature>com.android.ide.eclipse.adt.AndroidNature</nature> +		<nature>org.eclipse.jdt.core.javanature</nature> +	</natures> +</projectDescription>  | 
