Introduce PGPKeyImporter protocol to reduce code duplications

This commit is contained in:
Danny Moesch 2020-02-08 13:31:49 +01:00 committed by Mingshen Sun
parent 56b2205376
commit e447b1d9d3
9 changed files with 234 additions and 224 deletions

View file

@ -0,0 +1,65 @@
//
// PGPKeyImporter.swift
// pass
//
// Created by Danny Moesch on 07.02.20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import passKit
protocol PGPKeyImporter {
static var keySource: PGPKeySource { get }
static var label: String { get }
static var menuLabel: String { get }
func isReadyToUse() -> Bool
func importKeys() throws
func doAfterImport()
func saveImportedKeys()
}
extension PGPKeyImporter {
static var menuLabel: String {
if Defaults.pgpKeySource == Self.keySource {
return "\(Self.label)"
}
return Self.label
}
}
extension PGPKeyImporter where Self: UIViewController {
func savePassphraseDialog() {
let savePassphraseAlert = UIAlertController(title: "Passphrase".localize(), message: "WantToSavePassphrase?".localize(), preferredStyle: UIAlertController.Style.alert)
// Do not save the key's passphrase.
savePassphraseAlert.addAction(UIAlertAction(title: "No".localize(), style: UIAlertAction.Style.default) { _ in
AppKeychain.shared.removeContent(for: Globals.pgpKeyPassphrase)
Defaults.isRememberPGPPassphraseOn = false
self.saveImportedKeys()
})
// Save the key's passphrase.
savePassphraseAlert.addAction(UIAlertAction(title: "Yes".localize(), style: UIAlertAction.Style.destructive) {_ in
// Ask for the passphrase.
let alert = UIAlertController(title: "Passphrase".localize(), message: "FillInPgpPassphrase.".localize(), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in
AppKeychain.shared.add(string: alert.textFields?.first?.text, for: Globals.pgpKeyPassphrase)
Defaults.isRememberPGPPassphraseOn = true
self.saveImportedKeys()
}))
alert.addTextField { textField in
textField.text = AppKeychain.shared.get(for: Globals.pgpKeyPassphrase)
textField.isSecureTextEntry = true
}
self.present(alert, animated: true, completion: nil)
})
self.present(savePassphraseAlert, animated: true, completion: nil)
}
}