Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent double call to resolve/reject #51

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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