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
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