mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-27 04:42:51 +08:00
Merge pull request #1 from obrand/basic-auth
Added authentication framework
This commit is contained in:
commit
3466ef6dfd
51
framework/src/com/phonegap/AuthenticationToken.java
Normal file
51
framework/src/com/phonegap/AuthenticationToken.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package com.phonegap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Class AuthenticationToken defines the userName and password to be used for authenticating a web resource
|
||||||
|
*/
|
||||||
|
public class AuthenticationToken {
|
||||||
|
private String userName;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the user name.
|
||||||
|
*
|
||||||
|
* @return the user name
|
||||||
|
*/
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the user name.
|
||||||
|
*
|
||||||
|
* @param userName
|
||||||
|
* the new user name
|
||||||
|
*/
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the password.
|
||||||
|
*
|
||||||
|
* @return the password
|
||||||
|
*/
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the password.
|
||||||
|
*
|
||||||
|
* @param password
|
||||||
|
* the new password
|
||||||
|
*/
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -18,16 +18,18 @@
|
|||||||
*/
|
*/
|
||||||
package com.phonegap;
|
package com.phonegap;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Stack;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Stack;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
@ -55,6 +57,7 @@ import android.view.Window;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import android.webkit.ConsoleMessage;
|
import android.webkit.ConsoleMessage;
|
||||||
import android.webkit.GeolocationPermissions.Callback;
|
import android.webkit.GeolocationPermissions.Callback;
|
||||||
|
import android.webkit.HttpAuthHandler;
|
||||||
import android.webkit.JsPromptResult;
|
import android.webkit.JsPromptResult;
|
||||||
import android.webkit.JsResult;
|
import android.webkit.JsResult;
|
||||||
import android.webkit.SslErrorHandler;
|
import android.webkit.SslErrorHandler;
|
||||||
@ -67,11 +70,10 @@ import android.webkit.WebViewClient;
|
|||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
|
|
||||||
|
import com.phonegap.api.IPlugin;
|
||||||
import com.phonegap.api.LOG;
|
import com.phonegap.api.LOG;
|
||||||
import com.phonegap.api.PhonegapActivity;
|
import com.phonegap.api.PhonegapActivity;
|
||||||
import com.phonegap.api.IPlugin;
|
|
||||||
import com.phonegap.api.PluginManager;
|
import com.phonegap.api.PluginManager;
|
||||||
import org.xmlpull.v1.XmlPullParserException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is the main Android activity that represents the PhoneGap
|
* This class is the main Android activity that represents the PhoneGap
|
||||||
@ -199,6 +201,9 @@ public class DroidGap extends PhonegapActivity {
|
|||||||
// (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.
|
||||||
*/
|
*/
|
||||||
@ -215,6 +220,88 @@ public class DroidGap extends PhonegapActivity {
|
|||||||
// when another application (activity) is started.
|
// when another application (activity) is started.
|
||||||
protected boolean keepRunning = true;
|
protected boolean keepRunning = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
*
|
*
|
||||||
@ -1344,6 +1431,32 @@ public class DroidGap extends PhonegapActivity {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On received http auth request.
|
||||||
|
* The method reacts on all registered authentication tokens. There is one and only one authentication token for any host + realm combination
|
||||||
|
*
|
||||||
|
* @param view
|
||||||
|
* the view
|
||||||
|
* @param handler
|
||||||
|
* the handler
|
||||||
|
* @param host
|
||||||
|
* the host
|
||||||
|
* @param realm
|
||||||
|
* the realm
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host,
|
||||||
|
String realm) {
|
||||||
|
|
||||||
|
// get the authentication token
|
||||||
|
AuthenticationToken token = getAuthenticationToken(host,realm);
|
||||||
|
|
||||||
|
if(token != null) {
|
||||||
|
handler.proceed(token.getUserName(), token.getPassword());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user