diff --git a/framework/src/com/phonegap/AuthenticationToken.java b/framework/src/com/phonegap/AuthenticationToken.java new file mode 100644 index 00000000..a01acf1a --- /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; + } + + +} diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index f9065676..6c13f11d 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.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.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.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) {