Combine key scanning logic in one class
This commit is contained in:
parent
edd7398cd4
commit
078503f249
10 changed files with 405 additions and 112 deletions
|
|
@ -15,10 +15,10 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
@IBOutlet var scanPublicKeyCell: UITableViewCell!
|
||||
@IBOutlet var scanPrivateKeyCell: UITableViewCell!
|
||||
|
||||
var armorPublicKey: String?
|
||||
var armorPrivateKey: String?
|
||||
private var armorPublicKey: String?
|
||||
private var armorPrivateKey: String?
|
||||
|
||||
var scanned = ScannedPGPKey()
|
||||
private var scanner = QRKeyScanner(keyType: .pgpPublic)
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
|
@ -50,9 +50,9 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
let selectedCell = tableView.cellForRow(at: indexPath)
|
||||
if selectedCell == scanPublicKeyCell {
|
||||
scanned.reset(keytype: ScannedPGPKey.KeyType.publicKey)
|
||||
scanner = QRKeyScanner(keyType: .pgpPublic)
|
||||
} else if selectedCell == scanPrivateKeyCell {
|
||||
scanned.reset(keytype: ScannedPGPKey.KeyType.privateKey)
|
||||
scanner = QRKeyScanner(keyType: .pgpPrivate)
|
||||
}
|
||||
performSegue(withIdentifier: "showPGPScannerSegue", sender: self)
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
|
|
@ -60,19 +60,21 @@ class PGPKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
|
||||
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
||||
return scanned.addSegment(segment: line)
|
||||
func checkScannedOutput(line: String) -> (accepted: Bool, message: String) {
|
||||
scanner.add(segment: line).unrolled
|
||||
}
|
||||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
|
||||
func handleScannedOutput(line _: String) {
|
||||
let key = scanned.segments.joined()
|
||||
switch scanned.keyType {
|
||||
case .publicKey:
|
||||
let key = scanner.scannedKey
|
||||
switch scanner.keyType {
|
||||
case .pgpPublic:
|
||||
armorPublicKeyTextView.text += key
|
||||
case .privateKey:
|
||||
case .pgpPrivate:
|
||||
armorPrivateKeyTextView.text += key
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -416,11 +416,11 @@ extension PasswordEditorTableViewController: PasswordSettingSliderTableViewCellD
|
|||
// MARK: - QRScannerControllerDelegate
|
||||
|
||||
extension PasswordEditorTableViewController: QRScannerControllerDelegate {
|
||||
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
||||
func checkScannedOutput(line: String) -> (accepted: Bool, message: String) {
|
||||
if let url = URL(string: line), Token(url: url) != nil {
|
||||
return (accept: true, message: "ValidTokenUrl".localize())
|
||||
return (true, "ValidTokenUrl".localize())
|
||||
}
|
||||
return (accept: false, message: "InvalidTokenUrl".localize())
|
||||
return (false, "InvalidTokenUrl".localize())
|
||||
}
|
||||
|
||||
func handleScannedOutput(line: String) {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import SVProgressHUD
|
|||
import UIKit
|
||||
|
||||
protocol QRScannerControllerDelegate: AnyObject {
|
||||
func checkScannedOutput(line: String) -> (accept: Bool, message: String)
|
||||
func checkScannedOutput(line: String) -> (accepted: Bool, message: String)
|
||||
func handleScannedOutput(line: String)
|
||||
}
|
||||
|
||||
|
|
@ -107,13 +107,13 @@ class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDeleg
|
|||
scannerOutput.text = "NoStringValue".localize()
|
||||
return
|
||||
}
|
||||
guard let (accept, message) = delegate?.checkScannedOutput(line: scanned) else {
|
||||
guard let (accepted, message) = delegate?.checkScannedOutput(line: scanned) else {
|
||||
// no delegate, show the scanned result
|
||||
scannerOutput.text = scanned
|
||||
return
|
||||
}
|
||||
scannerOutput.text = message
|
||||
guard accept else {
|
||||
guard accepted else {
|
||||
return
|
||||
}
|
||||
captureSession?.stopRunning()
|
||||
|
|
|
|||
|
|
@ -13,42 +13,10 @@ class SSHKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
@IBOutlet var armorPrivateKeyTextView: UITextView!
|
||||
@IBOutlet var scanPrivateKeyCell: UITableViewCell!
|
||||
|
||||
var gitSSHPrivateKeyPassphrase: String?
|
||||
var armorPrivateKey: String?
|
||||
private var gitSSHPrivateKeyPassphrase: String?
|
||||
private var armorPrivateKey: String?
|
||||
|
||||
class ScannedSSHKey {
|
||||
var segments = [String]()
|
||||
var message = ""
|
||||
|
||||
func reset() {
|
||||
segments.removeAll()
|
||||
message = "LookingForStartingFrame.".localize()
|
||||
}
|
||||
|
||||
func addSegment(segment: String) -> (accept: Bool, message: String) {
|
||||
// Skip duplicated segments.
|
||||
guard segment != segments.last else {
|
||||
return (accept: false, message: message)
|
||||
}
|
||||
|
||||
// Check whether we have found the first block.
|
||||
guard !segments.isEmpty || segment.contains("-----BEGIN") else {
|
||||
return (accept: false, message: message)
|
||||
}
|
||||
|
||||
// Update the list of scanned segment and return.
|
||||
segments.append(segment)
|
||||
if segment.range(of: "-----END.*KEY-----", options: .regularExpression, range: nil, locale: nil) != nil {
|
||||
message = "Done".localize()
|
||||
return (accept: true, message: message)
|
||||
} else {
|
||||
message = "ScannedQrCodes(%d)".localize(segments.count)
|
||||
return (accept: false, message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var scanned = ScannedSSHKey()
|
||||
private var scanner = QRKeyScanner(keyType: .sshPrivate)
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
|
@ -76,7 +44,7 @@ class SSHKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
let selectedCell = tableView.cellForRow(at: indexPath)
|
||||
if selectedCell == scanPrivateKeyCell {
|
||||
scanned.reset()
|
||||
scanner = QRKeyScanner(keyType: .sshPrivate)
|
||||
performSegue(withIdentifier: "showSSHScannerSegue", sender: self)
|
||||
}
|
||||
tableView.deselectRow(at: indexPath, animated: true)
|
||||
|
|
@ -84,14 +52,14 @@ class SSHKeyArmorImportTableViewController: AutoCellHeightUITableViewController,
|
|||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
|
||||
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
||||
return scanned.addSegment(segment: line)
|
||||
func checkScannedOutput(line: String) -> (accepted: Bool, message: String) {
|
||||
scanner.add(segment: line).unrolled
|
||||
}
|
||||
|
||||
// MARK: - QRScannerControllerDelegate Methods
|
||||
|
||||
func handleScannedOutput(line _: String) {
|
||||
armorPrivateKeyTextView.text = scanned.segments.joined()
|
||||
armorPrivateKeyTextView.text = scanner.scannedKey
|
||||
}
|
||||
|
||||
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
|
||||
|
|
|
|||
73
pass/Models/QRKeyScanner.swift
Normal file
73
pass/Models/QRKeyScanner.swift
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
//
|
||||
// QRKeyScanner.swift
|
||||
// pass
|
||||
//
|
||||
// Created by Danny Moesch on 19.08.20.
|
||||
// Copyright © 2020 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
struct QRKeyScanner {
|
||||
enum Result: Equatable {
|
||||
case lookingForStart
|
||||
case wrongKeyType(ScannableKeyType)
|
||||
case completed
|
||||
case scanned(Int)
|
||||
|
||||
var message: String {
|
||||
switch self {
|
||||
case .lookingForStart:
|
||||
return "LookingForStartingFrame.".localize()
|
||||
case let .wrongKeyType(keyType):
|
||||
return "Scan\(keyType.visibility)Key.".localize()
|
||||
case .completed:
|
||||
return "Done".localize()
|
||||
case let .scanned(count):
|
||||
return "ScannedQrCodes(%d)".localize(count)
|
||||
}
|
||||
}
|
||||
|
||||
var unrolled: (accepted: Bool, message: String) {
|
||||
if self == .completed {
|
||||
return (true, message)
|
||||
}
|
||||
return (false, message)
|
||||
}
|
||||
}
|
||||
|
||||
private var segments = [String]()
|
||||
private var previousResult = Result.lookingForStart
|
||||
|
||||
let keyType: ScannableKeyType
|
||||
|
||||
init(keyType: ScannableKeyType) {
|
||||
self.keyType = keyType
|
||||
}
|
||||
|
||||
var scannedKey: String {
|
||||
segments.joined()
|
||||
}
|
||||
|
||||
mutating func add(segment: String) -> Result {
|
||||
// Skip duplicated segments.
|
||||
guard segment != segments.last else {
|
||||
return previousResult
|
||||
}
|
||||
|
||||
// Check whether we have found the first block.
|
||||
guard !segments.isEmpty || segment.contains(keyType.headerStart) else {
|
||||
// Check whether we are scanning the wrong key type.
|
||||
if let counterKeyType = keyType.counterType, segment.contains(counterKeyType.headerStart) {
|
||||
previousResult = .wrongKeyType(counterKeyType)
|
||||
}
|
||||
return previousResult
|
||||
}
|
||||
|
||||
// Update the list of scanned segments and return.
|
||||
segments.append(segment)
|
||||
if segment.starts(with: keyType.footerStart), segment.hasSuffix(keyType.footerEnd) {
|
||||
return .completed
|
||||
}
|
||||
previousResult = .scanned(segments.count)
|
||||
return previousResult
|
||||
}
|
||||
}
|
||||
60
pass/Models/ScannableKeyType.swift
Normal file
60
pass/Models/ScannableKeyType.swift
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
//
|
||||
// ScannableKeyType.swift
|
||||
// pass
|
||||
//
|
||||
// Created by Danny Moesch on 19.08.20.
|
||||
// Copyright © 2020 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
enum ScannableKeyType {
|
||||
case pgpPublic
|
||||
case pgpPrivate
|
||||
case sshPrivate
|
||||
|
||||
var visibility: String {
|
||||
switch self {
|
||||
case .pgpPublic:
|
||||
return "Public"
|
||||
case .pgpPrivate, .sshPrivate:
|
||||
return "Private"
|
||||
}
|
||||
}
|
||||
|
||||
var headerStart: String {
|
||||
switch self {
|
||||
case .pgpPublic, .pgpPrivate:
|
||||
return "-----BEGIN PGP \(visibility.uppercased()) KEY BLOCK-----"
|
||||
case .sshPrivate:
|
||||
return "-----BEGIN"
|
||||
}
|
||||
}
|
||||
|
||||
var footerStart: String {
|
||||
switch self {
|
||||
case .pgpPublic, .pgpPrivate:
|
||||
return "-----END PGP \(visibility.uppercased())"
|
||||
case .sshPrivate:
|
||||
return "-----END"
|
||||
}
|
||||
}
|
||||
|
||||
var footerEnd: String {
|
||||
switch self {
|
||||
case .pgpPublic, .pgpPrivate:
|
||||
return "KEY BLOCK-----"
|
||||
case .sshPrivate:
|
||||
return "KEY-----"
|
||||
}
|
||||
}
|
||||
|
||||
var counterType: Self? {
|
||||
switch self {
|
||||
case .pgpPublic:
|
||||
return .pgpPrivate
|
||||
case .pgpPrivate:
|
||||
return .pgpPublic
|
||||
case .sshPrivate:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
//
|
||||
// ScannedPGPKey.swift
|
||||
// pass
|
||||
//
|
||||
// Created by Danny Moesch on 05.07.20.
|
||||
// Copyright © 2020 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
class ScannedPGPKey {
|
||||
enum KeyType {
|
||||
case publicKey, privateKey
|
||||
}
|
||||
|
||||
var keyType = KeyType.publicKey
|
||||
var segments = [String]()
|
||||
var message = ""
|
||||
|
||||
func reset(keytype: KeyType) {
|
||||
keyType = keytype
|
||||
segments.removeAll()
|
||||
message = "LookingForStartingFrame.".localize()
|
||||
}
|
||||
|
||||
func addSegment(segment: String) -> (accept: Bool, message: String) {
|
||||
let keyTypeStr = keyType == .publicKey ? "Public" : "Private"
|
||||
let theOtherKeyTypeStr = keyType == .publicKey ? "Private" : "Public"
|
||||
|
||||
// Skip duplicated segments.
|
||||
guard segment != segments.last else {
|
||||
return (accept: false, message: message)
|
||||
}
|
||||
|
||||
// Check whether we have found the first block.
|
||||
guard !segments.isEmpty || segment.contains("-----BEGIN PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") else {
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Update the list of scanned segment and return.
|
||||
segments.append(segment)
|
||||
if segment.contains("-----END PGP \(keyTypeStr.uppercased()) KEY BLOCK-----") {
|
||||
message = "Done".localize()
|
||||
return (accept: true, message: message)
|
||||
} else {
|
||||
message = "ScannedQrCodes(%d)".localize(segments.count)
|
||||
return (accept: false, message: message)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue