Add ability to import SSH keys from the Files app

This commit is contained in:
Danny Moesch 2020-02-15 18:14:06 +01:00 committed by Mingshen Sun
parent 94a5f8c501
commit d33e63cd83
8 changed files with 176 additions and 0 deletions

View file

@ -247,6 +247,9 @@ class GitRepositorySettingsTableViewController: UITableViewController {
optionMenu.addAction(UIAlertAction(title: SSHKeyArmorImportTableViewController.menuLabel, style: .default) { _ in
self.performSegue(withIdentifier: "setGitSSHKeyByArmorSegue", sender: self)
})
optionMenu.addAction(UIAlertAction(title: SSHKeyFileImportTableViewController.menuLabel, style: .default) { _ in
self.performSegue(withIdentifier: "setGitSSHKeyByFileSegue", sender: self)
})
if isReadyToUse() {
optionMenu.addAction(UIAlertAction(title: "\(Self.menuLabel) (\("Import".localize()))", style: .default) { _ in

View file

@ -0,0 +1,72 @@
//
// SSHKeyFileImportTableViewController.swift
// pass
//
// Created by Danny Moesch on 15.02.20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import passKit
import SVProgressHUD
class SSHKeyFileImportTableViewController: AutoCellHeightUITableViewController {
@IBOutlet weak var sshPrivateKeyFile: UITableViewCell!
private var privateKey: String? = nil
@IBAction func doneButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "importSSHKeySegue", sender: self)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
let picker = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .open)
cell?.isSelected = false
guard cell == sshPrivateKeyFile else {
return
}
picker.delegate = self
if #available(iOS 13.0, *) {
picker.shouldShowFileExtensions = true
}
present(picker, animated: true, completion: nil)
}
}
extension SSHKeyFileImportTableViewController: UIDocumentPickerDelegate {
func documentPicker(_: UIDocumentPickerViewController, didPickDocumentsAt url: [URL]) {
guard let url = url.first else {
return
}
let fileName = url.lastPathComponent
do {
privateKey = try String(contentsOf: url, encoding: .ascii)
sshPrivateKeyFile.textLabel?.text = fileName
} catch {
Utils.alert(title: "CannotImportFile".localize(), message: "FileCannotBeImported.".localize(fileName), controller: self)
}
}
}
extension SSHKeyFileImportTableViewController: KeyImporter {
static let keySource = KeySource.file
static let label = "LoadFromFiles".localize()
func isReadyToUse() -> Bool {
guard privateKey != nil else {
Utils.alert(title: "CannotSave".localize(), message: "SetPrivateKeyUrl.".localize(), controller: self)
return false
}
return true
}
func importKeys() throws {
guard let privateKey = privateKey else {
return
}
try KeyFileManager.PrivateSsh.importKey(from: privateKey)
}
}