[FIX] Certificate stops working after app update on iOS (#3537)

This commit is contained in:
Diego Mello 2021-12-07 10:57:11 -03:00 committed by GitHub
parent 81dc10de30
commit 838be232ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 14 deletions

View File

@ -7,6 +7,21 @@ import I18n from '../i18n';
import { extractHostname } from './server'; import { extractHostname } from './server';
const { SSLPinning } = NativeModules; const { SSLPinning } = NativeModules;
const { documentDirectory } = FileSystem;
const extractFileScheme = path => path.replace('file://', ''); // file:// isn't allowed by obj-C
const getPath = name => `${documentDirectory}/${name}`;
const persistCertificate = async (name, password) => {
const certificatePath = getPath(name);
const certificate = {
path: extractFileScheme(certificatePath),
password
};
await UserPreferences.setMapAsync(name, certificate);
return certificate;
};
const RCSSLPinning = Platform.select({ const RCSSLPinning = Platform.select({
ios: { ios: {
@ -25,17 +40,9 @@ const RCSSLPinning = Platform.select({
text: 'OK', text: 'OK',
onPress: async password => { onPress: async password => {
try { try {
const certificatePath = `${FileSystem.documentDirectory}/${name}`; const certificatePath = getPath(name);
await FileSystem.copyAsync({ from: uri, to: certificatePath }); await FileSystem.copyAsync({ from: uri, to: certificatePath });
await persistCertificate(name, password);
const certificate = {
path: certificatePath.replace('file://', ''), // file:// isn't allowed by obj-C
password
};
await UserPreferences.setMapAsync(name, certificate);
resolve(name); resolve(name);
} catch (e) { } catch (e) {
reject(e); reject(e);
@ -49,16 +56,19 @@ const RCSSLPinning = Platform.select({
reject(e); reject(e);
} }
}), }),
setCertificate: async (alias, server) => { setCertificate: async (name, server) => {
if (alias) { if (name) {
const certificate = await UserPreferences.getMapAsync(alias); let certificate = await UserPreferences.getMapAsync(name);
if (!certificate.path.match(extractFileScheme(documentDirectory))) {
certificate = await persistCertificate(name, certificate.password);
}
await UserPreferences.setMapAsync(extractHostname(server), certificate); await UserPreferences.setMapAsync(extractHostname(server), certificate);
} }
} }
}, },
android: { android: {
pickCertificate: () => SSLPinning?.pickCertificate(), pickCertificate: () => SSLPinning?.pickCertificate(),
setCertificate: alias => SSLPinning?.setCertificate(alias) setCertificate: name => SSLPinning?.setCertificate(name)
} }
}); });