Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-14013: (android) Change the InAppBrowser to allow custom schemes for oAuth #263

Merged
Next Next commit
InAppBrowser.java: New method isURLWhileListed to check for whitelist…
…ing.

Newtest in shouldOverrideUrlLoading, to allow whitelisted custom schemes
like"mycoolapp://"

inappbrowser.js: Added "customscheme" channel.
SailingSteve committed Apr 4, 2018
commit 44d9bb0f6a4deff139a1968479030664bf49eff2
88 changes: 62 additions & 26 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
@@ -170,33 +170,10 @@ public void run() {
Boolean shouldAllowNavigation = null;
if (url.startsWith("javascript:")) {
shouldAllowNavigation = true;
} else {
shouldAllowNavigation = isURLWhiteListed(url);
}
if (shouldAllowNavigation == null) {
try {
Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class);
shouldAllowNavigation = (Boolean)iuw.invoke(null, url);
} catch (NoSuchMethodException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (IllegalAccessException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (InvocationTargetException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
}
if (shouldAllowNavigation == null) {
try {
Method gpm = webView.getClass().getMethod("getPluginManager");
PluginManager pm = (PluginManager)gpm.invoke(webView);
Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class);
shouldAllowNavigation = (Boolean)san.invoke(pm, url);
} catch (NoSuchMethodException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (IllegalAccessException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (InvocationTargetException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
}

// load in webview
if (Boolean.TRUE.equals(shouldAllowNavigation)) {
LOG.d(LOG_TAG, "loading in webview");
@@ -302,6 +279,47 @@ public void run() {
return true;
}

/**
* Is the URL or Scheme WhiteListed
* This code exists for compatibility between 3.x and 4.x versions of Cordova.
* Previously the Config class had a static method, isUrlWhitelisted(). That
* responsibility has been moved to the plugins, with an aggregating method in
* PluginManager.
*/
* @param url, the URL as a String
* @return true if WhiteListed, otherwise null or false
*/
private Boolean isURLWhiteListed(String url) {
Boolean shouldAllowNavigation = null;
if (shouldAllowNavigation == null) {
try {
Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class);
shouldAllowNavigation = (Boolean)iuw.invoke(null, url);
} catch (NoSuchMethodException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (IllegalAccessException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (InvocationTargetException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
}
if (shouldAllowNavigation == null) {
try {
Method gpm = webView.getClass().getMethod("getPluginManager");
PluginManager pm = (PluginManager)gpm.invoke(webView);
Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class);
shouldAllowNavigation = (Boolean)san.invoke(pm, url);
} catch (NoSuchMethodException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (IllegalAccessException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
} catch (InvocationTargetException e) {
LOG.d(LOG_TAG, e.getLocalizedMessage());
}
}
return shouldAllowNavigation;
}

/**
* Called when the view navigates.
*/
@@ -1110,6 +1128,24 @@ else if (url.startsWith("sms:")) {
LOG.e(LOG_TAG, "Error sending sms " + url + ":" + e.toString());
}
}
// Test for whitelisted custom scheme names, less than 20 chars long, like mycoolapp: or twitteroauthresponse: (Twitter Oauth Response)
else if (url.matches("^[a-z]{0,20}://.*?$")) {
if (Boolean.TRUE.equals(isURLWhiteListed(url))) {
try {
LOG.w("STEVE IN InAppBrowser.java, whiteliste url SUCCESS: ", url );
JSONObject obj = new JSONObject();
obj.put("type", "customscheme");
obj.put("url", url);
sendUpdate(obj, true);
return true;
} catch (JSONException ex) {
LOG.e(LOG_TAG, "Custom Scheme URI passed in has caused a JSON error.");
}
} else {
LOG.w("STEVE IN InAppBrowser.java, whitelisted url FAILURE: ", url );
}
}

return false;
}

1 change: 1 addition & 0 deletions www/inappbrowser.js
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@
'loadstart': channel.create('loadstart'),
'loadstop': channel.create('loadstop'),
'loaderror': channel.create('loaderror'),
'customscheme': channel.create('customscheme'),
'exit': channel.create('exit')
};
}