2020-02-09 14:13:51 +01:00
|
|
|
//
|
2021-08-28 07:32:31 +02:00
|
|
|
// PGPKeyFIleImportTableViewController.swift
|
2020-02-09 14:13:51 +01:00
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Danny Moesch on 01.02.20.
|
|
|
|
|
// Copyright © 2020 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import passKit
|
|
|
|
|
|
2022-01-09 21:38:39 -08:00
|
|
|
class PGPKeyFileImportTableViewController: AutoCellHeightUITableViewController, AlertPresenting {
|
2020-06-28 21:25:40 +02:00
|
|
|
@IBOutlet var pgpPublicKeyFile: UITableViewCell!
|
|
|
|
|
@IBOutlet var pgpPrivateKeyFile: UITableViewCell!
|
2020-02-09 14:13:51 +01:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
private var publicKey: String?
|
|
|
|
|
private var privateKey: String?
|
2020-02-09 14:13:51 +01:00
|
|
|
|
|
|
|
|
private enum KeyType { case none, `private`, `public` }
|
|
|
|
|
private var currentlyPicking = KeyType.none
|
|
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@IBAction
|
2020-07-05 22:49:53 +02:00
|
|
|
private func save(_: Any) {
|
2020-06-28 21:25:40 +02:00
|
|
|
saveImportedKeys()
|
2020-02-09 14:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
let cell = tableView.cellForRow(at: indexPath)
|
2022-01-09 21:38:39 -08:00
|
|
|
let picker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .open)
|
2020-02-11 19:53:36 +01:00
|
|
|
cell?.isSelected = false
|
2020-02-09 14:13:51 +01:00
|
|
|
if cell == pgpPublicKeyFile {
|
|
|
|
|
currentlyPicking = .public
|
|
|
|
|
} else if cell == pgpPrivateKeyFile {
|
|
|
|
|
currentlyPicking = .private
|
|
|
|
|
} else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
picker.delegate = self
|
|
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
|
picker.shouldShowFileExtensions = true
|
|
|
|
|
}
|
|
|
|
|
present(picker, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 23:28:04 +01:00
|
|
|
extension PGPKeyFileImportTableViewController: UIDocumentPickerDelegate {
|
2020-02-09 14:13:51 +01:00
|
|
|
func documentPicker(_: UIDocumentPickerViewController, didPickDocumentsAt url: [URL]) {
|
|
|
|
|
guard let url = url.first else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let fileName = url.lastPathComponent
|
|
|
|
|
do {
|
2020-02-27 01:17:39 +08:00
|
|
|
// Start accessing a security-scoped resource.
|
|
|
|
|
guard url.startAccessingSecurityScopedResource() else {
|
|
|
|
|
// Handle the failure here.
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.readingFile(fileName: fileName)
|
2020-02-27 01:17:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure you release the security-scoped resource when you are done.
|
|
|
|
|
defer { url.stopAccessingSecurityScopedResource() }
|
|
|
|
|
|
2020-02-09 14:13:51 +01:00
|
|
|
let fileContent = try String(contentsOf: url, encoding: .ascii)
|
|
|
|
|
switch currentlyPicking {
|
|
|
|
|
case .none:
|
|
|
|
|
return
|
|
|
|
|
case .public:
|
|
|
|
|
publicKey = fileContent
|
|
|
|
|
pgpPublicKeyFile.textLabel?.text = fileName
|
|
|
|
|
case .private:
|
|
|
|
|
privateKey = fileContent
|
|
|
|
|
pgpPrivateKeyFile.textLabel?.text = fileName
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
2020-02-27 01:17:39 +08:00
|
|
|
let message = "FileCannotBeImported.".localize(fileName) | "UnderlyingError".localize(error.localizedDescription)
|
2022-01-09 21:38:39 -08:00
|
|
|
presentFailureAlert(title: "CannotImportFile".localize(), message: message)
|
2020-02-09 14:13:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 23:28:04 +01:00
|
|
|
extension PGPKeyFileImportTableViewController: PGPKeyImporter {
|
2020-02-15 18:12:58 +01:00
|
|
|
static let keySource = KeySource.file
|
2020-02-09 14:13:51 +01:00
|
|
|
static let label = "LoadFromFiles".localize()
|
|
|
|
|
|
|
|
|
|
func isReadyToUse() -> Bool {
|
2022-01-09 21:38:39 -08:00
|
|
|
validate(key: publicKey) && (Defaults.isYubiKeyEnabled || validate(key: privateKey))
|
2020-02-09 14:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func importKeys() throws {
|
2022-01-09 21:38:39 -08:00
|
|
|
if let publicKey = publicKey {
|
|
|
|
|
try KeyFileManager.PublicPGP.importKey(from: publicKey)
|
|
|
|
|
}
|
|
|
|
|
if let privateKey = privateKey {
|
|
|
|
|
try KeyFileManager.PrivatePGP.importKey(from: privateKey)
|
2020-02-09 14:13:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func doAfterImport() {
|
2022-01-09 21:38:39 -08:00
|
|
|
presentAlert(title: "RememberToRemoveKey".localize(), message: "RememberToRemoveKeyFromLocation.".localize())
|
2020-02-09 14:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveImportedKeys() {
|
2020-02-09 22:43:21 +01:00
|
|
|
performSegue(withIdentifier: "savePGPKeySegue", sender: self)
|
2020-02-09 14:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func validate(key: String?) -> Bool {
|
|
|
|
|
guard key != nil else {
|
2022-01-09 21:38:39 -08:00
|
|
|
presentFailureAlert(title: "CannotSavePgpKey".localize(), message: "KeyFileNotSet.".localize())
|
2020-02-09 14:13:51 +01:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|