From e549db0714863f02fb4d71fceb2cddfdf4207c6f Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Sat, 3 Jun 2017 17:50:33 -0700 Subject: [PATCH] Change notification observer from "showPassword" to "decryptThenShowPassword" - avoid crash when password has been deleted --- .../PasswordDetailTableViewController.swift | 22 +++++++++++-------- pass/Helpers/Utils.swift | 6 ++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pass/Controllers/PasswordDetailTableViewController.swift b/pass/Controllers/PasswordDetailTableViewController.swift index 79e2742..97c5fbd 100644 --- a/pass/Controllers/PasswordDetailTableViewController.swift +++ b/pass/Controllers/PasswordDetailTableViewController.swift @@ -101,10 +101,10 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil) // 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 - 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) { @@ -140,20 +140,24 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni 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 { // decrypt password 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 { DispatchQueue.main.async { // remove the wrong passphrase so that users could enter it next time self.passwordStore.pgpKeyPassphrase = nil - let alert = UIAlertController(title: "Cannot Show Password", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert) - alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(UIAlertAction) -> Void in + Utils.alert(title: "Cannot Show Password", message: error.localizedDescription, controller: self, handler: {(UIAlertAction) -> Void in self.navigationController!.popViewController(animated: true) - })) - self.present(alert, animated: true, completion: nil) + }) } return } @@ -162,7 +166,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni } } - @objc private func showPassword() { + private func showPassword() { DispatchQueue.main.async { [weak self] in self?.indicator.stopAnimating() self?.setTableData() diff --git a/pass/Helpers/Utils.swift b/pass/Helpers/Utils.swift index dee5411..da3b779 100644 --- a/pass/Helpers/Utils.swift +++ b/pass/Helpers/Utils.swift @@ -64,13 +64,13 @@ class Utils { 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() 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) - } + static func removePGPKeys() { removeFileIfExists(atPath: Globals.pgpPublicKeyPath)