Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): Add SSL Pinning logic #6314

Merged
merged 9 commits into from
Feb 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
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;
import com.getcapacitor.PluginCall;
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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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");
Expand Down