Skip to content

Commit edee414

Browse files
Secrets plugin api (#1902)
* feat: secrets api * feat: add imports * Update src/plugins/pluginContext/www/PluginContext.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update src/plugins/pluginContext/www/PluginContext.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 5b2b0c2 commit edee414

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
import java.util.HashSet;
1515
import java.util.Set;
1616
import java.util.concurrent.ConcurrentHashMap;
17+
import android.content.Context;
18+
import org.apache.cordova.*;
19+
20+
//auth plugin
21+
import com.foxdebug.acode.rk.auth.EncryptedPreferenceManager;
1722

1823
public class Tee extends CordovaPlugin {
1924

@@ -26,10 +31,62 @@ public class Tee extends CordovaPlugin {
2631
// token : list of permissions
2732
private /*static*/ final Map<String, List<String>> permissionStore = new ConcurrentHashMap<>();
2833

34+
35+
36+
private Context context;
37+
38+
39+
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
40+
super.initialize(cordova, webView);
41+
this.context = cordova.getContext();
42+
}
43+
2944
@Override
3045
public boolean execute(String action, JSONArray args, CallbackContext callback)
3146
throws JSONException {
3247

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

129+
130+
private String getPluginIdFromToken(String token) {
131+
for (Map.Entry<String, String> entry : tokenStore.entrySet()) {
132+
if (entry.getValue().equals(token)) {
133+
return entry.getKey();
134+
}
135+
}
136+
return null;
137+
}
138+
72139
//============================================================
73140
//do not change function signatures
74141
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(key, defaultValue = "") {
39+
return new Promise((resolve, reject) => {
40+
exec(
41+
resolve,
42+
reject,
43+
"Tee",
44+
"get_secret",
45+
[this.uuid, key, defaultValue]
46+
);
47+
});
48+
}
49+
50+
51+
setSecret(key, value) {
52+
return new Promise((resolve, reject) => {
53+
exec(
54+
resolve,
55+
reject,
56+
"Tee",
57+
"set_secret",
58+
[this.uuid, key, value]
59+
);
60+
});
61+
}
3762
}
3863

3964
//Object.freeze(this);

0 commit comments

Comments
 (0)