Skip to content

Commit 9a9f1ad

Browse files
feat: secrets api
1 parent 10ee055 commit 9a9f1ad

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/plugins/pluginContext/src/android/Tee.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import java.util.Set;
1616
import java.util.concurrent.ConcurrentHashMap;
1717

18+
//auth plugin
19+
import com.foxdebug.acode.rk.auth.EncryptedPreferenceManager;
20+
1821
public class Tee extends CordovaPlugin {
1922

2023
// pluginId : token
@@ -26,10 +29,62 @@ public class Tee extends CordovaPlugin {
2629
// token : list of permissions
2730
private /*static*/ final Map<String, List<String>> permissionStore = new ConcurrentHashMap<>();
2831

32+
33+
34+
private Context context;
35+
36+
37+
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
38+
super.initialize(cordova, webView);
39+
this.context = cordova.getContext();
40+
}
41+
2942
@Override
3043
public boolean execute(String action, JSONArray args, CallbackContext callback)
3144
throws JSONException {
3245

46+
47+
if ("get_secret".equals(action)) {
48+
String token = args.getString(0);
49+
String key = args.getString(1);
50+
String defaultValue = args.getString(2);
51+
52+
String pluginId = getPluginIdFromToken(token);
53+
54+
if (pluginId == null) {
55+
callback.error("INVALID_TOKEN");
56+
return true;
57+
}
58+
59+
EncryptedPreferenceManager prefs =
60+
new EncryptedPreferenceManager(context, pluginId);
61+
62+
String value = prefs.getString(key, defaultValue);
63+
callback.success(value);
64+
return true;
65+
}
66+
67+
if ("set_secret".equals(action)) {
68+
String token = args.getString(0);
69+
String key = args.getString(1);
70+
String value = args.getString(2);
71+
72+
String pluginId = getPluginIdFromToken(token);
73+
74+
if (pluginId == null) {
75+
callback.error("INVALID_TOKEN");
76+
return true;
77+
}
78+
79+
EncryptedPreferenceManager prefs =
80+
new EncryptedPreferenceManager(context, pluginId);
81+
82+
prefs.setString(key, value);
83+
callback.success();
84+
return true;
85+
}
86+
87+
3388
if ("requestToken".equals(action)) {
3489
String pluginId = args.getString(0);
3590
String pluginJson = args.getString(1);
@@ -69,6 +124,16 @@ public boolean execute(String action, JSONArray args, CallbackContext callback)
69124
return false;
70125
}
71126

127+
128+
private String getPluginIdFromToken(String token) {
129+
for (Map.Entry<String, String> entry : tokenStore.entrySet()) {
130+
if (entry.getValue().equals(token)) {
131+
return entry.getKey();
132+
}
133+
}
134+
return null;
135+
}
136+
72137
//============================================================
73138
//do not change function signatures
74139
public boolean isTokenValid(String token, String pluginId) {

src/plugins/pluginContext/www/PluginContext.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ const PluginContext = (function () {
3434
exec(resolve, reject, "Tee", "listAllPermissions", [this.uuid]);
3535
});
3636
}
37+
38+
getSecret(token, key, defaultValue = "") {
39+
return new Promise((resolve, reject) => {
40+
exec(
41+
resolve,
42+
reject,
43+
"Tee",
44+
"get_secret",
45+
[token, key, defaultValue]
46+
);
47+
});
48+
}
49+
50+
51+
setSecret(token, key, value) {
52+
return new Promise((resolve, reject) => {
53+
exec(
54+
resolve,
55+
reject,
56+
"Tee",
57+
"set_secret",
58+
[token, key, value]
59+
);
60+
});
61+
}
3762
}
3863

3964
//Object.freeze(this);

0 commit comments

Comments
 (0)