Change notification observer from "showPassword" to "decryptThenShowPassword"

- avoid crash when password has been deleted
This commit is contained in:
Bob Sun 2017-06-03 17:50:33 -07:00
parent 174128998c
commit e549db0714
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 16 additions and 12 deletions

View file

@ -101,10 +101,10 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil)
// reset the data table if some password (maybe another one) has been updated // reset the data table if some password (maybe another one) has been updated
NotificationCenter.default.addObserver(self, selector: #selector(showPassword), name: .passwordStoreUpdated, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(decryptThenShowPassword), name: .passwordStoreUpdated, object: nil)
// reset the data table if the disaply settings have been changed // reset the data table if the disaply settings have been changed
NotificationCenter.default.addObserver(self, selector: #selector(showPassword), name: .passwordDetailDisplaySettingChanged, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(decryptThenShowPassword), name: .passwordDetailDisplaySettingChanged, object: nil)
} }
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {
@ -140,20 +140,24 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
return passphrase return passphrase
} }
private func decryptThenShowPassword() { @objc private func decryptThenShowPassword() {
guard let passwordEntity = passwordEntity else {
Utils.alert(title: "Cannot Show Password", message: "The password does not exist.", controller: self, handler: {(UIAlertAction) -> Void in
self.navigationController!.popViewController(animated: true)
})
return
}
DispatchQueue.global(qos: .userInitiated).async { DispatchQueue.global(qos: .userInitiated).async {
// decrypt password // decrypt password
do { do {
self.password = try self.passwordStore.decrypt(passwordEntity: self.passwordEntity!, requestPGPKeyPassphrase: self.requestPGPKeyPassphrase) self.password = try self.passwordStore.decrypt(passwordEntity: passwordEntity, requestPGPKeyPassphrase: self.requestPGPKeyPassphrase)
} catch { } catch {
DispatchQueue.main.async { DispatchQueue.main.async {
// remove the wrong passphrase so that users could enter it next time // remove the wrong passphrase so that users could enter it next time
self.passwordStore.pgpKeyPassphrase = nil self.passwordStore.pgpKeyPassphrase = nil
let alert = UIAlertController(title: "Cannot Show Password", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert) Utils.alert(title: "Cannot Show Password", message: error.localizedDescription, controller: self, handler: {(UIAlertAction) -> Void in
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(UIAlertAction) -> Void in
self.navigationController!.popViewController(animated: true) self.navigationController!.popViewController(animated: true)
})) })
self.present(alert, animated: true, completion: nil)
} }
return return
} }
@ -162,7 +166,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
} }
} }
@objc private func showPassword() { private func showPassword() {
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
self?.indicator.stopAnimating() self?.indicator.stopAnimating()
self?.setTableData() self?.setTableData()

View file

@ -64,13 +64,13 @@ class Utils {
return randomString return randomString
} }
static func alert(title: String, message: String, controller: UIViewController, completion: (() -> Void)?) { static func alert(title: String, message: String, controller: UIViewController, handler: ((UIAlertAction) -> Void)? = nil, completion: (() -> Void)? = nil) {
SVProgressHUD.dismiss() SVProgressHUD.dismiss()
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: handler))
controller.present(alert, animated: true, completion: completion) controller.present(alert, animated: true, completion: completion)
} }
static func removePGPKeys() { static func removePGPKeys() {
removeFileIfExists(atPath: Globals.pgpPublicKeyPath) removeFileIfExists(atPath: Globals.pgpPublicKeyPath)