Skip to content

Commit

Permalink
Merge pull request #51 from Dr-Horv/master
Browse files Browse the repository at this point in the history
Prevent double call to resolve/reject
  • Loading branch information
talut authored Jun 30, 2020
2 parents 22af08a + 8621f19 commit 3a4e26c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.taluttasgiran.rnsecurestorage;

import android.content.Context;
import android.os.Build;
import android.security.KeyPairGeneratorSpec;
import android.util.Log;

Expand All @@ -13,6 +12,7 @@
import java.security.GeneralSecurityException;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Calendar;
Expand All @@ -28,7 +28,7 @@
public class RNKeyStore {

private PublicKey getOrCreatePublicKey(Context context, String alias) throws GeneralSecurityException, IOException {
KeyStore keyStore = KeyStore.getInstance(getKeyStore());
KeyStore keyStore = getKeyStore();
keyStore.load(null);

if (!keyStore.containsAlias(alias) || keyStore.getCertificate(alias) == null) {
Expand All @@ -45,7 +45,7 @@ private PublicKey getOrCreatePublicKey(Context context, String alias) throws Gen
.setEndDate(end.getTime())
.build();

KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", getKeyStore());
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", keyStore.getProvider());
generator.initialize(spec);
generator.generateKeyPair();

Expand Down Expand Up @@ -104,7 +104,7 @@ public void setCipherText(Context context, String alias, String input) throws Ge
}

private PrivateKey getPrivateKey(String alias) throws GeneralSecurityException, IOException {
KeyStore keyStore = KeyStore.getInstance(getKeyStore());
KeyStore keyStore = getKeyStore();
keyStore.load(null);
return (PrivateKey) keyStore.getKey(alias, null);
}
Expand Down Expand Up @@ -150,16 +150,14 @@ public boolean exists(Context context, String alias) throws IOException {
}


private String getKeyStore() {
private KeyStore getKeyStore() throws KeyStoreException {
try {
KeyStore.getInstance(Constants.KEYSTORE_PROVIDER_1);
return Constants.KEYSTORE_PROVIDER_1;
return KeyStore.getInstance(Constants.KEYSTORE_PROVIDER_1);
} catch (Exception err) {
try {
KeyStore.getInstance(Constants.KEYSTORE_PROVIDER_2);
return Constants.KEYSTORE_PROVIDER_2;
return KeyStore.getInstance(Constants.KEYSTORE_PROVIDER_2);
} catch (Exception e) {
return Constants.KEYSTORE_PROVIDER_3;
return KeyStore.getInstance(Constants.KEYSTORE_PROVIDER_3);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import android.content.SharedPreferences;
import android.os.Build;

import androidx.annotation.Nullable;
import android.support.annotation.Nullable;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
Expand Down Expand Up @@ -49,21 +48,15 @@ public void set(String key, String value, @Nullable ReadableMap options, Promise
if (isRTL(initialLocale)) {
Locale.setDefault(Locale.ENGLISH);
rnKeyStore.setCipherText(getReactApplicationContext(), key, value);
promise.resolve("RNSecureStorage: Key stored/updated successfully");
Locale.setDefault(initialLocale);
promise.resolve("RNSecureStorage: Key stored/updated successfully");
} else {
rnKeyStore.setCipherText(getReactApplicationContext(), key, value);
promise.resolve("RNSecureStorage: Key stored/updated successfully");
}
} catch (Exception e) {
promise.reject(e);
}
try {
rnKeyStore.setCipherText(getReactApplicationContext(), key, value);
promise.resolve("RNSecureStorage: Key stored/updated successfully");
} catch (Exception e) {
promise.reject(e);
}
} else {
try {
SharedPreferences.Editor editor = prefs.edit();
Expand Down

0 comments on commit 3a4e26c

Please sign in to comment.