Refactoring based on review: CipherStorage.getAPILevel() renamed to getMinSupportedApiLevel(). Fixed issue with no handling of null service in PrefsStorage. Throwing of CryptoFailedException moved to getCipherStorageForCurrentAPILevel(). Removed unused constructor.
This commit is contained in:
parent
1c0552f88b
commit
a45f1351c2
|
@ -58,9 +58,6 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
throw new EmptyParameterException("you passed empty or null username/password");
|
||||
}
|
||||
CipherStorage currentCipherStorage = getCipherStorageForCurrentAPILevel();
|
||||
if (currentCipherStorage == null) {
|
||||
throw new CryptoFailedException("Unsupported Android SDK " + Build.VERSION.SDK_INT);
|
||||
}
|
||||
|
||||
EncryptionResult result = currentCipherStorage.encrypt(service, username, password);
|
||||
prefsStorage.storeEncryptedEntry(service, result);
|
||||
|
@ -79,9 +76,6 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
public void getGenericPasswordForOptions(String service, Promise promise) {
|
||||
try {
|
||||
CipherStorage currentCipherStorage = getCipherStorageForCurrentAPILevel();
|
||||
if (currentCipherStorage == null) {
|
||||
throw new CryptoFailedException("Unsupported Android SDK " + Build.VERSION.SDK_INT);
|
||||
}
|
||||
|
||||
final DecryptionResult decryptionResult;
|
||||
ResultSet resultSet = prefsStorage.getEncryptedEntry(service);
|
||||
|
@ -161,18 +155,21 @@ public class KeychainModule extends ReactContextBaseJavaModule {
|
|||
}
|
||||
|
||||
// The "Current" CipherStorage is the cipherStorage with the highest API level that is lower than or equal to the current API level
|
||||
private CipherStorage getCipherStorageForCurrentAPILevel() {
|
||||
private CipherStorage getCipherStorageForCurrentAPILevel() throws CryptoFailedException {
|
||||
int currentAPILevel = Build.VERSION.SDK_INT;
|
||||
CipherStorage currentCipherStorage = null;
|
||||
for (CipherStorage cipherStorage : cipherStorageMap.values()) {
|
||||
int cipherStorageAPILevel = cipherStorage.getAPILevel();
|
||||
int cipherStorageAPILevel = cipherStorage.getMinSupportedApiLevel();
|
||||
// Is the cipherStorage supported on the current API level?
|
||||
boolean isSupported = (cipherStorageAPILevel <= currentAPILevel);
|
||||
// Is the API level better than the one we previously selected (if any)?
|
||||
if (isSupported && (currentCipherStorage == null || cipherStorageAPILevel > currentCipherStorage.getAPILevel())) {
|
||||
if (isSupported && (currentCipherStorage == null || cipherStorageAPILevel > currentCipherStorage.getMinSupportedApiLevel())) {
|
||||
currentCipherStorage = cipherStorage;
|
||||
}
|
||||
}
|
||||
if (currentCipherStorage == null) {
|
||||
throw new CryptoFailedException("Unsupported Android SDK " + Build.VERSION.SDK_INT);
|
||||
}
|
||||
return currentCipherStorage;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.oblador.keychain.cipherStorage.CipherStorageFacebookConceal;
|
|||
|
||||
public class PrefsStorage {
|
||||
public static final String KEYCHAIN_DATA = "RN_KEYCHAIN";
|
||||
public static final String EMPTY_STRING = "";
|
||||
|
||||
static public class ResultSet {
|
||||
public final String cipherStorageName;
|
||||
|
@ -30,6 +31,8 @@ public class PrefsStorage {
|
|||
}
|
||||
|
||||
public ResultSet getEncryptedEntry(String service) {
|
||||
service = service == null ? EMPTY_STRING : service;
|
||||
|
||||
byte[] bytesForUsername = getBytesForUsername(service);
|
||||
byte[] bytesForPassword = getBytesForPassword(service);
|
||||
String cipherStorageName = getCipherStorageName(service);
|
||||
|
@ -44,6 +47,8 @@ public class PrefsStorage {
|
|||
}
|
||||
|
||||
public void resetPassword(String service) {
|
||||
service = service == null ? EMPTY_STRING : service;
|
||||
|
||||
String keyForUsername = getKeyForUsername(service);
|
||||
String keyForPassword = getKeyForPassword(service);
|
||||
String keyForCipherStorage = getKeyForCipherStorage(service);
|
||||
|
@ -55,6 +60,8 @@ public class PrefsStorage {
|
|||
}
|
||||
|
||||
public void storeEncryptedEntry(String service, EncryptionResult encryptionResult) {
|
||||
service = service == null ? EMPTY_STRING : service;
|
||||
|
||||
String keyForUsername = getKeyForUsername(service);
|
||||
String keyForPassword = getKeyForPassword(service);
|
||||
String keyForCipherStorage = getKeyForCipherStorage(service);
|
||||
|
|
|
@ -37,5 +37,5 @@ public interface CipherStorage {
|
|||
|
||||
String getCipherStorageName();
|
||||
|
||||
int getAPILevel();
|
||||
int getMinSupportedApiLevel();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ public class CipherStorageFacebookConceal implements CipherStorage {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int getAPILevel() {
|
||||
public int getMinSupportedApiLevel() {
|
||||
return Build.VERSION_CODES.JELLY_BEAN;
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ public class CipherStorageKeystoreAESCBC implements CipherStorage {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int getAPILevel() {
|
||||
public int getMinSupportedApiLevel() {
|
||||
return Build.VERSION_CODES.M;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
package com.oblador.keychain.exceptions;
|
||||
|
||||
public class KeyStoreAccessException extends Exception {
|
||||
public KeyStoreAccessException(Throwable t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
public KeyStoreAccessException(String message, Throwable t) {
|
||||
super(message, t);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue