You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bc...@apache.org on 2012/01/05 22:18:36 UTC
[4/6] git commit: Added authentication framework
Added authentication framework
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/e96ae918
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/e96ae918
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/e96ae918
Branch: refs/heads/master
Commit: e96ae9180034fc2c3b81276dfa444a5abaa3710e
Parents: 1d5af10
Author: Olivier Brand <ol...@passbox.com>
Authored: Wed Jan 4 13:47:44 2012 -0800
Committer: Olivier Brand <ol...@passbox.com>
Committed: Wed Jan 4 13:47:44 2012 -0800
----------------------------------------------------------------------
.../src/com/phonegap/AuthenticationToken.java | 49 ++++++
framework/src/com/phonegap/DroidGap.java | 125 ++++++++++++++-
2 files changed, 168 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/e96ae918/framework/src/com/phonegap/AuthenticationToken.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/AuthenticationToken.java b/framework/src/com/phonegap/AuthenticationToken.java
new file mode 100644
index 0000000..a01acf1
--- /dev/null
+++ b/framework/src/com/phonegap/AuthenticationToken.java
@@ -0,0 +1,49 @@
+package com.phonegap;
+
+/**
+ * The Class AuthenticationToken defines the principal and credentials to be used for authenticating a web resource
+ */
+public class AuthenticationToken {
+ private String principal;
+ private String credentials;
+
+ /**
+ * Gets the principal.
+ *
+ * @return the principal
+ */
+ public String getPrincipal() {
+ return principal;
+ }
+
+ /**
+ * Sets the principal.
+ *
+ * @param principal
+ * the new principal
+ */
+ public void setPrincipal(String principal) {
+ this.principal = principal;
+ }
+
+ /**
+ * Gets the credentials.
+ *
+ * @return the credentials
+ */
+ public String getCredentials() {
+ return credentials;
+ }
+
+ /**
+ * Sets the credentials.
+ *
+ * @param credentials
+ * the new credentials
+ */
+ public void setCredentials(String credentials) {
+ this.credentials = credentials;
+ }
+
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/e96ae918/framework/src/com/phonegap/DroidGap.java
----------------------------------------------------------------------
diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java
index f906567..6c13f11 100755
--- a/framework/src/com/phonegap/DroidGap.java
+++ b/framework/src/com/phonegap/DroidGap.java
@@ -18,16 +18,18 @@
*/
package com.phonegap;
-import java.util.HashMap;
+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.Pattern;
import java.util.regex.Matcher;
-import java.util.Iterator;
-import java.io.IOException;
+import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
+import org.xmlpull.v1.XmlPullParserException;
import android.app.AlertDialog;
import android.app.ProgressDialog;
@@ -55,6 +57,7 @@ import android.view.Window;
import android.view.WindowManager;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions.Callback;
+import android.webkit.HttpAuthHandler;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
@@ -67,11 +70,10 @@ import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.LinearLayout;
+import com.phonegap.api.IPlugin;
import com.phonegap.api.LOG;
import com.phonegap.api.PhonegapActivity;
-import com.phonegap.api.IPlugin;
import com.phonegap.api.PluginManager;
-import org.xmlpull.v1.XmlPullParserException;
/**
* 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)
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.
*/
@@ -215,6 +220,88 @@ public class DroidGap extends PhonegapActivity {
// when another application (activity) is started.
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.
*
@@ -1344,6 +1431,32 @@ public class DroidGap extends PhonegapActivity {
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.getPrincipal(), token.getCredentials());
+ }
+ }
+
+
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {