minor refactoring

This commit is contained in:
Vojtech Novak 2016-12-30 10:00:33 +01:00
parent c19efe56c5
commit 732b765c7a
1 changed files with 22 additions and 26 deletions

View File

@ -20,15 +20,16 @@ import java.nio.charset.Charset;
public class KeychainModule extends ReactContextBaseJavaModule { public class KeychainModule extends ReactContextBaseJavaModule {
public static final String REACT_CLASS = "RNKeychainManager"; public static final String KEYCHAIN_MODULE = "RNKeychainManager";
public static final String KEYCHAIN_DATA = "RN_KEYCHAIN"; public static final String KEYCHAIN_DATA = "RN_KEYCHAIN";
public static final String EMPTY_STRING = "";
private final Crypto crypto; private final Crypto crypto;
private final SharedPreferences prefs; private final SharedPreferences prefs;
@Override @Override
public String getName() { public String getName() {
return REACT_CLASS; return KEYCHAIN_MODULE;
} }
public KeychainModule(ReactApplicationContext reactContext) { public KeychainModule(ReactApplicationContext reactContext) {
@ -36,42 +37,37 @@ 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);
} }
@ReactMethod @ReactMethod
public void setGenericPasswordForService(String service, String username, String password, Callback callback) { public void setGenericPasswordForService(String service, String username, String password, Callback callback) {
if (!crypto.isAvailable()) { if (!crypto.isAvailable()) {
Log.e("KeychainModule", "Crypto is missing"); Log.e(KEYCHAIN_MODULE, "Crypto is missing");
callback.invoke("KeychainModule: crypto is missing"); callback.invoke("KeychainModule: crypto is missing");
return; return;
} }
if (username == null || username.isEmpty() || password == null || password.isEmpty()) { if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
Log.e("KeychainModule", "you passed empty or null username/password"); Log.e(KEYCHAIN_MODULE, "you passed empty or null username/password");
callback.invoke("KeychainModule: you passed empty or null username/password"); callback.invoke("KeychainModule: you passed empty or null username/password");
return; return;
} }
service = service == null ? "" : service; service = service == null ? EMPTY_STRING : service;
//Log.d("Crypto", service + username + password); //Log.d("Crypto", service + username + password);
Entity userentity = Entity.create(KEYCHAIN_DATA + ":" + service + "user"); Entity userentity = Entity.create(KEYCHAIN_DATA + ":" + service + "user");
Entity pwentity = Entity.create(KEYCHAIN_DATA + ":" + service + "pass"); Entity pwentity = Entity.create(KEYCHAIN_DATA + ":" + service + "pass");
try {
String encryptedUsername = encryptWithEntity(username, userentity, callback);
String encryptedPassword = encryptWithEntity(password, pwentity, callback);
SharedPreferences.Editor prefsEditor = prefs.edit(); String encryptedUsername = encryptWithEntity(username, userentity, callback);
prefsEditor.putString(service + ":u", encryptedUsername); String encryptedPassword = encryptWithEntity(password, pwentity, callback);
prefsEditor.putString(service + ":p", encryptedPassword);
prefsEditor.apply(); SharedPreferences.Editor prefsEditor = prefs.edit();
Log.d("KeychainModule", "saved the data"); prefsEditor.putString(service + ":u", encryptedUsername);
callback.invoke("", "KeychainModule saved the data"); prefsEditor.putString(service + ":p", encryptedPassword);
} catch (Exception e) { prefsEditor.apply();
Log.e("KeychainModule ", e.getLocalizedMessage()); Log.d(KEYCHAIN_MODULE, "saved the data");
callback.invoke(e.getLocalizedMessage()); callback.invoke(EMPTY_STRING, "KeychainModule saved the data");
}
} }
private String encryptWithEntity(String toEncypt, Entity entity, Callback callback) { private String encryptWithEntity(String toEncypt, Entity entity, Callback callback) {
@ -79,7 +75,7 @@ public class KeychainModule extends ReactContextBaseJavaModule {
byte[] encryptedBytes = crypto.encrypt(toEncypt.getBytes(Charset.forName("UTF-8")), entity); byte[] encryptedBytes = crypto.encrypt(toEncypt.getBytes(Charset.forName("UTF-8")), entity);
return Base64.encodeToString(encryptedBytes, Base64.DEFAULT); return Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
} catch (Exception e) { } catch (Exception e) {
Log.e("KeychainModule ", e.getLocalizedMessage()); Log.e(KEYCHAIN_MODULE, e.getLocalizedMessage());
callback.invoke(e.getLocalizedMessage()); callback.invoke(e.getLocalizedMessage());
return null; return null;
} }
@ -87,12 +83,12 @@ public class KeychainModule extends ReactContextBaseJavaModule {
@ReactMethod @ReactMethod
public void getGenericPasswordForService(String service, Callback callback) { public void getGenericPasswordForService(String service, Callback callback) {
service = service == null ? "" : service; service = service == null ? EMPTY_STRING : service;
String username = prefs.getString(service + ":u", "user_not_found"); String username = prefs.getString(service + ":u", "user_not_found");
String password = prefs.getString(service + ":p", "pass_not_found"); String password = prefs.getString(service + ":p", "pass_not_found");
if (username.equals("user_not_found") || password.equals("pass_not_found")) { if (username.equals("user_not_found") || password.equals("pass_not_found")) {
Log.e("KeychainModule ", "no keychain entry found for service: " + service); Log.e(KEYCHAIN_MODULE, "no keychain entry found for service: " + service);
callback.invoke("no keychain entry found for service: " + service); callback.invoke("no keychain entry found for service: " + service);
return; return;
} }
@ -107,23 +103,23 @@ public class KeychainModule extends ReactContextBaseJavaModule {
byte[] decryptedUsername = crypto.decrypt(recuser, userentity); byte[] decryptedUsername = crypto.decrypt(recuser, userentity);
byte[] decryptedPass = crypto.decrypt(recpass, pwentity); byte[] decryptedPass = crypto.decrypt(recpass, pwentity);
callback.invoke("", new String(decryptedUsername, Charset.forName("UTF-8")), new String(decryptedPass, Charset.forName("UTF-8"))); callback.invoke(EMPTY_STRING, new String(decryptedUsername, Charset.forName("UTF-8")), new String(decryptedPass, Charset.forName("UTF-8")));
} catch (Exception e) { } catch (Exception e) {
Log.e("KeychainModule ", e.getLocalizedMessage()); Log.e(KEYCHAIN_MODULE, e.getLocalizedMessage());
callback.invoke(e.getLocalizedMessage()); callback.invoke(e.getLocalizedMessage());
} }
} }
@ReactMethod @ReactMethod
public void resetGenericPasswordForService(String service, Callback callback) { public void resetGenericPasswordForService(String service, Callback callback) {
service = service == null ? "" : service; service = service == null ? EMPTY_STRING : service;
SharedPreferences.Editor prefsEditor = prefs.edit(); SharedPreferences.Editor prefsEditor = prefs.edit();
if (prefs.contains(service + ":u")) { if (prefs.contains(service + ":u")) {
prefsEditor.remove(service + ":u"); prefsEditor.remove(service + ":u");
prefsEditor.remove(service + ":p"); prefsEditor.remove(service + ":p");
prefsEditor.apply(); prefsEditor.apply();
callback.invoke("", "KeychainModule password was reset"); callback.invoke(EMPTY_STRING, "KeychainModule password was reset");
} else { } else {
callback.invoke("Error when resetting password: entry not found for service: " + service); callback.invoke("Error when resetting password: entry not found for service: " + service);
} }