mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 20:33:07 +08:00
Work on CB-369, Moving Authentication OUT of DroidGap
This commit is contained in:
parent
8ecfcb12c7
commit
6dabe4c010
@ -1,11 +1,16 @@
|
|||||||
package org.apache.cordova;
|
package org.apache.cordova;
|
||||||
|
|
||||||
|
import java.util.Hashtable;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
|
|
||||||
public class CordovaWebView extends WebView {
|
public class CordovaWebView extends WebView {
|
||||||
|
|
||||||
|
/** The authorization tokens. */
|
||||||
|
private Hashtable<String, AuthenticationToken> authenticationTokens = new Hashtable<String, AuthenticationToken>();
|
||||||
|
|
||||||
public CordovaWebView(Context context) {
|
public CordovaWebView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
}
|
}
|
||||||
@ -23,4 +28,85 @@ public class CordovaWebView extends WebView {
|
|||||||
super(context, attrs, defStyle, 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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,8 @@ public class CordovaWebViewClient extends WebViewClient {
|
|||||||
String realm) {
|
String realm) {
|
||||||
|
|
||||||
// get the authentication token
|
// 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) {
|
if(token != null) {
|
||||||
handler.proceed(token.getUserName(), token.getPassword());
|
handler.proceed(token.getUserName(), token.getPassword());
|
||||||
|
@ -191,9 +191,6 @@ public class DroidGap extends Activity implements CordovaInterface {
|
|||||||
// (this is not the color for the webview, which is set in HTML)
|
// (this is not the color for the webview, which is set in HTML)
|
||||||
private int backgroundColor = Color.BLACK;
|
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.
|
* The variables below are used to cache some of the activity properties.
|
||||||
*/
|
*/
|
||||||
@ -213,87 +210,6 @@ public class DroidGap extends Activity implements CordovaInterface {
|
|||||||
// preferences read from cordova.xml
|
// preferences read from cordova.xml
|
||||||
protected PreferenceSet preferences;
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the activity is first created.
|
* Called when the activity is first created.
|
||||||
|
Loading…
Reference in New Issue
Block a user