From 8e495d087b250f26f83ee985e377e0c6cc53663d Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 3 Oct 2019 12:24:56 +1000 Subject: [PATCH] Fix bugs in multi device pairing. --- .../thoughtcrime/securesms/jobs/PushDecryptJob.java | 13 +++++++++---- .../securesms/loki/DeviceLinkingDialog.kt | 3 +-- .../securesms/loki/DeviceLinkingView.kt | 4 +++- .../thoughtcrime/securesms/loki/LokiAPIDatabase.kt | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 7578d2db87..70b748653e 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -994,6 +994,11 @@ public class PushDecryptJob extends BaseJob implements InjectableType { private void handleTextMessage(@NonNull SignalServiceDataMessage message, @NonNull IncomingTextMessage textMessage, @NonNull Optional smsMessageId, @NonNull Optional messageServerIDOrNull) { SmsDatabase database = DatabaseFactory.getSmsDatabase(context); + + // Ignore the message if the body is empty + if (textMessage.getMessageBody().length() == 0) { return; } + + // Insert the message into the database Optional insertResult = database.insertMessageInbox(textMessage); Long threadId; @@ -1065,7 +1070,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType { return; } - if (LokiDeviceLinkingSession.Companion.getShared().isListeningForLinkingRequest()) { + if (!LokiDeviceLinkingSession.Companion.getShared().isListeningForLinkingRequest()) { Log.w("Loki", "Received authorisation but device is not is listening."); return; } @@ -1074,15 +1079,15 @@ public class PushDecryptJob extends BaseJob implements InjectableType { if (authorisation.getType() != LokiPairingAuthorisation.Type.GRANT) { return; } Log.d("Loki", "Receiving pairing authorisation from: " + authorisation.getPrimaryDevicePubKey()); + // Send out accept event + LokiDeviceLinkingSession.Companion.getShared().acceptedLinkingRequest(authorisation); + // Set the current device as secondary and update our authorisations String ourNumber = TextSecurePreferences.getLocalNumber(context); DatabaseFactory.getLokiAPIDatabase(context).removePairingAuthorisations(ourNumber); DatabaseFactory.getLokiAPIDatabase(context).insertOrUpdatePairingAuthorisation(authorisation); TextSecurePreferences.setIsSecondaryDevice(context, true); - // Send out accept event - LokiDeviceLinkingSession.Companion.getShared().acceptedLinkingRequest(authorisation); - // Send a background message to the primary device sendBackgroundMessage(authorisation.getPrimaryDevicePubKey()); diff --git a/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt b/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt index 2d0bb2496c..e9062dd14d 100644 --- a/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt +++ b/src/org/thoughtcrime/securesms/loki/DeviceLinkingDialog.kt @@ -12,7 +12,6 @@ import org.whispersystems.signalservice.loki.api.LokiStorageAPI import org.whispersystems.signalservice.loki.utilities.retryIfNeeded import org.thoughtcrime.securesms.util.Util - class DeviceLinkingDialog private constructor(private val context: Context, private val mode: DeviceLinkingView.Mode, private val delegate: DeviceLinkingDialogDelegate? = null): DeviceLinkingViewDelegate, LokiDeviceLinkingSessionListener { private lateinit var view: DeviceLinkingView private lateinit var dialog: AlertDialog @@ -56,7 +55,7 @@ class DeviceLinkingDialog private constructor(private val context: Context, priv // region Dialog View Delegate override fun authorise(pairing: LokiPairingAuthorisation): Boolean { val signedAuthorisation = pairing.sign(LokiPairingAuthorisation.Type.GRANT, userPrivateKey) - if (signedAuthorisation == null) { + if (signedAuthorisation == null || signedAuthorisation.type != LokiPairingAuthorisation.Type.GRANT) { Log.e("Loki", "Failed to sign grant authorisation") return false } diff --git a/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt b/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt index e63d19c41b..8973fde268 100644 --- a/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt +++ b/src/org/thoughtcrime/securesms/loki/DeviceLinkingView.kt @@ -134,7 +134,8 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe fun onDeviceLinkAuthorized(authorisation: LokiPairingAuthorisation) { // To be called when a device link was accepted by the primary device - if (mode == Mode.Master || authorisation != pairingAuthorisation) { return } + if (mode == Mode.Master || pairingAuthorisation != null) { return } + pairingAuthorisation = authorisation spinner.visibility = View.GONE val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams @@ -148,6 +149,7 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe titleTextView.text = resources.getString(R.string.view_device_linking_title_4) mnemonicTextView.visibility = View.GONE buttonContainer.visibility = View.GONE + cancelButton.visibility = View.GONE Handler().postDelayed({ delegate.handleDeviceLinkAuthorized() diff --git a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt index 7ec5174289..d7aa8a18b0 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiAPIDatabase.kt @@ -154,7 +154,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( override fun getPairingAuthorisations(pubKey: String): List { val database = databaseHelper.readableDatabase - return database.getAll(multiDeviceAuthTable, "$primaryDevice = ? OR $secondaryDevice = ?", arrayOf(pubKey)) { cursor -> + return database.getAll(multiDeviceAuthTable, "$primaryDevice = ? OR $secondaryDevice = ?", arrayOf(pubKey, pubKey)) { cursor -> val primaryDevicePubKey = cursor.getString(primaryDevice) val secondaryDevicePubKey = cursor.getString(secondaryDevice) val requestSignature: ByteArray? = if (cursor.isNull(cursor.getColumnIndexOrThrow(requestSignature))) null else cursor.getBase64EncodedData(requestSignature) @@ -175,7 +175,7 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( override fun removePairingAuthorisations(pubKey: String) { val database = databaseHelper.readableDatabase - database.delete(multiDeviceAuthTable, "$primaryDevice = ? OR $secondaryDevice = ?", arrayOf(pubKey)) + database.delete(multiDeviceAuthTable, "$primaryDevice = ? OR $secondaryDevice = ?", arrayOf(pubKey, pubKey)) } }