passforios/pass/Controllers/PGPKeySettingTableViewController.swift

70 lines
3.1 KiB
Swift
Raw Normal View History

//
// PGPKeySettingTableViewController.swift
// pass
//
// Created by Mingshen Sun on 21/1/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
import passKit
class PGPKeySettingTableViewController: AutoCellHeightUITableViewController {
2017-02-10 22:15:01 +08:00
@IBOutlet weak var pgpPublicKeyURLTextField: UITextField!
@IBOutlet weak var pgpPrivateKeyURLTextField: UITextField!
let passwordStore = PasswordStore.shared
let keychain = AppKeychain.shared
2018-12-09 16:59:07 -08:00
override func viewDidLoad() {
super.viewDidLoad()
pgpPublicKeyURLTextField.text = Defaults.pgpPublicKeyURL?.absoluteString
pgpPrivateKeyURLTextField.text = Defaults.pgpPrivateKeyURL?.absoluteString
}
2018-12-09 16:59:07 -08:00
private func validatePGPKeyURL(input: String?) -> Bool {
guard let path = input, let url = URL(string: path) else {
2019-01-14 20:57:45 +01:00
Utils.alert(title: "CannotSavePgpKey".localize(), message: "SetPgpKeyUrlFirst.".localize(), controller: self, completion: nil)
return false
}
2019-06-25 22:43:37 +02:00
guard let scheme = url.scheme, scheme == "https" else {
2019-01-14 20:57:45 +01:00
Utils.alert(title: "CannotSavePgpKey".localize(), message: "HttpNotSupported.".localize(), controller: self, completion: nil)
return false
}
return true
}
2018-12-09 16:59:07 -08:00
@IBAction func save(_ sender: Any) {
2017-07-27 21:12:58 +08:00
guard validatePGPKeyURL(input: pgpPublicKeyURLTextField.text) == true,
validatePGPKeyURL(input: pgpPrivateKeyURLTextField.text) == true else {
return
}
2019-05-01 17:49:27 +02:00
let savePassphraseAlert = UIAlertController(title: "Passphrase".localize(), message: "WantToSavePassphrase?".localize(), preferredStyle: UIAlertController.Style.alert)
// no
2019-05-01 17:49:27 +02:00
savePassphraseAlert.addAction(UIAlertAction(title: "No".localize(), style: UIAlertAction.Style.default) { _ in
self.keychain.removeContent(for: Globals.pgpKeyPassphrase)
Defaults.isRememberPGPPassphraseOn = false
self.performSegue(withIdentifier: "savePGPKeySegue", sender: self)
})
// yes
2019-05-01 17:49:27 +02:00
savePassphraseAlert.addAction(UIAlertAction(title: "Yes".localize(), style: UIAlertAction.Style.destructive) {_ in
// ask for the passphrase
2019-05-01 17:49:27 +02:00
let alert = UIAlertController(title: "Passphrase".localize(), message: "FillInPgpPassphrase.".localize(), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in
self.keychain.add(string: alert.textFields?.first?.text, for: Globals.pgpKeyPassphrase)
Defaults.isRememberPGPPassphraseOn = true
self.performSegue(withIdentifier: "savePGPKeySegue", sender: self)
}))
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = self.keychain.get(for: Globals.pgpKeyPassphrase)
textField.isSecureTextEntry = true
})
self.present(alert, animated: true, completion: nil)
})
self.present(savePassphraseAlert, animated: true, completion: nil)
}
2018-12-09 16:59:07 -08:00
}