Skip to content

Commit

Permalink
create Utils function for device owner verification
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Oct 19, 2024
1 parent dd396c6 commit 2437443
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import UIKit
import DcCore
import Intents
import LocalAuthentication

internal final class ChatsAndMediaViewController: UITableViewController {

Expand Down Expand Up @@ -158,7 +157,10 @@ internal final class ChatsAndMediaViewController: UITableViewController {
case .mediaQuality: showMediaQuality()
case .downloadOnDemand: showDownloadOnDemand()
case .receiptConfirmation: break
case .exportBackup: authenticateAndCreateBackup()
case .exportBackup:
Utils.authenticateDeviceOwner(reason: String.localized("pref_backup_explain")) { [weak self] in
self?.createBackup()
}
}
}

Expand All @@ -173,24 +175,7 @@ internal final class ChatsAndMediaViewController: UITableViewController {
// MARK: - actions

private func authenticateAndCreateBackup() {
let localAuthenticationContext = LAContext()
var error: NSError?
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = String.localized("pref_backup_explain")
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in
DispatchQueue.main.async {
guard let self = self else { return }
if success {
self.createBackup()
} else {
logger.info("local authentication aborted: \(String(describing: error))")
}
}
}
} else {
logger.info("local authentication unavailable: \(String(describing: error))")
createBackup()
}

}

private func createBackup() {
Expand Down
20 changes: 2 additions & 18 deletions deltachat-ios/Controller/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import UIKit
import DcCore
import Intents
import LocalAuthentication

internal final class SettingsViewController: UITableViewController {

Expand Down Expand Up @@ -288,23 +287,8 @@ internal final class SettingsViewController: UITableViewController {
title: String.localized("perm_continue"),
style: .default,
handler: { [weak self] _ in
guard let self else { return }
let localAuthenticationContext = LAContext()
var error: NSError?
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
let reason = String.localized("multidevice_this_creates_a_qr_code")
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in
DispatchQueue.main.async {
guard let self = self else { return }
if success {
self.navigationController?.pushViewController(BackupTransferViewController(dcAccounts: self.dcAccounts), animated: true)
} else {
logger.info("local authentication aborted: \(String(describing: error))")
}
}
}
} else {
logger.info("local authentication unavailable: \(String(describing: error))")
Utils.authenticateDeviceOwner(reason: String.localized("multidevice_this_creates_a_qr_code")) { [weak self] in
guard let self else { return }
self.navigationController?.pushViewController(BackupTransferViewController(dcAccounts: self.dcAccounts), animated: true)
}
}
Expand Down
21 changes: 20 additions & 1 deletion deltachat-ios/Helper/Utils.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import UIKit
import DcCore

import LocalAuthentication

extension URL {
var isDeltaChatInvitation: Bool {
Expand Down Expand Up @@ -129,4 +129,23 @@ struct Utils {
activityVC.popoverPresentationController?.sourceView = sourceView // iPad crashes without a source
parentViewController.present(activityVC, animated: true, completion: nil)
}

public static func authenticateDeviceOwner(reason: String, callback: @escaping () -> Void) {
let localAuthenticationContext = LAContext()
var error: NSError?
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { success, error in
DispatchQueue.main.async {
if success {
callback()
} else {
logger.info("local authentication aborted: \(String(describing: error))")
}
}
}
} else {
logger.info("local authentication unavailable: \(String(describing: error))")
callback()
}
}
}

0 comments on commit 2437443

Please sign in to comment.