2021-09-13 20:41:05 +00:00
|
|
|
import { Alert, NativeModules, Platform } from 'react-native';
|
2020-11-30 17:00:06 +00:00
|
|
|
import DocumentPicker from 'react-native-document-picker';
|
|
|
|
import * as FileSystem from 'expo-file-system';
|
|
|
|
|
|
|
|
import UserPreferences from '../lib/userPreferences';
|
|
|
|
import I18n from '../i18n';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { extractHostname } from './server';
|
2020-11-30 17:00:06 +00:00
|
|
|
|
|
|
|
const { SSLPinning } = NativeModules;
|
|
|
|
|
|
|
|
const RCSSLPinning = Platform.select({
|
|
|
|
ios: {
|
2021-09-13 20:41:05 +00:00
|
|
|
pickCertificate: () =>
|
|
|
|
new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const res = await DocumentPicker.pick({
|
|
|
|
type: ['com.rsa.pkcs-12']
|
|
|
|
});
|
|
|
|
const { uri, name } = res;
|
|
|
|
Alert.prompt(
|
|
|
|
I18n.t('Certificate_password'),
|
|
|
|
I18n.t('Whats_the_password_for_your_certificate'),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: 'OK',
|
|
|
|
onPress: async password => {
|
|
|
|
try {
|
|
|
|
const certificatePath = `${FileSystem.documentDirectory}/${name}`;
|
2020-11-30 17:00:06 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
await FileSystem.copyAsync({ from: uri, to: certificatePath });
|
2020-11-30 17:00:06 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const certificate = {
|
|
|
|
path: certificatePath.replace('file://', ''), // file:// isn't allowed by obj-C
|
|
|
|
password
|
|
|
|
};
|
2020-11-30 17:00:06 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
await UserPreferences.setMapAsync(name, certificate);
|
2020-11-30 17:00:06 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
resolve(name);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
2020-11-30 17:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
],
|
|
|
|
'secure-text'
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
setCertificate: async (alias, server) => {
|
2020-11-30 17:00:06 +00:00
|
|
|
if (alias) {
|
|
|
|
const certificate = await UserPreferences.getMapAsync(alias);
|
|
|
|
await UserPreferences.setMapAsync(extractHostname(server), certificate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
android: {
|
|
|
|
pickCertificate: () => SSLPinning?.pickCertificate(),
|
|
|
|
setCertificate: alias => SSLPinning?.setCertificate(alias)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default RCSSLPinning;
|