Return-Path: X-Original-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 23955995B for ; Thu, 5 Jan 2012 21:19:01 +0000 (UTC) Received: (qmail 97054 invoked by uid 500); 5 Jan 2012 21:19:01 -0000 Delivered-To: apmail-incubator-callback-commits-archive@incubator.apache.org Received: (qmail 97040 invoked by uid 500); 5 Jan 2012 21:19:01 -0000 Mailing-List: contact callback-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@incubator.apache.org Delivered-To: mailing list callback-commits@incubator.apache.org Received: (qmail 97033 invoked by uid 99); 5 Jan 2012 21:19:01 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 05 Jan 2012 21:19:01 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.114] (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 05 Jan 2012 21:18:57 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id AAA7D317AE5; Thu, 5 Jan 2012 21:18:36 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: bcurtis@apache.org To: callback-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [4/6] git commit: Added authentication framework Message-Id: <20120105211836.AAA7D317AE5@tyr.zones.apache.org> Date: Thu, 5 Jan 2012 21:18:36 +0000 (UTC) X-Virus-Checked: Checked by ClamAV on apache.org 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 Authored: Wed Jan 4 13:47:44 2012 -0800 Committer: Olivier Brand 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 authenticationTokens = new Hashtable(); + /* * 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) {