From ad0c39b54153f2b7883159c2ef4504859f9b1285 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Wed, 8 Mar 2017 00:57:49 -0800 Subject: [PATCH] Add option to select two flavors of password generator: random and apple's (keychain) --- pass/AppDelegate.swift | 1 + .../GeneralSettingsTableViewController.swift | 49 ++++++++++++++++++- pass/Helpers/DefaultsKeys.swift | 9 +--- pass/Helpers/Utils.swift | 9 +++- pass/Views/FillPasswordTableViewCell.swift | 2 +- 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/pass/AppDelegate.swift b/pass/AppDelegate.swift index b375d18..9b635e7 100644 --- a/pass/AppDelegate.swift +++ b/pass/AppDelegate.swift @@ -33,6 +33,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { self.perform(#selector(postSearchNotification), with: nil, afterDelay: 0.4) } } + Utils.initDefaultKeys() return true } diff --git a/pass/Controllers/GeneralSettingsTableViewController.swift b/pass/Controllers/GeneralSettingsTableViewController.swift index 36f2f6b..0e17f29 100644 --- a/pass/Controllers/GeneralSettingsTableViewController.swift +++ b/pass/Controllers/GeneralSettingsTableViewController.swift @@ -52,6 +52,11 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { [[.title: "About Repository", .action: "segue", .link: "showAboutRepositorySegue"],], // section 1 + [ + [.title: "Password Generator Flavor", .action: "none", .style: CellDataStyle.value1], + ], + + // section 2 [ [.title: "Remember Passphrase", .action: "none",], ], @@ -81,7 +86,7 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { cell.accessoryView = accessoryView cell.selectionStyle = .none hideUnknownSwitch.isOn = Defaults[.isHideUnknownOn] - case "Hide One Time Password Fields": + case "Hide OTP Fields": cell.accessoryType = .none let detailButton = UIButton(type: .detailDisclosure) hideOTPSwitch.frame = CGRect(x: detailButton.bounds.width+10, y: 0, width: hideOTPSwitch.bounds.width, height: hideOTPSwitch.bounds.height) @@ -101,11 +106,53 @@ class GeneralSettingsTableViewController: BasicStaticTableViewController { cell.accessoryType = .none cell.selectionStyle = .none cell.accessoryView = showFolderSwitch + case "Password Generator Flavor": + cell.accessoryType = .disclosureIndicator + cell.detailTextLabel?.text = Defaults[.passwordGeneratorFlavor] default: break } return cell } + override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + super.tableView(tableView, didSelectRowAt: indexPath) + let cell = tableView.cellForRow(at: indexPath)! + if cell.textLabel!.text! == "Password Generator Flavor" { + tableView.deselectRow(at: indexPath, animated: true) + showPasswordGeneratorFlavorActionSheet(sourceCell: cell) + } + } + + func showPasswordGeneratorFlavorActionSheet(sourceCell: UITableViewCell) { + let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) + var randomFlavorActionTitle = "" + var appleFlavorActionTitle = "" + if Defaults[.passwordGeneratorFlavor] == "Random" { + randomFlavorActionTitle = "✓ Random String" + appleFlavorActionTitle = "Apple's Keychain Style" + } else { + randomFlavorActionTitle = "Random String" + appleFlavorActionTitle = "✓ Apple's Keychain Style" + } + let randomFlavorAction = UIAlertAction(title: randomFlavorActionTitle, style: .default) { _ in + Defaults[.passwordGeneratorFlavor] = "Random" + sourceCell.detailTextLabel?.text = "Random" + } + + let appleFlavorAction = UIAlertAction(title: appleFlavorActionTitle, style: .default) { _ in + Defaults[.passwordGeneratorFlavor] = "Apple" + sourceCell.detailTextLabel?.text = "Apple" + } + + let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) + optionMenu.addAction(randomFlavorAction) + optionMenu.addAction(appleFlavorAction) + optionMenu.addAction(cancelAction) + optionMenu.popoverPresentationController?.sourceView = sourceCell + optionMenu.popoverPresentationController?.sourceRect = sourceCell.bounds + self.present(optionMenu, animated: true, completion: nil) + } + func tapHideUnknownSwitchDetailButton(_ sender: Any?) { let alertMessage = "Only \"key: value\" format in additional fields is supported. Unsupported fields will be given \"unkown\" keys. Turn on this switch to hide unsupported fields." let alertTitle = "Hide Unknown Fields" diff --git a/pass/Helpers/DefaultsKeys.swift b/pass/Helpers/DefaultsKeys.swift index 812b111..a658818 100644 --- a/pass/Helpers/DefaultsKeys.swift +++ b/pass/Helpers/DefaultsKeys.swift @@ -36,12 +36,7 @@ extension DefaultsKeys { static let isHideOTPOn = DefaultsKey("isHideOTPOn") static let isRememberPassphraseOn = DefaultsKey("isRememberPassphraseOn") static let isShowFolderOn = DefaultsKey("isShowFolderOn") - - static let passwordGenerationMethod = DefaultsKey("passwordGenerationMethod") + static let passwordGeneratorFlavor = DefaultsKey("passwordGeneratorFlavor") + - func initDefaultKeys() { - if Defaults[.passwordGenerationMethod] == "" { - Defaults[.passwordGenerationMethod] = "Random" - } - } } diff --git a/pass/Helpers/Utils.swift b/pass/Helpers/Utils.swift index e6ed64b..76902cc 100644 --- a/pass/Helpers/Utils.swift +++ b/pass/Helpers/Utils.swift @@ -38,10 +38,10 @@ class Utils { } static func generatePassword(length: Int) -> String{ - switch Defaults[.passwordGenerationMethod] { + switch Defaults[.passwordGeneratorFlavor] { case "Random": return randomString(length: length) - case "Keychain": + case "Apple": return Keychain.generatePassword() default: return randomString(length: length) @@ -138,6 +138,11 @@ class Utils { } return attributedPassword } + static func initDefaultKeys() { + if Defaults[.passwordGeneratorFlavor] == "" { + Defaults[.passwordGeneratorFlavor] = "Random" + } + } } // https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516 diff --git a/pass/Views/FillPasswordTableViewCell.swift b/pass/Views/FillPasswordTableViewCell.swift index 03b4dcc..a22b88e 100644 --- a/pass/Views/FillPasswordTableViewCell.swift +++ b/pass/Views/FillPasswordTableViewCell.swift @@ -23,7 +23,7 @@ class FillPasswordTableViewCell: ContentTableViewCell { } @IBAction func generatePassword(_ sender: UIButton) { - let plainPassword = Utils.randomString(length: 16) + let plainPassword = Utils.generatePassword(length: 16) contentTextField.attributedText = Utils.attributedPassword(plainPassword: plainPassword) Utils.copyToPasteboard(textToCopy: plainPassword) }