passforios/pass/Controllers/SettingsTableViewController.swift

221 lines
11 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 SwiftyUserDefaults
2017-02-07 20:24:58 +08:00
import PasscodeLock
2017-01-19 21:15:47 +08:00
class SettingsTableViewController: UITableViewController {
2017-02-07 20:24:58 +08:00
2017-02-07 20:57:06 +08:00
let touchIDSwitch = UISwitch(frame: CGRect.zero)
@IBOutlet weak var pgpKeyTableViewCell: UITableViewCell!
2017-02-07 17:56:31 +08:00
@IBOutlet weak var touchIDTableViewCell: UITableViewCell!
@IBOutlet weak var passcodeTableViewCell: UITableViewCell!
2017-02-09 21:45:31 +08:00
2017-02-17 13:44:25 +08:00
@IBAction func cancelPGPKey(segue: UIStoryboardSegue) {
2017-01-19 21:15:47 +08:00
}
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 {
Defaults[.pgpPrivateKeyURL] = URL(string: controller.pgpPrivateKeyURLTextField.text!)
Defaults[.pgpPublicKeyURL] = URL(string: controller.pgpPublicKeyURLTextField.text!)
PasswordStore.shared.pgpKeyPassphrase = controller.pgpPassphrase
Defaults[.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 PasswordStore.shared.initPGP(pgpPublicKeyURL: Defaults[.pgpPublicKeyURL]!,
pgpPublicKeyLocalPath: Globals.pgpPublicKeyPath,
pgpPrivateKeyURL: Defaults[.pgpPrivateKeyURL]!,
pgpPrivateKeyLocalPath: Globals.pgpPrivateKeyPath)
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
SVProgressHUD.showSuccess(withStatus: "Success.")
SVProgressHUD.dismiss(withDelay: 1)
Utils.alert(title: "Rememver 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"
Defaults[.pgpKeyID] = nil
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 1)
}
}
}
2017-02-17 13:44:25 +08:00
} else if let controller = segue.source as? PGPKeyArmorSettingTableViewController {
Defaults[.pgpKeySource] = "armor"
PasswordStore.shared.pgpKeyPassphrase = controller.pgpPassphrase
Utils.addPasswrodToKeychain(name: "pgpKeyPassphrase", password: controller.pgpPassphrase!)
2017-02-17 13:44:25 +08:00
Defaults[.pgpPublicKeyArmor] = controller.armorPublicKeyTextView.text!
Defaults[.pgpPrivateKeyArmor] = controller.armorPrivateKeyTextView.text!
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "Fetching PGP Key")
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do {
try PasswordStore.shared.initPGP(pgpPublicKeyArmor: controller.armorPublicKeyTextView.text!,
pgpPublicKeyLocalPath: Globals.pgpPublicKeyPath,
pgpPrivateKeyArmor: controller.armorPrivateKeyTextView.text!,
pgpPrivateKeyLocalPath: Globals.pgpPrivateKeyPath)
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
SVProgressHUD.showSuccess(withStatus: "Success.")
SVProgressHUD.dismiss(withDelay: 1)
}
} catch {
DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
Defaults[.pgpKeyID] = nil
SVProgressHUD.showError(withStatus: error.localizedDescription)
SVProgressHUD.dismiss(withDelay: 1)
}
}
}
2017-01-19 21:15:47 +08:00
}
}
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil)
2017-02-08 00:43:59 +08:00
touchIDSwitch.onTintColor = UIColor(displayP3Red: 0, green: 122.0/255, blue: 1, alpha: 1)
2017-02-07 17:56:31 +08:00
touchIDTableViewCell.accessoryView = touchIDSwitch
touchIDSwitch.addTarget(self, action: #selector(touchIDSwitchAction), for: UIControlEvents.valueChanged)
if Defaults[.pgpKeyID] == "" {
pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
} else {
pgpKeyTableViewCell.detailTextLabel?.text = Defaults[.pgpKeyID]
}
2017-02-07 20:24:58 +08:00
if Defaults[.isTouchIDOn] {
touchIDSwitch.isOn = true
} else {
touchIDSwitch.isOn = false
}
2017-02-07 20:57:06 +08:00
if PasscodeLockRepository().hasPasscode {
2017-02-07 20:24:58 +08:00
self.passcodeTableViewCell.detailTextLabel?.text = "On"
} else {
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
2017-02-07 20:57:06 +08:00
touchIDSwitch.isEnabled = false
2017-02-07 20:24:58 +08:00
}
2017-02-07 17:56:31 +08:00
}
func actOnPasswordStoreErasedNotification() {
pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
touchIDSwitch.isOn = false
self.passcodeTableViewCell.detailTextLabel?.text = "Off"
Globals.passcodeConfiguration.isTouchIDAllowed = false
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration)
}
2017-02-07 17:56:31 +08:00
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath) == passcodeTableViewCell {
2017-02-07 20:24:58 +08:00
if Defaults[.passcodeKey] != nil{
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
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2017-01-19 21:15:47 +08:00
}
2017-02-07 17:56:31 +08:00
func touchIDSwitchAction(uiSwitch: UISwitch) {
if uiSwitch.isOn {
2017-02-07 20:57:06 +08:00
Defaults[.isTouchIDOn] = true
2017-02-08 10:15:38 +08:00
Globals.passcodeConfiguration.isTouchIDAllowed = true
2017-02-07 17:56:31 +08:00
} else {
2017-02-07 20:57:06 +08:00
Defaults[.isTouchIDOn] = false
2017-02-08 10:15:38 +08:00
Globals.passcodeConfiguration.isTouchIDAllowed = false
2017-02-07 17:56:31 +08:00
}
2017-02-08 14:53:07 +08:00
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration)
2017-02-07 17:56:31 +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"
if Defaults[.pgpKeySource] == "url" {
urlActionTitle = "\(urlActionTitle)"
} else if Defaults[.pgpKeySource] == "armor" {
armorActionTitle = "\(armorActionTitle)"
}
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)
if Defaults[.pgpKeySource] != nil {
let deleteAction = UIAlertAction(title: "Remove PGP Keys", style: .destructive) { _ in
Utils.removePGPKeys()
self.pgpKeyTableViewCell.detailTextLabel?.text = "Not Set"
}
optionMenu.addAction(deleteAction)
}
optionMenu.addAction(cancelAction)
self.present(optionMenu, animated: true, completion: nil)
}
2017-02-07 17:56:31 +08:00
func showPasscodeActionSheet() {
2017-02-08 10:15:38 +08:00
let passcodeChangeViewController = PasscodeLockViewController(state: .change, configuration: Globals.passcodeConfiguration)
let passcodeRemoveViewController = PasscodeLockViewController(state: .remove, configuration: Globals.passcodeConfiguration)
2017-02-07 20:24:58 +08:00
2017-02-07 17:56:31 +08:00
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
2017-02-10 00:56:12 +08:00
let removePasscodeAction = UIAlertAction(title: "Remove Passcode", style: .destructive) { [weak self] _ in
2017-02-07 20:24:58 +08:00
passcodeRemoveViewController.successCallback = { _ in
2017-02-10 00:56:12 +08:00
self?.passcodeTableViewCell.detailTextLabel?.text = "Off"
self?.touchIDSwitch.isEnabled = false
2017-02-08 16:29:03 +08:00
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.passcodeLockPresenter = PasscodeLockPresenter(mainWindow: appDelegate.window, configuration: Globals.passcodeConfiguration)
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?.present(passcodeChangeViewController, animated: true, completion: nil)
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)
self.present(optionMenu, animated: true, completion: nil)
}
2017-02-07 20:24:58 +08:00
func setPasscodeLock() {
2017-02-08 10:15:38 +08:00
let passcodeSetViewController = PasscodeLockViewController(state: .set, configuration: Globals.passcodeConfiguration)
2017-02-07 20:24:58 +08:00
passcodeSetViewController.successCallback = { _ in
self.passcodeTableViewCell.detailTextLabel?.text = "On"
2017-02-07 20:57:06 +08:00
self.touchIDSwitch.isEnabled = true
2017-02-07 20:24:58 +08:00
}
present(passcodeSetViewController, animated: true, completion: nil)
}
2017-01-19 21:15:47 +08:00
}