Fix fail-safe mechanism for other decryption scenarios

This commit is contained in:
Mingshen Sun 2020-04-18 22:35:17 -07:00
parent 3e114daca1
commit fcc8961e46
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
15 changed files with 153 additions and 100 deletions

View file

@ -80,9 +80,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
super.viewWillAppear(animated)
if self.shouldPopCurrentView {
let alert = UIAlertController(title: "Notice".localize(), message: "PreviousChangesDiscarded.".localize(), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in
_ = self.navigationController?.popViewController(animated: true)
}))
alert.addAction(UIAlertAction.okAndPopView(controller: self))
self.present(alert, animated: true, completion: nil)
}
}
@ -99,6 +97,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
do {
let requestPGPKeyPassphrase = Utils.createRequestPGPKeyPassphraseHandler(controller: self)
self.password = try self.passwordStore.decrypt(passwordEntity: passwordEntity, keyID: keyID, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
self.showPassword()
} catch AppError.PgpPrivateKeyNotFound(let key) {
DispatchQueue.main.async {
// alert: cancel or try again
@ -111,7 +110,6 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
self.present(alert, animated: true, completion: nil)
}
return
} catch {
DispatchQueue.main.async {
// alert: cancel or try again
@ -122,10 +120,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
})
self.present(alert, animated: true, completion: nil)
}
return
}
// display password
self.showPassword()
}
}

View file

@ -294,7 +294,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
}
self.reloadTableView(data: filteredPasswordsTableEntries, label: .unsynced)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
let cancelAction = UIAlertAction.cancel()
ac.addAction(allAction)
ac.addAction(unsyncedAction)
@ -453,32 +453,49 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
private func decryptThenCopyPassword(from indexPath: IndexPath) {
guard PGPAgent.shared.isPrepared else {
Utils.alert(title: "CannotCopyPassword".localize(), message: "PgpKeyNotSet.".localize(), controller: self, completion: nil)
Utils.alert(title: "CannotCopyPassword".localize(), message: "PgpKeyNotSet.".localize(), controller: self)
return
}
let passwordEntity = getPasswordEntry(by: indexPath).passwordEntity
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
SVProgressHUD.dismiss()
self.decryptPassword(passwordEntity: passwordEntity)
}
private func decryptPassword(passwordEntity: PasswordEntity, keyID: String? = nil) {
DispatchQueue.global(qos: .userInteractive).async {
var decryptedPassword: Password?
do {
let requestPGPKeyPassphrase = Utils.createRequestPGPKeyPassphraseHandler(controller: self)
decryptedPassword = try self.passwordStore.decrypt(passwordEntity: passwordEntity, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
let decryptedPassword = try self.passwordStore.decrypt(passwordEntity: passwordEntity, keyID: keyID, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
DispatchQueue.main.async {
SecurePasteboard.shared.copy(textToCopy: decryptedPassword?.password)
SecurePasteboard.shared.copy(textToCopy: decryptedPassword.password)
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.dark)
SVProgressHUD.showSuccess(withStatus: "PasswordCopiedToPasteboard.".localize())
SVProgressHUD.dismiss(withDelay: 0.6)
}
} catch {
} catch AppError.PgpPrivateKeyNotFound(let key) {
DispatchQueue.main.async {
Utils.alert(title: "CannotCopyPassword".localize(), message: error.localizedDescription, controller: self, completion: nil)
// alert: cancel or try again
let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.PgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction.cancelAndPopView(controller: self))
let selectKey = UIAlertAction.selectKey(controller: self) { action in
self.decryptPassword(passwordEntity: passwordEntity, keyID: action.title)
}
alert.addAction(selectKey)
self.present(alert, animated: true)
}
} catch {
DispatchQueue.main.async {
Utils.alert(title: "CannotCopyPassword".localize(), message: error.localizedDescription, controller: self)
}
}
}
}
private func generateSections(item: [PasswordTableEntry]) {
let collation = UILocalizedIndexedCollation.current()
let sectionTitles = collation.sectionIndexTitles

View file

@ -1,44 +0,0 @@
//
// UIAlertActionExtension.swift
// passKit
//
// Created by Sun, Mingshen on 4/17/20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import UIKit
import Foundation
import passKit
extension UIAlertAction {
static func cancelAndPopView(controller: UIViewController) -> UIAlertAction {
UIAlertAction(title: "Cancel".localize(), style: .cancel) { _ in
controller.navigationController?.popViewController(animated: true)
}
}
static func cancel() -> UIAlertAction {
cancel(with: "Cancel")
}
static func dismiss() -> UIAlertAction {
cancel(with: "Dismiss")
}
static func cancel(with title: String) -> UIAlertAction {
UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil)
}
static func selectKey(controller: UIViewController, handler: ((UIAlertAction) -> Void)?) -> UIAlertAction {
UIAlertAction(title: "Select Key", style: .default) { _ in
let selectKeyAlert = UIAlertController(title: "Select from imported keys", message: nil, preferredStyle: .actionSheet)
try? PGPAgent.shared.getShortKeyID().forEach({ k in
let action = UIAlertAction(title: k, style: .default, handler: handler)
selectKeyAlert.addAction(action)
})
selectKeyAlert.addAction(UIAlertAction.cancelAndPopView(controller: controller))
controller.present(selectKeyAlert, animated: true, completion: nil)
}
}
}

View file

@ -31,11 +31,11 @@ public func requestGitCredentialPassword(credential: GitCredential.Credential,
$0.text = lastPassword ?? ""
$0.isSecureTextEntry = true
}
alert.addAction(UIAlertAction(title: "Ok".localize(), style: .default) { _ in
alert.addAction(UIAlertAction.ok() { _ in
password = alert.textFields?.first?.text
sem.signal()
})
alert.addAction(UIAlertAction(title: "Cancel".localize(), style: .cancel) { _ in
alert.addAction(UIAlertAction.cancel() { _ in
password = nil
sem.signal()
})