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 SharedPreferences prefs;
|
||||
private final PrefsUtils prefsUtils;
|
||||
|
||||
private class ResultSet {
|
||||
final String service;
|
||||
|
@ -95,6 +96,7 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
KeyChain keyChain = new SharedPrefsBackedKeyChain(getReactApplicationContext(), CryptoConfig.KEY_256);
|
||||
crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
|
||||
prefs = this.getReactApplicationContext().getSharedPreferences(KEYCHAIN_DATA, Context.MODE_PRIVATE);
|
||||
prefsUtils = new PrefsUtils(this.prefs);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
|
@ -156,10 +158,7 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
String encryptedUsername = encryptString(key, service, username);
|
||||
String encryptedPassword = encryptString(key, service, password);
|
||||
|
||||
SharedPreferences.Editor prefsEditor = prefs.edit();
|
||||
prefsEditor.putString(service + DELIMITER + "u", encryptedUsername);
|
||||
prefsEditor.putString(service + DELIMITER + "p", encryptedPassword);
|
||||
prefsEditor.apply();
|
||||
prefsUtils.storeEncryptedValues(service, DELIMITER, encryptedUsername, encryptedPassword);
|
||||
Log.d(KEYCHAIN_MODULE, "saved the data");
|
||||
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchProviderException | UnrecoverableKeyException e) {
|
||||
throw new CryptoFailedException("Could not encrypt data for service " + service, e);
|
||||
|
@ -194,8 +193,8 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
try {
|
||||
final byte[] decryptedUsername;
|
||||
final byte[] decryptedPassword;
|
||||
byte[] recuser = getBytesFromPrefs(service, DELIMITER + "u");
|
||||
byte[] recpass = getBytesFromPrefs(service, DELIMITER + "p");
|
||||
byte[] recuser = prefsUtils.getBytesForUsername(service, DELIMITER);
|
||||
byte[] recpass = prefsUtils.getBytesForPassword(service, DELIMITER);
|
||||
if (recuser == null || recpass == null) {
|
||||
// Check if the values are stored using the LEGACY_DELIMITER and thus encrypted using FaceBook's Conceal
|
||||
ResultSet resultSet = getGenericPasswordForOptionsUsingConceal(originalService);
|
||||
|
@ -283,8 +282,8 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
}
|
||||
service = service == null ? EMPTY_STRING : service;
|
||||
|
||||
byte[] recuser = getBytesFromPrefs(service, LEGACY_DELIMITER + "u");
|
||||
byte[] recpass = getBytesFromPrefs(service, LEGACY_DELIMITER + "p");
|
||||
byte[] recuser = prefsUtils.getBytesForUsername(service, LEGACY_DELIMITER);
|
||||
byte[] recpass = prefsUtils.getBytesForPassword(service, LEGACY_DELIMITER);
|
||||
if (recuser == null || recpass == 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
|
||||
public void resetGenericPasswordForOptions(String service, Promise promise) {
|
||||
try {
|
||||
|
@ -333,25 +324,13 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
keyStore.deleteEntry(service);
|
||||
}
|
||||
|
||||
SharedPreferences.Editor prefsEditor = prefs.edit();
|
||||
|
||||
if (prefs.contains(service + DELIMITER + "u")) {
|
||||
prefsEditor.remove(service + DELIMITER + "u");
|
||||
prefsEditor.remove(service + DELIMITER + "p");
|
||||
prefsEditor.apply();
|
||||
}
|
||||
prefsUtils.resetPassword(service, DELIMITER);
|
||||
}
|
||||
|
||||
private void resetGenericPasswordForOptionsLegacy(String service) throws KeyStoreException, KeyStoreAccessException {
|
||||
service = service == null ? EMPTY_STRING : service;
|
||||
|
||||
SharedPreferences.Editor prefsEditor = prefs.edit();
|
||||
|
||||
if (prefs.contains(service + LEGACY_DELIMITER + "u")) {
|
||||
prefsEditor.remove(service + LEGACY_DELIMITER + "u");
|
||||
prefsEditor.remove(service + LEGACY_DELIMITER + "p");
|
||||
prefsEditor.apply();
|
||||
}
|
||||
prefsUtils.resetPassword(service, LEGACY_DELIMITER);
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
|
|
|
@ -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…
Reference in New Issue