2020-02-08 13:31:49 +01:00
|
|
|
//
|
2020-02-12 23:28:04 +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 UIKit
|
|
|
|
|
import passKit
|
|
|
|
|
|
2020-02-12 23:28:04 +01:00
|
|
|
class PGPKeyUrlImportTableViewController: AutoCellHeightUITableViewController {
|
2020-02-08 13:31:49 +01:00
|
|
|
|
|
|
|
|
@IBOutlet weak var pgpPublicKeyURLTextField: UITextField!
|
|
|
|
|
@IBOutlet weak var pgpPrivateKeyURLTextField: UITextField!
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@IBAction func save(_ sender: Any) {
|
2020-04-13 19:15:52 -07:00
|
|
|
guard let publicKeyURLText = pgpPublicKeyURLTextField.text,
|
|
|
|
|
let publicKeyURL = URL(string: publicKeyURLText),
|
|
|
|
|
let privateKeyURLText = pgpPrivateKeyURLTextField.text,
|
|
|
|
|
let privateKeyURL = URL(string: privateKeyURLText) else {
|
|
|
|
|
Utils.alert(title: "CannotSavePgpKey".localize(), message: "SetPgpKeyUrlsFirst.".localize(), controller: self)
|
|
|
|
|
return
|
2020-02-09 22:45:48 +01:00
|
|
|
}
|
2020-04-13 19:15:52 -07:00
|
|
|
if privateKeyURL.scheme?.lowercased() == "http" || publicKeyURL.scheme?.lowercased() == "http" {
|
|
|
|
|
Utils.alert(title: "HttpNotSecure".localize(), message: "ReallyUseHttp.".localize(), controller: self)
|
2020-02-09 22:45:48 +01:00
|
|
|
}
|
2020-04-13 19:15:52 -07:00
|
|
|
pgpPrivateKeyURL = privateKeyURL
|
|
|
|
|
pgpPublicKeyURL = publicKeyURL
|
|
|
|
|
self.saveImportedKeys()
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 23:28:04 +01:00
|
|
|
extension PGPKeyUrlImportTableViewController: PGPKeyImporter {
|
2020-02-08 13:31:49 +01:00
|
|
|
|
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 {
|
2020-04-13 19:15:52 -07:00
|
|
|
return validate(pgpKeyUrl: pgpPublicKeyURLTextField.text ?? "")
|
|
|
|
|
&& validate(pgpKeyUrl: pgpPrivateKeyURLTextField.text ?? "")
|
2020-02-08 13:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-09 22:39:59 +01:00
|
|
|
func importKeys() throws {
|
2020-04-13 19:15:52 -07:00
|
|
|
Defaults.pgpPrivateKeyURL = pgpPrivateKeyURL
|
|
|
|
|
Defaults.pgpPublicKeyURL = pgpPublicKeyURL
|
2020-02-08 13:31:49 +01:00
|
|
|
|
|
|
|
|
try KeyFileManager.PublicPgp.importKey(from: Defaults.pgpPublicKeyURL!)
|
|
|
|
|
try KeyFileManager.PrivatePgp.importKey(from: Defaults.pgpPrivateKeyURL!)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func doAfterImport() {
|
2020-02-09 22:43:21 +01:00
|
|
|
Utils.alert(title: "RememberToRemoveKey".localize(), message: "RememberToRemoveKeyFromServer.".localize(), controller: self)
|
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
|
|
|
}
|
|
|
|
|
|
2020-04-13 19:15:52 -07:00
|
|
|
private func validate(pgpKeyUrl: String) -> Bool {
|
|
|
|
|
guard let url = URL(string: pgpKeyUrl) else {
|
2020-02-09 22:45:48 +01:00
|
|
|
Utils.alert(title: "CannotSavePgpKey".localize(), message: "SetPgpKeyUrlsFirst.".localize(), controller: self)
|
2020-02-08 13:31:49 +01:00
|
|
|
return false
|
|
|
|
|
}
|
2020-04-13 19:15:52 -07:00
|
|
|
guard url.scheme == "https" || url.scheme == "http" else {
|
2020-02-09 22:45:48 +01:00
|
|
|
Utils.alert(title: "CannotSavePgpKey".localize(), message: "UseEitherHttpsOrHttp.".localize(), controller: self)
|
2020-02-08 13:31:49 +01:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|