Skip to content

Commit

Permalink
Android SSL Pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
ywongweb committed Sep 7, 2020
1 parent be2871e commit 7ca12ad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import React, {useState} from 'react'
import {StyleSheet, View, Text, Button} from 'react-native'
import {StyleSheet, View, Text, Button, Platform} from 'react-native'

const URL = 'https://busdue.com'

Expand All @@ -32,7 +32,7 @@ const App: () => React$Node = () => {
return (
<View style={styles.container}>
<Text style={styles.title}>React Native SSL Pinning</Text>
<Text style={styles.title}>(iOS)</Text>
<Text style={styles.title}>({Platform.OS.toUpperCase()})</Text>
<Text style={styles.header}>Certificate status:</Text>
<Text
style={[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down Expand Up @@ -43,6 +44,9 @@ public ReactNativeHost getReactNativeHost() {
@Override
public void onCreate() {
super.onCreate();
// Provide a client factory to React Native's OkHttpClientProvider and it will
// use it instead of the default one.
OkHttpClientProvider.setOkHttpClientFactory(new SSLPinnerFactory());
SoLoader.init(this, /* native exopackage */ false);
initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.blogreactnativesslpinning;

import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientProvider;

import okhttp3.CertificatePinner;
import okhttp3.OkHttpClient;

public class SSLPinnerFactory implements OkHttpClientFactory {
private static String hostname = "busdue.com";

public OkHttpClient createNewNetworkModuleClient() {
CertificatePinner certificatePinner = new CertificatePinner.Builder()
.add(hostname, "sha256/dz0GbS1i4LnBsJwhRw3iuZmVcgqpn+AlxSBRxUbOz0k=")
.build();
// Get a OkHttpClient builder with all the React Native defaults
OkHttpClient.Builder clientBuilder = OkHttpClientProvider.createClientBuilder();
return clientBuilder
.certificatePinner(certificatePinner)
.build();
}
}

0 comments on commit 7ca12ad

Please sign in to comment.