From 87dd4f96a3cf40da250e1ad3b5631ced438ae315 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Tue, 21 Feb 2017 13:07:14 +0800 Subject: [PATCH] simplify password repository setting --- pass.xcodeproj/project.pbxproj | 4 - pass/Base.lproj/Main.storyboard | 105 ++++++++++-------- .../GitServerSettingTableViewController.swift | 44 +++++--- ...epositorySettingsTableViewController.swift | 102 ----------------- .../SettingsTableViewController.swift | 66 +++++++++++ pass/Models/PasswordStore.swift | 1 + 6 files changed, 153 insertions(+), 169 deletions(-) delete mode 100644 pass/Controllers/PasswordRepositorySettingsTableViewController.swift diff --git a/pass.xcodeproj/project.pbxproj b/pass.xcodeproj/project.pbxproj index 3a3abdb..40d6ae1 100644 --- a/pass.xcodeproj/project.pbxproj +++ b/pass.xcodeproj/project.pbxproj @@ -12,7 +12,6 @@ DC037CA81E4B898100609409 /* BasicStaticTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CA71E4B898100609409 /* BasicStaticTableViewController.swift */; }; DC037CAA1E4B8EAE00609409 /* SpecialThanksTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CA91E4B8EAE00609409 /* SpecialThanksTableViewController.swift */; }; DC037CAC1E4C1C7100609409 /* FavIcon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC037CAB1E4C1C7100609409 /* FavIcon.framework */; }; - DC037CAE1E4C9B9B00609409 /* PasswordRepositorySettingsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CAD1E4C9B9B00609409 /* PasswordRepositorySettingsTableViewController.swift */; }; DC037CB01E4CA51F00609409 /* GeneralSettingsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CAF1E4CA51F00609409 /* GeneralSettingsTableViewController.swift */; }; DC037CB21E4CAB1700609409 /* AboutRepositoryTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CB11E4CAB1700609409 /* AboutRepositoryTableViewController.swift */; }; DC037CB81E4DD1A500609409 /* AddPasswordTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC037CB71E4DD1A500609409 /* AddPasswordTableViewController.swift */; }; @@ -69,7 +68,6 @@ DC037CA71E4B898100609409 /* BasicStaticTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasicStaticTableViewController.swift; sourceTree = ""; }; DC037CA91E4B8EAE00609409 /* SpecialThanksTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpecialThanksTableViewController.swift; sourceTree = ""; }; DC037CAB1E4C1C7100609409 /* FavIcon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FavIcon.framework; path = Carthage/Build/iOS/FavIcon.framework; sourceTree = ""; }; - DC037CAD1E4C9B9B00609409 /* PasswordRepositorySettingsTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordRepositorySettingsTableViewController.swift; sourceTree = ""; }; DC037CAF1E4CA51F00609409 /* GeneralSettingsTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneralSettingsTableViewController.swift; sourceTree = ""; }; DC037CB11E4CAB1700609409 /* AboutRepositoryTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AboutRepositoryTableViewController.swift; sourceTree = ""; }; DC037CB71E4DD1A500609409 /* AddPasswordTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AddPasswordTableViewController.swift; sourceTree = ""; }; @@ -164,7 +162,6 @@ DC037CA51E4B883900609409 /* OpenSourceComponentsTableViewController.swift */, DC4914981E434600007FF592 /* PasswordDetailTableViewController.swift */, DCFB77A81E502FF6008DE471 /* PasswordEditorTableViewController.swift */, - DC037CAD1E4C9B9B00609409 /* PasswordRepositorySettingsTableViewController.swift */, DC5734AD1E439AD400D09270 /* PasswordsViewController.swift */, DCA0499B1E3362F400522E8F /* PGPKeySettingTableViewController.swift */, DCAAF7441E2FA66800AB94BC /* SettingsTableViewController.swift */, @@ -432,7 +429,6 @@ DCFB77A71E502DF9008DE471 /* EditPasswordTableViewController.swift in Sources */, DCA0499A1E335CC800522E8F /* GitServerSettingTableViewController.swift in Sources */, DCDDEAB31E4896BF00F68193 /* PasswordDetailTitleTableViewCell.swift in Sources */, - DC037CAE1E4C9B9B00609409 /* PasswordRepositorySettingsTableViewController.swift in Sources */, DCC277D21E30D6EA00402246 /* pass.xcdatamodeld in Sources */, DC4914991E434600007FF592 /* PasswordDetailTableViewController.swift in Sources */, DC962CDF1E4B62C10033B5D8 /* AboutTableViewController.swift in Sources */, diff --git a/pass/Base.lproj/Main.storyboard b/pass/Base.lproj/Main.storyboard index 82e3452..9efba5f 100644 --- a/pass/Base.lproj/Main.storyboard +++ b/pass/Base.lproj/Main.storyboard @@ -100,7 +100,7 @@ - + @@ -108,16 +108,23 @@ + - + @@ -248,6 +255,7 @@ + @@ -270,10 +278,10 @@ - + - + @@ -296,10 +304,10 @@ - + - + @@ -321,38 +329,65 @@ - - + + - + - + + + + + + + + + - - + + - - + + - + + + + + + + + @@ -378,17 +413,18 @@ - - + + + - + @@ -791,29 +827,6 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/pass/Controllers/GitServerSettingTableViewController.swift b/pass/Controllers/GitServerSettingTableViewController.swift index 4e479dc..ba536c5 100644 --- a/pass/Controllers/GitServerSettingTableViewController.swift +++ b/pass/Controllers/GitServerSettingTableViewController.swift @@ -19,7 +19,22 @@ class GitServerSettingTableViewController: UITableViewController { var authenticationMethod = Defaults[.gitRepositoryAuthenticationMethod] - + private func checkAuthenticationMethod(method: String) { + let passwordCheckView = authPasswordCell.viewWithTag(1001)! + let sshKeyCheckView = authSSHKeyCell.viewWithTag(1001)! + + switch method { + case "Password": + passwordCheckView.isHidden = false + sshKeyCheckView.isHidden = true + case "SSH Key": + passwordCheckView.isHidden = true + sshKeyCheckView.isHidden = false + default: + passwordCheckView.isHidden = false + sshKeyCheckView.isHidden = true + } + } override func viewDidLoad() { super.viewDidLoad() if let url = Defaults[.gitRepositoryURL] { @@ -27,18 +42,18 @@ class GitServerSettingTableViewController: UITableViewController { } usernameTextField.text = Defaults[.gitRepositoryUsername] password = PasswordStore.shared.gitRepositoryPassword + authenticationMethod = Defaults[.gitRepositoryAuthenticationMethod] if authenticationMethod == nil { - authPasswordCell.accessoryType = .checkmark authenticationMethod = "Password" - } else { - switch authenticationMethod! { - case "Password": - authPasswordCell.accessoryType = .checkmark - case "SSH Key": - authSSHKeyCell.accessoryType = .checkmark - default: - authPasswordCell.accessoryType = .checkmark - } + } + checkAuthenticationMethod(method: authenticationMethod!) + authSSHKeyCell.accessoryType = .detailButton + } + + override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) { + let cell = tableView.cellForRow(at: indexPath) + if cell == authSSHKeyCell { + performSegue(withIdentifier: "showSSHKeySettingSegue", sender: self) } } @@ -72,21 +87,16 @@ class GitServerSettingTableViewController: UITableViewController { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) if cell == authPasswordCell { - authPasswordCell.accessoryType = .checkmark - authSSHKeyCell.accessoryType = .none authenticationMethod = "Password" } else if cell == authSSHKeyCell { - authPasswordCell.accessoryType = .none - authSSHKeyCell.accessoryType = .checkmark if Defaults[.gitRepositorySSHPublicKeyURL] == nil && Defaults[.gitRepositorySSHPrivateKeyURL] == nil { Utils.alert(title: "Cannot Select SSH Key", message: "Please setup SSH key first.", controller: self, completion: nil) authenticationMethod = "Password" - authSSHKeyCell.accessoryType = .none - authPasswordCell.accessoryType = .checkmark } else { authenticationMethod = "SSH Key" } } + checkAuthenticationMethod(method: authenticationMethod!) tableView.deselectRow(at: indexPath, animated: true) } diff --git a/pass/Controllers/PasswordRepositorySettingsTableViewController.swift b/pass/Controllers/PasswordRepositorySettingsTableViewController.swift deleted file mode 100644 index ee80d4e..0000000 --- a/pass/Controllers/PasswordRepositorySettingsTableViewController.swift +++ /dev/null @@ -1,102 +0,0 @@ -// -// PasswordRepositorySettingsTableViewController.swift -// pass -// -// Created by Mingshen Sun on 9/2/2017. -// Copyright © 2017 Bob Sun. All rights reserved. -// - -import UIKit -import SwiftyUserDefaults -import SVProgressHUD - -class PasswordRepositorySettingsTableViewController: BasicStaticTableViewController { - override func viewDidLoad() { - let url = Defaults[.gitRepositoryURL]?.host - tableData = [ - [[.style: CellDataStyle.value1, .title: "Git Server", .action: "segue", .link: "showGitServerSettingSegue", .detailText: url ?? ""], - [.style: CellDataStyle.value1, .title: "SSH Key", .action: "segue", .link: "showSSHKeySettingSegue", .detailText: "Not Set"],], - ] - navigationItemTitle = "Repository" - super.viewDidLoad() - } - - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - if let url = Defaults[.gitRepositoryURL] { - if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) { - cell.detailTextLabel!.text = url.host - } - } - if Defaults[.gitRepositorySSHPublicKeyURL] != nil { - if let cell = tableView.cellForRow(at: IndexPath(row: 1, section: 0)) { - cell.detailTextLabel!.text = "Set" - } - } - } - - - @IBAction func cancelGitServerSetting(segue: UIStoryboardSegue) { - } - - @IBAction func saveGitServerSetting(segue: UIStoryboardSegue) { - if let controller = segue.source as? GitServerSettingTableViewController { - let gitRepostiroyURL = controller.gitRepositoryURLTextField.text! - let username = controller.usernameTextField.text! - let password = controller.password - let auth = controller.authenticationMethod - - if Defaults[.gitRepositoryURL] == nil || - Defaults[.gitRepositoryURL]!.absoluteString != gitRepostiroyURL || - auth != Defaults[.gitRepositoryAuthenticationMethod] || - username != Defaults[.gitRepositoryUsername] || - password != PasswordStore.shared.gitRepositoryPassword { - - SVProgressHUD.setDefaultMaskType(.black) - SVProgressHUD.setDefaultStyle(.light) - SVProgressHUD.show(withStatus: "Prepare Repository") - var gitCredential: GitCredential - if auth == "Password" { - gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password!)) - } else { - gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyURL, privateKeyFile: Globals.sshPrivateKeyURL)) - } - let dispatchQueue = DispatchQueue.global(qos: .userInitiated) - dispatchQueue.async { - do { - try PasswordStore.shared.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!, - credential: gitCredential, - transferProgressBlock:{ (git_transfer_progress, stop) in - DispatchQueue.main.async { - SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Clone Remote Repository") - } - }, - checkoutProgressBlock: { (path, completedSteps, totalSteps) in - DispatchQueue.main.async { - SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch") - } - }) - DispatchQueue.main.async { - PasswordStore.shared.updatePasswordEntityCoreData() - Defaults[.lastUpdatedTime] = Date() - NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) - Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL) - Defaults[.gitRepositoryUsername] = username - Defaults[.gitRepositoryAuthenticationMethod] = auth - Defaults[.gitRepositoryPasswordAttempts] = 0 - SVProgressHUD.showSuccess(withStatus: "Done") - SVProgressHUD.dismiss(withDelay: 1) - } - } catch { - DispatchQueue.main.async { - print(error) - SVProgressHUD.showError(withStatus: error.localizedDescription) - SVProgressHUD.dismiss(withDelay: 1) - } - } - - } - } - } - } -} diff --git a/pass/Controllers/SettingsTableViewController.swift b/pass/Controllers/SettingsTableViewController.swift index 1289d63..3f892b3 100644 --- a/pass/Controllers/SettingsTableViewController.swift +++ b/pass/Controllers/SettingsTableViewController.swift @@ -19,6 +19,7 @@ class SettingsTableViewController: UITableViewController { @IBOutlet weak var pgpKeyTableViewCell: UITableViewCell! @IBOutlet weak var touchIDTableViewCell: UITableViewCell! @IBOutlet weak var passcodeTableViewCell: UITableViewCell! + @IBOutlet weak var passwordRepositoryTableViewCell: UITableViewCell! @IBAction func cancelPGPKey(segue: UIStoryboardSegue) { } @@ -89,6 +90,71 @@ class SettingsTableViewController: UITableViewController { } } + @IBAction func cancelGitServerSetting(segue: UIStoryboardSegue) { + } + + @IBAction func saveGitServerSetting(segue: UIStoryboardSegue) { + if let controller = segue.source as? GitServerSettingTableViewController { + let gitRepostiroyURL = controller.gitRepositoryURLTextField.text! + let username = controller.usernameTextField.text! + let password = controller.password + let auth = controller.authenticationMethod + + if Defaults[.gitRepositoryURL] == nil || + Defaults[.gitRepositoryURL]!.absoluteString != gitRepostiroyURL || + auth != Defaults[.gitRepositoryAuthenticationMethod] || + username != Defaults[.gitRepositoryUsername] || + password != PasswordStore.shared.gitRepositoryPassword { + + SVProgressHUD.setDefaultMaskType(.black) + SVProgressHUD.setDefaultStyle(.light) + SVProgressHUD.show(withStatus: "Prepare Repository") + var gitCredential: GitCredential + if auth == "Password" { + gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password!)) + } else { + gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyURL, privateKeyFile: Globals.sshPrivateKeyURL)) + } + let dispatchQueue = DispatchQueue.global(qos: .userInitiated) + dispatchQueue.async { + do { + try PasswordStore.shared.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!, + credential: gitCredential, + transferProgressBlock:{ (git_transfer_progress, stop) in + DispatchQueue.main.async { + SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Clone Remote Repository") + } + }, + checkoutProgressBlock: { (path, completedSteps, totalSteps) in + DispatchQueue.main.async { + SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch") + } + }) + DispatchQueue.main.async { + PasswordStore.shared.updatePasswordEntityCoreData() + Defaults[.lastUpdatedTime] = Date() + NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) + Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL) + Defaults[.gitRepositoryUsername] = username + Defaults[.gitRepositoryAuthenticationMethod] = auth + Defaults[.gitRepositoryPasswordAttempts] = 0 + self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitRepositoryURL]?.host + SVProgressHUD.showSuccess(withStatus: "Done") + SVProgressHUD.dismiss(withDelay: 1) + } + } catch { + DispatchQueue.main.async { + print(error) + SVProgressHUD.showError(withStatus: error.localizedDescription) + SVProgressHUD.dismiss(withDelay: 1) + } + } + + } + } + } + } + override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(SettingsTableViewController.actOnPasswordStoreErasedNotification), name: NSNotification.Name(rawValue: "passwordStoreErased"), object: nil) diff --git a/pass/Models/PasswordStore.swift b/pass/Models/PasswordStore.swift index ba26ff5..8b38b91 100644 --- a/pass/Models/PasswordStore.swift +++ b/pass/Models/PasswordStore.swift @@ -64,6 +64,7 @@ struct GitCredential { return nil } Defaults[.gitRepositoryPasswordAttempts] += 1 + PasswordStore.shared.gitRepositoryPassword = newPassword credential = try? GTCredential(userName: userName, password: newPassword) case let .ssh(userName, password, publicKeyFile, privateKeyFile): credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: password)