From 632f66f2bac829912c1f913dc3aaef47f23335ff Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Tue, 28 Apr 2020 14:14:28 +1000 Subject: [PATCH] Make device link authorization more reliable --- .../View Controllers/DeviceLinkingModal.swift | 43 ++++++++++--------- .../src/Loki/View Controllers/LandingVC.swift | 2 +- .../translations/en.lproj/Localizable.strings | 2 + .../Multi Device/MultiDeviceProtocol.swift | 2 +- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift index 4865ac877..e3324ae53 100644 --- a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift +++ b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift @@ -162,28 +162,30 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { @objc private func authorizeDeviceLink() { let deviceLink = self.deviceLink! + DeviceLinkingSession.current!.markLinkingRequestAsProcessed() + DeviceLinkingSession.current!.stopListeningForLinkingRequests() let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink) - ThreadUtil.enqueue(linkingAuthorizationMessage) - SSKEnvironment.shared.messageSender.send(linkingAuthorizationMessage, success: { - let _ = SSKEnvironment.shared.syncManager.syncAllContacts() - let _ = SSKEnvironment.shared.syncManager.syncAllGroups() - let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups() - let thread = TSContactThread.getOrCreateThread(contactId: deviceLink.slave.hexEncodedPublicKey) - thread.friendRequestStatus = .friends - thread.save() - }) { _ in - print("[Loki] Failed to send device link authorization message.") - } - let session = DeviceLinkingSession.current! - session.stopListeningForLinkingRequests() - session.markLinkingRequestAsProcessed() - dismiss(animated: true, completion: nil) let master = DeviceLink.Device(hexEncodedPublicKey: deviceLink.master.hexEncodedPublicKey, signature: linkingAuthorizationMessage.masterSignature) let signedDeviceLink = DeviceLink(between: master, and: deviceLink.slave) - LokiFileServerAPI.addDeviceLink(signedDeviceLink).done { - self.delegate?.handleDeviceLinkAuthorized(signedDeviceLink) // Intentionally capture self strongly - }.catch { error in + LokiFileServerAPI.addDeviceLink(signedDeviceLink).done { [weak self] in + SSKEnvironment.shared.messageSender.send(linkingAuthorizationMessage, success: { + let thread = TSContactThread.getOrCreateThread(contactId: deviceLink.slave.hexEncodedPublicKey) + thread.save() + let _ = SSKEnvironment.shared.syncManager.syncAllContacts() + let _ = SSKEnvironment.shared.syncManager.syncAllGroups() + let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups() + thread.friendRequestStatus = .friends + thread.save() + self?.delegate?.handleDeviceLinkAuthorized(signedDeviceLink) // Intentionally capture self strongly + self?.dismiss(animated: true, completion: nil) + }, failure: { error in + print("[Loki] Failed to send device link authorization message.") + let _ = LokiFileServerAPI.removeDeviceLink(signedDeviceLink) // Attempt to roll back + self?.close() + }) + }.catch { [weak self] error in print("[Loki] Failed to add device link due to error: \(error).") + self?.close() // TODO: Show a message to the user } } @@ -214,8 +216,9 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { session.markLinkingRequestAsProcessed() // Only relevant in master mode delegate?.handleDeviceLinkingModalDismissed() // Only relevant in slave mode if let deviceLink = deviceLink { - OWSPrimaryStorage.shared().dbReadWriteConnection.readWrite { transaction in - OWSPrimaryStorage.shared().removePreKeyBundle(forContact: deviceLink.slave.hexEncodedPublicKey, transaction: transaction) + let storage = OWSPrimaryStorage.shared() + storage.dbReadWriteConnection.readWrite { transaction in + storage.removePreKeyBundle(forContact: deviceLink.slave.hexEncodedPublicKey, transaction: transaction) } } dismiss(animated: true, completion: nil) diff --git a/Signal/src/Loki/View Controllers/LandingVC.swift b/Signal/src/Loki/View Controllers/LandingVC.swift index 3b8eda399..5005becca 100644 --- a/Signal/src/Loki/View Controllers/LandingVC.swift +++ b/Signal/src/Loki/View Controllers/LandingVC.swift @@ -142,7 +142,7 @@ final class LandingVC : BaseVC, LinkDeviceVCDelegate, DeviceLinkingModalDelegate // MARK: Device Linking func requestDeviceLink(with hexEncodedPublicKey: String) { guard ECKeyPair.isValidHexEncodedPublicKey(candidate: hexEncodedPublicKey) else { - let alert = UIAlertController(title: NSLocalizedString("Invalid Public Key", comment: ""), message: NSLocalizedString("Please make sure the public key you entered is correct and try again.", comment: ""), preferredStyle: .alert) + let alert = UIAlertController(title: NSLocalizedString("Invalid Session ID", comment: ""), message: NSLocalizedString("Please make sure the Session ID you entered is correct and try again.", comment: ""), preferredStyle: .alert) alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), accessibilityIdentifier: nil, style: .default, handler: nil)) return present(alert, animated: true, completion: nil) } diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index c6ccfc8fc..7bfc47232 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -2823,3 +2823,5 @@ "Confirm" = "Confirm"; "Skip" = "Skip"; "Link Previews" = "Link Previews"; +"Invalid Session ID" = "Invalid Session ID"; +"Please make sure the Session ID you entered is correct and try again." = "Please make sure the Session ID you entered is correct and try again."; diff --git a/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift index 183fb8c90..6f9c8d939 100644 --- a/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Multi Device/MultiDeviceProtocol.swift @@ -135,7 +135,7 @@ public final class MultiDeviceProtocol : NSObject { return OWSMessageSend(message: message, thread: thread, recipient: recipient, senderCertificate: senderCertificate, udAccess: recipientUDAccess, localNumber: getUserHexEncodedPublicKey(), success: { - }, failure: { error in + }, failure: { _ in }) }