2017-02-02 21:04:31 +08:00
|
|
|
//
|
|
|
|
|
// PasswordDetailTableViewController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 2/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2017-02-09 13:17:11 +08:00
|
|
|
import FavIcon
|
2022-01-09 21:38:39 -08:00
|
|
|
import Gopenpgp
|
|
|
|
|
import passAutoFillExtension
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2020-06-28 21:25:40 +02:00
|
|
|
import SVProgressHUD
|
|
|
|
|
import UIKit
|
2022-05-23 10:54:11 -07:00
|
|
|
import YubiKit
|
2017-02-02 21:04:31 +08:00
|
|
|
|
2022-01-09 21:38:39 -08:00
|
|
|
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate, AlertPresenting {
|
2023-03-10 06:33:19 +01:00
|
|
|
var passwordEntity: PasswordEntity? {
|
|
|
|
|
didSet {
|
|
|
|
|
passwordPath = passwordEntity?.path
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private var password: Password?
|
|
|
|
|
private var passwordImage: UIImage?
|
2020-06-28 21:25:40 +02:00
|
|
|
private var oneTimePasswordIndexPath: IndexPath?
|
2017-03-22 01:39:26 +08:00
|
|
|
private var shouldPopCurrentView = false
|
|
|
|
|
private let passwordStore = PasswordStore.shared
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2023-03-10 06:33:19 +01:00
|
|
|
// preserve path so it can be reloaded even if the passwordEntity is deleted during the update process
|
|
|
|
|
private var passwordPath: String?
|
|
|
|
|
|
2023-04-23 22:01:37 +02:00
|
|
|
private lazy var editUIBarButtonItem: UIBarButtonItem = .init(barButtonSystemItem: .edit, target: self, action: #selector(pressEdit))
|
2017-02-28 12:25:52 +08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private struct TableSection {
|
2017-03-31 00:28:37 -07:00
|
|
|
var type: PasswordDetailTableViewControllerSectionType
|
|
|
|
|
var header: String?
|
2018-12-15 21:48:35 +01:00
|
|
|
var item: [AdditionField] = []
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-12-15 21:48:35 +01:00
|
|
|
init(type: PasswordDetailTableViewControllerSectionType, header: String? = nil) {
|
|
|
|
|
self.type = type
|
2017-03-31 00:28:37 -07:00
|
|
|
self.header = header
|
|
|
|
|
}
|
2017-02-04 20:38:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-12-15 21:48:35 +01:00
|
|
|
private var tableData = [TableSection]()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 00:28:37 -07:00
|
|
|
private enum PasswordDetailTableViewControllerSectionType {
|
|
|
|
|
case name, main, addition, misc
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-02 21:04:31 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
|
2017-02-06 20:48:20 +08:00
|
|
|
tableView.register(UINib(nibName: "PasswordDetailTitleTableViewCell", bundle: nil), forCellReuseIdentifier: "passwordDetailTitleTableViewCell")
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2021-10-03 05:46:07 +02:00
|
|
|
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapMenu))
|
2017-03-31 22:44:30 -07:00
|
|
|
tapGesture.cancelsTouchesInView = false
|
2017-02-05 00:35:23 +08:00
|
|
|
tableView.addGestureRecognizer(tapGesture)
|
|
|
|
|
tapGesture.delegate = self
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-05-01 17:49:27 +02:00
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
2017-02-05 14:08:19 +08:00
|
|
|
tableView.estimatedRowHeight = 52
|
2023-03-18 17:29:11 -07:00
|
|
|
let adjustForTabbarInsets = UIEdgeInsets(top: 0, left: 0, bottom: tabBarController!.tabBar.frame.height, right: 0)
|
|
|
|
|
tableView.contentInset = adjustForTabbarInsets
|
|
|
|
|
tableView.scrollIndicatorInsets = adjustForTabbarInsets
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-02-15 21:25:03 +08:00
|
|
|
editUIBarButtonItem.isEnabled = false
|
|
|
|
|
navigationItem.rightBarButtonItem = editUIBarButtonItem
|
2021-08-16 21:59:46 +02:00
|
|
|
navigationItem.largeTitleDisplayMode = .never
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2018-11-10 22:38:12 -08:00
|
|
|
if let imageData = passwordEntity?.getImage() {
|
2017-02-09 14:41:59 +08:00
|
|
|
let image = UIImage(data: imageData as Data)
|
|
|
|
|
passwordImage = image
|
|
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
decryptThenShowPassword()
|
|
|
|
|
setupOneTimePasswordAutoRefresh()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-24 20:40:24 +08:00
|
|
|
// pop the current view because this password might be "discarded"
|
2017-03-22 01:39:26 +08:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(setShouldPopCurrentView), name: .passwordStoreChangeDiscarded, object: nil)
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-24 20:40:24 +08:00
|
|
|
// reset the data table if the disaply settings have been changed
|
2020-12-30 16:26:13 -08:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(decryptThenShowPasswordSelector), name: .passwordDetailDisplaySettingChanged, object: nil)
|
2023-03-10 06:33:19 +01:00
|
|
|
|
|
|
|
|
// A Siri shortcut can change the state of the app in the background. Hence, reload when opening the app.
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(actOnPossiblePasswordStoreUpdate), name: UIApplication.willEnterForegroundNotification, object: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
2020-07-06 10:04:41 +02:00
|
|
|
super.viewDidAppear(animated)
|
2020-06-28 21:25:40 +02:00
|
|
|
if shouldPopCurrentView {
|
2019-05-01 17:49:27 +02:00
|
|
|
let alert = UIAlertController(title: "Notice".localize(), message: "PreviousChangesDiscarded.".localize(), preferredStyle: UIAlertController.Style.alert)
|
2020-04-18 22:35:17 -07:00
|
|
|
alert.addAction(UIAlertAction.okAndPopView(controller: self))
|
2020-06-28 21:25:40 +02:00
|
|
|
present(alert, animated: true, completion: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2017-02-28 12:25:52 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@objc
|
2021-01-31 13:17:37 +01:00
|
|
|
private func decryptThenShowPasswordSelector(_: Any) {
|
2020-12-30 16:26:13 -08:00
|
|
|
decryptThenShowPassword()
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-09 21:38:39 -08:00
|
|
|
private func decryptThenShowPassword() {
|
2023-03-17 22:20:50 -07:00
|
|
|
if Defaults.isYubiKeyEnabled, YubiKitDeviceCapabilities.supportsISO7816NFCTags {
|
2022-01-09 21:38:39 -08:00
|
|
|
decryptThenShowPasswordYubiKey()
|
|
|
|
|
} else {
|
|
|
|
|
decryptThenShowPasswordLocalKey()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func decryptThenShowPasswordLocalKey(keyID: String? = nil) {
|
2023-04-23 22:01:37 +02:00
|
|
|
guard let passwordEntity else {
|
2020-09-19 15:20:50 +02:00
|
|
|
Utils.alert(title: "CannotShowPassword".localize(), message: "PasswordDoesNotExist".localize(), controller: self, completion: {
|
2017-06-03 17:50:33 -07:00
|
|
|
self.navigationController!.popViewController(animated: true)
|
2020-09-19 15:20:50 +02:00
|
|
|
})
|
2017-06-03 17:50:33 -07:00
|
|
|
return
|
|
|
|
|
}
|
2017-02-08 18:57:15 +08:00
|
|
|
DispatchQueue.global(qos: .userInitiated).async {
|
2017-03-22 00:22:47 +08:00
|
|
|
// decrypt password
|
2017-02-06 15:03:34 +08:00
|
|
|
do {
|
2020-04-13 15:16:03 -07:00
|
|
|
let requestPGPKeyPassphrase = Utils.createRequestPGPKeyPassphraseHandler(controller: self)
|
2020-04-17 23:56:14 -07:00
|
|
|
self.password = try self.passwordStore.decrypt(passwordEntity: passwordEntity, keyID: keyID, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
2020-04-18 22:35:17 -07:00
|
|
|
self.showPassword()
|
2020-09-20 15:07:18 +02:00
|
|
|
} catch let AppError.pgpPrivateKeyNotFound(keyID: key) {
|
2020-04-17 23:56:14 -07:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
// alert: cancel or try again
|
2020-09-20 15:07:18 +02:00
|
|
|
let alert = UIAlertController(title: "CannotShowPassword".localize(), message: AppError.pgpPrivateKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert)
|
2020-04-17 23:56:14 -07:00
|
|
|
alert.addAction(UIAlertAction.cancelAndPopView(controller: self))
|
|
|
|
|
let selectKey = UIAlertAction.selectKey(controller: self) { action in
|
2022-01-09 21:38:39 -08:00
|
|
|
self.decryptThenShowPasswordLocalKey(keyID: action.title)
|
2020-04-17 23:56:14 -07:00
|
|
|
}
|
|
|
|
|
alert.addAction(selectKey)
|
|
|
|
|
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
}
|
2017-02-06 15:03:34 +08:00
|
|
|
} catch {
|
2017-02-22 12:56:06 +08:00
|
|
|
DispatchQueue.main.async {
|
2017-06-07 21:11:01 +08:00
|
|
|
// alert: cancel or try again
|
2020-04-17 23:56:14 -07:00
|
|
|
let alert = UIAlertController(title: "CannotShowPassword".localize(), message: error.localizedDescription, preferredStyle: .alert)
|
|
|
|
|
alert.addAction(UIAlertAction.cancelAndPopView(controller: self))
|
2020-07-05 00:16:22 +02:00
|
|
|
alert.addAction(
|
|
|
|
|
UIAlertAction(title: "TryAgain".localize(), style: .default) { _ in
|
2022-01-09 21:38:39 -08:00
|
|
|
self.decryptThenShowPasswordLocalKey()
|
2020-07-05 00:16:22 +02:00
|
|
|
}
|
|
|
|
|
)
|
2017-06-07 21:11:01 +08:00
|
|
|
self.present(alert, animated: true, completion: nil)
|
2017-02-22 12:56:06 +08:00
|
|
|
}
|
2017-02-06 15:03:34 +08:00
|
|
|
}
|
2017-02-28 12:25:52 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-03 17:50:33 -07:00
|
|
|
private func showPassword() {
|
2017-03-22 00:22:47 +08:00
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
|
self?.setTableData()
|
2017-04-10 22:09:39 -07:00
|
|
|
self?.tableView.reloadData()
|
2017-03-22 00:22:47 +08:00
|
|
|
self?.editUIBarButtonItem.isEnabled = true
|
2020-01-02 00:48:00 +01:00
|
|
|
if !Defaults.isHidePasswordImagesOn {
|
2019-02-27 21:49:21 +10:00
|
|
|
if let urlString = self?.password?.urlString {
|
|
|
|
|
if self?.passwordEntity?.getImage() == nil {
|
|
|
|
|
self?.updatePasswordImage(urlString: urlString)
|
|
|
|
|
}
|
2017-03-22 00:22:47 +08:00
|
|
|
}
|
2017-02-09 13:17:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private func setupOneTimePasswordAutoRefresh() {
|
2020-07-04 21:55:23 +02:00
|
|
|
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
|
2017-03-05 02:54:36 +08:00
|
|
|
// bail out of the timer code if the object has been freed
|
|
|
|
|
guard let strongSelf = self,
|
2020-11-07 12:06:28 +01:00
|
|
|
let otpType = strongSelf.password?.otpType,
|
|
|
|
|
otpType != .none,
|
|
|
|
|
let indexPath = strongSelf.oneTimePasswordIndexPath,
|
|
|
|
|
let cell = strongSelf.tableView.cellForRow(at: indexPath) as? LabelTableViewCell else {
|
2020-06-28 21:25:40 +02:00
|
|
|
return
|
2017-03-05 02:54:36 +08:00
|
|
|
}
|
2017-03-24 23:14:44 +08:00
|
|
|
switch otpType {
|
|
|
|
|
case .totp:
|
2017-03-07 09:50:18 +08:00
|
|
|
if let (title, otp) = strongSelf.password?.getOtpStrings() {
|
2018-12-15 21:48:35 +01:00
|
|
|
strongSelf.tableData[indexPath.section].item[indexPath.row] = title => otp
|
2017-03-07 09:50:18 +08:00
|
|
|
cell.cellData?.title = title
|
2017-03-05 02:54:36 +08:00
|
|
|
cell.cellData?.content = otp
|
|
|
|
|
}
|
2017-03-07 09:50:18 +08:00
|
|
|
default:
|
|
|
|
|
break
|
2017-03-05 02:54:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@objc
|
|
|
|
|
private func pressEdit(_: Any?) {
|
2017-02-13 01:15:42 +08:00
|
|
|
performSegue(withIdentifier: "editPasswordSegue", sender: self)
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@objc
|
|
|
|
|
private func setShouldPopCurrentView() {
|
|
|
|
|
shouldPopCurrentView = true
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@IBAction
|
|
|
|
|
private func cancelEditPassword(segue _: UIStoryboardSegue) {}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@IBAction
|
|
|
|
|
private func saveEditPassword(segue _: UIStoryboardSegue) {
|
|
|
|
|
if password!.changed != 0 {
|
|
|
|
|
saveEditPassword(password: password!)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-04-17 23:56:14 -07:00
|
|
|
private func saveEditPassword(password: Password, keyID: String? = nil) {
|
|
|
|
|
SVProgressHUD.show(withStatus: "Saving".localize())
|
|
|
|
|
do {
|
2020-06-28 21:25:40 +02:00
|
|
|
passwordEntity = try passwordStore.edit(passwordEntity: passwordEntity!, password: password, keyID: keyID)
|
|
|
|
|
setTableData()
|
|
|
|
|
tableView.reloadData()
|
2020-04-17 23:56:14 -07:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "Success".localize())
|
|
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2020-09-20 15:07:18 +02:00
|
|
|
} catch let AppError.pgpPublicKeyNotFound(keyID: key) {
|
2020-06-28 21:25:40 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
// alert: cancel or select keys
|
|
|
|
|
SVProgressHUD.dismiss()
|
2020-09-20 15:07:18 +02:00
|
|
|
let alert = UIAlertController(title: "Cannot Edit Password", message: AppError.pgpPublicKeyNotFound(keyID: key).localizedDescription, preferredStyle: .alert)
|
2020-06-28 21:25:40 +02:00
|
|
|
alert.addAction(UIAlertAction.cancelAndPopView(controller: self))
|
|
|
|
|
let selectKey = UIAlertAction.selectKey(controller: self) { action in
|
|
|
|
|
self.saveEditPassword(password: password, keyID: action.title)
|
|
|
|
|
}
|
|
|
|
|
alert.addAction(selectKey)
|
|
|
|
|
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
} catch {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-17 23:56:14 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@IBAction
|
|
|
|
|
private func deletePassword(segue _: UIStoryboardSegue) {
|
2017-04-25 13:01:17 -07:00
|
|
|
do {
|
|
|
|
|
try passwordStore.delete(passwordEntity: passwordEntity!)
|
|
|
|
|
} catch {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: error.localizedDescription, controller: self, completion: nil)
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
_ = navigationController?.popViewController(animated: true)
|
2017-03-21 13:16:25 -07:00
|
|
|
}
|
2017-03-21 13:34:26 -07:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private func setTableData() {
|
2020-06-28 21:25:40 +02:00
|
|
|
tableData = [TableSection]()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 00:28:37 -07:00
|
|
|
// name section
|
|
|
|
|
var section = TableSection(type: .name)
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(AdditionField())
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
|
|
|
|
|
|
|
|
|
// main section
|
|
|
|
|
section = TableSection(type: .main)
|
2023-04-23 22:01:37 +02:00
|
|
|
let password = password!
|
2018-06-23 20:42:56 +02:00
|
|
|
if let username = password.username {
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(Constants.USERNAME_KEYWORD => username)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2023-01-21 16:52:46 +01:00
|
|
|
if let user = password.username {
|
|
|
|
|
section.item.append(Constants.USER_KEYWORD => user)
|
|
|
|
|
}
|
2018-06-23 20:42:56 +02:00
|
|
|
if let login = password.login {
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(Constants.LOGIN_KEYWORD => login)
|
2017-06-28 00:32:50 +08:00
|
|
|
}
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(Constants.PASSWORD_KEYWORD => password.password)
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
|
|
|
|
|
|
|
|
|
// addition section
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-05 02:54:36 +08:00
|
|
|
// show one time password
|
2017-03-24 23:14:44 +08:00
|
|
|
if password.otpType != .none {
|
|
|
|
|
if let (title, otp) = self.password?.getOtpStrings() {
|
2019-01-14 20:57:45 +01:00
|
|
|
section = TableSection(type: .addition, header: "OneTimePassword".localize())
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(title => otp)
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
2017-04-01 08:59:22 -07:00
|
|
|
oneTimePasswordIndexPath = IndexPath(row: 0, section: tableData.count - 1)
|
2017-03-03 14:45:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-05 02:54:36 +08:00
|
|
|
// show additional information
|
2017-06-28 00:18:06 +08:00
|
|
|
let filteredAdditionKeys = password.getFilteredAdditions()
|
2020-06-28 21:25:40 +02:00
|
|
|
if !filteredAdditionKeys.isEmpty {
|
2019-02-13 21:31:40 +01:00
|
|
|
section = TableSection(type: .addition, header: "Additions".localize())
|
2018-12-15 21:48:35 +01:00
|
|
|
section.item.append(contentsOf: filteredAdditionKeys)
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
2017-03-03 17:12:25 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 00:28:37 -07:00
|
|
|
// misc section
|
|
|
|
|
section = TableSection(type: .misc)
|
2019-01-14 20:57:45 +01:00
|
|
|
section.item.append(AdditionField(title: "ShowRaw".localize()))
|
2017-03-31 00:28:37 -07:00
|
|
|
tableData.append(section)
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
|
2017-02-13 01:15:42 +08:00
|
|
|
if segue.identifier == "editPasswordSegue" {
|
|
|
|
|
if let controller = segue.destination as? UINavigationController {
|
|
|
|
|
if let editController = controller.viewControllers.first as? EditPasswordTableViewController {
|
|
|
|
|
editController.password = password
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-31 22:44:30 -07:00
|
|
|
} else if segue.identifier == "showRawPasswordSegue" {
|
|
|
|
|
if let controller = segue.destination as? UINavigationController {
|
|
|
|
|
if let controller = controller.viewControllers.first as? RawPasswordViewController {
|
|
|
|
|
controller.password = password
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-13 01:15:42 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
private func updatePasswordImage(urlString: String) {
|
2021-12-28 02:57:11 +01:00
|
|
|
var newURLString = urlString
|
2017-03-15 14:14:42 -07:00
|
|
|
if urlString.lowercased().hasPrefix("http://") {
|
|
|
|
|
// try to replace http url to https url
|
2021-12-28 02:57:11 +01:00
|
|
|
newURLString = urlString.replacingOccurrences(
|
2020-07-05 00:16:22 +02:00
|
|
|
of: "http://",
|
|
|
|
|
with: "https://",
|
|
|
|
|
options: .caseInsensitive,
|
|
|
|
|
range: urlString.range(of: "http://")
|
|
|
|
|
)
|
2017-03-15 14:14:42 -07:00
|
|
|
} else if urlString.lowercased().hasPrefix("https://") {
|
|
|
|
|
// do nothing here
|
|
|
|
|
} else {
|
|
|
|
|
// if a url does not start with http or https, try to add https
|
2021-12-28 02:57:11 +01:00
|
|
|
newURLString = "https://\(urlString)"
|
2017-03-15 14:14:42 -07:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2021-12-28 02:57:11 +01:00
|
|
|
try? FavIcon.downloadPreferred(newURLString) { [weak self] result in
|
2018-11-17 21:27:06 -08:00
|
|
|
if case let .success(image) = result {
|
|
|
|
|
let indexPath = IndexPath(row: 0, section: 0)
|
|
|
|
|
self?.passwordImage = image
|
2019-05-01 17:49:27 +02:00
|
|
|
self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
|
|
|
|
|
let imageData = image.jpegData(compressionQuality: 1)
|
2018-11-17 21:27:06 -08:00
|
|
|
if let entity = self?.passwordEntity {
|
|
|
|
|
self?.passwordStore.updateImage(passwordEntity: entity, image: imageData)
|
2017-02-09 13:17:11 +08:00
|
|
|
}
|
2017-02-06 15:03:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-05 00:35:23 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@objc
|
|
|
|
|
private func tapMenu(recognizer: UITapGestureRecognizer) {
|
2019-05-01 17:49:27 +02:00
|
|
|
if recognizer.state == UIGestureRecognizer.State.ended {
|
2020-06-28 21:25:40 +02:00
|
|
|
let tapLocation = recognizer.location(in: tableView)
|
|
|
|
|
if let tapIndexPath = tableView.indexPathForRow(at: tapLocation) {
|
|
|
|
|
if let tappedCell = tableView.cellForRow(at: tapIndexPath) as? LabelTableViewCell {
|
2017-02-05 00:35:23 +08:00
|
|
|
tappedCell.becomeFirstResponder()
|
|
|
|
|
let menuController = UIMenuController.shared
|
2021-10-03 05:46:07 +02:00
|
|
|
let revealItem = UIMenuItem(title: "Reveal".localize(), action: #selector(LabelTableViewCell.revealPassword))
|
|
|
|
|
let concealItem = UIMenuItem(title: "Conceal".localize(), action: #selector(LabelTableViewCell.concealPassword))
|
|
|
|
|
let nextHOTPItem = UIMenuItem(title: "NextPassword".localize(), action: #selector(LabelTableViewCell.getNextHOTP))
|
|
|
|
|
let openURLItem = UIMenuItem(title: "CopyAndOpen".localize(), action: #selector(LabelTableViewCell.openLink))
|
2017-03-22 01:39:26 +08:00
|
|
|
menuController.menuItems = [revealItem, concealItem, nextHOTPItem, openURLItem]
|
2023-03-10 20:56:56 -08:00
|
|
|
menuController.showMenu(from: tappedCell.contentLabel.superview!, rect: tappedCell.contentLabel.frame)
|
2017-02-05 00:35:23 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-04 20:38:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
func gestureRecognizer(_: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
|
2017-04-01 08:36:47 -07:00
|
|
|
if touch.view!.isKind(of: UIButton.classForCoder()) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
@IBAction
|
2020-07-05 22:49:53 +02:00
|
|
|
private func back(segue _: UIStoryboardSegue) {}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
func getNextHOTP() {
|
|
|
|
|
guard password != nil, passwordEntity != nil, password?.otpType == .hotp else {
|
|
|
|
|
DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: "GetNextPasswordOfNonHotp.".localize(), controller: self, completion: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2020-06-28 21:25:40 +02:00
|
|
|
return
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-04-09 02:54:05 +08:00
|
|
|
// copy HOTP to pasteboard (will update counter)
|
|
|
|
|
if let plainPassword = password!.getNextHotp() {
|
2017-07-27 23:56:24 +08:00
|
|
|
SecurePasteboard.shared.copy(textToCopy: plainPassword)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-22 01:39:26 +08:00
|
|
|
// commit the change of HOTP counter
|
2017-04-25 13:01:17 -07:00
|
|
|
if password!.changed != 0 {
|
|
|
|
|
do {
|
2020-06-28 21:25:40 +02:00
|
|
|
passwordEntity = try passwordStore.edit(passwordEntity: passwordEntity!, password: password!)
|
2019-07-15 01:37:20 +08:00
|
|
|
SVProgressHUD.showSuccess(withStatus: "PasswordCopied".localize() | "CounterUpdated".localize())
|
|
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2017-04-25 13:01:17 -07:00
|
|
|
} catch {
|
2019-07-15 01:37:20 +08:00
|
|
|
SVProgressHUD.showSuccess(withStatus: error.localizedDescription)
|
|
|
|
|
SVProgressHUD.dismiss(withDelay: 1)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-07 21:55:30 +01:00
|
|
|
|
|
|
|
|
func openLink(to address: String?) {
|
|
|
|
|
guard address != nil, let url = URL(string: formActualWebAddress(from: address!)) else {
|
2022-06-16 04:55:02 +02:00
|
|
|
DispatchQueue.main.async {
|
2019-01-14 20:57:45 +01:00
|
|
|
Utils.alert(title: "Error".localize(), message: "CannotFindValidUrl".localize(), controller: self, completion: nil)
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2022-06-16 04:55:02 +02:00
|
|
|
return
|
2017-03-22 01:39:26 +08:00
|
|
|
}
|
2017-07-27 23:56:24 +08:00
|
|
|
SecurePasteboard.shared.copy(textToCopy: password?.password)
|
2017-03-22 01:39:26 +08:00
|
|
|
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
|
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
|
2018-12-07 21:55:30 +01:00
|
|
|
private func formActualWebAddress(from: String) -> String {
|
|
|
|
|
let lowercased = from.lowercased()
|
|
|
|
|
if !(lowercased.starts(with: "https://") || lowercased.starts(with: "http://")) {
|
2018-12-07 21:56:48 +01:00
|
|
|
return "https://\(from)"
|
2018-12-07 21:55:30 +01:00
|
|
|
}
|
|
|
|
|
return from
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func numberOfSections(in _: UITableView) -> Int {
|
|
|
|
|
tableData.count
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
|
tableData[section].item.count
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2017-02-06 20:48:20 +08:00
|
|
|
let sectionIndex = indexPath.section
|
|
|
|
|
let rowIndex = indexPath.row
|
2017-03-31 00:28:37 -07:00
|
|
|
let tableDataItem = tableData[sectionIndex].item[rowIndex]
|
2020-06-28 21:25:40 +02:00
|
|
|
switch tableData[sectionIndex].type {
|
2017-03-31 00:28:37 -07:00
|
|
|
case .name:
|
2017-02-06 20:48:20 +08:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordDetailTitleTableViewCell", for: indexPath) as! PasswordDetailTitleTableViewCell
|
2020-01-02 00:48:00 +01:00
|
|
|
if !Defaults.isHidePasswordImagesOn {
|
2019-03-08 00:26:53 +10:00
|
|
|
cell.labelCellConstraint.isActive = false
|
|
|
|
|
cell.labelImageConstraint.isActive = true
|
|
|
|
|
cell.passwordImageImageView.image = passwordImage ?? #imageLiteral(resourceName: "PasswordImagePlaceHolder")
|
|
|
|
|
cell.passwordImageImageView.isHidden = false
|
2019-02-27 21:49:21 +10:00
|
|
|
} else {
|
2019-03-08 00:26:53 +10:00
|
|
|
cell.passwordImageImageView.image = nil
|
|
|
|
|
cell.passwordImageImageView.isHidden = true
|
|
|
|
|
cell.labelImageConstraint.isActive = false
|
|
|
|
|
cell.labelCellConstraint.isActive = true
|
2019-02-27 21:49:21 +10:00
|
|
|
}
|
2018-11-10 22:38:12 -08:00
|
|
|
let passwordName = passwordEntity!.getName()
|
|
|
|
|
if passwordEntity!.synced == false {
|
|
|
|
|
cell.nameLabel.text = "\(passwordName) ↻"
|
|
|
|
|
} else {
|
|
|
|
|
cell.nameLabel.text = passwordName
|
2017-02-19 00:55:13 +08:00
|
|
|
}
|
2017-04-23 10:03:09 -07:00
|
|
|
cell.categoryLabel.text = passwordEntity!.getCategoryText()
|
2017-03-31 22:44:30 -07:00
|
|
|
cell.selectionStyle = .none
|
2017-02-06 20:48:20 +08:00
|
|
|
return cell
|
2020-12-18 10:03:47 +01:00
|
|
|
case .addition, .main:
|
2017-02-06 20:48:20 +08:00
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! LabelTableViewCell
|
2017-03-31 00:28:37 -07:00
|
|
|
let titleData = tableDataItem.title
|
|
|
|
|
let contentData = tableDataItem.content
|
2017-03-22 01:39:26 +08:00
|
|
|
cell.delegatePasswordTableView = self
|
2017-02-06 20:48:20 +08:00
|
|
|
cell.cellData = LabelTableViewCellData(title: titleData, content: contentData)
|
2017-03-31 22:44:30 -07:00
|
|
|
cell.selectionStyle = .none
|
2017-02-06 20:48:20 +08:00
|
|
|
return cell
|
2017-03-31 00:28:37 -07:00
|
|
|
case .misc:
|
2018-12-08 00:57:54 +01:00
|
|
|
let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
|
2017-03-31 00:28:37 -07:00
|
|
|
cell.textLabel?.text = tableDataItem.title
|
2017-03-31 22:44:30 -07:00
|
|
|
cell.selectionStyle = .default
|
2018-12-08 00:57:54 +01:00
|
|
|
addHiddenFieldInformation(to: cell)
|
2017-03-31 00:28:37 -07:00
|
|
|
return cell
|
2017-02-06 20:48:20 +08:00
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|
2018-12-08 00:57:54 +01:00
|
|
|
|
|
|
|
|
private func addHiddenFieldInformation(to cell: UITableViewCell) {
|
|
|
|
|
guard password != nil, let detailTextLabel = cell.detailTextLabel else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var numberOfHiddenFields = 0
|
2020-01-02 00:48:00 +01:00
|
|
|
numberOfHiddenFields += Defaults.isHideUnknownOn ? password!.numberOfUnknowns : 0
|
|
|
|
|
numberOfHiddenFields += Defaults.isHideOTPOn ? password!.numberOfOtpRelated : 0
|
2018-12-08 00:57:54 +01:00
|
|
|
guard numberOfHiddenFields > 0 else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
detailTextLabel.textAlignment = .center
|
|
|
|
|
detailTextLabel.textColor = .gray
|
2019-01-14 20:57:45 +01:00
|
|
|
detailTextLabel.text = "HiddenFields(%d)".localize(numberOfHiddenFields)
|
2018-12-08 00:57:54 +01:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
|
tableData[section].header
|
2017-02-04 20:38:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2023-03-13 21:33:54 -07:00
|
|
|
override func tableView(_: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
|
|
|
|
|
if tableData[section].header != nil {
|
|
|
|
|
return 30
|
|
|
|
|
} else {
|
|
|
|
|
return UITableView.automaticDimension
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 08:54:51 +08:00
|
|
|
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
|
|
|
|
|
if section == tableData.count - 1 {
|
|
|
|
|
let view = UIView()
|
2017-02-27 15:21:25 +08:00
|
|
|
let footerLabel = UILabel(frame: CGRect(x: 15, y: 15, width: tableView.frame.width, height: 60))
|
2017-02-27 08:54:51 +08:00
|
|
|
footerLabel.numberOfLines = 0
|
|
|
|
|
footerLabel.font = UIFont.preferredFont(forTextStyle: .footnote)
|
2017-02-27 17:37:53 +08:00
|
|
|
footerLabel.textColor = UIColor.gray
|
2020-06-28 21:25:40 +02:00
|
|
|
let dateString = passwordStore.getLatestUpdateInfo(filename: password!.url.path)
|
2019-01-14 20:57:45 +01:00
|
|
|
footerLabel.text = "LastUpdated".localize(dateString)
|
2017-02-27 08:54:51 +08:00
|
|
|
view.addSubview(footerLabel)
|
|
|
|
|
return view
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func tableView(_: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender _: Any?) {
|
2017-02-03 18:02:40 +08:00
|
|
|
if action == #selector(copy(_:)) {
|
2017-07-27 23:56:24 +08:00
|
|
|
SecurePasteboard.shared.copy(textToCopy: tableData[indexPath.section].item[indexPath.row].content)
|
2017-02-03 18:02:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func tableView(_: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender _: Any?) -> Bool {
|
2017-03-31 22:44:30 -07:00
|
|
|
let section = tableData[indexPath.section]
|
2020-06-28 21:25:40 +02:00
|
|
|
switch section.type {
|
2020-12-18 10:03:47 +01:00
|
|
|
case .addition, .main:
|
2021-10-03 05:46:07 +02:00
|
|
|
return action == #selector(UIResponderStandardEditActions.copy)
|
2017-03-31 22:44:30 -07:00
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-02-03 18:02:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
override func tableView(_: UITableView, shouldShowMenuForRowAt _: IndexPath) -> Bool {
|
|
|
|
|
true
|
2017-02-03 18:02:40 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-03-31 22:44:30 -07:00
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
let section = tableData[indexPath.section]
|
|
|
|
|
if section.type == .misc {
|
2019-01-14 20:57:45 +01:00
|
|
|
if section.item[indexPath.row].title == "ShowRaw".localize() {
|
2017-03-31 22:44:30 -07:00
|
|
|
performSegue(withIdentifier: "showRawPasswordSegue", sender: self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
}
|
2017-02-02 21:04:31 +08:00
|
|
|
}
|
2022-01-09 21:38:39 -08:00
|
|
|
|
|
|
|
|
extension PasswordDetailTableViewController {
|
2023-03-10 06:33:19 +01:00
|
|
|
@objc
|
|
|
|
|
func actOnPossiblePasswordStoreUpdate() {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
if let path = self.passwordPath {
|
|
|
|
|
// reload PasswordEntity because all PasswordEntities are re-created on PasswordStore update
|
|
|
|
|
self.passwordEntity = PasswordStore.shared.fetchPasswordEntity(with: path)
|
|
|
|
|
|
|
|
|
|
// dismiss if the PasswordEntity does not exist anymore
|
|
|
|
|
if self.passwordEntity == nil {
|
|
|
|
|
self.navigationController?.popToRootViewController(animated: true)
|
|
|
|
|
} else {
|
|
|
|
|
self.decryptThenShowPassword()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 02:22:45 +00:00
|
|
|
private func requestYubiKeyPIN(completion: @escaping (String) -> Void, cancellation: @escaping () -> Void) {
|
2022-01-09 21:38:39 -08:00
|
|
|
let alert = UIAlertController(title: "YubiKey PIN", message: "Verify YubiKey OpenPGP PIN.", preferredStyle: .alert)
|
|
|
|
|
alert.addAction(
|
|
|
|
|
UIAlertAction.cancel { _ in
|
|
|
|
|
self.navigationController!.popViewController(animated: true)
|
2022-12-30 02:22:45 +00:00
|
|
|
cancellation()
|
2022-01-09 21:38:39 -08:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
alert.addAction(
|
|
|
|
|
UIAlertAction.ok { _ in
|
|
|
|
|
let pin = alert.textFields?.first?.text ?? ""
|
|
|
|
|
completion(pin)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
alert.addTextField { textField in
|
|
|
|
|
textField.isSecureTextEntry = true
|
|
|
|
|
}
|
|
|
|
|
present(alert, animated: true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func handleError(error: AppError) {
|
|
|
|
|
switch error {
|
|
|
|
|
case let .yubiKey(yubiKeyError):
|
|
|
|
|
let errorMessage = yubiKeyError.localizedDescription
|
2023-02-04 15:27:37 -08:00
|
|
|
YubiKitManager.shared.stopNFCConnection(withErrorMessage: errorMessage)
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.navigationController?.popViewController(animated: true)
|
2022-01-09 21:38:39 -08:00
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.presentFailureAlert(message: error.localizedDescription) { _ in
|
|
|
|
|
self.navigationController?.popViewController(animated: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 02:22:45 +00:00
|
|
|
private func handleCancellation() {
|
2022-01-09 21:38:39 -08:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.navigationController?.popViewController(animated: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func decryptThenShowPasswordYubiKey() {
|
2023-04-23 22:01:37 +02:00
|
|
|
guard let passwordEntity else {
|
2022-01-09 21:38:39 -08:00
|
|
|
handleError(error: AppError.other(message: "PasswordDoesNotExist"))
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-01-09 22:12:41 -08:00
|
|
|
yubiKeyDecrypt(passwordEntity: passwordEntity, requestPIN: requestYubiKeyPIN, errorHandler: handleError, cancellation: handleCancellation) { password in
|
2022-01-09 21:38:39 -08:00
|
|
|
self.password = password
|
|
|
|
|
self.showPassword()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|