| 9 | import org.json.JSONException; |
| 10 | |
| 11 | public class Authenticator extends CordovaPlugin { |
| 12 | private static final String TAG = "AcodeAuth"; |
| 13 | private static final String PREFS_FILENAME = "acode_auth_secure"; |
| 14 | private static final String KEY_TOKEN = "auth_token"; |
| 15 | private static final String PRO_PURCHASED = "pro_purchased"; |
| 16 | private static final String KEY_MIGRATED_V2 = "migrated_host_to_domain_cookies"; |
| 17 | private static final String[] API_ORIGINS = { |
| 18 | "https://acode.app" |
| 19 | }; |
| 20 | private static final String[] LEGACY_ORIGINS = { |
| 21 | "https://acode.app", |
| 22 | "https://dev.acode.app" |
| 23 | }; |
| 24 | private EncryptedPreferenceManager prefManager; |
| 25 | |
| 26 | @Override |
| 27 | protected void pluginInitialize() { |
| 28 | Log.d(TAG, "Initializing Authenticator Plugin..."); |
| 29 | this.prefManager = new EncryptedPreferenceManager(this.cordova.getContext(), PREFS_FILENAME); |
| 30 | |
| 31 | WebView androidWebView = (WebView) webView.getView(); |
| 32 | CookieManager.getInstance().setAcceptThirdPartyCookies(androidWebView, true); |
| 33 | |
| 34 | if (!prefManager.getBoolean(KEY_MIGRATED_V2, false)) { |
| 35 | Log.d(TAG, "Migrating: clearing legacy host-scoped cookies"); |
| 36 | clearLegacyCookies(); |
| 37 | prefManager.setBoolean(KEY_MIGRATED_V2, true); |
| 38 | } |
| 39 | |
| 40 | String token = prefManager.getString(KEY_TOKEN, ""); |
| 41 | if (!token.isEmpty()) { |
| 42 | setTokenCookie(token); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { |
| 48 | Log.i(TAG, "Native Action Called: " + action); |
| 49 | |
| 50 | switch (action) { |
| 51 | case "logout": |
| 52 | prefManager.remove(KEY_TOKEN); |
| 53 | cordova.getActivity().runOnUiThread(() -> clearTokenCookie()); |
| 54 | if (callbackContext != null) callbackContext.success(); |
| 55 | return true; |
| 56 | case "saveToken": |
| 57 | String token = args.getString(0); |
| 58 | Log.d(TAG, "Saving new token..."); |
| 59 | prefManager.setString(KEY_TOKEN, token); |
| 60 | cordova.getActivity().runOnUiThread(() -> setTokenCookie(token)); |
| 61 | callbackContext.success(); |
| 62 | return true; |
| 63 | default: |
| 64 | Log.w(TAG, "Attempted to call unknown action: " + action); |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 |
nothing calls this directly
no outgoing calls
no test coverage detected