handle wrong GPG passphrase
This commit is contained in:
parent
c50cf8e0a7
commit
fd8cb300d7
3 changed files with 46 additions and 31 deletions
|
|
@ -10,10 +10,16 @@ import Foundation
|
||||||
import SwiftyUserDefaults
|
import SwiftyUserDefaults
|
||||||
|
|
||||||
class Password {
|
class Password {
|
||||||
var name = ""
|
var name: String
|
||||||
var password = ""
|
var password: String
|
||||||
var additions: [String: String]
|
var additions: [String: String]
|
||||||
|
|
||||||
|
init() {
|
||||||
|
name = ""
|
||||||
|
password = ""
|
||||||
|
additions = [:]
|
||||||
|
}
|
||||||
|
|
||||||
init(name: String, password: String, additions: [String: String]) {
|
init(name: String, password: String, additions: [String: String]) {
|
||||||
self.name = name
|
self.name = name
|
||||||
self.password = password
|
self.password = password
|
||||||
|
|
@ -22,10 +28,9 @@ class Password {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension PasswordEntity {
|
extension PasswordEntity {
|
||||||
func decrypt() -> Password? {
|
func decrypt() throws -> Password? {
|
||||||
var password: Password?
|
var password: Password?
|
||||||
let encryptedDataPath = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(rawPath!)")
|
let encryptedDataPath = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(rawPath!)")
|
||||||
do {
|
|
||||||
let encryptedData = try Data(contentsOf: encryptedDataPath)
|
let encryptedData = try Data(contentsOf: encryptedDataPath)
|
||||||
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
|
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
|
||||||
let plain = String(data: decryptedData, encoding: .ascii) ?? ""
|
let plain = String(data: decryptedData, encoding: .ascii) ?? ""
|
||||||
|
|
@ -42,9 +47,6 @@ extension PasswordEntity {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
password = Password(name: name!, password: decrypted_password, additions: decrypted_addtions)
|
password = Password(name: name!, password: decrypted_password, additions: decrypted_addtions)
|
||||||
} catch let error as NSError {
|
|
||||||
print(error.debugDescription)
|
|
||||||
}
|
|
||||||
return password
|
return password
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import UIKit
|
||||||
|
|
||||||
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
|
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
|
||||||
var passwordEntity: PasswordEntity?
|
var passwordEntity: PasswordEntity?
|
||||||
|
var password = Password()
|
||||||
|
|
||||||
struct TableCell {
|
struct TableCell {
|
||||||
var title: String
|
var title: String
|
||||||
|
|
@ -26,7 +27,15 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
|
tableView.register(UINib(nibName: "LabelTableViewCell", bundle: nil), forCellReuseIdentifier: "labelCell")
|
||||||
let password = passwordEntity!.decrypt()!
|
do {
|
||||||
|
password = try passwordEntity!.decrypt()!
|
||||||
|
} catch {
|
||||||
|
let alert = UIAlertController(title: "Cannot Show Password", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
|
||||||
|
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(UIAlertAction) -> Void in
|
||||||
|
self.navigationController!.popViewController(animated: true)
|
||||||
|
}))
|
||||||
|
self.present(alert, animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
|
||||||
var tableDataIndex = 0
|
var tableDataIndex = 0
|
||||||
tableData.append(TableSection(title: "", item: []))
|
tableData.append(TableSection(title: "", item: []))
|
||||||
|
|
|
||||||
|
|
@ -121,8 +121,12 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
} else {
|
} else {
|
||||||
password = passwordEntities![index]
|
password = passwordEntities![index]
|
||||||
}
|
}
|
||||||
let decryptedPassword = password.decrypt()!
|
do {
|
||||||
|
let decryptedPassword = try password.decrypt()!
|
||||||
UIPasteboard.general.string = decryptedPassword.password
|
UIPasteboard.general.string = decryptedPassword.password
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateSections(item: [PasswordEntity]) {
|
func generateSections(item: [PasswordEntity]) {
|
||||||
|
|
@ -174,14 +178,14 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
if let viewController = segue.destination as? PasswordDetailTableViewController {
|
if let viewController = segue.destination as? PasswordDetailTableViewController {
|
||||||
let selectedIndexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
|
let selectedIndexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
|
||||||
let index = sections[selectedIndexPath.section].index + selectedIndexPath.row
|
let index = sections[selectedIndexPath.section].index + selectedIndexPath.row
|
||||||
let password: PasswordEntity
|
let passwordEntity: PasswordEntity
|
||||||
if searchController.isActive && searchController.searchBar.text != "" {
|
if searchController.isActive && searchController.searchBar.text != "" {
|
||||||
password = filteredPasswordEntities[index]
|
passwordEntity = filteredPasswordEntities[index]
|
||||||
} else {
|
} else {
|
||||||
password = passwordEntities![index]
|
passwordEntity = passwordEntities![index]
|
||||||
}
|
}
|
||||||
viewController.passwordEntity = password
|
viewController.passwordEntity = passwordEntity
|
||||||
viewController.navigationItem.title = password.name
|
viewController.navigationItem.title = passwordEntity.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue