mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
257 lines
12 KiB
Swift
257 lines
12 KiB
Swift
6 years ago
|
import NVActivityIndicatorView
|
||
|
|
||
|
@objc(LKDeviceLinkingModal)
|
||
6 years ago
|
final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate {
|
||
6 years ago
|
private let mode: Mode
|
||
|
private let delegate: DeviceLinkingModalDelegate?
|
||
6 years ago
|
private var deviceLink: DeviceLink?
|
||
5 years ago
|
private var hasAuthorizedDeviceLink = false
|
||
6 years ago
|
|
||
6 years ago
|
// MARK: Types
|
||
|
enum Mode : String { case master, slave }
|
||
|
|
||
6 years ago
|
// MARK: Components
|
||
5 years ago
|
private lazy var spinner = NVActivityIndicatorView(frame: CGRect.zero, type: .circleStrokeSpin, color: Colors.text, padding: nil)
|
||
6 years ago
|
|
||
5 years ago
|
private lazy var qrCodeImageViewContainer: UIView = {
|
||
|
let result = UIView()
|
||
|
result.addSubview(qrCodeImageView)
|
||
|
qrCodeImageView.pin(.top, to: .top, of: result)
|
||
|
qrCodeImageView.pin(.bottom, to: .bottom, of: result)
|
||
|
qrCodeImageView.center(.horizontal, in: result)
|
||
|
return result
|
||
|
}()
|
||
|
|
||
6 years ago
|
private lazy var qrCodeImageView: UIImageView = {
|
||
|
let result = UIImageView()
|
||
|
result.contentMode = .scaleAspectFit
|
||
5 years ago
|
let size: CGFloat = 128
|
||
|
result.set(.width, to: size)
|
||
|
result.set(.height, to: size)
|
||
6 years ago
|
return result
|
||
|
}()
|
||
|
|
||
6 years ago
|
private lazy var titleLabel: UILabel = {
|
||
|
let result = UILabel()
|
||
5 years ago
|
result.textColor = Colors.text
|
||
|
result.font = .boldSystemFont(ofSize: Values.mediumFontSize)
|
||
6 years ago
|
result.numberOfLines = 0
|
||
|
result.lineBreakMode = .byWordWrapping
|
||
|
result.textAlignment = .center
|
||
|
return result
|
||
|
}()
|
||
|
|
||
|
private lazy var subtitleLabel: UILabel = {
|
||
|
let result = UILabel()
|
||
5 years ago
|
result.textColor = Colors.text
|
||
5 years ago
|
result.font = .systemFont(ofSize: Values.smallFontSize)
|
||
6 years ago
|
result.numberOfLines = 0
|
||
|
result.lineBreakMode = .byWordWrapping
|
||
|
result.textAlignment = .center
|
||
|
return result
|
||
|
}()
|
||
|
|
||
6 years ago
|
private lazy var mnemonicLabel: UILabel = {
|
||
|
let result = UILabel()
|
||
5 years ago
|
result.textColor = Colors.text
|
||
|
result.font = .systemFont(ofSize: Values.smallFontSize)
|
||
6 years ago
|
result.numberOfLines = 0
|
||
|
result.lineBreakMode = .byWordWrapping
|
||
|
result.textAlignment = .center
|
||
|
return result
|
||
|
}()
|
||
6 years ago
|
|
||
|
private lazy var buttonStackView: UIStackView = {
|
||
5 years ago
|
let result = UIStackView(arrangedSubviews: [ cancelButton, authorizeButton ])
|
||
6 years ago
|
result.axis = .horizontal
|
||
5 years ago
|
result.spacing = Values.mediumSpacing
|
||
6 years ago
|
result.distribution = .fillEqually
|
||
|
return result
|
||
|
}()
|
||
6 years ago
|
|
||
5 years ago
|
private lazy var authorizeButton: UIButton = {
|
||
|
let result = UIButton()
|
||
|
result.set(.height, to: Values.mediumButtonHeight)
|
||
|
result.layer.cornerRadius = Values.modalButtonCornerRadius
|
||
|
result.backgroundColor = Colors.accent
|
||
|
result.titleLabel!.font = .systemFont(ofSize: Values.smallFontSize)
|
||
|
result.setTitleColor(Colors.text, for: UIControl.State.normal)
|
||
5 years ago
|
result.setTitle(NSLocalizedString("modal_link_device_master_mode_authorize_button_title", comment: ""), for: UIControl.State.normal)
|
||
6 years ago
|
return result
|
||
|
}()
|
||
5 years ago
|
|
||
|
private lazy var mainStackView: UIStackView = {
|
||
|
let result = UIStackView(arrangedSubviews: [ titleLabel, subtitleLabel, mnemonicLabel, buttonStackView ])
|
||
|
result.spacing = Values.largeSpacing
|
||
|
result.axis = .vertical
|
||
|
return result
|
||
|
}()
|
||
6 years ago
|
|
||
6 years ago
|
// MARK: Lifecycle
|
||
6 years ago
|
init(mode: Mode, delegate: DeviceLinkingModalDelegate?) {
|
||
|
self.mode = mode
|
||
|
if mode == .slave {
|
||
|
guard delegate != nil else { preconditionFailure("Missing delegate for device linking modal in slave mode.") }
|
||
|
}
|
||
|
self.delegate = delegate
|
||
|
super.init(nibName: nil, bundle: nil)
|
||
|
}
|
||
|
|
||
6 years ago
|
@objc(initWithMode:delegate:)
|
||
|
convenience init(modeAsString: String, delegate: DeviceLinkingModalDelegate?) {
|
||
6 years ago
|
guard let mode = Mode(rawValue: modeAsString) else { preconditionFailure("Invalid mode: \(modeAsString).") }
|
||
|
self.init(mode: mode, delegate: delegate)
|
||
|
}
|
||
|
|
||
|
required init?(coder: NSCoder) { preconditionFailure() }
|
||
|
override init(nibName: String?, bundle: Bundle?) { preconditionFailure() }
|
||
|
|
||
6 years ago
|
override func viewDidLoad() {
|
||
|
super.viewDidLoad()
|
||
6 years ago
|
switch mode {
|
||
|
case .master: let _ = DeviceLinkingSession.startListeningForLinkingRequests(with: self)
|
||
|
case .slave: let _ = DeviceLinkingSession.startListeningForLinkingAuthorization(with: self)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
override func populateContentView() {
|
||
6 years ago
|
switch mode {
|
||
5 years ago
|
case .master: mainStackView.insertArrangedSubview(qrCodeImageViewContainer, at: 0)
|
||
|
case .slave: mainStackView.insertArrangedSubview(spinner, at: 0)
|
||
6 years ago
|
}
|
||
5 years ago
|
contentView.addSubview(mainStackView)
|
||
6 years ago
|
switch mode {
|
||
|
case .master:
|
||
5 years ago
|
let hexEncodedPublicKey = getUserHexEncodedPublicKey()
|
||
5 years ago
|
qrCodeImageView.image = QRCode.generate(for: hexEncodedPublicKey, hasBackground: true)
|
||
6 years ago
|
case .slave:
|
||
|
spinner.set(.height, to: 64)
|
||
|
spinner.startAnimating()
|
||
|
}
|
||
6 years ago
|
titleLabel.text = {
|
||
|
switch mode {
|
||
5 years ago
|
case .master: return NSLocalizedString("modal_link_device_master_mode_title_1", comment: "")
|
||
|
case .slave: return NSLocalizedString("modal_link_device_slave_mode_title_1", comment: "")
|
||
6 years ago
|
}
|
||
|
}()
|
||
|
subtitleLabel.text = {
|
||
|
switch mode {
|
||
5 years ago
|
case .master: return NSLocalizedString("modal_link_device_master_mode_explanation_1", comment: "")
|
||
|
case .slave: return NSLocalizedString("modal_link_device_slave_mode_explanation_1", comment: "")
|
||
6 years ago
|
}
|
||
|
}()
|
||
|
mnemonicLabel.isHidden = (mode == .master)
|
||
6 years ago
|
if mode == .slave {
|
||
5 years ago
|
let hexEncodedPublicKey = getUserHexEncodedPublicKey().removing05PrefixIfNeeded()
|
||
6 years ago
|
mnemonicLabel.text = Mnemonic.hash(hexEncodedString: hexEncodedPublicKey)
|
||
6 years ago
|
}
|
||
5 years ago
|
authorizeButton.addTarget(self, action: #selector(authorizeDeviceLink), for: UIControl.Event.touchUpInside)
|
||
6 years ago
|
authorizeButton.isHidden = true
|
||
5 years ago
|
mainStackView.pin(.leading, to: .leading, of: contentView, withInset: Values.largeSpacing)
|
||
|
mainStackView.pin(.top, to: .top, of: contentView, withInset: Values.largeSpacing)
|
||
|
contentView.pin(.trailing, to: .trailing, of: mainStackView, withInset: Values.largeSpacing)
|
||
|
contentView.pin(.bottom, to: .bottom, of: mainStackView, withInset: Values.largeSpacing)
|
||
6 years ago
|
}
|
||
|
|
||
|
// MARK: Device Linking
|
||
6 years ago
|
func requestUserAuthorization(for deviceLink: DeviceLink) {
|
||
6 years ago
|
self.deviceLink = deviceLink
|
||
5 years ago
|
qrCodeImageViewContainer.isHidden = true
|
||
5 years ago
|
titleLabel.text = NSLocalizedString("modal_link_device_master_mode_title_2", comment: "")
|
||
|
subtitleLabel.text = NSLocalizedString("modal_link_device_master_mode_explanation_2", comment: "")
|
||
5 years ago
|
let hexEncodedPublicKey = deviceLink.slave.publicKey.removing05PrefixIfNeeded()
|
||
6 years ago
|
mnemonicLabel.text = Mnemonic.hash(hexEncodedString: hexEncodedPublicKey)
|
||
6 years ago
|
mnemonicLabel.isHidden = false
|
||
|
authorizeButton.isHidden = false
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
@objc private func authorizeDeviceLink() {
|
||
5 years ago
|
guard !hasAuthorizedDeviceLink else { return }
|
||
|
hasAuthorizedDeviceLink = true
|
||
|
mainStackView.removeArrangedSubview(qrCodeImageViewContainer)
|
||
|
mainStackView.insertArrangedSubview(spinner, at: 0)
|
||
|
spinner.set(.height, to: 64)
|
||
|
spinner.startAnimating()
|
||
5 years ago
|
titleLabel.text = NSLocalizedString("modal_link_device_master_mode_title_3", comment: "")
|
||
|
subtitleLabel.text = NSLocalizedString("modal_link_device_master_mode_explanation_3", comment: "")
|
||
5 years ago
|
mnemonicLabel.isHidden = true
|
||
|
buttonStackView.isHidden = true
|
||
6 years ago
|
let deviceLink = self.deviceLink!
|
||
5 years ago
|
DeviceLinkingSession.current!.markLinkingRequestAsProcessed()
|
||
|
DeviceLinkingSession.current!.stopListeningForLinkingRequests()
|
||
6 years ago
|
let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink)
|
||
5 years ago
|
let master = DeviceLink.Device(publicKey: deviceLink.master.publicKey, signature: linkingAuthorizationMessage.masterSignature)
|
||
6 years ago
|
let signedDeviceLink = DeviceLink(between: master, and: deviceLink.slave)
|
||
5 years ago
|
FileServerAPI.addDeviceLink(signedDeviceLink).done(on: DispatchQueue.main) { [weak self] in
|
||
5 years ago
|
SSKEnvironment.shared.messageSender.send(linkingAuthorizationMessage, success: {
|
||
5 years ago
|
let slavePublicKey = deviceLink.slave.publicKey
|
||
5 years ago
|
Storage.writeSync { transaction in
|
||
5 years ago
|
let thread = TSContactThread.getOrCreateThread(withContactId: slavePublicKey, transaction: transaction)
|
||
5 years ago
|
thread.save(with: transaction)
|
||
|
}
|
||
5 years ago
|
let _ = SSKEnvironment.shared.syncManager.syncAllGroups().ensure {
|
||
|
// Closed groups first because we prefer the session request mechanism
|
||
|
// to the AFR mechanism
|
||
|
let _ = SSKEnvironment.shared.syncManager.syncAllContacts()
|
||
|
}
|
||
5 years ago
|
let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups()
|
||
5 years ago
|
DispatchQueue.main.async {
|
||
|
self?.dismiss(animated: true, completion: nil)
|
||
|
self?.delegate?.handleDeviceLinkAuthorized(signedDeviceLink)
|
||
|
}
|
||
5 years ago
|
}, failure: { error in
|
||
|
print("[Loki] Failed to send device link authorization message.")
|
||
5 years ago
|
let _ = FileServerAPI.removeDeviceLink(signedDeviceLink) // Attempt to roll back
|
||
5 years ago
|
DispatchQueue.main.async {
|
||
|
self?.close()
|
||
5 years ago
|
let alert = UIAlertController(title: "Device Linking Failed", message: "Please check your internet connection and try again", preferredStyle: .alert)
|
||
5 years ago
|
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil))
|
||
|
self?.presentingViewController?.present(alert, animated: true, completion: nil)
|
||
|
}
|
||
5 years ago
|
})
|
||
|
}.catch { [weak self] error in
|
||
6 years ago
|
print("[Loki] Failed to add device link due to error: \(error).")
|
||
5 years ago
|
DispatchQueue.main.async {
|
||
|
self?.close() // TODO: Show a message to the user
|
||
5 years ago
|
let alert = UIAlertController(title: "Device Linking Failed", message: "Please check your internet connection and try again", preferredStyle: .alert)
|
||
5 years ago
|
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil))
|
||
|
self?.presentingViewController?.present(alert, animated: true, completion: nil)
|
||
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
func handleDeviceLinkAuthorized(_ deviceLink: DeviceLink) {
|
||
6 years ago
|
let session = DeviceLinkingSession.current!
|
||
|
session.stopListeningForLinkingAuthorization()
|
||
6 years ago
|
spinner.stopAnimating()
|
||
|
spinner.isHidden = true
|
||
5 years ago
|
titleLabel.text = NSLocalizedString("modal_link_device_slave_mode_title_2", comment: "")
|
||
|
subtitleLabel.text = NSLocalizedString("modal_link_device_slave_mode_explanation_2", comment: "")
|
||
6 years ago
|
mnemonicLabel.isHidden = true
|
||
|
buttonStackView.isHidden = true
|
||
5 years ago
|
FileServerAPI.addDeviceLink(deviceLink).catch { error in
|
||
6 years ago
|
print("[Loki] Failed to add device link due to error: \(error).")
|
||
|
}
|
||
6 years ago
|
Timer.scheduledTimer(withTimeInterval: 2, repeats: false) { _ in
|
||
5 years ago
|
self.dismiss(animated: true) {
|
||
|
self.delegate?.handleDeviceLinkAuthorized(deviceLink)
|
||
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
@objc override func close() {
|
||
6 years ago
|
guard let session = DeviceLinkingSession.current else {
|
||
|
return print("[Loki] Device linking session missing.") // Should never occur
|
||
|
}
|
||
|
session.stopListeningForLinkingRequests()
|
||
|
session.markLinkingRequestAsProcessed() // Only relevant in master mode
|
||
6 years ago
|
delegate?.handleDeviceLinkingModalDismissed() // Only relevant in slave mode
|
||
6 years ago
|
if let deviceLink = deviceLink {
|
||
5 years ago
|
Storage.writeSync { transaction in
|
||
5 years ago
|
OWSPrimaryStorage.shared().removePreKeyBundle(forContact: deviceLink.slave.publicKey, transaction: transaction)
|
||
6 years ago
|
}
|
||
|
}
|
||
6 years ago
|
dismiss(animated: true, completion: nil)
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|