passforios/pass/Controllers/PGPKeyArmorImportTableViewController.swift

163 lines
5.9 KiB
Swift
Raw Normal View History

2017-02-17 13:44:25 +08:00
//
// PGPKeyArmorImportTableViewController.swift
2017-02-17 13:44:25 +08:00
// pass
//
// Created by Mingshen Sun on 17/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import passKit
import UIKit
2017-02-17 13:44:25 +08:00
class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController, UITextViewDelegate, QRScannerControllerDelegate {
@IBOutlet var armorPublicKeyTextView: UITextView!
@IBOutlet var armorPrivateKeyTextView: UITextView!
@IBOutlet var scanPublicKeyCell: UITableViewCell!
@IBOutlet var scanPrivateKeyCell: UITableViewCell!
var armorPublicKey: String?
var armorPrivateKey: String?
class ScannedPGPKey {
enum KeyType {
case publicKey, privateKey
}
var keyType = KeyType.publicKey
2019-10-03 14:27:00 +08:00
var segments = [String]()
var message = ""
2018-12-09 16:59:07 -08:00
func reset(keytype: KeyType) {
keyType = keytype
segments.removeAll()
2019-01-14 20:57:45 +01:00
message = "LookingForStartingFrame.".localize()
}
2018-12-09 16:59:07 -08:00
2019-10-03 14:27:00 +08:00
func addSegment(segment: String) -> (accept: Bool, message: String) {
let keyTypeStr = keyType == .publicKey ? "Public" : "Private"
let theOtherKeyTypeStr = keyType == .publicKey ? "Private" : "Public"
2019-10-03 14:27:00 +08:00
// Skip duplicated segments.
guard segment != segments.last else {
return (accept: false, message: message)
}
2018-12-09 16:59:07 -08:00
2019-10-03 14:27:00 +08:00
// Check whether we have found the first block.
guard !segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else {
2019-10-03 14:27:00 +08:00
// Check whether we are scanning the wrong key type.
if segment.contains("-----BEGIN PGP \(theOtherKeyTypeStr.uppercased()) KEY BLOCK-----") {
message = "Scan\(keyTypeStr)Key.".localize()
}
return (accept: false, message: message)
}
2019-10-03 14:27:00 +08:00
// Update the list of scanned segment and return.
segments.append(segment)
2019-11-24 10:09:36 -08:00
if segment.contains("-----END PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") {
message = "Done".localize()
return (accept: true, message: message)
2019-10-03 14:27:00 +08:00
} else {
message = "ScannedQrCodes(%d)".localize(segments.count)
return (accept: false, message: message)
}
}
}
var scanned = ScannedPGPKey()
2018-12-09 16:59:07 -08:00
2017-02-17 13:44:25 +08:00
override func viewDidLoad() {
super.viewDidLoad()
2018-12-09 16:59:07 -08:00
2019-01-14 20:57:45 +01:00
scanPublicKeyCell?.textLabel?.text = "ScanPublicKeyQrCodes".localize()
scanPublicKeyCell?.selectionStyle = .default
scanPublicKeyCell?.accessoryType = .disclosureIndicator
2019-01-14 20:57:45 +01:00
scanPrivateKeyCell?.textLabel?.text = "ScanPrivateKeyQrCodes".localize()
scanPrivateKeyCell?.selectionStyle = .default
scanPrivateKeyCell?.accessoryType = .disclosureIndicator
}
2018-12-09 16:59:07 -08:00
@IBAction
func save(_: Any) {
armorPublicKey = armorPublicKeyTextView.text
armorPrivateKey = armorPrivateKeyTextView.text
saveImportedKeys()
2017-02-17 13:44:25 +08:00
}
2018-12-09 16:59:07 -08:00
func textView(_: UITextView, shouldChangeTextIn _: NSRange, replacementText text: String) -> Bool {
if text == UIPasteboard.general.string {
// user pastes something, do the copy here again and clear the pasteboard in 45s
SecurePasteboard.shared.copy(textToCopy: text)
}
return true
}
2018-12-09 16:59:07 -08:00
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
if selectedCell == scanPublicKeyCell {
scanned.reset(keytype: ScannedPGPKey.KeyType.publicKey)
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
} else if selectedCell == scanPrivateKeyCell {
scanned.reset(keytype: ScannedPGPKey.KeyType.privateKey)
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
}
tableView.deselectRow(at: indexPath, animated: true)
}
2018-12-09 16:59:07 -08:00
// MARK: - QRScannerControllerDelegate Methods
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
2019-10-03 14:27:00 +08:00
return scanned.addSegment(segment: line)
}
2018-12-09 16:59:07 -08:00
// MARK: - QRScannerControllerDelegate Methods
func handleScannedOutput(line _: String) {
2019-10-03 14:27:00 +08:00
let key = scanned.segments.joined(separator: "")
switch scanned.keyType {
case .publicKey:
armorPublicKeyTextView.text += key
case .privateKey:
armorPrivateKeyTextView.text += key
}
}
2018-12-09 16:59:07 -08:00
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
if segue.identifier == "showPGPScannerSegue" {
if let navController = segue.destination as? UINavigationController {
if let viewController = navController.topViewController as? QRScannerController {
viewController.delegate = self
}
} else if let viewController = segue.destination as? QRScannerController {
viewController.delegate = self
}
}
}
2017-02-17 13:44:25 +08:00
}
extension PGPKeyArmorImportTableViewController: PGPKeyImporter {
static let keySource = KeySource.armor
static let label = "AsciiArmorEncryptedKey".localize()
func isReadyToUse() -> Bool {
guard !armorPublicKeyTextView.text.isEmpty else {
Utils.alert(title: "CannotSave".localize(), message: "SetPublicKey.".localize(), controller: self, completion: nil)
return false
}
guard !armorPrivateKeyTextView.text.isEmpty else {
Utils.alert(title: "CannotSave".localize(), message: "SetPrivateKey.".localize(), controller: self, completion: nil)
return false
}
return true
}
func importKeys() throws {
try KeyFileManager.PublicPgp.importKey(from: armorPublicKey ?? "")
try KeyFileManager.PrivatePgp.importKey(from: armorPrivateKey ?? "")
}
func saveImportedKeys() {
performSegue(withIdentifier: "savePGPKeySegue", sender: self)
}
}