Skip to content

Commit

Permalink
feat(android): Add SSL Pinning logic (#6314)
Browse files Browse the repository at this point in the history
Co-authored-by: IT-MikeS <[email protected]>
  • Loading branch information
ItsChaceD and IT-MikeS authored Feb 22, 2023
1 parent cb00e49 commit 07f113e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
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

0 comments on commit 07f113e

Please sign in to comment.