From 6dabe4c0102e5474a887a3297a11abbaff50d9ae Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Mon, 26 Mar 2012 10:22:37 -0700 Subject: [PATCH] Work on CB-369, Moving Authentication OUT of DroidGap --- .../org/apache/cordova/CordovaWebView.java | 90 ++++++++++++++++++- .../apache/cordova/CordovaWebViewClient.java | 3 +- .../src/org/apache/cordova/DroidGap.java | 84 ----------------- 3 files changed, 90 insertions(+), 87 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index ff809925..526a1832 100644 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -1,11 +1,16 @@ package org.apache.cordova; +import java.util.Hashtable; + import android.content.Context; import android.util.AttributeSet; import android.webkit.WebView; public class CordovaWebView extends WebView { - + + /** The authorization tokens. */ + private Hashtable authenticationTokens = new Hashtable(); + public CordovaWebView(Context context) { super(context); } @@ -22,5 +27,86 @@ public class CordovaWebView extends WebView { boolean privateBrowsing) { super(context, attrs, defStyle, privateBrowsing); } - + + /** + * Sets the authentication token. + * + * @param authenticationToken + * the authentication token + * @param host + * the host + * @param realm + * the realm + */ + public void setAuthenticationToken(AuthenticationToken authenticationToken, String host, String realm) { + + if(host == null) { + host = ""; + } + + if(realm == null) { + realm = ""; + } + + authenticationTokens.put(host.concat(realm), authenticationToken); + } + + /** + * Removes the authentication token. + * + * @param host + * the host + * @param realm + * the realm + * @return the authentication token or null if did not exist + */ + public AuthenticationToken removeAuthenticationToken(String host, String realm) { + return authenticationTokens.remove(host.concat(realm)); + } + + /** + * Gets the authentication token. + * + * In order it tries: + * 1- host + realm + * 2- host + * 3- realm + * 4- no host, no realm + * + * @param host + * the host + * @param realm + * the realm + * @return the authentication token + */ + public AuthenticationToken getAuthenticationToken(String host, String realm) { + AuthenticationToken token = null; + + token = authenticationTokens.get(host.concat(realm)); + + if(token == null) { + // try with just the host + token = authenticationTokens.get(host); + + // Try the realm + if(token == null) { + token = authenticationTokens.get(realm); + } + + // if no host found, just query for default + if(token == null) { + token = authenticationTokens.get(""); + } + } + + return token; + } + + /** + * Clear all authentication tokens. + */ + public void clearAuthenticationTokens() { + authenticationTokens.clear(); + } + } diff --git a/framework/src/org/apache/cordova/CordovaWebViewClient.java b/framework/src/org/apache/cordova/CordovaWebViewClient.java index 5eb24064..26516873 100755 --- a/framework/src/org/apache/cordova/CordovaWebViewClient.java +++ b/framework/src/org/apache/cordova/CordovaWebViewClient.java @@ -173,7 +173,8 @@ public class CordovaWebViewClient extends WebViewClient { String realm) { // get the authentication token - AuthenticationToken token = ctx.getAuthenticationToken(host,realm); + // Note: The WebView MUST be a CordoaWebView + AuthenticationToken token = ((CordovaWebView) view).getAuthenticationToken(host,realm); if(token != null) { handler.proceed(token.getUserName(), token.getPassword()); diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java index 39d5c31a..f5e256ec 100755 --- a/framework/src/org/apache/cordova/DroidGap.java +++ b/framework/src/org/apache/cordova/DroidGap.java @@ -191,9 +191,6 @@ public class DroidGap extends Activity implements CordovaInterface { // (this is not the color for the webview, which is set in HTML) private int backgroundColor = Color.BLACK; - /** The authorization tokens. */ - private Hashtable authenticationTokens = new Hashtable(); - /* * The variables below are used to cache some of the activity properties. */ @@ -212,87 +209,6 @@ public class DroidGap extends Activity implements CordovaInterface { // preferences read from cordova.xml protected PreferenceSet preferences; - - /** - * Sets the authentication token. - * - * @param authenticationToken - * the authentication token - * @param host - * the host - * @param realm - * the realm - */ - public void setAuthenticationToken(AuthenticationToken authenticationToken, String host, String realm) { - - if(host == null) { - host = ""; - } - - if(realm == null) { - realm = ""; - } - - authenticationTokens.put(host.concat(realm), authenticationToken); - } - - /** - * Removes the authentication token. - * - * @param host - * the host - * @param realm - * the realm - * @return the authentication token or null if did not exist - */ - public AuthenticationToken removeAuthenticationToken(String host, String realm) { - return authenticationTokens.remove(host.concat(realm)); - } - - /** - * Gets the authentication token. - * - * In order it tries: - * 1- host + realm - * 2- host - * 3- realm - * 4- no host, no realm - * - * @param host - * the host - * @param realm - * the realm - * @return the authentication token - */ - public AuthenticationToken getAuthenticationToken(String host, String realm) { - AuthenticationToken token = null; - - token = authenticationTokens.get(host.concat(realm)); - - if(token == null) { - // try with just the host - token = authenticationTokens.get(host); - - // Try the realm - if(token == null) { - token = authenticationTokens.get(realm); - } - - // if no host found, just query for default - if(token == null) { - token = authenticationTokens.get(""); - } - } - - return token; - } - - /** - * Clear all authentication tokens. - */ - public void clearAuthenticationTokens() { - authenticationTokens.clear(); - } /**