From 0297f4b70361b36c5e7b3c64f7d9133200a57487 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 31 May 2019 18:13:04 +0400 Subject: [PATCH] feat: Make more securely storing login credentials --- app/build.gradle | 1 + .../chronosx88/influence/XMPPConnection.java | 19 +++++++------------ .../influence/XMPPConnectionService.java | 1 - .../influence/helpers/AppHelper.java | 16 +++++++++++----- .../chronosx88/influence/logic/MainLogic.java | 3 +++ .../influence/views/LoginActivity.java | 9 ++++----- 6 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7cb3453..e57d749 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -86,6 +86,7 @@ dependencies { implementation 'org.igniterealtime.smack:smack-experimental:4.3.3' implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' + implementation "de.adorsys.android:securestoragelibrary:1.0.3" } repositories { mavenCentral() diff --git a/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java b/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java index 398d80f..07b6bb9 100644 --- a/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java +++ b/app/src/main/java/io/github/chronosx88/influence/XMPPConnection.java @@ -17,11 +17,8 @@ package io.github.chronosx88.influence; import android.content.Context; -import android.content.SharedPreferences; import android.util.Log; -import androidx.preference.PreferenceManager; - import org.greenrobot.eventbus.EventBus; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.ConnectionListener; @@ -40,11 +37,11 @@ import org.jivesoftware.smackx.mam.MamManager; import org.jivesoftware.smackx.vcardtemp.VCardManager; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.EntityBareJid; -import org.jxmpp.jid.impl.JidCreate; import java.io.IOException; import java.util.Set; +import de.adorsys.android.securestoragelibrary.SecurePreferences; import io.github.chronosx88.influence.helpers.AppHelper; import io.github.chronosx88.influence.helpers.NetworkHandler; import io.github.chronosx88.influence.models.appEvents.AuthenticationStatusEvent; @@ -53,7 +50,6 @@ public class XMPPConnection implements ConnectionListener { private final static String LOG_TAG = "XMPPConnection"; private LoginCredentials credentials = new LoginCredentials(); private XMPPTCPConnection connection = null; - private SharedPreferences prefs; private NetworkHandler networkHandler; private Context context; private Roster roster; @@ -70,10 +66,9 @@ public class XMPPConnection implements ConnectionListener { } public XMPPConnection(Context context) { - this.prefs = PreferenceManager.getDefaultSharedPreferences(context); this.context = context; - String jid = prefs.getString("chatID", null); - String password = prefs.getString("pass", null); + String jid = SecurePreferences.getStringValue("jid", null); + String password = SecurePreferences.getStringValue("pass", null); if(jid != null && password != null) { String username = jid.split("@")[0]; String jabberHost = jid.split("@")[1]; @@ -138,7 +133,7 @@ public class XMPPConnection implements ConnectionListener { } public void disconnect() { - prefs.edit().putBoolean("logged_in", false).apply(); + SecurePreferences.setValue("logged_in", false); if(connection != null) { connection.disconnect(); connection = null; @@ -153,7 +148,7 @@ public class XMPPConnection implements ConnectionListener { @Override public void authenticated(org.jivesoftware.smack.XMPPConnection connection, boolean resumed) { XMPPConnectionService.SESSION_STATE = SessionState.LOGGED_IN; - prefs.edit().putBoolean("logged_in", true).apply(); + SecurePreferences.setValue("logged_in", true); EventBus.getDefault().post(new AuthenticationStatusEvent(AuthenticationStatusEvent.CONNECT_AND_LOGIN_SUCCESSFUL)); } @@ -161,14 +156,14 @@ public class XMPPConnection implements ConnectionListener { public void connectionClosed() { XMPPConnectionService.CONNECTION_STATE = ConnectionState.DISCONNECTED; XMPPConnectionService.SESSION_STATE = SessionState.LOGGED_OUT; - prefs.edit().putBoolean("logged_in", false).apply(); + SecurePreferences.setValue("logged_in", false); } @Override public void connectionClosedOnError(Exception e) { XMPPConnectionService.CONNECTION_STATE = ConnectionState.DISCONNECTED; XMPPConnectionService.SESSION_STATE = SessionState.LOGGED_OUT; - prefs.edit().putBoolean("logged_in", false).apply(); + SecurePreferences.setValue("logged_in", false); Log.e(LOG_TAG, "Connection closed, exception occurred"); e.printStackTrace(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java b/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java index a20b8a1..4ed9006 100644 --- a/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java +++ b/app/src/main/java/io/github/chronosx88/influence/XMPPConnectionService.java @@ -109,7 +109,6 @@ public class XMPPConnectionService extends Service { @Override public void onDestroy() { - super.onDestroy(); onServiceStop(); } diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java index 414c6f3..2efb9c8 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/AppHelper.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import de.adorsys.android.securestoragelibrary.SecurePreferences; import io.github.chronosx88.influence.LoginCredentials; import io.github.chronosx88.influence.XMPPConnection; @@ -89,10 +90,10 @@ public class AppHelper extends MultiDexApplication { AppHelper.xmppConnection = xmppConnection; } - private static void loadLoginCredentials() { + public static void loadLoginCredentials() { currentLoginCredentials = new LoginCredentials(); - String jid = preferences.getString("chatID", null); - String password = preferences.getString("pass", null); + String jid = SecurePreferences.getStringValue("jid", null); + String password = SecurePreferences.getStringValue("pass", null); if(jid != null && password != null) { String username = jid.split("@")[0]; String jabberHost = jid.split("@")[1]; @@ -105,8 +106,9 @@ public class AppHelper extends MultiDexApplication { public static void resetLoginCredentials() { currentLoginCredentials = new LoginCredentials(); - preferences.edit().remove("chatID").apply(); - preferences.edit().remove("pass").apply(); + SecurePreferences.removeValue("jid"); + SecurePreferences.removeValue("pass"); + SecurePreferences.removeValue("logged_in"); } private static void initTrueTime() { @@ -159,4 +161,8 @@ public class AppHelper extends MultiDexApplication { public static void setCurrentChatActivity(String currentChatActivity) { AppHelper.currentChatActivity = currentChatActivity; } + + public static LoginCredentials getCurrentLoginCredentials() { + return currentLoginCredentials; + } } \ No newline at end of file diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java index 7509c76..74f5f4f 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java @@ -61,5 +61,8 @@ public class MainLogic implements CoreContracts.IMainLogicContract { AppHelper.resetLoginCredentials(); context.unbindService(AppHelper.getServiceConnection()); context.stopService(new Intent(context, XMPPConnectionService.class)); + AppHelper.setXmppConnection(null); + AppHelper.setServiceConnection(null); + AppHelper.setJid(null); } } diff --git a/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java b/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java index 8a2d8e2..93103f5 100644 --- a/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java +++ b/app/src/main/java/io/github/chronosx88/influence/views/LoginActivity.java @@ -39,6 +39,7 @@ import org.greenrobot.eventbus.ThreadMode; import java.util.Timer; import java.util.TimerTask; +import de.adorsys.android.securestoragelibrary.SecurePreferences; import io.github.chronosx88.influence.R; import io.github.chronosx88.influence.XMPPConnectionService; import io.github.chronosx88.influence.contracts.CoreContracts; @@ -136,11 +137,9 @@ public class LoginActivity extends AppCompatActivity implements CoreContracts.IL } private void saveLoginCredentials() { - AppHelper.getPreferences().edit() - .putString("chatID", jidEditText.getText().toString()) - .putString("pass", passwordEditText.getText().toString()) - .putBoolean("logged_in", true) - .apply(); + SecurePreferences.setValue("jid", jidEditText.getText().toString()); + SecurePreferences.setValue("pass", passwordEditText.getText().toString()); + SecurePreferences.setValue("logged_in", true); } private void doLogin() {