Refactor the encryption preference authenticator
authorDa Risk <da_risk@beem-project.com>
Thu, 07 Jan 2016 16:38:16 +0100
changeset 1075 af8866eba015
parent 1074 fde61b09cd8d
child 1076 137d0bf0e959
Refactor the encryption preference authenticator
app/build.gradle
app/src/main/java/com/beem/project/beem/BeemApplication.java
app/src/main/java/com/beem/project/beem/BeemService.java
app/src/main/java/com/beem/project/beem/service/auth/EncryptedPasswordPreferenceAuthenticator.java
app/src/main/java/com/beem/project/beem/service/auth/PreferenceAuthenticator.java
app/src/main/java/com/beem/project/beem/ui/wizard/AccountConfigureFragment.java
--- a/app/build.gradle	Fri Nov 13 18:17:25 2015 +0100
+++ b/app/build.gradle	Thu Jan 07 16:38:16 2016 +0100
@@ -33,7 +33,7 @@
     compile project(":third_parties:memorizingtrustmanager")
     compile project(":third_parties:stroke")
     compile project(":third_parties:mmssmiley")
-    compile 'com.android.support:support-v4:22.0.0'
+    compile 'com.android.support:support-v4:23.1.1'
     compile 'org.jitsi:org.otr4j:0.22'
 }
 
--- a/app/src/main/java/com/beem/project/beem/BeemApplication.java	Fri Nov 13 18:17:25 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/BeemApplication.java	Thu Jan 07 16:38:16 2016 +0100
@@ -69,7 +69,7 @@
     /** Preference key to know if the account password is encrypted. */
     public static final String ACCOUNT_PASSWORD_IS_ENCRYPTED_KEY = "account_password_is_encrypted";
     /** Preference key to store the the account password encryption IV. */
-    public static final String ACCOUNT_PASSWORD_ENCRYPTION_IV = "account_password_encryption_iv";
+    public static final String ACCOUNT_PASSWORD_ENCRYPTION_IV_KEY = "account_password_encryption_iv";
     /** Preference key set to true if using an Android account . */
     public static final String USE_SYSTEM_ACCOUNT_KEY = "use_system_account";
 
@@ -128,6 +128,11 @@
     /** Preference key to show the jid in the contact list. */
     public static final String SHOW_JID = "show_jid";
 
+    /**
+     * Key alias used to retrieve the encryption key for the account password.
+     */
+    public static final String PASSWORD_ENCRYPTION_KEY_ALIAS = "Beem-password-key";
+
     //TODO add the other one
 
     private boolean mIsConnected;
--- a/app/src/main/java/com/beem/project/beem/BeemService.java	Fri Nov 13 18:17:25 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/BeemService.java	Thu Jan 07 16:38:16 2016 +0100
@@ -72,6 +72,7 @@
 import com.beem.project.beem.service.XmppFacade;
 import com.beem.project.beem.service.aidl.IXmppFacade;
 import com.beem.project.beem.service.auth.AccountAuthenticator;
+import com.beem.project.beem.service.auth.EncryptedPasswordPreferenceAuthenticator;
 import com.beem.project.beem.service.auth.PreferenceAuthenticator;
 import com.beem.project.beem.smack.avatar.AvatarMetadataProvider;
 import com.beem.project.beem.smack.avatar.AvatarProvider;
@@ -171,7 +172,8 @@
 		mConnectionConfiguration = new ConnectionConfiguration(mHost, mPort, mService, proxyInfo);
 	    else
 		mConnectionConfiguration = new ConnectionConfiguration(mService, proxyInfo);
-	    mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
+		PreferenceAuthenticator authenticator = createPreferenceAuthenticator();
+		mConnectionConfiguration.setCallbackHandler(authenticator);
 	}
 
 	if (mSettings.getBoolean("settings_key_xmpp_tls_use", false)
@@ -189,6 +191,14 @@
 	    mConnectionConfiguration.setCustomSSLContext(sslContext);
     }
 
+	private PreferenceAuthenticator createPreferenceAuthenticator() {
+		if (!mSettings.getBoolean(BeemApplication.ACCOUNT_PASSWORD_IS_ENCRYPTED_KEY, false)) {
+			return new PreferenceAuthenticator(this);
+		}
+		String base64Iv = mSettings.getString(BeemApplication.ACCOUNT_PASSWORD_ENCRYPTION_IV_KEY, "");
+		return new EncryptedPasswordPreferenceAuthenticator(this, BeemApplication.PASSWORD_ENCRYPTION_KEY_ALIAS, base64Iv);
+	}
+
     /**
      * Get the save proxy configuration.
      *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/src/main/java/com/beem/project/beem/service/auth/EncryptedPasswordPreferenceAuthenticator.java	Thu Jan 07 16:38:16 2016 +0100
@@ -0,0 +1,33 @@
+package com.beem.project.beem.service.auth;
+
+import android.content.Context;
+import com.beem.project.beem.utils.EncryptionManager;
+import org.jivesoftware.smack.util.Base64;
+
+/**
+ * Created by darisk on 07/01/16.
+ */
+public class EncryptedPasswordPreferenceAuthenticator extends PreferenceAuthenticator {
+    private final String encryptionKeyAlias;
+    private final String base64Iv;
+    private final EncryptionManager encryptionManager;
+
+    /**
+     * Create a PreferenceAuthenticator.
+     *
+     * @param context the Android context.
+     */
+    public EncryptedPasswordPreferenceAuthenticator(Context context, String encryptionKeyAlias, String base64Iv) {
+        super(context);
+        this.encryptionKeyAlias = encryptionKeyAlias;
+        this.base64Iv = base64Iv;
+        encryptionManager = new EncryptionManager();
+    }
+
+    @Override
+    protected String getPassword() {
+        String password = super.getPassword();
+        byte[] encryptionIv = Base64.decode(base64Iv);
+        return encryptionManager.decryptString(password, encryptionKeyAlias, encryptionIv);
+    }
+}
--- a/app/src/main/java/com/beem/project/beem/service/auth/PreferenceAuthenticator.java	Fri Nov 13 18:17:25 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/service/auth/PreferenceAuthenticator.java	Thu Jan 07 16:38:16 2016 +0100
@@ -33,15 +33,12 @@
 import android.preference.PreferenceManager;
 
 import com.beem.project.beem.BeemApplication;
-import com.beem.project.beem.utils.EncryptionManager;
-
 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.Base64;
 import org.jivesoftware.smack.util.StringUtils;
 
 /**
@@ -50,7 +47,6 @@
 public class PreferenceAuthenticator implements CallbackHandler {
 
     private final SharedPreferences settings;
-	private final EncryptionManager encryptionManager;
 
     /**
      * Create a PreferenceAuthenticator.
@@ -59,7 +55,6 @@
      */
     public PreferenceAuthenticator(final Context context) {
 	settings = PreferenceManager.getDefaultSharedPreferences(context);
-		encryptionManager = new EncryptionManager();
 	}
 
     @Override
@@ -94,13 +89,8 @@
 	}
     }
 
-	private String getPassword() {
+	protected String getPassword() {
 		String password = settings.getString(BeemApplication.ACCOUNT_PASSWORD_KEY, "");
-		if (settings.getBoolean(BeemApplication.ACCOUNT_PASSWORD_IS_ENCRYPTED_KEY, false)) {
-			byte[] encryptionIv = Base64.decode(settings.getString(BeemApplication.ACCOUNT_PASSWORD_ENCRYPTION_IV, ""));
-
-			password = encryptionManager.decryptString(password, "Beem-password-key", encryptionIv);
-		}
 		return password;
 	}
 
--- a/app/src/main/java/com/beem/project/beem/ui/wizard/AccountConfigureFragment.java	Fri Nov 13 18:17:25 2015 +0100
+++ b/app/src/main/java/com/beem/project/beem/ui/wizard/AccountConfigureFragment.java	Thu Jan 07 16:38:16 2016 +0100
@@ -85,6 +85,8 @@
 import org.jivesoftware.smack.util.Base64;
 import org.jivesoftware.smack.util.StringUtils;
 
+import static com.beem.project.beem.BeemApplication.PASSWORD_ENCRYPTION_KEY_ALIAS;
+
 /**
  * Fragment to enter the information required in order to configure a XMPP account.
  *
@@ -206,12 +208,17 @@
 	} else if (v == mManualConfigButton) {
 	    onManualConfigurationSelected();
 	} else if (v == mSelectAccountButton) {
-	    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
-		Intent i = AccountManager.newChooseAccountIntent(null, null,
-								 new String[] {GOOGLE_ACCOUNT_TYPE},
-								 true, null, null, null, null);
+		Intent i = null;
+		if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
+			i = AccountManager.newChooseAccountIntent(null, null,
+					new String[]{GOOGLE_ACCOUNT_TYPE},
+					null, null, null, null);
+		} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
+			i = AccountManager.newChooseAccountIntent(null, null,
+					new String[]{GOOGLE_ACCOUNT_TYPE},
+					true, null, null, null, null);
+		}
 		startActivityForResult(i, SELECT_ACCOUNT_CODE);
-	    }
 	}
     }
 
@@ -296,13 +303,13 @@
 		String encryptedPass = pass;
 		boolean isEncryptedPass = false;
 		if (encryptionManager.isEncryptionAvailable()) {
-			if (!encryptionManager.hasEncryptionKey("Beem-password-key")) {
-				encryptionManager.generateEncryptionKey("Beem-password-key");
+			if (!encryptionManager.hasEncryptionKey(PASSWORD_ENCRYPTION_KEY_ALIAS)) {
+				encryptionManager.generateEncryptionKey(PASSWORD_ENCRYPTION_KEY_ALIAS);
 			}
-			encryptedPass = encryptionManager.encryptString(encryptedPass, "Beem-password-key");
+			encryptedPass = encryptionManager.encryptString(encryptedPass, PASSWORD_ENCRYPTION_KEY_ALIAS);
 			if (encryptedPass != null) {
 				String encryptionIV = Base64.encodeBytes(encryptionManager.getLatestEncryptionIv());
-				edit.putString(BeemApplication.ACCOUNT_PASSWORD_ENCRYPTION_IV, encryptionIV);
+				edit.putString(BeemApplication.ACCOUNT_PASSWORD_ENCRYPTION_IV_KEY, encryptionIV);
 				isEncryptedPass = true;
 			}
 		}
@@ -538,7 +545,7 @@
     /**
      * A progress Fragment.
      */
-    public static class ProgressFragment extends DialogFragment	 {
+    public static class ProgressFragment extends DialogFragment {
 
 	/**
 	 * Create a new ProgressFragment.