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?) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue