passforios/pass/Controllers/SettingsTableViewController.swift

325 lines
16 KiB
Swift
Raw Normal View History

2017-01-19 21:15:47 +08:00
//
// SettingsTableViewController.swift
// pass
//
// Created by Mingshen Sun on 18/1/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
import SVProgressHUD
import CoreData
import passKit
2017-01-19 21:15:47 +08:00
2018-01-16 20:26:41 -08:00
class SettingsTableViewController: UITableViewController, UITabBarControllerDelegate {
@IBOutlet weak var pgpKeyTableViewCell: UITableViewCell!
2017-02-07 17:56:31 +08:00
@IBOutlet weak var passcodeTableViewCell: UITableViewCell!
2017-02-21 13:07:14 +08:00
@IBOutlet weak var passwordRepositoryTableViewCell: UITableViewCell!
var setPasscodeLockAlert: UIAlertController?
let passwordStore = PasswordStore.shared
var passcodeLock = PasscodeLock.shared
2017-01-19 21:15:47 +08:00
2018-01-16 20:26:41 -08:00
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
navigationController?.popViewController(animated: true)
}
2017-02-17 13:44:25 +08:00
@IBAction func savePGPKey(segue: UIStoryboardSegue) {
2017-02-09 21:45:31 +08:00
if let controller = segue.source as? PGPKeySettingTableViewController {
SharedDefaults[.pgpPrivateKeyURL] = URL(string: controller.pgpPrivateKeyURLTextField.text!.trimmed)
SharedDefaults[.pgpPublicKeyURL] = URL(string: controller.pgpPublicKeyURLTextField.text!.trimmed)
if SharedDefaults[.isRememberPGPPassphraseOn] {
self.passwordStore.pgpKeyPassphrase = controller.pgpPassphrase
}
SharedDefaults[.pgpKeySource] = "url"
2017-02-17 13:44:25 +08:00
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "Fetching PGP Key")
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do {
try self.passwordStore.initPGPKey(from: SharedDefaults[.pgpPublicKeyURL]!, keyType: .public)
try self.passwordStore.initPGPKey(from: SharedDefaults[.pgpPrivateKeyURL]!, keyType: .secret)
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = self.passwordStore.pgpKeyID
2017-02-20 22:02:49 +08:00
SVProgressHUD.showSuccess(withStatus: "Success")
SVProgressHUD.dismiss(withDelay: 1)
2017-04-23 10:23:54 -07:00
Utils.alert(title: "Remember to Remove the Key", message: "Remember to remove the key from the server.", controller: self, completion: nil)
}
} catch {
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
2017-03-16 23:12:31 -07:00
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
}
}
}
2017-02-17 13:44:25 +08:00
} else if let controller = segue.source as? PGPKeyArmorSettingTableViewController {
SharedDefaults[.pgpKeySource] = "armor"
if SharedDefaults[.isRememberPGPPassphraseOn] {
self.passwordStore.pgpKeyPassphrase = controller.pgpPassphrase
}
SharedDefaults[.pgpPublicKeyArmor] = controller.armorPublicKeyTextView.text!
SharedDefaults[.pgpPrivateKeyArmor] = controller.armorPrivateKeyTextView.text!
2017-02-17 13:44:25 +08:00
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "Fetching PGP Key")
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do {
try self.passwordStore.initPGPKey(with: SharedDefaults[.pgpPublicKeyArmor] ?? "", keyType: .public)
try self.passwordStore.initPGPKey(with: SharedDefaults[.pgpPrivateKeyArmor] ?? "", keyType: .secret)
2017-02-17 13:44:25 +08:00
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = self.passwordStore.pgpKeyID
2017-02-20 22:02:49 +08:00
SVProgressHUD.showSuccess(withStatus: "Success")
2017-02-17 13:44:25 +08:00
SVProgressHUD.dismiss(withDelay: 1)
}
} catch {
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
2017-03-16 23:12:31 -07:00
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
2017-02-17 13:44:25 +08:00
}
}
}
2017-01-19 21:15:47 +08:00
}
}
private func saveImportedPGPKey() {
// load keys
SharedDefaults[.pgpKeySource] = "file"
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "Fetching PGP Key")
passwordStore.pgpKeyImportFromFileSharing()
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do {
try self.passwordStore.initPGPKeys()
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = self.passwordStore.pgpKeyID
SVProgressHUD.showSuccess(withStatus: "Imported")
SVProgressHUD.dismiss(withDelay: 1)
}
} catch {
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
}
}
}
}
2017-02-21 13:07:14 +08:00
@IBAction func saveGitServerSetting(segue: UIStoryboardSegue) {
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]?.host
2017-02-21 13:07:14 +08:00
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return super.tableView(tableView, numberOfRowsInSection: section)
}
2017-01-19 21:15:47 +08:00
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: .passwordStoreErased, object: nil)
self.passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]?.host
setPGPKeyTableViewCellDetailText()
setPasswordRepositoryTableViewCellDetailText()
setPasscodeLockCell()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
tabBarController!.delegate = self
}
private func setPasscodeLockCell() {
if passcodeLock.hasPasscode {
self.passcodeTableViewCell.detailTextLabel?.text = "On"
} else {
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
}
}
private func setPGPKeyTableViewCellDetailText() {
if let pgpKeyID = self.passwordStore.pgpKeyID {
pgpKeyTableViewCell.detailTextLabel?.text = pgpKeyID
} else {
pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
}
}
private func setPasswordRepositoryTableViewCellDetailText() {
if SharedDefaults[.gitURL] == nil {
passwordRepositoryTableViewCell.detailTextLabel?.text = "Not Set"
} else {
passwordRepositoryTableViewCell.detailTextLabel?.text = SharedDefaults[.gitURL]!.host
}
}
@objc func actOnPasswordStoreErasedNotification() {
setPGPKeyTableViewCellDetailText()
setPasswordRepositoryTableViewCellDetailText()
setPasscodeLockCell()
}
2017-02-07 17:56:31 +08:00
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath) == passcodeTableViewCell {
if SharedDefaults[.passcodeKey] != nil{
2017-02-07 20:24:58 +08:00
showPasscodeActionSheet()
} else {
setPasscodeLock()
}
2017-02-17 13:44:25 +08:00
} else if tableView.cellForRow(at: indexPath) == pgpKeyTableViewCell {
showPGPKeyActionSheet()
2017-02-07 17:56:31 +08:00
}
2017-02-17 13:44:25 +08:00
tableView.deselectRow(at: indexPath, animated: true)
2017-02-07 16:45:14 +08:00
}
2017-02-17 13:44:25 +08:00
func showPGPKeyActionSheet() {
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
var urlActionTitle = "Download from URL"
var armorActionTitle = "ASCII-Armor Encrypted Key"
var fileActionTitle = "iTunes File Sharing"
2017-02-17 13:44:25 +08:00
if SharedDefaults[.pgpKeySource] == "url" {
2017-02-17 13:44:25 +08:00
urlActionTitle = "\(urlActionTitle)"
} else if SharedDefaults[.pgpKeySource] == "armor" {
2017-02-17 13:44:25 +08:00
armorActionTitle = "\(armorActionTitle)"
} else if SharedDefaults[.pgpKeySource] == "file" {
2017-02-24 17:59:04 +03:00
fileActionTitle = "\(fileActionTitle)"
2017-02-17 13:44:25 +08:00
}
let urlAction = UIAlertAction(title: urlActionTitle, style: .default) { _ in
self.performSegue(withIdentifier: "setPGPKeyByURLSegue", sender: self)
}
let armorAction = UIAlertAction(title: armorActionTitle, style: .default) { _ in
self.performSegue(withIdentifier: "setPGPKeyByASCIISegue", sender: self)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(urlAction)
optionMenu.addAction(armorAction)
2017-02-24 17:59:04 +03:00
if passwordStore.pgpKeyExists(inFileSharing: true) {
fileActionTitle.append(" (Import)")
2017-02-24 17:59:04 +03:00
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
// passphrase related
let savePassphraseAlert = UIAlertController(title: "Passphrase", message: "Do you want to save the passphrase for later decryption?", preferredStyle: UIAlertControllerStyle.alert)
// no
savePassphraseAlert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default) { _ in
self.passwordStore.pgpKeyPassphrase = nil
SharedDefaults[.isRememberPGPPassphraseOn] = false
self.saveImportedPGPKey()
})
// yes
savePassphraseAlert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.destructive) {_ in
// ask for the passphrase
let alert = UIAlertController(title: "Passphrase", message: "Please fill in the passphrase of your PGP secret key.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
self.passwordStore.pgpKeyPassphrase = alert.textFields?.first?.text
SharedDefaults[.isRememberPGPPassphraseOn] = true
self.saveImportedPGPKey()
}))
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = ""
textField.isSecureTextEntry = true
})
self.present(alert, animated: true, completion: nil)
})
self.present(savePassphraseAlert, animated: true, completion: nil)
2017-02-24 17:59:04 +03:00
}
optionMenu.addAction(fileAction)
} else {
fileActionTitle.append(" (Tips)")
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
let title = "Tips"
let message = "Copy your ASCII-armored public and private keys to Pass with names \"gpg_key.pub\" and \"gpg_key\" (without quotes) via iTunes. Then come back and click \"iTunes File Sharing\" to finish."
Utils.alert(title: title, message: message, controller: self)
}
2017-02-24 17:59:04 +03:00
optionMenu.addAction(fileAction)
}
2017-02-17 13:44:25 +08:00
if SharedDefaults[.pgpKeySource] != nil {
2017-02-17 13:44:25 +08:00
let deleteAction = UIAlertAction(title: "Remove PGP Keys", style: .destructive) { _ in
2017-06-03 18:12:33 -07:00
self.passwordStore.removePGPKeys()
2017-02-17 13:44:25 +08:00
self.pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
}
optionMenu.addAction(deleteAction)
}
optionMenu.addAction(cancelAction)
optionMenu.popoverPresentationController?.sourceView = pgpKeyTableViewCell
optionMenu.popoverPresentationController?.sourceRect = pgpKeyTableViewCell.bounds
2017-02-17 13:44:25 +08:00
self.present(optionMenu, animated: true, completion: nil)
}
2017-02-07 17:56:31 +08:00
func showPasscodeActionSheet() {
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let passcodeRemoveViewController = PasscodeLockViewController()
2017-02-10 00:56:12 +08:00
let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive) { [weak self] _ in
passcodeRemoveViewController.successCallback = {
self?.passcodeLock.delete()
self?.setPasscodeLockCell()
2017-02-07 20:24:58 +08:00
}
2017-02-10 00:56:12 +08:00
self?.present(passcodeRemoveViewController, animated: true, completion: nil)
2017-02-07 20:24:58 +08:00
}
2017-02-10 00:56:12 +08:00
let changePasscodeAction = UIAlertAction(title: "Change Passcode", style: .default) { [weak self] _ in
self?.setPasscodeLock()
2017-02-07 20:24:58 +08:00
}
2017-02-07 17:56:31 +08:00
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
optionMenu.addAction(removePasscodeAction)
optionMenu.addAction(changePasscodeAction)
optionMenu.addAction(cancelAction)
optionMenu.popoverPresentationController?.sourceView = passcodeTableViewCell
optionMenu.popoverPresentationController?.sourceRect = passcodeTableViewCell.bounds
2017-02-07 17:56:31 +08:00
self.present(optionMenu, animated: true, completion: nil)
}
@objc func alertTextFieldDidChange(_ sender: UITextField) {
// check whether we should enable the Save button in setPasscodeLockAlert
if let setPasscodeLockAlert = self.setPasscodeLockAlert,
let setPasscodeLockAlertTextFields0 = setPasscodeLockAlert.textFields?[0],
let setPasscodeLockAlertTextFields1 = setPasscodeLockAlert.textFields?[1] {
if sender == setPasscodeLockAlertTextFields0 || sender == setPasscodeLockAlertTextFields1 {
// two passwords should be the same, and length >= 4
let passcodeText = setPasscodeLockAlertTextFields0.text!
let passcodeConfirmationText = setPasscodeLockAlertTextFields1.text!
setPasscodeLockAlert.actions[0].isEnabled = passcodeText == passcodeConfirmationText && passcodeText.count >= 4
}
}
}
2017-02-07 20:24:58 +08:00
func setPasscodeLock() {
// prepare the alert for setting the passcode
setPasscodeLockAlert = UIAlertController(title: "Set passcode", message: "Fill in your passcode for Pass (at least 4 characters)", preferredStyle: .alert)
setPasscodeLockAlert?.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
textField.placeholder = "Password"
textField.isSecureTextEntry = true
textField.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControlEvents.editingChanged)
})
setPasscodeLockAlert?.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
textField.placeholder = "Password Confirmation"
textField.isSecureTextEntry = true
textField.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControlEvents.editingChanged)
})
// save action
let saveAction = UIAlertAction(title: "Save", style: .default) { (action:UIAlertAction) -> Void in
let passcode: String = self.setPasscodeLockAlert!.textFields![0].text!
self.passcodeLock.save(passcode: passcode)
// refresh the passcode lock cell ("On")
self.setPasscodeLockCell()
2017-02-07 20:24:58 +08:00
}
saveAction.isEnabled = false // disable the Save button by default
// cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
// present
setPasscodeLockAlert?.addAction(saveAction)
setPasscodeLockAlert?.addAction(cancelAction)
self.present(setPasscodeLockAlert!, animated: true, completion: nil)
2017-02-07 20:24:58 +08:00
}
2017-01-19 21:15:47 +08:00
}