Skip to content
Open
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
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"com.foxdebug.acode.rk.exec.proot": {},
"com.foxdebug.acode.rk.exec.terminal": {},
"com.foxdebug.acode.rk.customtabs": {},
"com.foxdebug.acode.rk.plugin.plugincontext": {},
"com.foxdebug.acode.rk.auth": {}
},
"platforms": [
Expand Down Expand Up @@ -69,6 +70,7 @@
"com.foxdebug.acode.rk.customtabs": "file:src/plugins/custom-tabs",
"com.foxdebug.acode.rk.exec.proot": "file:src/plugins/proot",
"com.foxdebug.acode.rk.exec.terminal": "file:src/plugins/terminal",
"com.foxdebug.acode.rk.plugin.plugincontext": "file:src/plugins/pluginContext",
"cordova-android": "^14.0.1",
"cordova-clipboard": "^1.3.0",
"cordova-plugin-advanced-http": "^3.3.1",
Expand Down
4 changes: 4 additions & 0 deletions src/lib/loadPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
cacheFileUrl: await helpers.toInternalUri(cacheFile),
cacheFile: fsOperation(cacheFile),
firstInit: justInstalled,
ctx: await PluginContext.generate(
pluginId,
JSON.stringify(pluginJson),
),
Comment on lines +59 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No null check for PluginContext before use. If the pluginContext Cordova plugin fails to load, this will throw an error and prevent all plugins from loading.

});

resolve();
Expand Down
17 changes: 17 additions & 0 deletions src/plugins/pluginContext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "com.foxdebug.acode.rk.plugin.plugincontext",
"version": "1.0.0",
"description": "PluginContext",
"cordova": {
"id": "com.foxdebug.acode.rk.plugin.plugincontext",
"platforms": [
"android"
]
},
"keywords": [
"ecosystem:cordova",
"cordova-android"
],
"author": "@RohitKushvaha01",
"license": "MIT"
}
22 changes: 22 additions & 0 deletions src/plugins/pluginContext/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="com.foxdebug.acode.rk.plugin.plugincontext" version="1.0.0">
<name>PluginContext</name>


<js-module name="PluginContext" src="www/PluginContext.js">
<clobbers target="window.PluginContext" />
</js-module>


<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="Tee">
<param name="android-package" value="com.foxdebug.acode.rk.plugin.Tee" />
</feature>
</config-file>

<source-file src="src/android/Tee.java" target-dir="src/com/foxdebug/acode/rk/plugin" />


</platform>
</plugin>
136 changes: 136 additions & 0 deletions src/plugins/pluginContext/src/android/Tee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.foxdebug.acode.rk.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.UUID;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;

public class Tee extends CordovaPlugin {

// pluginId : token
private /*static*/ final Map<String, String> tokenStore = new HashMap<>();

//assigned tokens
private /*static*/ final HashSet<String> disclosed = new HashSet<>();

// token : list of permissions
private /*static*/ final Map<String, List<String>> permissionStore = new HashMap<>();

@Override
public boolean execute(String action, JSONArray args, CallbackContext callback)
throws JSONException {

if ("requestToken".equals(action)) {
String pluginId = args.getString(0);
String pluginJson = args.getString(1);
handleTokenRequest(pluginId, pluginJson, callback);
return true;
}

if ("grantedPermission".equals(action)) {
String token = args.getString(0);
String permission = args.getString(1);

if (!permissionStore.containsKey(token)) {
callback.error("INVALID_TOKEN");
return true;
}

boolean granted = grantedPermission(token, permission);
callback.success(granted ? 1 : 0);
return true;
}

if ("listAllPermissions".equals(action)) {
String token = args.getString(0);

if (!permissionStore.containsKey(token)) {
callback.error("INVALID_TOKEN");
return true;
}

List<String> permissions = listAllPermissions(token);
JSONArray result = new JSONArray(permissions);

callback.success(result);
return true;
}

return false;
}

//============================================================
//do not change function signatures
public boolean isTokenValid(String token, String pluginId) {
String storedToken = tokenStore.get(pluginId);
return storedToken != null && token.equals(storedToken);
}


public boolean grantedPermission(String token, String permission) {
List<String> permissions = permissionStore.get(token);
return permissions != null && permissions.contains(permission);
}

public List<String> listAllPermissions(String token) {
List<String> permissions = permissionStore.get(token);

if (permissions == null) {
return new ArrayList<>();
}

return new ArrayList<>(permissions); // return copy (safe)
}
//============================================================


private synchronized void handleTokenRequest(
String pluginId,
String pluginJson,
CallbackContext callback
) {

if (disclosed.contains(pluginId)) {
callback.error("TOKEN_ALREADY_ISSUED");
return;
}

String token = tokenStore.get(pluginId);

if (token == null) {
token = UUID.randomUUID().toString();
tokenStore.put(pluginId, token);
}

try {
JSONObject json = new JSONObject(pluginJson);
JSONArray permissions = json.optJSONArray("permissions");

List<String> permissionList = new ArrayList<>();

if (permissions != null) {
for (int i = 0; i < permissions.length(); i++) {
permissionList.add(permissions.getString(i));
}
}

// Bind permissions to token
permissionStore.put(token, permissionList);

} catch (JSONException e) {
callback.error("INVALID_PLUGIN_JSON");
return;
}

disclosed.add(pluginId);
callback.success(token);
}
}
65 changes: 65 additions & 0 deletions src/plugins/pluginContext/www/PluginContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var exec = require("cordova/exec");

const PluginContext = (function () {
//=============================
class _PluginContext {
constructor(uuid) {
this.created_at = Date.now();
this.uuid = uuid;
Object.freeze(this);
}

toString() {
return this.uuid;
}

[Symbol.toPrimitive](hint) {
if (hint === "number") {
return NaN; // prevent numeric coercion
}
return this.uuid;
}

grantedPermission(permission) {
return new Promise((resolve, reject) => {
exec(resolve, reject, "Tee", "grantedPermission", [
this.uuid,
permission,
]);
});
}

listAllPermissions() {
return new Promise((resolve, reject) => {
exec(resolve, reject, "Tee", "listAllPermissions", [this.uuid]);
});
}
}

//Object.freeze(this);

//===============================

return {
generate: async function (pluginId, pluginJson) {
try {
function requestToken(pluginId) {
return new Promise((resolve, reject) => {
exec(resolve, reject, "Tee", "requestToken", [
pluginId,
pluginJson,
]);
});
}

const uuid = await requestToken(pluginId);
return new _PluginContext(uuid);
} catch (err) {
console.warn(`PluginContext creation failed for pluginId ${pluginId}:`);
return null;
}
},
};
})();

module.exports = PluginContext;