Introduce KeyImporter protocol to reduce code duplications in SSH key importers
This commit is contained in:
parent
6aa39db657
commit
94a5f8c501
14 changed files with 189 additions and 144 deletions
|
|
@ -6,34 +6,55 @@
|
|||
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import SVProgressHUD
|
||||
import passKit
|
||||
|
||||
class SSHKeyUrlImportTableViewController: AutoCellHeightUITableViewController {
|
||||
|
||||
@IBOutlet weak var privateKeyURLTextField: UITextField!
|
||||
|
||||
let passwordStore = PasswordStore.shared
|
||||
|
||||
@IBAction func doneButtonTapped(_ sender: UIButton) {
|
||||
guard let privateKeyURL = URL(string: privateKeyURLTextField.text!.trimmed) else {
|
||||
Utils.alert(title: "CannotSave".localize(), message: "SetPrivateKeyUrl.".localize(), controller: self, completion: nil)
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
try Data(contentsOf: privateKeyURL).write(to: URL(fileURLWithPath: SshKey.PRIVATE.getFileSharingPath()), options: .atomic)
|
||||
try self.passwordStore.gitSSHKeyImportFromFileSharing()
|
||||
Defaults.gitSSHKeySource = .file
|
||||
Defaults.gitAuthenticationMethod = .key
|
||||
SVProgressHUD.showSuccess(withStatus: "Imported".localize())
|
||||
SVProgressHUD.dismiss(withDelay: 1)
|
||||
} catch {
|
||||
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
||||
}
|
||||
Defaults.gitSSHKeySource = .url
|
||||
self.navigationController!.popViewController(animated: true)
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
privateKeyURLTextField.text = Defaults.gitSSHPrivateKeyURL?.absoluteString
|
||||
}
|
||||
|
||||
@IBAction func doneButtonTapped(_ sender: UIButton) {
|
||||
if getScheme(from: privateKeyURLTextField.text?.trimmed) == "http" {
|
||||
let savePassphraseAlert = UIAlertController(title: "HttpNotSecure".localize(), message: "ReallyUseHttp?".localize(), preferredStyle: .alert)
|
||||
savePassphraseAlert.addAction(UIAlertAction(title: "No".localize(), style: .default) { _ in })
|
||||
savePassphraseAlert.addAction(UIAlertAction(title: "Yes".localize(), style: .destructive) { _ in
|
||||
self.performSegue(withIdentifier: "importSSHKeySegue", sender: self)
|
||||
})
|
||||
return present(savePassphraseAlert, animated: true)
|
||||
}
|
||||
performSegue(withIdentifier: "importSSHKeySegue", sender: self)
|
||||
}
|
||||
|
||||
private func getScheme(from url: String?) -> String? {
|
||||
return url.flatMap(URL.init(string:))?.scheme
|
||||
}
|
||||
}
|
||||
|
||||
extension SSHKeyUrlImportTableViewController: KeyImporter {
|
||||
|
||||
static let keySource = KeySource.url
|
||||
static let label = "DownloadFromUrl".localize()
|
||||
|
||||
func isReadyToUse() -> Bool {
|
||||
guard let scheme = getScheme(from: privateKeyURLTextField.text?.trimmed) else {
|
||||
Utils.alert(title: "CannotSave".localize(), message: "SetPrivateKeyUrl.".localize(), controller: self)
|
||||
return false
|
||||
}
|
||||
guard scheme == "https" || scheme == "http" else {
|
||||
Utils.alert(title: "CannotSave".localize(), message: "UseEitherHttpsOrHttp.".localize(), controller: self)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func importKeys() throws {
|
||||
Defaults.gitSSHPrivateKeyURL = URL(string: privateKeyURLTextField.text!.trimmed)
|
||||
|
||||
try KeyFileManager.PrivateSsh.importKey(from: Defaults.gitSSHPrivateKeyURL!)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue