From d44aec6527cc1945a6f64a4390844927ac45a258 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Thu, 2 Feb 2017 18:02:43 +0800 Subject: [PATCH] refactor PasswordEntitiy model, add decrypt extension --- pass.xcodeproj/project.pbxproj | 16 +++++--- pass/Password.swift | 38 +++++++++++++++++++ pass/PasswordDetailViewController.swift | 20 +--------- .../pass.xcdatamodel/contents | 2 +- 4 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 pass/Password.swift diff --git a/pass.xcodeproj/project.pbxproj b/pass.xcodeproj/project.pbxproj index 4e5be19..0595126 100644 --- a/pass.xcodeproj/project.pbxproj +++ b/pass.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ DC1208561E35D0BA0042942E /* PasswordsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC1208551E35D0BA0042942E /* PasswordsTableViewController.swift */; }; DC1208581E35EBE60042942E /* ObjectiveGit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC1208571E35EBE60042942E /* ObjectiveGit.framework */; }; DC3DC8391E2F63240062A988 /* PasswordDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC3DC8381E2F63240062A988 /* PasswordDetailViewController.swift */; }; + DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC7E6EE91E432E48006C2443 /* Password.swift */; }; DC8963BE1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC8963BD1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift */; }; DC8963C01E38EEB900828B09 /* SSHKeySettingTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC8963BF1E38EEB900828B09 /* SSHKeySettingTableViewController.swift */; }; DC917BD71E2E8231000FDF54 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC917BD61E2E8231000FDF54 /* AppDelegate.swift */; }; @@ -37,6 +38,7 @@ DC1208571E35EBE60042942E /* ObjectiveGit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ObjectiveGit.framework; path = Carthage/Build/iOS/ObjectiveGit.framework; sourceTree = ""; }; DC3DC8381E2F63240062A988 /* PasswordDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordDetailViewController.swift; sourceTree = ""; }; DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Objective-CBridgingHeader.h"; sourceTree = ""; }; + DC7E6EE91E432E48006C2443 /* Password.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Password.swift; sourceTree = ""; }; DC8963BD1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitRepositoryAuthenticationSettingTableViewController.swift; sourceTree = ""; }; DC8963BF1E38EEB900828B09 /* SSHKeySettingTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SSHKeySettingTableViewController.swift; sourceTree = ""; }; DC917BD31E2E8231000FDF54 /* pass.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = pass.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -103,23 +105,24 @@ DC917BD51E2E8231000FDF54 /* pass */ = { isa = PBXGroup; children = ( - DC1208551E35D0BA0042942E /* PasswordsTableViewController.swift */, DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */, + DC1208551E35D0BA0042942E /* PasswordsTableViewController.swift */, DC917BD61E2E8231000FDF54 /* AppDelegate.swift */, - DC917BDA1E2E8231000FDF54 /* Main.storyboard */, DC8963BF1E38EEB900828B09 /* SSHKeySettingTableViewController.swift */, DC8963BD1E38AD8300828B09 /* GitRepositoryAuthenticationSettingTableViewController.swift */, DCA0499B1E3362F400522E8F /* PGPKeySettingTableViewController.swift */, DCA049991E335CC800522E8F /* GitServerSettingTableViewController.swift */, DCAAF7441E2FA66800AB94BC /* SettingsTableViewController.swift */, - DCC408C81E30BA1300F29B0E /* pass.xcdatamodeld */, DC3DC8381E2F63240062A988 /* PasswordDetailViewController.swift */, - DC917BDD1E2E8231000FDF54 /* Assets.xcassets */, - DC917BDF1E2E8231000FDF54 /* LaunchScreen.storyboard */, - DC917BE21E2E8231000FDF54 /* Info.plist */, DCC408A31E2FCC9E00F29B0E /* PasswordStore.swift */, + DC7E6EE91E432E48006C2443 /* Password.swift */, DCA049971E33586A00522E8F /* DefaultKeys.swift */, DCA0499D1E33BAC100522E8F /* Globals.swift */, + DCC408C81E30BA1300F29B0E /* pass.xcdatamodeld */, + DC917BDD1E2E8231000FDF54 /* Assets.xcassets */, + DC917BDA1E2E8231000FDF54 /* Main.storyboard */, + DC917BDF1E2E8231000FDF54 /* LaunchScreen.storyboard */, + DC917BE21E2E8231000FDF54 /* Info.plist */, ); path = pass; sourceTree = ""; @@ -281,6 +284,7 @@ DCC408A41E2FCC9E00F29B0E /* PasswordStore.swift in Sources */, DC8963C01E38EEB900828B09 /* SSHKeySettingTableViewController.swift in Sources */, DCA0499C1E3362F400522E8F /* PGPKeySettingTableViewController.swift in Sources */, + DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */, DC1208561E35D0BA0042942E /* PasswordsTableViewController.swift in Sources */, DCAAF7451E2FA66800AB94BC /* SettingsTableViewController.swift in Sources */, DCA0499A1E335CC800522E8F /* GitServerSettingTableViewController.swift in Sources */, diff --git a/pass/Password.swift b/pass/Password.swift new file mode 100644 index 0000000..fd4b581 --- /dev/null +++ b/pass/Password.swift @@ -0,0 +1,38 @@ +// +// Password.swift +// pass +// +// Created by Mingshen Sun on 2/2/2017. +// Copyright © 2017 Bob Sun. All rights reserved. +// + +import Foundation +import SwiftyUserDefaults + +class Password { + var name = "" + var password = "" + var additions: [String: String]? + + init(name: String, password: String, additions: [String: String]?) { + self.name = name + self.password = password + self.additions = additions + } +} + +extension PasswordEntity { + func decrypt() -> 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) ?? "" + password = Password(name: name!, password: plain, additions: nil) + } catch let error as NSError { + print(error.debugDescription) + } + return password + } +} diff --git a/pass/PasswordDetailViewController.swift b/pass/PasswordDetailViewController.swift index dbef66d..0b038d0 100644 --- a/pass/PasswordDetailViewController.swift +++ b/pass/PasswordDetailViewController.swift @@ -16,23 +16,7 @@ class PasswordDetailViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - let encryptedDataURL = URL(fileURLWithPath: "\(Globals.shared.documentPath)/\(passwordEntity!.rawPath!)") - let fm = FileManager.default - if fm.fileExists(atPath: encryptedDataURL.path){ - print("file exist") - } else { - print("file doesnt exist") - } - - do { - let encryptedData = try Data(contentsOf: encryptedDataURL) - let decryptedData = try PasswordStore.shared.pgp.decryptData(encryptedData, passphrase: Defaults[.pgpKeyPassphrase]) - let plain = String(data: decryptedData, encoding: .ascii) ?? "" - print(plain) - passwordTextView.text = plain - } catch let error as NSError { - print(error.debugDescription) - } - + let password = passwordEntity!.decrypt()! + passwordTextView.text = password.password } } diff --git a/pass/pass.xcdatamodeld/pass.xcdatamodel/contents b/pass/pass.xcdatamodeld/pass.xcdatamodel/contents index fee499c..0cee249 100644 --- a/pass/pass.xcdatamodeld/pass.xcdatamodel/contents +++ b/pass/pass.xcdatamodeld/pass.xcdatamodel/contents @@ -1,5 +1,5 @@ - +