-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[native] Secure store - add android part
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
Showing
4 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 2 additions & 1 deletion
3
native/android/app/src/main/java/app/comm/android/CommCoreJSIModulePackage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
native/android/app/src/main/java/app/comm/android/fbjni/CommSecureStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |