passforios/pass/Controllers/PasswordEditorTableViewController.swift

366 lines
17 KiB
Swift
Raw Normal View History

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
import SafariServices
import OneTimePassword
import passKit
2017-02-13 01:15:42 +08:00
enum PasswordEditorCellType {
case nameCell, fillPasswordCell, passwordLengthCell, additionsCell, deletePasswordCell, scanQRCodeCell, memorablePasswordGeneratorCell
2017-02-13 01:15:42 +08:00
}
enum PasswordEditorCellKey {
case type, title, content, placeholders
}
class PasswordEditorTableViewController: UITableViewController, FillPasswordTableViewCellDelegate, PasswordSettingSliderTableViewCellDelegate, QRScannerControllerDelegate, UITextFieldDelegate, UITextViewDelegate, SFSafariViewControllerDelegate {
2018-12-09 16:59:07 -08:00
2017-02-13 01:15:42 +08:00
var tableData = [
[Dictionary<PasswordEditorCellKey, Any>]
]()
var password: Password?
2018-12-09 16:59:07 -08:00
private var navigationItemTitle: String?
2018-12-09 16:59:07 -08:00
2019-02-13 21:31:40 +01:00
private var sectionHeaderTitles = ["Name".localize(), "Password".localize(), "Additions".localize(),""].map {$0.uppercased()}
2019-01-14 20:57:45 +01:00
private var sectionFooterTitles = ["", "", "UseKeyValueFormat.".localize(), ""]
private let nameSection = 0
private let passwordSection = 1
private let additionsSection = 2
private var hidePasswordSettings = true
2018-12-09 16:59:07 -08:00
2017-04-27 23:26:12 +08:00
var nameCell: TextFieldTableViewCell?
var fillPasswordCell: FillPasswordTableViewCell?
private var passwordLengthCell: SliderTableViewCell?
2017-04-27 23:26:12 +08:00
var additionsCell: TextViewTableViewCell?
private var deletePasswordCell: UITableViewCell?
private var scanQRCodeCell: UITableViewCell?
private var memorablePasswordGeneratorCell: UITableViewCell?
2018-12-09 16:59:07 -08:00
var plainText: String {
var plainText = (fillPasswordCell?.getContent())!
if let additionsString = additionsCell?.getContent(), !additionsString.isEmpty {
plainText.append("\n")
plainText.append(additionsString)
}
if !plainText.trimmingCharacters(in: .whitespaces).hasSuffix("\n") {
plainText.append("\n")
}
return plainText
}
2017-03-21 13:16:25 -07:00
override func loadView() {
super.loadView()
2018-12-09 16:59:07 -08:00
2017-03-21 13:16:25 -07:00
deletePasswordCell = UITableViewCell(style: .default, reuseIdentifier: "default")
2019-01-14 20:57:45 +01:00
deletePasswordCell!.textLabel?.text = "DeletePassword".localize()
deletePasswordCell!.textLabel?.textColor = Colors.systemRed
2017-03-21 13:16:25 -07:00
deletePasswordCell?.selectionStyle = .default
2018-12-09 16:59:07 -08:00
scanQRCodeCell = UITableViewCell(style: .default, reuseIdentifier: "default")
2019-01-14 20:57:45 +01:00
scanQRCodeCell?.textLabel?.text = "AddOneTimePassword".localize()
scanQRCodeCell?.textLabel?.textColor = Colors.systemBlue
scanQRCodeCell?.selectionStyle = .default
2017-04-10 23:04:23 +08:00
scanQRCodeCell?.accessoryType = .disclosureIndicator
2018-12-09 16:59:07 -08:00
memorablePasswordGeneratorCell = UITableViewCell(style: .default, reuseIdentifier: "default")
2019-01-14 20:57:45 +01:00
memorablePasswordGeneratorCell?.textLabel?.text = "GetMemorableOne".localize()
memorablePasswordGeneratorCell?.textLabel?.textColor = Colors.systemBlue
memorablePasswordGeneratorCell?.selectionStyle = .default
memorablePasswordGeneratorCell?.accessoryType = .disclosureIndicator
2017-03-21 13:16:25 -07:00
}
2018-12-09 16:59:07 -08:00
2017-02-13 01:15:42 +08:00
override func viewDidLoad() {
super.viewDidLoad()
if navigationItemTitle != nil {
navigationItem.title = navigationItemTitle
}
2018-12-09 16:59:07 -08:00
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")
2018-12-09 16:59:07 -08:00
2019-05-01 17:49:27 +02:00
tableView.rowHeight = UITableView.automaticDimension
2017-02-13 01:15:42 +08:00
tableView.estimatedRowHeight = 48
2019-05-01 17:49:27 +02:00
self.tableView.sectionFooterHeight = UITableView.automaticDimension;
self.tableView.estimatedSectionFooterHeight = 0;
2017-02-13 01:15:42 +08:00
}
override func viewDidLayoutSubviews() {
2019-06-09 15:25:07 -07:00
additionsCell?.contentTextView.setContentOffset(.zero, animated: false)
}
2018-12-09 16:59:07 -08:00
2017-02-13 01:15:42 +08:00
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellData = tableData[indexPath.section][indexPath.row]
2018-12-09 16:59:07 -08:00
2017-02-13 01:15:42 +08:00
switch cellData[PasswordEditorCellKey.type] as! PasswordEditorCellType {
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:
fillPasswordCell = tableView.dequeueReusableCell(withIdentifier: "fillPasswordCell", for: indexPath) as? FillPasswordTableViewCell
fillPasswordCell?.delegate = self
2017-07-07 11:32:03 +08:00
fillPasswordCell?.contentTextField.delegate = self
fillPasswordCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
if tableData[passwordSection].count == 1 {
fillPasswordCell?.settingButton.isHidden = true
}
return fillPasswordCell!
2017-03-09 03:19:36 +08:00
case .passwordLengthCell:
passwordLengthCell = tableView.dequeueReusableCell(withIdentifier: "passwordLengthCell", for: indexPath) as? SliderTableViewCell
let lengthSetting = PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]).defaultLength
let minimumLength = lengthSetting.min
let maximumLength = lengthSetting.max
var defaultLength = lengthSetting.def
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
}
2019-01-14 20:57:45 +01:00
passwordLengthCell?.reset(title: "Length".localize(),
2017-07-07 11:32:03 +08:00
minimumValue: minimumLength,
maximumValue: maximumLength,
defaultValue: defaultLength)
passwordLengthCell?.delegate = self
2017-03-21 13:16:25 -07:00
return passwordLengthCell!
case .memorablePasswordGeneratorCell:
return memorablePasswordGeneratorCell!
case .additionsCell:
additionsCell = tableView.dequeueReusableCell(withIdentifier: "textViewCell", for: indexPath) as?TextViewTableViewCell
additionsCell?.contentTextView.delegate = self
additionsCell?.setContent(content: cellData[PasswordEditorCellKey.content] as? String)
additionsCell?.contentTextView.textColor = Colors.label
return additionsCell!
2017-03-21 13:16:25 -07:00
case .deletePasswordCell:
return deletePasswordCell!
case .scanQRCodeCell:
return scanQRCodeCell!
2017-02-13 01:15:42 +08:00
}
}
2018-12-09 16:59:07 -08:00
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 {
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-02-13 01:15:42 +08:00
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionHeaderTitles[section]
}
2018-12-09 16:59:07 -08:00
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return sectionFooterTitles[section]
}
2018-12-09 16:59:07 -08:00
2017-03-21 13:16:25 -07:00
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell = tableView.cellForRow(at: indexPath)
if selectedCell == deletePasswordCell {
2019-05-01 17:49:27 +02:00
let alert = UIAlertController(title: "DeletePassword?".localize(), message: nil, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Delete".localize(), style: UIAlertAction.Style.destructive, handler: {[unowned self] (action) -> Void in
2017-03-21 13:16:25 -07:00
self.performSegue(withIdentifier: "deletePasswordSegue", sender: self)
}))
2019-05-01 17:49:27 +02:00
alert.addAction(UIAlertAction(title: "Cancel".localize(), style: UIAlertAction.Style.cancel, handler:nil))
2017-03-21 13:16:25 -07:00
self.present(alert, animated: true, completion: nil)
} else if selectedCell == scanQRCodeCell {
self.performSegue(withIdentifier: "showQRScannerSegue", sender: self)
} else if selectedCell == memorablePasswordGeneratorCell {
// open the webpage
if let url = URL(string: "https://xkpasswd.net/") {
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: false)
vc.delegate = self
present(vc, animated: true)
2018-12-09 16:59:07 -08:00
}
2017-03-21 13:16:25 -07:00
}
tableView.deselectRow(at: indexPath, animated: true)
}
2018-12-09 16:59:07 -08:00
// generate password, copy to pasteboard, and set the cell
// check whether the current password looks like an OTP field
func generateAndCopyPassword() {
if let currentPassword = fillPasswordCell?.getContent(), Constants.isOtpRelated(line: currentPassword) {
2019-05-01 17:49:27 +02:00
let alert = UIAlertController(title: "Overwrite?".localize(), message: "OverwriteOtpConfiguration?".localize(), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Yes".localize(), style: UIAlertAction.Style.destructive, handler: {_ in
self.generateAndCopyPasswordNoOtpCheck()
}))
2019-05-01 17:49:27 +02:00
alert.addAction(UIAlertAction(title: "Cancel".localize(), style: UIAlertAction.Style.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
} else {
self.generateAndCopyPasswordNoOtpCheck()
}
}
2018-12-09 16:59:07 -08:00
// generate the password, don't care whether the original line is otp
func generateAndCopyPasswordNoOtpCheck() {
// show password settings (e.g., the length slider)
showPasswordSettings()
2018-12-09 16:59:07 -08:00
let length = passwordLengthCell?.roundedValue ?? 0
let plainPassword = PasswordGeneratorFlavour.from(SharedDefaults[.passwordGeneratorFlavor]).generatePassword(length: length)
2018-12-09 16:59:07 -08:00
// update tableData so to make sure reloadData() works correctly
tableData[passwordSection][0][PasswordEditorCellKey.content] = plainPassword
2018-12-09 16:59:07 -08:00
// update cell manually, no need to call reloadData()
fillPasswordCell?.setContent(content: plainPassword)
}
2018-12-09 16:59:07 -08:00
// show password settings (e.g., the length slider)
func showPasswordSettings() {
if hidePasswordSettings == true {
hidePasswordSettings = false
tableView.reloadSections([passwordSection], with: .fade)
}
}
2018-12-09 16:59:07 -08:00
// show/hide password settings (e.g., the length slider)
func showHidePasswordSettings() {
hidePasswordSettings = !hidePasswordSettings
tableView.reloadSections([passwordSection], with: .fade)
2017-03-09 03:19:36 +08:00
}
2018-12-09 16:59:07 -08:00
func insertScannedOTPFields(_ otpauth: String) {
// update tableData
2017-04-27 23:26:12 +08:00
var additionsString = ""
if let additionsPlainText = (tableData[additionsSection][0][PasswordEditorCellKey.content] as? String)?.trimmed, additionsPlainText != "" {
2017-04-27 23:26:12 +08:00
additionsString = additionsPlainText + "\n" + otpauth
} else {
2017-04-27 23:26:12 +08:00
additionsString = otpauth
}
2017-04-27 23:26:12 +08:00
tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsString
2018-12-09 16:59:07 -08:00
2017-04-27 23:26:12 +08:00
// reload the additions cell
additionsCell?.setContent(content: additionsString)
}
2018-12-09 16:59:07 -08:00
// MARK: - QRScannerControllerDelegate Methods
func checkScannedOutput(line: String) -> (accept: Bool, message: String) {
if let url = URL(string: line), let _ = Token(url: url) {
2019-01-14 20:57:45 +01:00
return (accept: true, message: "ValidTokenUrl".localize())
} else {
2019-01-14 20:57:45 +01:00
return (accept: false, message: "InvalidTokenUrl".localize())
}
}
2018-12-09 16:59:07 -08:00
// MARK: - QRScannerControllerDelegate Methods
func handleScannedOutput(line: String) {
insertScannedOTPFields(line)
}
2018-12-09 16:59:07 -08:00
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showQRScannerSegue" {
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-07-07 11:32:03 +08:00
// update tableData so to make sure reloadData() works correctly
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 {
if let plainPassword = fillPasswordCell?.getContent() {
tableData[passwordSection][0][PasswordEditorCellKey.content] = plainPassword
}
}
}
2018-12-09 16:59:07 -08:00
2017-07-07 11:32:03 +08:00
// update tableData so to make sure reloadData() works correctly
func textViewDidEndEditing(_ textView: UITextView) {
if textView == additionsCell?.contentTextView {
tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsCell?.getContent()
}
}
2018-12-09 16:59:07 -08:00
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == fillPasswordCell?.contentTextField {
// show password generation settings automatically
showPasswordSettings()
}
}
2018-12-09 16:59:07 -08:00
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)
}
2018-12-09 16:59:07 -08:00
2017-10-15 21:37:00 +08:00
func checkName() -> Bool {
// the name field should not be empty
guard let name = nameCell?.getContent(), name.isEmpty == false else {
2019-01-14 20:57:45 +01:00
Utils.alert(title: "CannotSave".localize(), message: "FillInName.".localize(), controller: self, completion: nil)
2017-10-15 21:37:00 +08:00
return false
}
2018-12-09 16:59:07 -08:00
2017-10-15 21:37:00 +08:00
// the name should not start with /
guard name.hasPrefix("/") == false else {
2019-01-14 20:57:45 +01:00
Utils.alert(title: "CannotSave".localize(), message: "RemovePrefix.".localize(), controller: self, completion: nil)
2017-10-15 21:37:00 +08:00
return false
}
2018-12-09 16:59:07 -08:00
2017-10-15 21:37:00 +08:00
// the name field should be a valid url
guard let path = name.stringByAddingPercentEncodingForRFC3986(),
var passwordURL = URL(string: path) else {
2019-06-09 15:25:07 -07:00
Utils.alert(title: "CannotSave".localize(), message: "PasswordNameInvalid.".localize(), controller: self, completion: nil)
return false
2017-10-15 21:37:00 +08:00
}
2018-12-09 16:59:07 -08:00
2017-10-15 21:37:00 +08:00
// 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 {
2019-01-14 20:57:45 +01:00
Utils.alert(title: "CannotSave".localize(), message: "CannotParseFilename.".localize(), controller: self, completion: nil)
2017-10-15 21:37:00 +08:00
return false
}
2017-10-16 04:14:11 +08:00
previousPathLength = passwordURL.path.count
2017-10-15 21:37:00 +08:00
}
2018-12-09 16:59:07 -08:00
2017-10-15 21:37:00 +08:00
return true
}
2018-12-09 16:59:07 -08:00
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
let copiedLinesSplit = UIPasteboard.general.string?.components(separatedBy: CharacterSet.whitespacesAndNewlines).filter({ !$0.isEmpty })
if copiedLinesSplit?.count ?? 0 > 0 {
let generatedPassword = copiedLinesSplit![0]
2019-05-01 17:49:27 +02:00
let alert = UIAlertController(title: "WannaUseIt?".localize(), message: "", preferredStyle: UIAlertController.Style.alert)
2019-01-14 20:57:45 +01:00
let message = NSMutableAttributedString(string: "\("SeemsLikeYouHaveCopiedSomething.".localize()) \("FirstStringIs:".localize())\n")
message.append(Utils.attributedPassword(plainPassword: generatedPassword))
alert.setValue(message, forKey: "attributedMessage")
2019-05-01 17:49:27 +02:00
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: {[unowned self] (action) -> Void in
// update tableData so to make sure reloadData() works correctly
self.tableData[self.passwordSection][0][PasswordEditorCellKey.content] = generatedPassword
// update cell manually, no need to call reloadData()
self.fillPasswordCell?.setContent(content: generatedPassword)
}))
2019-05-01 17:49:27 +02:00
alert.addAction(UIAlertAction(title: "Cancel".localize(), style: UIAlertAction.Style.cancel, handler:nil))
self.present(alert, animated: true, completion: nil)
}
}
2017-02-13 01:15:42 +08:00
}