diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java index 01b5d04abf..73201df908 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java @@ -31,6 +31,7 @@ private void http(final PluginCall call, final String httpMethod) { @Override public void run() { try { + HttpRequestHandler.bridge = bridge; JSObject response = HttpRequestHandler.request(call, httpMethod); call.resolve(response); } catch (Exception e) { diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java index f843df4332..ed9f977174 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java @@ -3,6 +3,7 @@ import android.os.Build; import android.os.LocaleList; import android.text.TextUtils; +import com.getcapacitor.Bridge; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.JSValue; @@ -10,6 +11,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.SocketTimeoutException; @@ -21,6 +23,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLSocketFactory; import org.json.JSONException; public class CapacitorHttpUrlConnection implements ICapacitorHttpUrlConnection { @@ -363,4 +367,16 @@ private String buildDefaultAcceptLanguageProperty() { } return result; } + + public void setSSLSocketFactory(Bridge bridge) { + // Attach SSL Certificates if Enterprise Plugin is available + try { + Class sslPinningImpl = Class.forName("io.ionic.sslpinning.SSLPinning"); + Method method = sslPinningImpl.getDeclaredMethod("getSSLSocketFactory", Bridge.class); + SSLSocketFactory sslSocketFactory = (SSLSocketFactory) method.invoke(sslPinningImpl.newInstance(), bridge); + if (sslSocketFactory != null) { + ((HttpsURLConnection) this.connection).setSSLSocketFactory(sslSocketFactory); + } + } catch (Exception ignored) {} + } } diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java index 8dfb948ef2..6e8d35f1ea 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java @@ -2,6 +2,7 @@ import android.text.TextUtils; import android.util.Base64; +import com.getcapacitor.Bridge; import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.getcapacitor.JSValue; @@ -26,6 +27,8 @@ public class HttpRequestHandler { + public static Bridge bridge = null; + /** * An enum specifying conventional HTTP Response Types * See https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType @@ -391,6 +394,10 @@ public static JSObject request(PluginCall call, String httpMethod) throws IOExce CapacitorHttpUrlConnection connection = connectionBuilder.build(); + if (null != bridge) { + connection.setSSLSocketFactory(bridge); + } + // Set HTTP body on a non GET or HEAD request if (isHttpMutate) { JSValue data = new JSValue(call, "data");