Clear database on unpair.

This commit is contained in:
Mikunj 2019-11-22 09:35:15 +11:00
parent f753dd1ea9
commit 733ab06e70
4 changed files with 42 additions and 6 deletions

View File

@ -70,6 +70,7 @@ import org.thoughtcrime.securesms.loki.LokiAPIDatabase;
import org.thoughtcrime.securesms.loki.LokiPublicChatManager; import org.thoughtcrime.securesms.loki.LokiPublicChatManager;
import org.thoughtcrime.securesms.loki.LokiRSSFeedPoller; import org.thoughtcrime.securesms.loki.LokiRSSFeedPoller;
import org.thoughtcrime.securesms.loki.LokiUserDatabase; import org.thoughtcrime.securesms.loki.LokiUserDatabase;
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.providers.BlobProvider; import org.thoughtcrime.securesms.providers.BlobProvider;
@ -202,6 +203,9 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
// Loki - Update device mappings // Loki - Update device mappings
if (setUpStorageAPIIfNeeded()) { if (setUpStorageAPIIfNeeded()) {
LokiStorageAPI.Companion.getShared().updateUserDeviceMappings(); LokiStorageAPI.Companion.getShared().updateUserDeviceMappings();
if (TextSecurePreferences.needsRevocationCheck(this)) {
checkNeedsRevocation();
}
} }
} }
@ -595,13 +599,20 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
} }
// endregion // endregion
public void checkNeedsRevocation() {
MultiDeviceUtilities.checkForRevocation(this);
}
public void checkNeedsDatabaseReset() { public void checkNeedsDatabaseReset() {
if (TextSecurePreferences.resetDatabase(this)) { if (TextSecurePreferences.resetDatabase(this)) {
PreferenceManager.getDefaultSharedPreferences(this).edit().clear().commit(); boolean wasUnlinked = TextSecurePreferences.databaseResetFromUnpair(this);
TextSecurePreferences.clearAll(this);
MasterSecretUtil.clear(this); MasterSecretUtil.clear(this);
if (this.deleteDatabase("signal.db")) { if (this.deleteDatabase("signal.db")) {
Log.d("Loki", "Deleted database"); Log.d("Loki", "Deleted database");
} }
// Loki - Re-set the preference so we can use it in the starting screen to determine whether device was unlinked or not
TextSecurePreferences.setDatabaseResetFromUnpair(this, wasUnlinked);
} }
} }

View File

@ -331,7 +331,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
SignalServiceDataMessage message = content.getDataMessage().get(); SignalServiceDataMessage message = content.getDataMessage().get();
boolean isMediaMessage = message.getAttachments().isPresent() || message.getQuote().isPresent() || message.getSharedContacts().isPresent() || message.getPreviews().isPresent() || message.getSticker().isPresent(); boolean isMediaMessage = message.getAttachments().isPresent() || message.getQuote().isPresent() || message.getSharedContacts().isPresent() || message.getPreviews().isPresent() || message.getSticker().isPresent();
if (message.isUnpairingRequest()) { if (!envelope.isFriendRequest() && message.isUnpairingRequest()) {
// Make sure we got the request from our primary device // Make sure we got the request from our primary device
String ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context); String ourPrimaryDevice = TextSecurePreferences.getMasterHexEncodedPublicKey(context);
if (ourPrimaryDevice != null && ourPrimaryDevice.equals(content.getSender())) { if (ourPrimaryDevice != null && ourPrimaryDevice.equals(content.getSender())) {

View File

@ -8,6 +8,7 @@ import nl.komponents.kovenant.functional.bind
import nl.komponents.kovenant.functional.map import nl.komponents.kovenant.functional.map
import nl.komponents.kovenant.then import nl.komponents.kovenant.then
import nl.komponents.kovenant.toFailVoid import nl.komponents.kovenant.toFailVoid
import nl.komponents.kovenant.ui.successUi
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.Address
@ -35,13 +36,15 @@ fun checkForRevocation(context: Context) {
LokiStorageAPI.shared.fetchDeviceMappings(primaryDevice).bind { mappings -> LokiStorageAPI.shared.fetchDeviceMappings(primaryDevice).bind { mappings ->
val ourMapping = mappings.find { it.secondaryDevicePublicKey == ourDevice } val ourMapping = mappings.find { it.secondaryDevicePublicKey == ourDevice }
if (ourMapping != null) throw Error("Device has not been revoked") if (ourMapping != null) throw Error("Device has not been revoked")
// remove pairing auths for our device // remove pairing authorisations for our device
DatabaseFactory.getLokiAPIDatabase(context).removePairingAuthorisations(ourDevice) DatabaseFactory.getLokiAPIDatabase(context).removePairingAuthorisations(ourDevice)
LokiStorageAPI.shared.updateUserDeviceMappings() LokiStorageAPI.shared.updateUserDeviceMappings()
}.success { }.successUi {
// TODO: Revoke here TextSecurePreferences.setNeedsRevocationCheck(context, false)
ApplicationContext.getInstance(context).clearData()
}.fail { error -> }.fail { error ->
Log.d("Loki", "Revocation check failed: $error") TextSecurePreferences.setNeedsRevocationCheck(context, true)
Log.d("Loki", "Revocation check failed: ${error.message ?: error}")
} }
} }

View File

@ -1187,11 +1187,33 @@ public class TextSecurePreferences {
} }
public static void setResetDatabase(Context context, boolean resetDatabase) { public static void setResetDatabase(Context context, boolean resetDatabase) {
// We do it this way so that it gets persisted in storage straight away
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("database_reset", resetDatabase).commit(); PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("database_reset", resetDatabase).commit();
} }
public static boolean resetDatabase(Context context) { public static boolean resetDatabase(Context context) {
return getBooleanPreference(context, "database_reset", false); return getBooleanPreference(context, "database_reset", false);
} }
public static void setDatabaseResetFromUnpair(Context context, boolean value) {
// We do it this way so that it gets persisted in storage straight away
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("database_reset_unpair", value).commit();
}
public static boolean databaseResetFromUnpair(Context context) {
return getBooleanPreference(context, "database_reset_unpair", false);
}
public static void setNeedsRevocationCheck(Context context, boolean needsCheck) {
setBooleanPreference(context, "needs_revocation", needsCheck);
}
public static boolean needsRevocationCheck(Context context) {
return getBooleanPreference(context, "needs_revocation", false);
}
// endregion // endregion
public static void clearAll(Context context) {
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();
}
} }