feat: Make more securely storing login credentials

This commit is contained in:
ChronosX88 2019-05-31 18:13:04 +04:00
parent 759dd48b86
commit 0297f4b703
6 changed files with 26 additions and 23 deletions

View File

@ -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()

View File

@ -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();
}

View File

@ -109,7 +109,6 @@ public class XMPPConnectionService extends Service {
@Override
public void onDestroy() {
super.onDestroy();
onServiceStop();
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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() {