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
|
||||
|
||||
class Password {
|
||||
var name = ""
|
||||
var password = ""
|
||||
var name: String
|
||||
var password: String
|
||||
var additions: [String: String]
|
||||
|
||||
init() {
|
||||
name = ""
|
||||
password = ""
|
||||
additions = [:]
|
||||
}
|
||||
|
||||
init(name: String, password: String, additions: [String: String]) {
|
||||
self.name = name
|
||||
self.password = password
|
||||
|
|
@ -22,10 +28,9 @@ class Password {
|
|||
}
|
||||
|
||||
extension PasswordEntity {
|
||||
func decrypt() -> Password? {
|
||||
func decrypt() throws -> Password? {
|
||||
var password: Password?
|
||||
let encryptedDataPath = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(rawPath!)")
|
||||
do {
|
||||
let encryptedData = try Data(contentsOf: encryptedDataPath)
|
||||
let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase])
|
||||
let plain = String(data: decryptedData, encoding: .ascii) ?? ""
|
||||
|
|
@ -42,9 +47,6 @@ extension PasswordEntity {
|
|||
}
|
||||
})
|
||||
password = Password(name: name!, password: decrypted_password, additions: decrypted_addtions)
|
||||
} catch let error as NSError {
|
||||
print(error.debugDescription)
|
||||
}
|
||||
return password
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import UIKit
|
|||
|
||||
class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate {
|
||||
var passwordEntity: PasswordEntity?
|
||||
var password = Password()
|
||||
|
||||
struct TableCell {
|
||||
var title: String
|
||||
|
|
@ -26,7 +27,15 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
|||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
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
|
||||
tableData.append(TableSection(title: "", item: []))
|
||||
|
|
|
|||
|
|
@ -121,8 +121,12 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
|||
} else {
|
||||
password = passwordEntities![index]
|
||||
}
|
||||
let decryptedPassword = password.decrypt()!
|
||||
do {
|
||||
let decryptedPassword = try password.decrypt()!
|
||||
UIPasteboard.general.string = decryptedPassword.password
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
|
||||
func generateSections(item: [PasswordEntity]) {
|
||||
|
|
@ -174,14 +178,14 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
|||
if let viewController = segue.destination as? PasswordDetailTableViewController {
|
||||
let selectedIndexPath = self.tableView.indexPath(for: sender as! UITableViewCell)!
|
||||
let index = sections[selectedIndexPath.section].index + selectedIndexPath.row
|
||||
let password: PasswordEntity
|
||||
let passwordEntity: PasswordEntity
|
||||
if searchController.isActive && searchController.searchBar.text != "" {
|
||||
password = filteredPasswordEntities[index]
|
||||
passwordEntity = filteredPasswordEntities[index]
|
||||
} else {
|
||||
password = passwordEntities![index]
|
||||
passwordEntity = passwordEntities![index]
|
||||
}
|
||||
viewController.passwordEntity = password
|
||||
viewController.navigationItem.title = password.name
|
||||
viewController.passwordEntity = passwordEntity
|
||||
viewController.navigationItem.title = passwordEntity.name
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue