mirror of
https://github.com/status-im/react-native-keychain.git
synced 2025-02-17 23:57:22 +00:00
Refactored methods regarding the SharedPreferences and moved them to a utility class.
This commit is contained in:
parent
544b92f262
commit
025aab835a
@ -71,6 +71,7 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||||||
|
|
||||||
private final Crypto crypto;
|
private final Crypto crypto;
|
||||||
private final SharedPreferences prefs;
|
private final SharedPreferences prefs;
|
||||||
|
private final PrefsUtils prefsUtils;
|
||||||
|
|
||||||
private class ResultSet {
|
private class ResultSet {
|
||||||
final String service;
|
final String service;
|
||||||
@ -95,6 +96,7 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||||||
KeyChain keyChain = new SharedPrefsBackedKeyChain(getReactApplicationContext(), CryptoConfig.KEY_256);
|
KeyChain keyChain = new SharedPrefsBackedKeyChain(getReactApplicationContext(), CryptoConfig.KEY_256);
|
||||||
crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
|
crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
|
||||||
prefs = this.getReactApplicationContext().getSharedPreferences(KEYCHAIN_DATA, Context.MODE_PRIVATE);
|
prefs = this.getReactApplicationContext().getSharedPreferences(KEYCHAIN_DATA, Context.MODE_PRIVATE);
|
||||||
|
prefsUtils = new PrefsUtils(this.prefs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
@ -156,10 +158,7 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||||||
String encryptedUsername = encryptString(key, service, username);
|
String encryptedUsername = encryptString(key, service, username);
|
||||||
String encryptedPassword = encryptString(key, service, password);
|
String encryptedPassword = encryptString(key, service, password);
|
||||||
|
|
||||||
SharedPreferences.Editor prefsEditor = prefs.edit();
|
prefsUtils.storeEncryptedValues(service, DELIMITER, encryptedUsername, encryptedPassword);
|
||||||
prefsEditor.putString(service + DELIMITER + "u", encryptedUsername);
|
|
||||||
prefsEditor.putString(service + DELIMITER + "p", encryptedPassword);
|
|
||||||
prefsEditor.apply();
|
|
||||||
Log.d(KEYCHAIN_MODULE, "saved the data");
|
Log.d(KEYCHAIN_MODULE, "saved the data");
|
||||||
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchProviderException | UnrecoverableKeyException e) {
|
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchProviderException | UnrecoverableKeyException e) {
|
||||||
throw new CryptoFailedException("Could not encrypt data for service " + service, e);
|
throw new CryptoFailedException("Could not encrypt data for service " + service, e);
|
||||||
@ -194,8 +193,8 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||||||
try {
|
try {
|
||||||
final byte[] decryptedUsername;
|
final byte[] decryptedUsername;
|
||||||
final byte[] decryptedPassword;
|
final byte[] decryptedPassword;
|
||||||
byte[] recuser = getBytesFromPrefs(service, DELIMITER + "u");
|
byte[] recuser = prefsUtils.getBytesForUsername(service, DELIMITER);
|
||||||
byte[] recpass = getBytesFromPrefs(service, DELIMITER + "p");
|
byte[] recpass = prefsUtils.getBytesForPassword(service, DELIMITER);
|
||||||
if (recuser == null || recpass == null) {
|
if (recuser == null || recpass == null) {
|
||||||
// Check if the values are stored using the LEGACY_DELIMITER and thus encrypted using FaceBook's Conceal
|
// Check if the values are stored using the LEGACY_DELIMITER and thus encrypted using FaceBook's Conceal
|
||||||
ResultSet resultSet = getGenericPasswordForOptionsUsingConceal(originalService);
|
ResultSet resultSet = getGenericPasswordForOptionsUsingConceal(originalService);
|
||||||
@ -283,8 +282,8 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
service = service == null ? EMPTY_STRING : service;
|
service = service == null ? EMPTY_STRING : service;
|
||||||
|
|
||||||
byte[] recuser = getBytesFromPrefs(service, LEGACY_DELIMITER + "u");
|
byte[] recuser = prefsUtils.getBytesForUsername(service, LEGACY_DELIMITER);
|
||||||
byte[] recpass = getBytesFromPrefs(service, LEGACY_DELIMITER + "p");
|
byte[] recpass = prefsUtils.getBytesForPassword(service, LEGACY_DELIMITER);
|
||||||
if (recuser == null || recpass == null) {
|
if (recuser == null || recpass == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -302,14 +301,6 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] getBytesFromPrefs(String service, String prefix) {
|
|
||||||
String value = prefs.getString(service + prefix, null);
|
|
||||||
if (value != null) {
|
|
||||||
return Base64.decode(value, Base64.DEFAULT);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void resetGenericPasswordForOptions(String service, Promise promise) {
|
public void resetGenericPasswordForOptions(String service, Promise promise) {
|
||||||
try {
|
try {
|
||||||
@ -333,25 +324,13 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||||||
keyStore.deleteEntry(service);
|
keyStore.deleteEntry(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPreferences.Editor prefsEditor = prefs.edit();
|
prefsUtils.resetPassword(service, DELIMITER);
|
||||||
|
|
||||||
if (prefs.contains(service + DELIMITER + "u")) {
|
|
||||||
prefsEditor.remove(service + DELIMITER + "u");
|
|
||||||
prefsEditor.remove(service + DELIMITER + "p");
|
|
||||||
prefsEditor.apply();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetGenericPasswordForOptionsLegacy(String service) throws KeyStoreException, KeyStoreAccessException {
|
private void resetGenericPasswordForOptionsLegacy(String service) throws KeyStoreException, KeyStoreAccessException {
|
||||||
service = service == null ? EMPTY_STRING : service;
|
service = service == null ? EMPTY_STRING : service;
|
||||||
|
|
||||||
SharedPreferences.Editor prefsEditor = prefs.edit();
|
prefsUtils.resetPassword(service, LEGACY_DELIMITER);
|
||||||
|
|
||||||
if (prefs.contains(service + LEGACY_DELIMITER + "u")) {
|
|
||||||
prefsEditor.remove(service + LEGACY_DELIMITER + "u");
|
|
||||||
prefsEditor.remove(service + LEGACY_DELIMITER + "p");
|
|
||||||
prefsEditor.apply();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
|
61
android/src/main/java/com/oblador/keychain/PrefsUtils.java
Normal file
61
android/src/main/java/com/oblador/keychain/PrefsUtils.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package com.oblador.keychain;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.util.Base64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by pcoltau on 6/15/17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class PrefsUtils {
|
||||||
|
private final SharedPreferences prefs;
|
||||||
|
|
||||||
|
public PrefsUtils(SharedPreferences prefs) {
|
||||||
|
this.prefs = prefs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytesForUsername(String service, String delimiter) {
|
||||||
|
String key = getKeyForUsername(service, delimiter);
|
||||||
|
return getBytes(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytesForPassword(String service, String delimiter) {
|
||||||
|
String key = getKeyForPassword(service, delimiter);
|
||||||
|
return getBytes(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetPassword(String service, String delimiter) {
|
||||||
|
SharedPreferences.Editor prefsEditor = prefs.edit();
|
||||||
|
String keyForUsername = getKeyForUsername(service, delimiter);
|
||||||
|
String keyForPassword = getKeyForPassword(service, delimiter);
|
||||||
|
|
||||||
|
if (prefs.contains(keyForUsername) || prefs.contains(keyForPassword)) {
|
||||||
|
prefsEditor.remove(keyForUsername);
|
||||||
|
prefsEditor.remove(keyForPassword);
|
||||||
|
prefsEditor.apply();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void storeEncryptedValues(String service, String delimiter, String encryptedUsername, String encryptedPassword) {
|
||||||
|
SharedPreferences.Editor prefsEditor = prefs.edit();
|
||||||
|
prefsEditor.putString(getKeyForUsername(service, delimiter), encryptedUsername);
|
||||||
|
prefsEditor.putString(getKeyForPassword(service, delimiter), encryptedPassword);
|
||||||
|
prefsEditor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getKeyForUsername(String service, String delimiter) {
|
||||||
|
return service + delimiter + "u";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getKeyForPassword(String service, String delimiter) {
|
||||||
|
return service + delimiter + "p";
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] getBytes(String key) {
|
||||||
|
String value = this.prefs.getString(key, null);
|
||||||
|
if (value != null) {
|
||||||
|
return Base64.decode(value, Base64.DEFAULT);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user