2017-04-02 11:21:24 -07:00
|
|
|
//
|
2020-02-12 23:28:04 +01:00
|
|
|
// SSHKeyArmorImportTableViewController.swift
|
2017-04-02 11:21:24 -07:00
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 2/4/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-04-02 11:21:24 -07:00
|
|
|
|
2020-02-12 23:28:04 +01:00
|
|
|
class SSHKeyArmorImportTableViewController: AutoCellHeightUITableViewController, UITextViewDelegate, QRScannerControllerDelegate {
|
2017-04-02 11:21:24 -07:00
|
|
|
@IBOutlet weak var armorPrivateKeyTextView: UITextView!
|
2017-06-07 02:12:54 +08:00
|
|
|
@IBOutlet weak var scanPrivateKeyCell: UITableViewCell!
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-02 11:21:24 -07:00
|
|
|
var gitSSHPrivateKeyPassphrase: String?
|
|
|
|
|
let passwordStore = PasswordStore.shared
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 02:12:54 +08:00
|
|
|
class ScannedSSHKey {
|
2019-10-03 14:27:00 +08:00
|
|
|
var segments = [String]()
|
2017-06-07 02:12:54 +08:00
|
|
|
var message = ""
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 02:12:54 +08:00
|
|
|
func reset() {
|
2019-10-03 14:27:00 +08:00
|
|
|
self.segments.removeAll()
|
2019-01-14 20:57:45 +01:00
|
|
|
message = "LookingForStartingFrame.".localize()
|
2017-06-07 02:12:54 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-10-03 14:27:00 +08:00
|
|
|
func addSegment(segment: String) -> (accept: Bool, message: String) {
|
|
|
|
|
// Skip duplicated segments.
|
|
|
|
|
guard segment != self.segments.last else {
|
|
|
|
|
return (accept: false, message: self.message)
|
2017-06-07 02:12:54 +08:00
|
|
|
}
|
2019-10-03 14:27:00 +08:00
|
|
|
|
|
|
|
|
// Check whether we have found the first block.
|
|
|
|
|
guard !self.segments.isEmpty || segment.contains("-----BEGIN") else {
|
|
|
|
|
return (accept: false, message: self.message)
|
2017-06-07 02:12:54 +08:00
|
|
|
}
|
2019-10-03 14:27:00 +08:00
|
|
|
|
|
|
|
|
// Update the list of scanned segment and return.
|
|
|
|
|
self.segments.append(segment)
|
|
|
|
|
if segment.range(of: "-----END.*KEY-----", options: .regularExpression, range: nil, locale: nil) != nil {
|
|
|
|
|
self.message = "Done".localize()
|
|
|
|
|
return (accept: true, message: self.message)
|
|
|
|
|
} else {
|
|
|
|
|
self.message = "ScannedQrCodes(%d)".localize(self.segments.count)
|
|
|
|
|
return (accept: false, message: self.message)
|
2017-06-07 02:12:54 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var scanned = ScannedSSHKey()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-02 11:21:24 -07:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2017-04-04 00:03:21 +08:00
|
|
|
armorPrivateKeyTextView.delegate = self
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-01-14 20:57:45 +01:00
|
|
|
scanPrivateKeyCell?.textLabel?.text = "ScanPrivateKeyQrCodes".localize()
|
2019-10-01 22:36:22 +02:00
|
|
|
scanPrivateKeyCell?.textLabel?.textColor = Colors.systemBlue
|
2017-06-07 02:12:54 +08:00
|
|
|
scanPrivateKeyCell?.selectionStyle = .default
|
|
|
|
|
scanPrivateKeyCell?.accessoryType = .disclosureIndicator
|
2017-04-02 11:21:24 -07:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-06 22:44:44 +08:00
|
|
|
@IBAction func doneButtonTapped(_ sender: Any) {
|
2020-02-09 14:06:08 +01:00
|
|
|
AppKeychain.shared.add(string: armorPrivateKeyTextView.text, for: SshKey.PRIVATE.getKeychainKey())
|
2020-01-02 00:48:00 +01:00
|
|
|
Defaults.gitSSHKeySource = .armor
|
|
|
|
|
Defaults.gitAuthenticationMethod = .key
|
2017-04-28 20:33:41 -07:00
|
|
|
self.navigationController!.popViewController(animated: true)
|
2017-04-02 11:21:24 -07:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-04 00:03:21 +08:00
|
|
|
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
|
|
|
|
if text == UIPasteboard.general.string {
|
2018-09-30 23:11:48 +08:00
|
|
|
// user pastes something, do the copy here again and clear the pasteboard in 45s
|
|
|
|
|
SecurePasteboard.shared.copy(textToCopy: text)
|
2017-04-04 00:03:21 +08:00
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 02:12:54 +08:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
let selectedCell = tableView.cellForRow(at: indexPath)
|
|
|
|
|
if selectedCell == scanPrivateKeyCell {
|
|
|
|
|
scanned.reset()
|
|
|
|
|
self.performSegue(withIdentifier: "showSSHScannerSegue", sender: self)
|
|
|
|
|
}
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
|
|
|
|
|
2017-06-07 02:12:54 +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)
|
2017-06-07 02:12:54 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 02:12:54 +08:00
|
|
|
// MARK: - QRScannerControllerDelegate Methods
|
|
|
|
|
func handleScannedOutput(line: String) {
|
2019-10-03 14:27:00 +08:00
|
|
|
armorPrivateKeyTextView.text = scanned.segments.joined(separator: "")
|
2017-06-07 02:12:54 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 02:12:54 +08:00
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
|
if segue.identifier == "showSSHScannerSegue" {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 02:12:54 +08:00
|
|
|
@IBAction private func cancelSSHScanner(segue: UIStoryboardSegue) {
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-07 02:12:54 +08:00
|
|
|
}
|
|
|
|
|
|
2017-04-02 11:21:24 -07:00
|
|
|
}
|