Work on CB-369, Moving Authentication OUT of DroidGap

This commit is contained in:
Joe Bowser 2012-03-26 10:22:37 -07:00
parent 8ecfcb12c7
commit 6dabe4c010
3 changed files with 90 additions and 87 deletions

View File

@ -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<String, AuthenticationToken> authenticationTokens = new Hashtable<String, AuthenticationToken>();
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();
}
}

View File

@ -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());

View File

@ -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<String, AuthenticationToken> authenticationTokens = new Hashtable<String, AuthenticationToken>();
/*
* 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();
}
/**