Make device link authorization more reliable

pull/170/head
nielsandriesse 5 years ago
parent 37f52b655c
commit 632f66f2ba

@ -162,28 +162,30 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate {
@objc private func authorizeDeviceLink() { @objc private func authorizeDeviceLink() {
let deviceLink = self.deviceLink! let deviceLink = self.deviceLink!
DeviceLinkingSession.current!.markLinkingRequestAsProcessed()
DeviceLinkingSession.current!.stopListeningForLinkingRequests()
let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink) let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink)
ThreadUtil.enqueue(linkingAuthorizationMessage) let master = DeviceLink.Device(hexEncodedPublicKey: deviceLink.master.hexEncodedPublicKey, signature: linkingAuthorizationMessage.masterSignature)
let signedDeviceLink = DeviceLink(between: master, and: deviceLink.slave)
LokiFileServerAPI.addDeviceLink(signedDeviceLink).done { [weak self] in
SSKEnvironment.shared.messageSender.send(linkingAuthorizationMessage, success: { 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.syncAllContacts()
let _ = SSKEnvironment.shared.syncManager.syncAllGroups() let _ = SSKEnvironment.shared.syncManager.syncAllGroups()
let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups() let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups()
let thread = TSContactThread.getOrCreateThread(contactId: deviceLink.slave.hexEncodedPublicKey)
thread.friendRequestStatus = .friends thread.friendRequestStatus = .friends
thread.save() thread.save()
}) { _ in 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.") print("[Loki] Failed to send device link authorization message.")
} let _ = LokiFileServerAPI.removeDeviceLink(signedDeviceLink) // Attempt to roll back
let session = DeviceLinkingSession.current! self?.close()
session.stopListeningForLinkingRequests() })
session.markLinkingRequestAsProcessed() }.catch { [weak self] error in
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
print("[Loki] Failed to add device link due to error: \(error).") 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 session.markLinkingRequestAsProcessed() // Only relevant in master mode
delegate?.handleDeviceLinkingModalDismissed() // Only relevant in slave mode delegate?.handleDeviceLinkingModalDismissed() // Only relevant in slave mode
if let deviceLink = deviceLink { if let deviceLink = deviceLink {
OWSPrimaryStorage.shared().dbReadWriteConnection.readWrite { transaction in let storage = OWSPrimaryStorage.shared()
OWSPrimaryStorage.shared().removePreKeyBundle(forContact: deviceLink.slave.hexEncodedPublicKey, transaction: transaction) storage.dbReadWriteConnection.readWrite { transaction in
storage.removePreKeyBundle(forContact: deviceLink.slave.hexEncodedPublicKey, transaction: transaction)
} }
} }
dismiss(animated: true, completion: nil) dismiss(animated: true, completion: nil)

@ -142,7 +142,7 @@ final class LandingVC : BaseVC, LinkDeviceVCDelegate, DeviceLinkingModalDelegate
// MARK: Device Linking // MARK: Device Linking
func requestDeviceLink(with hexEncodedPublicKey: String) { func requestDeviceLink(with hexEncodedPublicKey: String) {
guard ECKeyPair.isValidHexEncodedPublicKey(candidate: hexEncodedPublicKey) else { 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)) alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), accessibilityIdentifier: nil, style: .default, handler: nil))
return present(alert, animated: true, completion: nil) return present(alert, animated: true, completion: nil)
} }

@ -2823,3 +2823,5 @@
"Confirm" = "Confirm"; "Confirm" = "Confirm";
"Skip" = "Skip"; "Skip" = "Skip";
"Link Previews" = "Link Previews"; "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.";

@ -135,7 +135,7 @@ public final class MultiDeviceProtocol : NSObject {
return OWSMessageSend(message: message, thread: thread, recipient: recipient, senderCertificate: senderCertificate, return OWSMessageSend(message: message, thread: thread, recipient: recipient, senderCertificate: senderCertificate,
udAccess: recipientUDAccess, localNumber: getUserHexEncodedPublicKey(), success: { udAccess: recipientUDAccess, localNumber: getUserHexEncodedPublicKey(), success: {
}, failure: { error in }, failure: { _ in
}) })
} }

Loading…
Cancel
Save