2020-02-08 13:31:49 +01:00
|
|
|
//
|
2021-12-28 02:57:11 +01:00
|
|
|
// PGPKeyURLImportTableViewController.swift
|
2020-02-08 13:31:49 +01:00
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 21/1/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import passKit
|
2020-06-28 21:25:40 +02:00
|
|
|
import UIKit
|
2020-02-08 13:31:49 +01:00
|
|
|
|
2022-01-09 21:38:39 -08:00
|
|
|
class PGPKeyURLImportTableViewController: AutoCellHeightUITableViewController, AlertPresenting {
|
2020-06-28 21:25:40 +02:00
|
|
|
@IBOutlet var pgpPublicKeyURLTextField: UITextField!
|
|
|
|
|
@IBOutlet var pgpPrivateKeyURLTextField: UITextField!
|
2020-02-08 13:31:49 +01:00
|
|
|
|
2020-04-13 19:15:52 -07:00
|
|
|
var pgpPrivateKeyURL: URL?
|
|
|
|
|
var pgpPublicKeyURL: URL?
|
|
|
|
|
|
2020-02-08 13:31:49 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
pgpPublicKeyURLTextField.text = Defaults.pgpPublicKeyURL?.absoluteString
|
|
|
|
|
pgpPrivateKeyURLTextField.text = Defaults.pgpPrivateKeyURL?.absoluteString
|
|
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
|
|
|
|
|
@IBAction
|
2020-07-05 22:49:53 +02:00
|
|
|
private func save(_: Any) {
|
2022-01-09 21:38:39 -08:00
|
|
|
pgpPublicKeyURL = validate(pgpKeyURLText: pgpPublicKeyURLTextField.text)
|
|
|
|
|
|
|
|
|
|
if !Defaults.isYubiKeyEnabled {
|
|
|
|
|
pgpPrivateKeyURL = validate(pgpKeyURLText: pgpPrivateKeyURLTextField.text)
|
2020-02-09 22:45:48 +01:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
saveImportedKeys()
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 02:57:11 +01:00
|
|
|
extension PGPKeyURLImportTableViewController: PGPKeyImporter {
|
2020-02-15 18:12:58 +01:00
|
|
|
static let keySource = KeySource.url
|
2020-02-08 13:31:49 +01:00
|
|
|
static let label = "DownloadFromUrl".localize()
|
|
|
|
|
|
|
|
|
|
func isReadyToUse() -> Bool {
|
2022-01-09 21:38:39 -08:00
|
|
|
validate(pgpKeyURLText: pgpPublicKeyURLTextField.text) != nil
|
|
|
|
|
&& (Defaults.isYubiKeyEnabled || validate(pgpKeyURLText: pgpPrivateKeyURLTextField.text ?? "") != nil)
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-09 22:39:59 +01:00
|
|
|
func importKeys() throws {
|
2022-01-09 21:38:39 -08:00
|
|
|
if let pgpPrivateKeyURL = pgpPrivateKeyURL {
|
|
|
|
|
Defaults.pgpPrivateKeyURL = pgpPrivateKeyURL
|
|
|
|
|
try KeyFileManager.PrivatePGP.importKey(from: pgpPrivateKeyURL)
|
|
|
|
|
}
|
2020-02-08 13:31:49 +01:00
|
|
|
|
2022-01-09 21:38:39 -08:00
|
|
|
if let pgpPublicKeyURL = pgpPublicKeyURL {
|
|
|
|
|
Defaults.pgpPublicKeyURL = pgpPublicKeyURL
|
|
|
|
|
try KeyFileManager.PublicPGP.importKey(from: pgpPublicKeyURL)
|
|
|
|
|
}
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func doAfterImport() {
|
2022-01-09 21:38:39 -08:00
|
|
|
presentAlert(title: "RememberToRemoveKey".localize(), message: "RememberToRemoveKeyFromServer.".localize())
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveImportedKeys() {
|
2020-02-09 22:43:21 +01:00
|
|
|
performSegue(withIdentifier: "savePGPKeySegue", sender: self)
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-09 21:38:39 -08:00
|
|
|
private func validate(pgpKeyURLText: String?) -> URL? {
|
|
|
|
|
guard let pgpKeyURL = pgpKeyURLText, let url = URL(string: pgpKeyURL) else {
|
|
|
|
|
presentFailureAlert(title: "CannotSavePgpKey".localize(), message: "SetPgpKeyUrlsFirst.".localize())
|
|
|
|
|
return nil
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
2020-04-13 19:15:52 -07:00
|
|
|
guard url.scheme == "https" || url.scheme == "http" else {
|
2022-01-09 21:38:39 -08:00
|
|
|
presentFailureAlert(title: "CannotSavePgpKey".localize(), message: "UseEitherHttpsOrHttp.".localize())
|
|
|
|
|
return nil
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
2022-01-09 21:38:39 -08:00
|
|
|
return url
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
}
|