2017-02-13 01:15:42 +08:00
|
|
|
//
|
|
|
|
|
// PasswordEditorTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 12/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2017-04-08 03:08:12 +08:00
|
|
|
import OneTimePassword
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-03-29 00:00:24 +08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
enum PasswordEditorCellType {
|
2017-04-27 23:07:22 +08:00
|
|
|
case nameCell, fillPasswordCell, passwordLengthCell, additionsCell, deletePasswordCell, scanQRCodeCell
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum PasswordEditorCellKey {
|
|
|
|
|
case type, title, content, placeholders
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 23:07:22 +08:00
|
|
|
class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate, PasswordSettingSliderTableViewCellDelegate, QRScannerControllerDelegate, UITextFieldDelegate, UITextViewDelegate {
|
2017-03-24 01:23:46 +08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
var tableData = [
|
|
|
|
|
[Dictionary<PasswordEditorCellKey, Any>]
|
2017-03-24 01:23:46 +08:00
|
|
|
]()
|
|
|
|
|
var password: Password?
|
2017-03-09 03:19:36 +08:00
|
|
|
|
2017-03-24 01:23:46 +08:00
|
|
|
private var navigationItemTitle: String?
|
|
|
|
|
|
|
|
|
|
private var sectionHeaderTitles = ["name", "password", "additions",""].map {$0.uppercased()}
|
2018-02-26 16:53:39 +01:00
|
|
|
private var sectionFooterTitles = ["", "", "Use YAML format for additional fields.", ""]
|
2017-04-27 23:07:22 +08:00
|
|
|
private let nameSection = 0
|
2017-03-24 01:23:46 +08:00
|
|
|
private let passwordSection = 1
|
2017-04-08 03:08:12 +08:00
|
|
|
private let additionsSection = 2
|
2017-03-24 01:23:46 +08:00
|
|
|
private var hidePasswordSettings = true
|
|
|
|
|
|
2017-04-27 23:26:12 +08:00
|
|
|
var nameCell: TextFieldTableViewCell?
|
|
|
|
|
var fillPasswordCell: FillPasswordTableViewCell?
|
2017-03-24 01:23:46 +08:00
|
|
|
private var passwordLengthCell: SliderTableViewCell?
|
2017-04-27 23:26:12 +08:00
|
|
|
var additionsCell: TextViewTableViewCell?
|
2017-03-24 01:23:46 +08:00
|
|
|
private var deletePasswordCell: UITableViewCell?
|
2017-04-08 03:08:12 +08:00
|
|
|
private var scanQRCodeCell: UITableViewCell?
|
2017-03-21 13:16:25 -07:00
|
|
|
|
|
|
|
|
override func loadView() {
|
|
|
|
|
super.loadView()
|
|
|
|
|
|
|
|
|
|
deletePasswordCell = UITableViewCell(style: .default, reuseIdentifier: "default")
|
|
|
|
|
deletePasswordCell!.textLabel?.text = "Delete Password"
|
|
|
|
|
deletePasswordCell!.textLabel?.textColor = Globals.red
|
|
|
|
|
deletePasswordCell?.selectionStyle = .default
|
2017-04-08 03:08:12 +08:00
|
|
|
|
|
|
|
|
scanQRCodeCell = UITableViewCell(style: .default, reuseIdentifier: "default")
|
|
|
|
|
scanQRCodeCell?.textLabel?.text = "Add One-Time Password"
|
|
|
|
|
scanQRCodeCell?.textLabel?.textColor = Globals.blue
|
|
|
|
|
scanQRCodeCell?.selectionStyle = .default
|
2017-04-10 23:04:23 +08:00
|
|
|
scanQRCodeCell?.accessoryType = .disclosureIndicator
|
2017-04-08 03:08:12 +08:00
|
|
|
// scanQRCodeCell?.imageView?.image = #imageLiteral(resourceName: "Camera").withRenderingMode(.alwaysTemplate)
|
|
|
|
|
// scanQRCodeCell?.imageView?.tintColor = Globals.blue
|
|
|
|
|
// scanQRCodeCell?.imageView?.contentMode = .scaleAspectFit
|
2017-03-21 13:16:25 -07:00
|
|
|
}
|
2017-03-24 01:23:46 +08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2017-04-07 01:59:03 +08:00
|
|
|
if navigationItemTitle != nil {
|
|
|
|
|
navigationItem.title = navigationItemTitle
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
tableView.register(UINib(nibName: "TextFieldTableViewCell", bundle: nil), forCellReuseIdentifier: "textFieldCell")
|
|
|
|
|
tableView.register(UINib(nibName: "TextViewTableViewCell", bundle: nil), forCellReuseIdentifier: "textViewCell")
|
|
|
|
|
tableView.register(UINib(nibName: "FillPasswordTableViewCell", bundle: nil), forCellReuseIdentifier: "fillPasswordCell")
|
2017-03-09 03:19:36 +08:00
|
|
|
tableView.register(UINib(nibName: "SliderTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordLengthCell")
|
2017-02-13 01:15:42 +08:00
|
|
|
|
|
|
|
|
tableView.rowHeight = UITableViewAutomaticDimension
|
|
|
|
|
tableView.estimatedRowHeight = 48
|
2017-02-21 01:06:03 +08:00
|
|
|
self.tableView.sectionFooterHeight = UITableViewAutomaticDimension;
|
|
|
|
|
self.tableView.estimatedSectionFooterHeight = 0;
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2017-10-07 21:07:02 -07:00
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
|
additionsCell?.contentTextView.setContentOffset(.zero, animated: false)
|
|
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
|
let cellData = tableData[indexPath.section][indexPath.row]
|
|
|
|
|
|
|
|
|
|
switch cellData[PasswordEditorCellKey.type] as! PasswordEditorCellType {
|
2017-04-27 23:07:22 +08:00
|
|
|
case .nameCell:
|
|
|
|
|
nameCell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as? TextFieldTableViewCell
|
|
|
|
|
nameCell?.contentTextField.delegate = self
|
|
|
|
|
nameCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
|
|
|
|
|
return nameCell!
|
2017-02-13 01:15:42 +08:00
|
|
|
case .fillPasswordCell:
|
2017-03-23 01:28:46 +08:00
|
|
|
fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as? FillPasswordTableViewCell
|
|
|
|
|
fillPasswordCell?.delegate = self
|
2017-07-07 11:32:03 +08:00
|
|
|
fillPasswordCell?.contentTextField.delegate = self
|
2017-03-23 01:28:46 +08:00
|
|
|
fillPasswordCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
|
2017-05-26 00:37:00 +08:00
|
|
|
if tableData[passwordSection].count == 1 {
|
|
|
|
|
fillPasswordCell?.settingButton.isHidden = true
|
|
|
|
|
}
|
2017-03-23 01:28:46 +08:00
|
|
|
return fillPasswordCell!
|
2017-03-09 03:19:36 +08:00
|
|
|
case .passwordLengthCell:
|
|
|
|
|
passwordLengthCell = tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as? SliderTableViewCell
|
2017-06-13 11:42:49 +08:00
|
|
|
let lengthSetting = Globals.passwordDefaultLength[SharedDefaults[.passwordGeneratorFlavor]] ??
|
2017-03-29 00:00:24 +08:00
|
|
|
Globals.passwordDefaultLength["Random"]
|
2017-07-07 11:32:03 +08:00
|
|
|
let minimumLength = lengthSetting?.min ?? 0
|
|
|
|
|
let maximumLength = lengthSetting?.max ?? 0
|
|
|
|
|
var defaultLength = lengthSetting?.def ?? 0
|
2017-10-09 22:14:48 -07:00
|
|
|
if let currentPasswordLength = (tableData[passwordSection][0][PasswordEditorCellKey.content] as? String)?.count,
|
2017-07-07 11:32:03 +08:00
|
|
|
currentPasswordLength >= minimumLength,
|
|
|
|
|
currentPasswordLength <= maximumLength {
|
|
|
|
|
defaultLength = currentPasswordLength
|
|
|
|
|
}
|
2017-03-29 00:00:24 +08:00
|
|
|
passwordLengthCell?.reset(title: "Length",
|
2017-07-07 11:32:03 +08:00
|
|
|
minimumValue: minimumLength,
|
|
|
|
|
maximumValue: maximumLength,
|
|
|
|
|
defaultValue: defaultLength)
|
2017-03-23 01:28:46 +08:00
|
|
|
passwordLengthCell?.delegate = self
|
2017-03-21 13:16:25 -07:00
|
|
|
return passwordLengthCell!
|
2017-04-27 23:07:22 +08:00
|
|
|
case .additionsCell:
|
|
|
|
|
additionsCell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as?TextViewTableViewCell
|
|
|
|
|
additionsCell?.contentTextView.delegate = self
|
|
|
|
|
additionsCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
|
|
|
|
|
return additionsCell!
|
2017-03-21 13:16:25 -07:00
|
|
|
case .deletePasswordCell:
|
|
|
|
|
return deletePasswordCell!
|
2017-04-08 03:08:12 +08:00
|
|
|
case .scanQRCodeCell:
|
|
|
|
|
return scanQRCodeCell!
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
|
|
return 44
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
|
return tableData.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2017-03-24 01:10:26 +08:00
|
|
|
if section == passwordSection, hidePasswordSettings {
|
|
|
|
|
// hide the password section, only the password should be shown
|
|
|
|
|
return 1
|
|
|
|
|
} else {
|
|
|
|
|
return tableData[section].count
|
|
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2017-03-23 01:28:46 +08:00
|
|
|
|
2017-02-13 01:15:42 +08:00
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
|
return sectionHeaderTitles[section]
|
|
|
|
|
}
|
2017-02-21 01:06:03 +08:00
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
|
|
|
|
return sectionFooterTitles[section]
|
|
|
|
|
}
|
2017-03-09 03:19:36 +08:00
|
|
|
|
2017-03-21 13:16:25 -07:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2017-04-08 03:08:12 +08:00
|
|
|
let selectedCell = tableView.cellForRow(at: indexPath)
|
|
|
|
|
if selectedCell == deletePasswordCell {
|
2017-03-21 13:16:25 -07:00
|
|
|
let alert = UIAlertController(title: "Delete Password?", message: nil, preferredStyle: UIAlertControllerStyle.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Delete", style: UIAlertActionStyle.destructive, handler: {[unowned self] (action) -> Void in
|
|
|
|
|
self.performSegue(withIdentifier: "deletePasswordSegue", sender: self)
|
|
|
|
|
}))
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler:nil))
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
2017-04-08 03:08:12 +08:00
|
|
|
} else if selectedCell == scanQRCodeCell {
|
|
|
|
|
self.performSegue(withIdentifier: "showQRScannerSegue", sender: self)
|
2017-03-21 13:16:25 -07:00
|
|
|
}
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 01:28:46 +08:00
|
|
|
// generate password, copy to pasteboard, and set the cell
|
2017-03-25 00:05:45 +08:00
|
|
|
// check whether the current password looks like an OTP field
|
2017-03-23 01:28:46 +08:00
|
|
|
func generateAndCopyPassword() {
|
2017-03-25 00:05:45 +08:00
|
|
|
if let currentPassword = fillPasswordCell?.getContent(),
|
|
|
|
|
Password.LooksLikeOTP(line: currentPassword) {
|
|
|
|
|
let alert = UIAlertController(title: "Overwrite?", message: "Overwrite the one-time password configuration?", preferredStyle: UIAlertControllerStyle.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.destructive, handler: {_ in
|
|
|
|
|
self.generateAndCopyPasswordNoOtpCheck()
|
|
|
|
|
}))
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
} else {
|
|
|
|
|
self.generateAndCopyPasswordNoOtpCheck()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// generate the password, don't care whether the original line is otp
|
|
|
|
|
func generateAndCopyPasswordNoOtpCheck() {
|
2017-03-24 01:10:26 +08:00
|
|
|
// show password settings (e.g., the length slider)
|
|
|
|
|
if hidePasswordSettings == true {
|
|
|
|
|
hidePasswordSettings = false
|
|
|
|
|
tableView.reloadSections([passwordSection], with: .fade)
|
2017-03-23 01:28:46 +08:00
|
|
|
}
|
2017-03-29 00:00:24 +08:00
|
|
|
let length = passwordLengthCell?.roundedValue ?? 0
|
2017-03-23 01:28:46 +08:00
|
|
|
let plainPassword = Utils.generatePassword(length: length)
|
2017-07-27 23:56:24 +08:00
|
|
|
SecurePasteboard.shared.copy(textToCopy: plainPassword)
|
2017-03-24 01:10:26 +08:00
|
|
|
|
|
|
|
|
// update tableData so to make sure reloadData() works correctly
|
|
|
|
|
tableData[passwordSection][0][PasswordEditorCellKey.content] = plainPassword
|
|
|
|
|
|
|
|
|
|
// update cell manually, no need to call reloadData()
|
2017-03-23 01:28:46 +08:00
|
|
|
fillPasswordCell?.setContent(content: plainPassword)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 01:17:22 +08:00
|
|
|
func showHidePasswordSettings() {
|
|
|
|
|
hidePasswordSettings = !hidePasswordSettings
|
|
|
|
|
tableView.reloadSections([passwordSection], with: .fade)
|
2017-03-09 03:19:36 +08:00
|
|
|
}
|
2017-04-08 03:08:12 +08:00
|
|
|
|
|
|
|
|
func insertScannedOTPFields(_ otpauth: String) {
|
|
|
|
|
// update tableData
|
2017-04-27 23:26:12 +08:00
|
|
|
var additionsString = ""
|
2017-04-08 03:08:12 +08:00
|
|
|
if let additionsPlainText = (tableData[additionsSection][0][PasswordEditorCellKey.content] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines), additionsPlainText != "" {
|
2017-04-27 23:26:12 +08:00
|
|
|
additionsString = additionsPlainText + "\n" + otpauth
|
2017-04-08 03:08:12 +08:00
|
|
|
} else {
|
2017-04-27 23:26:12 +08:00
|
|
|
additionsString = otpauth
|
2017-04-08 03:08:12 +08:00
|
|
|
}
|
2017-04-27 23:26:12 +08:00
|
|
|
tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsString
|
|
|
|
|
|
|
|
|
|
// reload the additions cell
|
|
|
|
|
additionsCell?.setContent(content: additionsString)
|
2017-04-08 03:08:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - QRScannerControllerDelegate Methods
|
|
|
|
|
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
|
|
|
|
|
if let url = URL(string: line), let _ = Token(url: url) {
|
|
|
|
|
return (accept: true, message: "Valid token URL")
|
|
|
|
|
} else {
|
|
|
|
|
return (accept: false, message: "Invalid token URL")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - QRScannerControllerDelegate Methods
|
|
|
|
|
func handleScannedOutput(line: String) {
|
|
|
|
|
insertScannedOTPFields(line)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
|
|
|
if segue.identifier == "showQRScannerSegue" {
|
2017-04-10 02:15:16 +08:00
|
|
|
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 {
|
2017-04-08 03:08:12 +08:00
|
|
|
viewController.delegate = self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-10 02:15:16 +08:00
|
|
|
|
2017-07-07 11:32:03 +08:00
|
|
|
// update tableData so to make sure reloadData() works correctly
|
2017-04-27 23:07:22 +08:00
|
|
|
func textFieldDidEndEditing(_ textField: UITextField) {
|
|
|
|
|
if textField == nameCell?.contentTextField {
|
|
|
|
|
tableData[nameSection][0][PasswordEditorCellKey.content] = nameCell?.getContent()
|
2017-07-07 11:32:03 +08:00
|
|
|
} else if textField == fillPasswordCell?.contentTextField {
|
|
|
|
|
tableData[passwordSection][0][PasswordEditorCellKey.content] = fillPasswordCell?.getContent()
|
2017-04-27 23:07:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-07 11:32:03 +08:00
|
|
|
// update tableData so to make sure reloadData() works correctly
|
2017-04-27 23:07:22 +08:00
|
|
|
func textViewDidEndEditing(_ textView: UITextView) {
|
|
|
|
|
if textView == additionsCell?.contentTextView {
|
|
|
|
|
tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsCell?.getContent()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-15 21:37:00 +08:00
|
|
|
|
|
|
|
|
func getNameURL() -> (String, URL) {
|
|
|
|
|
let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())!
|
|
|
|
|
let name = URL(string: encodedName)!.lastPathComponent
|
|
|
|
|
let url = URL(string: encodedName)!.appendingPathExtension("gpg")
|
|
|
|
|
return (name, url)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkName() -> Bool {
|
|
|
|
|
// the name field should not be empty
|
|
|
|
|
guard let name = nameCell?.getContent(), name.isEmpty == false else {
|
|
|
|
|
Utils.alert(title: "Cannot Save", message: "Please fill in the name.", controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the name should not start with /
|
|
|
|
|
guard name.hasPrefix("/") == false else {
|
|
|
|
|
Utils.alert(title: "Cannot Save", message: "Please remove the prefix \"/\" from your password name.", controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the name field should be a valid url
|
|
|
|
|
guard let path = name.stringByAddingPercentEncodingForRFC3986(),
|
|
|
|
|
var passwordURL = URL(string: path) else {
|
|
|
|
|
Utils.alert(title: "Cannot Save", message: "Password name is invalid.", controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check whether we can parse the filename (be consistent with PasswordStore::addPasswordEntities)
|
2017-10-16 04:14:11 +08:00
|
|
|
var previousPathLength = Int.max
|
2017-10-15 21:37:00 +08:00
|
|
|
while passwordURL.path != "." {
|
|
|
|
|
passwordURL = passwordURL.deletingLastPathComponent()
|
2017-10-16 04:14:11 +08:00
|
|
|
if passwordURL.path != "." && passwordURL.path.count >= previousPathLength {
|
2017-10-15 21:37:00 +08:00
|
|
|
Utils.alert(title: "Cannot Save", message: "Cannot parse the filename. Please check and simplify the password name.", controller: self, completion: nil)
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-10-16 04:14:11 +08:00
|
|
|
previousPathLength = passwordURL.path.count
|
2017-10-15 21:37:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|