|
| 1 | +package com.foxdebug.acode.rk.plugin; |
| 2 | + |
| 3 | +import org.apache.cordova.CallbackContext; |
| 4 | +import org.apache.cordova.CordovaPlugin; |
| 5 | +import org.json.JSONArray; |
| 6 | +import org.json.JSONException; |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +import java.util.UUID; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
| 17 | + |
| 18 | +public class Tee extends CordovaPlugin { |
| 19 | + |
| 20 | + // pluginId : token |
| 21 | + private /*static*/ final Map<String, String> tokenStore = new ConcurrentHashMap<>(); |
| 22 | + |
| 23 | + //assigned tokens |
| 24 | + private /*static*/ final Set<String> disclosed = ConcurrentHashMap.newKeySet(); |
| 25 | + |
| 26 | + // token : list of permissions |
| 27 | + private /*static*/ final Map<String, List<String>> permissionStore = new ConcurrentHashMap<>(); |
| 28 | + |
| 29 | + @Override |
| 30 | + public boolean execute(String action, JSONArray args, CallbackContext callback) |
| 31 | + throws JSONException { |
| 32 | + |
| 33 | + if ("requestToken".equals(action)) { |
| 34 | + String pluginId = args.getString(0); |
| 35 | + String pluginJson = args.getString(1); |
| 36 | + handleTokenRequest(pluginId, pluginJson, callback); |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + if ("grantedPermission".equals(action)) { |
| 41 | + String token = args.getString(0); |
| 42 | + String permission = args.getString(1); |
| 43 | + |
| 44 | + if (!permissionStore.containsKey(token)) { |
| 45 | + callback.error("INVALID_TOKEN"); |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + boolean granted = grantedPermission(token, permission); |
| 50 | + callback.success(granted ? 1 : 0); |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + if ("listAllPermissions".equals(action)) { |
| 55 | + String token = args.getString(0); |
| 56 | + |
| 57 | + if (!permissionStore.containsKey(token)) { |
| 58 | + callback.error("INVALID_TOKEN"); |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + List<String> permissions = listAllPermissions(token); |
| 63 | + JSONArray result = new JSONArray(permissions); |
| 64 | + |
| 65 | + callback.success(result); |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + //============================================================ |
| 73 | + //do not change function signatures |
| 74 | + public boolean isTokenValid(String token, String pluginId) { |
| 75 | + String storedToken = tokenStore.get(pluginId); |
| 76 | + return storedToken != null && token.equals(storedToken); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + public boolean grantedPermission(String token, String permission) { |
| 81 | + List<String> permissions = permissionStore.get(token); |
| 82 | + return permissions != null && permissions.contains(permission); |
| 83 | + } |
| 84 | + |
| 85 | + public List<String> listAllPermissions(String token) { |
| 86 | + List<String> permissions = permissionStore.get(token); |
| 87 | + |
| 88 | + if (permissions == null) { |
| 89 | + return new ArrayList<>(); |
| 90 | + } |
| 91 | + |
| 92 | + return new ArrayList<>(permissions); // return copy (safe) |
| 93 | + } |
| 94 | + //============================================================ |
| 95 | + |
| 96 | + |
| 97 | + private synchronized void handleTokenRequest( |
| 98 | + String pluginId, |
| 99 | + String pluginJson, |
| 100 | + CallbackContext callback |
| 101 | + ) { |
| 102 | + |
| 103 | + if (disclosed.contains(pluginId)) { |
| 104 | + callback.error("TOKEN_ALREADY_ISSUED"); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + String token = tokenStore.get(pluginId); |
| 109 | + |
| 110 | + if (token == null) { |
| 111 | + token = UUID.randomUUID().toString(); |
| 112 | + tokenStore.put(pluginId, token); |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + JSONObject json = new JSONObject(pluginJson); |
| 117 | + JSONArray permissions = json.optJSONArray("permissions"); |
| 118 | + |
| 119 | + List<String> permissionList = new ArrayList<>(); |
| 120 | + |
| 121 | + if (permissions != null) { |
| 122 | + for (int i = 0; i < permissions.length(); i++) { |
| 123 | + permissionList.add(permissions.getString(i)); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Bind permissions to token |
| 128 | + permissionStore.put(token, permissionList); |
| 129 | + |
| 130 | + } catch (JSONException e) { |
| 131 | + callback.error("INVALID_PLUGIN_JSON"); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + disclosed.add(pluginId); |
| 136 | + callback.success(token); |
| 137 | + } |
| 138 | +} |
0 commit comments