Use auth CallbackHandler to abstract the way credentials are retrieved.
authorDa Risk <da_risk@beem-project.com>
Sun, 23 Sep 2012 03:22:37 +0200
changeset 989 1d65e00847a1
parent 988 586e34fc7594
child 990 b8430fb9b6dd
Use auth CallbackHandler to abstract the way credentials are retrieved.
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/service/auth/PreferenceAuthenticator.java
src/com/beem/project/beem/service/auth/package-info.java
--- a/src/com/beem/project/beem/BeemService.java	Sun Sep 23 03:22:02 2012 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Sun Sep 23 03:22:37 2012 +0200
@@ -44,6 +44,7 @@
 package com.beem.project.beem;
 
 import java.security.GeneralSecurityException;
+
 import javax.net.ssl.SSLContext;
 
 import android.app.Notification;
@@ -66,6 +67,7 @@
 import com.beem.project.beem.service.XmppConnectionAdapter;
 import com.beem.project.beem.service.XmppFacade;
 import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.service.auth.PreferenceAuthenticator;
 import com.beem.project.beem.smack.avatar.AvatarMetadataProvider;
 import com.beem.project.beem.smack.avatar.AvatarProvider;
 import com.beem.project.beem.smack.caps.CapsProvider;
@@ -114,7 +116,6 @@
     private XmppConnectionAdapter mConnection;
     private SharedPreferences mSettings;
     private String mLogin;
-    private String mPassword;
     private String mHost;
     private String mService;
     private int mPort;
@@ -172,6 +173,7 @@
 	mConnectionConfiguration.setTruststorePath("/system/etc/security/cacerts.bks");
 	if (sslContext != null)
 	    mConnectionConfiguration.setCustomSSLContext(sslContext);
+	mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
     }
 
     /**
@@ -209,7 +211,6 @@
 	}
 	String tmpJid = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "").trim();
 	mLogin = StringUtils.parseName(tmpJid);
-	mPassword = mSettings.getString(BeemApplication.ACCOUNT_PASSWORD_KEY, "");
 	mPort = DEFAULT_XMPP_PORT;
 	mService = StringUtils.parseServer(tmpJid);
 	mHost = mService;
@@ -271,7 +272,7 @@
     public XmppConnectionAdapter createConnection() {
 	if (mConnection == null) {
 	    initConnectionConfig();
-	    mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
+	    mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, null, this);
 	}
 	return mConnection;
     }
@@ -346,7 +347,7 @@
      * Utility method to create and make a connection asynchronously.
      */
     private synchronized void createConnectAsync() {
-	if (mConnection == null ) {
+	if (mConnection == null) {
 	    new Thread(new Runnable() {
 
 		@Override
@@ -505,6 +506,11 @@
 	pm.addExtensionProvider("session-expired", COMMAND_NAMESPACE,
 	    new AdHocCommandDataProvider.SessionExpiredError());
 	 */
+
+	// register additionnals sasl mechanisms
+	SASLAuthentication.registerSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME,
+	    SASLGoogleOAuth2Mechanism.class);
+	SASLAuthentication.supportSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME);
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/service/auth/PreferenceAuthenticator.java	Sun Sep 23 03:22:37 2012 +0200
@@ -0,0 +1,92 @@
+/*
+    BEEM is a videoconference application on the Android Platform.
+
+    Copyright (C) 2009-2012 by Frederic-Charles Barthelery,
+                               Nikita Kozlov,
+                               Vincent Veronis.
+
+    This file is part of BEEM.
+
+    BEEM is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BEEM is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
+
+    Please send bug reports with examples or suggestions to
+    contact@beem-project.com or http://www.beem-project.com/
+
+*/
+package com.beem.project.beem.service.auth;
+
+import java.io.IOException;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+
+import com.beem.project.beem.BeemApplication;
+
+import org.apache.harmony.javax.security.auth.callback.Callback;
+import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
+import org.apache.harmony.javax.security.auth.callback.NameCallback;
+import org.apache.harmony.javax.security.auth.callback.PasswordCallback;
+import org.apache.harmony.javax.security.auth.callback.UnsupportedCallbackException;
+import org.apache.harmony.javax.security.sasl.RealmCallback;
+import org.jivesoftware.smack.util.StringUtils;
+
+/**
+ * An Authenticator which look for credentials stored in preferences.
+ */
+public class PreferenceAuthenticator implements CallbackHandler {
+
+    private final SharedPreferences settings;
+
+    /**
+     * Create a PreferenceAuthenticator.
+     *
+     * @param context the Android context.
+     */
+    public PreferenceAuthenticator(final Context context) {
+	settings = PreferenceManager.getDefaultSharedPreferences(context);
+    }
+
+    @Override
+    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+	String tmpJid = settings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "").trim();
+	String service = StringUtils.parseServer(tmpJid);
+
+	for (int i = 0; i < callbacks.length; i++) {
+	    if (callbacks[i] instanceof NameCallback) {
+		String authenticationId = StringUtils.parseName(tmpJid);
+		if (settings.getBoolean(BeemApplication.FULL_JID_LOGIN_KEY, false)
+			|| "gmail.com".equals(service) || "googlemail.com".equals(service)) {
+		    authenticationId = tmpJid;
+		}
+		NameCallback ncb = (NameCallback) callbacks[i];
+		ncb.setName(authenticationId);
+	    } else if (callbacks[i] instanceof PasswordCallback) {
+		PasswordCallback pcb = (PasswordCallback) callbacks[i];
+		// skip if password is asked for PKCS11 (SSL keystore)
+		String prompt = pcb.getPrompt();
+		if (prompt != null && prompt.startsWith("PKCS11 Password:"))
+			continue;
+		String password = settings.getString(BeemApplication.ACCOUNT_PASSWORD_KEY, "");
+		pcb.setPassword(password.toCharArray());
+	    } else if (callbacks[i] instanceof RealmCallback) {
+		RealmCallback rcb = (RealmCallback) callbacks[i];
+		rcb.setText(service);
+	    } else {
+		throw new UnsupportedCallbackException(callbacks[i]);
+	    }
+	}
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/service/auth/package-info.java	Sun Sep 23 03:22:37 2012 +0200
@@ -0,0 +1,31 @@
+/*
+    BEEM is a videoconference application on the Android Platform.
+
+    Copyright (C) 2009-2012 by Frederic-Charles Barthelery,
+                               Nikita Kozlov,
+                               Vincent Veronis.
+
+    This file is part of BEEM.
+
+    BEEM is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BEEM is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
+
+    Please send bug reports with examples or suggestions to
+    contact@beem-project.com or http://www.beem-project.com/
+
+*/
+
+/**
+ * This package contains different Authenticator used to login.
+ */
+package com.beem.project.beem.service.auth;