Skip to content

Commit

Permalink
[native] Secure store - add android part
Browse files Browse the repository at this point in the history
Summary: Android implementation for our code of `expo-secure-store`

Test Plan:
I used the code below to test these changes:
```
static bool randInit = false;
  {
    if (randInit) {
      Logger::log("here init rand");
      randInit = true;
      srand((unsigned)time(0));
    }
    auto generateRandomString = [](size_t size) {
      std::string str;
      for (size_t i = 0; i < size; ++i) {
        str += 'A' + rand() % 24;
      }
      return str;
    };

    Logger::log("here test secure store #0");
    SecureStore secureStore;
    std::string key = generateRandomString(20);
    Logger::log("here test secure store #1(write): " + key);
    secureStore.set("pickleKey", key);
    std::string result = secureStore.get("pickleKey");
    Logger::log("here test secure store #2 (read): " + result);
  }
```

Reviewers: palys-swm, ashoat

Reviewed By: palys-swm, ashoat

Subscribers: ashoat, palys-swm, Adrian, atul

Differential Revision: https://phabricator.ashoat.com/D1823
  • Loading branch information
karol-bisztyga committed Aug 10, 2021
1 parent 9ef3bd1 commit 7e1773a
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 5 deletions.
39 changes: 39 additions & 0 deletions native/android/app/src/cpp/CommSecureStore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "CommSecureStore.h"
#include <fbjni/fbjni.h>

using namespace facebook::jni;

class CommSecureStoreJavaClass : public JavaClass<CommSecureStoreJavaClass> {
public:
static auto constexpr kJavaDescriptor =
"Lapp/comm/android/fbjni/CommSecureStore;";

static void set(std::string key, std::string value) {
static const auto cls = javaClassStatic();
static auto method =
cls->getStaticMethod<void(std::string, std::string)>("set");
method(cls, key, value);
}

static folly::Optional<std::string> get(std::string key) {
static const auto cls = javaClassStatic();
static auto method =
cls->getStaticMethod<facebook::jni::JString(std::string)>("get");
const auto result = method(cls, key);
return (result) ? folly::Optional<std::string>(result->toStdString())
: folly::none;
}
};

namespace comm {

void CommSecureStore::set(const std::string key, const std::string value)
const {
CommSecureStoreJavaClass::set(key, value);
}

folly::Optional<std::string> CommSecureStore::get(const std::string key) const {
return CommSecureStoreJavaClass::get(key);
}

} // namespace comm
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package app.comm.android;

import com.facebook.react.bridge.JSIModulePackage;
import com.facebook.react.bridge.JSIModuleSpec;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactApplicationContext;
import java.util.Collections;
import java.util.List;
import app.comm.android.fbjni.CommHybrid;

import com.swmansion.reanimated.ReanimatedJSIModulePackage;

public class CommCoreJSIModulePackage extends ReanimatedJSIModulePackage {

@Override
public List<JSIModuleSpec> getJSIModules(
ReactApplicationContext reactApplicationContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.comm.android;

import app.comm.android.fbjni.CommSecureStore;
import app.comm.android.generated.BasePackageList;

import androidx.multidex.MultiDexApplication;
Expand All @@ -9,19 +10,16 @@

import org.unimodules.adapters.react.ModuleRegistryAdapter;
import org.unimodules.adapters.react.ReactModuleRegistryProvider;
import org.unimodules.core.interfaces.SingletonModule;

import com.facebook.react.PackageList;
import com.facebook.hermes.reactexecutor.HermesExecutorFactory;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.JSIModulePackage;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import expo.modules.securestore.SecureStoreModule;
import io.invertase.firebase.messaging.RNFirebaseMessagingPackage;
import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;

Expand Down Expand Up @@ -70,6 +68,10 @@ protected String getJSMainModuleName() {

@Override
protected JSIModulePackage getJSIModulePackage() {
SecureStoreModule secureStoreModule = (SecureStoreModule)
mModuleRegistryProvider.get(getApplicationContext())
.getExportedModuleOfClass(SecureStoreModule.class);
CommSecureStore.getInstance().initialize(secureStoreModule);
return new CommCoreJSIModulePackage();
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package app.comm.android.fbjni;

import org.unimodules.core.Promise;
import org.unimodules.core.arguments.MapArguments;
import org.unimodules.core.arguments.ReadableArguments;

import expo.modules.securestore.SecureStoreModule;

public class CommSecureStore {

private static final CommSecureStore instance = new CommSecureStore();
private SecureStoreModule secureStoreModule = null;
private final ReadableArguments readableArguments;

private CommSecureStore() {
this.readableArguments = new MapArguments();
}

public static CommSecureStore getInstance() {
return CommSecureStore.instance;
}

public void initialize(SecureStoreModule secureStoreModule) {
this.secureStoreModule = secureStoreModule;
}

private void checkModule() {
if (this.secureStoreModule == null) {
throw new RuntimeException(
"secure store module has not been initialized"
);
}
}

private void internalSet(String key, String value) {
this.checkModule();
Promise promise = new Promise() {
@Override
public void resolve(Object value) {}

@Override
public void reject(String code, String message, Throwable e) {
throw new RuntimeException("secure store set error: " + message);
}
};
this.secureStoreModule.setValueWithKeyAsync(
value,
key,
this.readableArguments,
promise
);
}

private String internalGet(String key) {
this.checkModule();
final String[] result = {null};

Promise promise = new Promise() {
@Override
public void resolve(Object value) {
result[0] = (String)value;
}

@Override
public void reject(String code, String message, Throwable e) {
throw new RuntimeException("secure store get error: " + message);
}
};
// The following call will resolve the promise before it returns
this.secureStoreModule.getValueWithKeyAsync(
key,
this.readableArguments,
promise
);

return result[0];
}

public static void set(String key, String value) {
getInstance().internalSet(key, value);
}

public static String get(String key) {
return getInstance().internalGet(key);
}
}

0 comments on commit 7e1773a

Please sign in to comment.