2017-01-22 01:42:36 +08:00
|
|
|
//
|
|
|
|
|
// PGPKeySettingTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 21/1/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-01-22 01:42:36 +08:00
|
|
|
|
2019-02-17 19:01:08 +01:00
|
|
|
class PGPKeySettingTableViewController: AutoCellHeightUITableViewController {
|
2017-01-22 01:42:36 +08:00
|
|
|
|
2017-02-10 22:15:01 +08:00
|
|
|
@IBOutlet weak var pgpPublicKeyURLTextField: UITextField!
|
|
|
|
|
@IBOutlet weak var pgpPrivateKeyURLTextField: UITextField!
|
2019-09-08 23:00:46 +02:00
|
|
|
|
2017-03-16 22:39:03 -07:00
|
|
|
let passwordStore = PasswordStore.shared
|
2019-09-08 23:00:46 +02:00
|
|
|
let keychain = AppKeychain.shared
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2020-01-02 00:48:00 +01:00
|
|
|
pgpPublicKeyURLTextField.text = Defaults.pgpPublicKeyURL?.absoluteString
|
|
|
|
|
pgpPrivateKeyURLTextField.text = Defaults.pgpPrivateKeyURL?.absoluteString
|
2017-01-22 01:42:36 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-05-11 22:55:55 -07: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)
|
2017-05-11 22:55:55 -07:00
|
|
|
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)
|
2017-05-11 22:55:55 -07:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-17 14:58:27 +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)
|
2017-06-07 21:11:01 +08:00
|
|
|
// no
|
2019-05-01 17:49:27 +02:00
|
|
|
savePassphraseAlert.addAction(UIAlertAction(title: "No".localize(), style: UIAlertAction.Style.default) { _ in
|
2019-09-08 23:00:46 +02:00
|
|
|
self.keychain.removeContent(for: Globals.pgpKeyPassphrase)
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.isRememberPGPPassphraseOn = false
|
2017-06-07 21:11:01 +08:00
|
|
|
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
|
2017-06-07 21:11:01 +08:00
|
|
|
// 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
|
2019-09-08 23:00:46 +02:00
|
|
|
self.keychain.add(string: alert.textFields?.first?.text, for: Globals.pgpKeyPassphrase)
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.isRememberPGPPassphraseOn = true
|
2017-06-07 21:11:01 +08:00
|
|
|
self.performSegue(withIdentifier: "savePGPKeySegue", sender: self)
|
|
|
|
|
}))
|
|
|
|
|
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
2019-09-08 23:00:46 +02:00
|
|
|
textField.text = self.keychain.get(for: Globals.pgpKeyPassphrase)
|
2017-06-07 21:11:01 +08:00
|
|
|
textField.isSecureTextEntry = true
|
|
|
|
|
})
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
2017-02-17 14:58:27 +08:00
|
|
|
})
|
2017-06-07 21:11:01 +08:00
|
|
|
self.present(savePassphraseAlert, animated: true, completion: nil)
|
2017-02-17 14:58:27 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
|
|
|
|
|
2017-01-22 01:42:36 +08:00
|
|
|
}
|