Polish and simplify PasswordStore model class
This commit is contained in:
parent
fb42da4013
commit
eba79da0e6
5 changed files with 230 additions and 168 deletions
|
|
@ -59,6 +59,8 @@
|
||||||
DCC408C71E307DBB00F29B0E /* SVProgressHUD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DCC408C61E307DBB00F29B0E /* SVProgressHUD.framework */; };
|
DCC408C71E307DBB00F29B0E /* SVProgressHUD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DCC408C61E307DBB00F29B0E /* SVProgressHUD.framework */; };
|
||||||
DCC441521E8F6C06008A90C4 /* RawPasswordViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC441511E8F6C06008A90C4 /* RawPasswordViewController.swift */; };
|
DCC441521E8F6C06008A90C4 /* RawPasswordViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC441511E8F6C06008A90C4 /* RawPasswordViewController.swift */; };
|
||||||
DCC441541E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC441531E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift */; };
|
DCC441541E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC441531E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift */; };
|
||||||
|
DCD9AD131EB678500093499A /* GitCredential.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCD9AD121EB678500093499A /* GitCredential.swift */; };
|
||||||
|
DCD9AD151EB6829A0093499A /* AppError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCD9AD141EB6829A0093499A /* AppError.swift */; };
|
||||||
DCDDEAB01E4639F300F68193 /* LabelTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = DCDDEAAF1E4639F300F68193 /* LabelTableViewCell.xib */; };
|
DCDDEAB01E4639F300F68193 /* LabelTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = DCDDEAAF1E4639F300F68193 /* LabelTableViewCell.xib */; };
|
||||||
DCDDEAB31E4896BF00F68193 /* PasswordDetailTitleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCDDEAB11E4896BF00F68193 /* PasswordDetailTitleTableViewCell.swift */; };
|
DCDDEAB31E4896BF00F68193 /* PasswordDetailTitleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCDDEAB11E4896BF00F68193 /* PasswordDetailTitleTableViewCell.swift */; };
|
||||||
DCFB779A1E4F3BCF008DE471 /* TitleTextFieldTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCFB77981E4F3BCF008DE471 /* TitleTextFieldTableViewCell.swift */; };
|
DCFB779A1E4F3BCF008DE471 /* TitleTextFieldTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCFB77981E4F3BCF008DE471 /* TitleTextFieldTableViewCell.swift */; };
|
||||||
|
|
@ -142,6 +144,8 @@
|
||||||
DCC408C91E30BA1300F29B0E /* pass.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = pass.xcdatamodel; sourceTree = "<group>"; };
|
DCC408C91E30BA1300F29B0E /* pass.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = pass.xcdatamodel; sourceTree = "<group>"; };
|
||||||
DCC441511E8F6C06008A90C4 /* RawPasswordViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawPasswordViewController.swift; sourceTree = "<group>"; };
|
DCC441511E8F6C06008A90C4 /* RawPasswordViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawPasswordViewController.swift; sourceTree = "<group>"; };
|
||||||
DCC441531E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitSSHKeyArmorSettingTableViewController.swift; sourceTree = "<group>"; };
|
DCC441531E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GitSSHKeyArmorSettingTableViewController.swift; sourceTree = "<group>"; };
|
||||||
|
DCD9AD121EB678500093499A /* GitCredential.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = GitCredential.swift; path = pass/Controllers/GitCredential.swift; sourceTree = SOURCE_ROOT; };
|
||||||
|
DCD9AD141EB6829A0093499A /* AppError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppError.swift; sourceTree = "<group>"; };
|
||||||
DCDDEAAF1E4639F300F68193 /* LabelTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LabelTableViewCell.xib; sourceTree = "<group>"; };
|
DCDDEAAF1E4639F300F68193 /* LabelTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LabelTableViewCell.xib; sourceTree = "<group>"; };
|
||||||
DCDDEAB11E4896BF00F68193 /* PasswordDetailTitleTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordDetailTitleTableViewCell.swift; sourceTree = "<group>"; };
|
DCDDEAB11E4896BF00F68193 /* PasswordDetailTitleTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordDetailTitleTableViewCell.swift; sourceTree = "<group>"; };
|
||||||
DCFB77981E4F3BCF008DE471 /* TitleTextFieldTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TitleTextFieldTableViewCell.swift; sourceTree = "<group>"; };
|
DCFB77981E4F3BCF008DE471 /* TitleTextFieldTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TitleTextFieldTableViewCell.swift; sourceTree = "<group>"; };
|
||||||
|
|
@ -238,6 +242,7 @@
|
||||||
DCC408A31E2FCC9E00F29B0E /* PasswordStore.swift */,
|
DCC408A31E2FCC9E00F29B0E /* PasswordStore.swift */,
|
||||||
DC193FFF1E49E1A60077E0A3 /* PasscodeLockConfiguration.swift */,
|
DC193FFF1E49E1A60077E0A3 /* PasscodeLockConfiguration.swift */,
|
||||||
DC193FFD1E49E0760077E0A3 /* PasscodeLockRepository.swift */,
|
DC193FFD1E49E0760077E0A3 /* PasscodeLockRepository.swift */,
|
||||||
|
DCD9AD121EB678500093499A /* GitCredential.swift */,
|
||||||
);
|
);
|
||||||
path = Models;
|
path = Models;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
|
|
@ -302,6 +307,7 @@
|
||||||
children = (
|
children = (
|
||||||
DC917BE21E2E8231000FDF54 /* Info.plist */,
|
DC917BE21E2E8231000FDF54 /* Info.plist */,
|
||||||
DC917BD61E2E8231000FDF54 /* AppDelegate.swift */,
|
DC917BD61E2E8231000FDF54 /* AppDelegate.swift */,
|
||||||
|
DCD9AD141EB6829A0093499A /* AppError.swift */,
|
||||||
DC19400D1E4B3A340077E0A3 /* Models */,
|
DC19400D1E4B3A340077E0A3 /* Models */,
|
||||||
DC19400C1E4B39400077E0A3 /* Controllers */,
|
DC19400C1E4B39400077E0A3 /* Controllers */,
|
||||||
DC19400F1E4B3A9E0077E0A3 /* Views */,
|
DC19400F1E4B3A9E0077E0A3 /* Views */,
|
||||||
|
|
@ -526,6 +532,7 @@
|
||||||
files = (
|
files = (
|
||||||
DCC408A41E2FCC9E00F29B0E /* PasswordStore.swift in Sources */,
|
DCC408A41E2FCC9E00F29B0E /* PasswordStore.swift in Sources */,
|
||||||
DC037CBF1E4ED4E100609409 /* TextViewTableViewCell.swift in Sources */,
|
DC037CBF1E4ED4E100609409 /* TextViewTableViewCell.swift in Sources */,
|
||||||
|
DCD9AD151EB6829A0093499A /* AppError.swift in Sources */,
|
||||||
DCC441541E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift in Sources */,
|
DCC441541E916382008A90C4 /* GitSSHKeyArmorSettingTableViewController.swift in Sources */,
|
||||||
DC8963C01E38EEB900828B09 /* SSHKeySettingTableViewController.swift in Sources */,
|
DC8963C01E38EEB900828B09 /* SSHKeySettingTableViewController.swift in Sources */,
|
||||||
DC193FFA1E49B4430077E0A3 /* AdvancedSettingsTableViewController.swift in Sources */,
|
DC193FFA1E49B4430077E0A3 /* AdvancedSettingsTableViewController.swift in Sources */,
|
||||||
|
|
@ -545,6 +552,7 @@
|
||||||
DC4914991E434600007FF592 /* PasswordDetailTableViewController.swift in Sources */,
|
DC4914991E434600007FF592 /* PasswordDetailTableViewController.swift in Sources */,
|
||||||
DC962CDF1E4B62C10033B5D8 /* AboutTableViewController.swift in Sources */,
|
DC962CDF1E4B62C10033B5D8 /* AboutTableViewController.swift in Sources */,
|
||||||
DC5734AE1E439AD400D09270 /* PasswordsViewController.swift in Sources */,
|
DC5734AE1E439AD400D09270 /* PasswordsViewController.swift in Sources */,
|
||||||
|
DCD9AD131EB678500093499A /* GitCredential.swift in Sources */,
|
||||||
DC3E64E61E656F11009A83DE /* CommitLogsTableViewController.swift in Sources */,
|
DC3E64E61E656F11009A83DE /* CommitLogsTableViewController.swift in Sources */,
|
||||||
DC037CAA1E4B8EAE00609409 /* SpecialThanksTableViewController.swift in Sources */,
|
DC037CAA1E4B8EAE00609409 /* SpecialThanksTableViewController.swift in Sources */,
|
||||||
DC037CA61E4B883900609409 /* OpenSourceComponentsTableViewController.swift in Sources */,
|
DC037CA61E4B883900609409 /* OpenSourceComponentsTableViewController.swift in Sources */,
|
||||||
|
|
|
||||||
35
pass/AppError.swift
Normal file
35
pass/AppError.swift
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
//
|
||||||
|
// AppError.swift
|
||||||
|
// pass
|
||||||
|
//
|
||||||
|
// Created by Mingshen Sun on 30/4/2017.
|
||||||
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
enum AppError: Error {
|
||||||
|
case RepositoryNotSetError
|
||||||
|
case RepositoryRemoteMasterNotFoundError
|
||||||
|
case KeyImportError
|
||||||
|
case PasswordDuplicatedError
|
||||||
|
case GitResetError
|
||||||
|
case UnknownError
|
||||||
|
|
||||||
|
var localizedDescription: String {
|
||||||
|
switch self {
|
||||||
|
case .RepositoryNotSetError:
|
||||||
|
return "Git repository is not set."
|
||||||
|
case .RepositoryRemoteMasterNotFoundError:
|
||||||
|
return "Cannot find remote branch origin/master."
|
||||||
|
case .KeyImportError:
|
||||||
|
return "Cannot import the key."
|
||||||
|
case .PasswordDuplicatedError:
|
||||||
|
return "Cannot add the password: password duplicated."
|
||||||
|
case .GitResetError:
|
||||||
|
return "Cannot decide how to reset."
|
||||||
|
case .UnknownError:
|
||||||
|
return "Unknown error."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ class CommitLogsTableViewController: UITableViewController {
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(updateCommitLogs), name: .passwordStoreUpdated, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(updateCommitLogs), name: .passwordStoreUpdated, object: nil)
|
||||||
commits = passwordStore.getRecentCommits(count: 20)
|
commits = getCommitLogs()
|
||||||
self.tableView.estimatedRowHeight = 50
|
self.tableView.estimatedRowHeight = 50
|
||||||
self.tableView.rowHeight = UITableViewAutomaticDimension
|
self.tableView.rowHeight = UITableViewAutomaticDimension
|
||||||
}
|
}
|
||||||
|
|
@ -41,8 +41,17 @@ class CommitLogsTableViewController: UITableViewController {
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateCommitLogs () {
|
func updateCommitLogs() {
|
||||||
commits = passwordStore.getRecentCommits(count: 20)
|
commits = getCommitLogs()
|
||||||
tableView.reloadData()
|
tableView.reloadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func getCommitLogs() -> [GTCommit] {
|
||||||
|
do {
|
||||||
|
return try passwordStore.getRecentCommits(count: 20)
|
||||||
|
} catch {
|
||||||
|
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
107
pass/Controllers/GitCredential.swift
Normal file
107
pass/Controllers/GitCredential.swift
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
//
|
||||||
|
// GitCredential.swift
|
||||||
|
// pass
|
||||||
|
//
|
||||||
|
// Created by Mingshen Sun on 30/4/2017.
|
||||||
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import UIKit
|
||||||
|
import SwiftyUserDefaults
|
||||||
|
import ObjectiveGit
|
||||||
|
import SVProgressHUD
|
||||||
|
|
||||||
|
struct GitCredential {
|
||||||
|
var credential: Credential
|
||||||
|
|
||||||
|
enum Credential {
|
||||||
|
case http(userName: String, controller: UIViewController)
|
||||||
|
case ssh(userName: String, publicKeyFile: URL, privateKeyFile: URL, controller: UIViewController)
|
||||||
|
}
|
||||||
|
|
||||||
|
init(credential: Credential) {
|
||||||
|
self.credential = credential
|
||||||
|
}
|
||||||
|
|
||||||
|
func credentialProvider() throws -> GTCredentialProvider {
|
||||||
|
var attempts = 0
|
||||||
|
var lastPassword: String? = nil
|
||||||
|
return GTCredentialProvider { (_, _, _) -> (GTCredential?) in
|
||||||
|
var credential: GTCredential? = nil
|
||||||
|
|
||||||
|
switch self.credential {
|
||||||
|
case let .http(userName, controller):
|
||||||
|
var newPassword = Utils.getPasswordFromKeychain(name: "gitPassword")
|
||||||
|
if newPassword == nil || attempts != 0 {
|
||||||
|
if let requestedPassword = self.requestGitPassword(controller, lastPassword) {
|
||||||
|
newPassword = requestedPassword
|
||||||
|
Utils.addPasswordToKeychain(name: "gitPassword", password: newPassword)
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
attempts += 1
|
||||||
|
lastPassword = newPassword
|
||||||
|
credential = try? GTCredential(userName: userName, password: newPassword!)
|
||||||
|
case let .ssh(userName, publicKeyFile, privateKeyFile, controller):
|
||||||
|
var newPassword = Utils.getPasswordFromKeychain(name: "gitSSHKeyPassphrase")
|
||||||
|
if newPassword == nil || attempts != 0 {
|
||||||
|
if let requestedPassword = self.requestGitPassword(controller, lastPassword) {
|
||||||
|
newPassword = requestedPassword
|
||||||
|
Utils.addPasswordToKeychain(name: "gitSSHKeyPassphrase", password: newPassword)
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
attempts += 1
|
||||||
|
lastPassword = newPassword
|
||||||
|
credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: newPassword!)
|
||||||
|
}
|
||||||
|
return credential
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func delete() {
|
||||||
|
switch credential {
|
||||||
|
case .http:
|
||||||
|
Utils.removeKeychain(name: "gitPassword")
|
||||||
|
case .ssh:
|
||||||
|
Utils.removeKeychain(name: "gitSSHKeyPassphrase")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func requestGitPassword(_ controller: UIViewController, _ lastPassword: String?) -> String? {
|
||||||
|
let sem = DispatchSemaphore(value: 0)
|
||||||
|
var password: String?
|
||||||
|
var message = ""
|
||||||
|
switch credential {
|
||||||
|
case .http:
|
||||||
|
message = "Please fill in the password of your Git account."
|
||||||
|
case .ssh:
|
||||||
|
message = "Please fill in the password of your SSH key."
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
SVProgressHUD.dismiss()
|
||||||
|
let alert = UIAlertController(title: "Password", message: message, preferredStyle: UIAlertControllerStyle.alert)
|
||||||
|
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
||||||
|
textField.text = lastPassword ?? ""
|
||||||
|
textField.isSecureTextEntry = true
|
||||||
|
})
|
||||||
|
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
|
||||||
|
password = alert.textFields!.first!.text
|
||||||
|
sem.signal()
|
||||||
|
}))
|
||||||
|
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
|
||||||
|
password = nil
|
||||||
|
sem.signal()
|
||||||
|
})
|
||||||
|
controller.present(alert, animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = sem.wait(timeout: .distantFuture)
|
||||||
|
return password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -13,99 +13,6 @@ import SwiftyUserDefaults
|
||||||
import ObjectiveGit
|
import ObjectiveGit
|
||||||
import SVProgressHUD
|
import SVProgressHUD
|
||||||
|
|
||||||
struct GitCredential {
|
|
||||||
var credential: Credential
|
|
||||||
|
|
||||||
enum Credential {
|
|
||||||
case http(userName: String, controller: UIViewController)
|
|
||||||
case ssh(userName: String, publicKeyFile: URL, privateKeyFile: URL, controller: UIViewController)
|
|
||||||
}
|
|
||||||
|
|
||||||
init(credential: Credential) {
|
|
||||||
self.credential = credential
|
|
||||||
}
|
|
||||||
|
|
||||||
func credentialProvider() throws -> GTCredentialProvider {
|
|
||||||
var attempts = 0
|
|
||||||
var lastPassword: String? = nil
|
|
||||||
return GTCredentialProvider { (_, _, _) -> (GTCredential?) in
|
|
||||||
var credential: GTCredential? = nil
|
|
||||||
|
|
||||||
switch self.credential {
|
|
||||||
case let .http(userName, controller):
|
|
||||||
var newPassword = Utils.getPasswordFromKeychain(name: "gitPassword")
|
|
||||||
if newPassword == nil || attempts != 0 {
|
|
||||||
if let requestedPassword = self.requestGitPassword(controller, lastPassword) {
|
|
||||||
newPassword = requestedPassword
|
|
||||||
Utils.addPasswordToKeychain(name: "gitPassword", password: newPassword)
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
attempts += 1
|
|
||||||
lastPassword = newPassword
|
|
||||||
credential = try? GTCredential(userName: userName, password: newPassword!)
|
|
||||||
case let .ssh(userName, publicKeyFile, privateKeyFile, controller):
|
|
||||||
var newPassword = Utils.getPasswordFromKeychain(name: "gitSSHKeyPassphrase")
|
|
||||||
if newPassword == nil || attempts != 0 {
|
|
||||||
if let requestedPassword = self.requestGitPassword(controller, lastPassword) {
|
|
||||||
newPassword = requestedPassword
|
|
||||||
Utils.addPasswordToKeychain(name: "gitSSHKeyPassphrase", password: newPassword)
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
attempts += 1
|
|
||||||
lastPassword = newPassword
|
|
||||||
credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: newPassword!)
|
|
||||||
}
|
|
||||||
return credential
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func delete() {
|
|
||||||
switch credential {
|
|
||||||
case .http:
|
|
||||||
Utils.removeKeychain(name: "gitPassword")
|
|
||||||
case .ssh:
|
|
||||||
Utils.removeKeychain(name: "gitSSHKeyPassphrase")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func requestGitPassword(_ controller: UIViewController, _ lastPassword: String?) -> String? {
|
|
||||||
let sem = DispatchSemaphore(value: 0)
|
|
||||||
var password: String?
|
|
||||||
var message = ""
|
|
||||||
switch credential {
|
|
||||||
case .http:
|
|
||||||
message = "Please fill in the password of your Git account."
|
|
||||||
case .ssh:
|
|
||||||
message = "Please fill in the password of your SSH key."
|
|
||||||
}
|
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
SVProgressHUD.dismiss()
|
|
||||||
let alert = UIAlertController(title: "Password", message: message, preferredStyle: UIAlertControllerStyle.alert)
|
|
||||||
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
|
||||||
textField.text = lastPassword ?? ""
|
|
||||||
textField.isSecureTextEntry = true
|
|
||||||
})
|
|
||||||
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
|
|
||||||
password = alert.textFields!.first!.text
|
|
||||||
sem.signal()
|
|
||||||
}))
|
|
||||||
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
|
|
||||||
password = nil
|
|
||||||
sem.signal()
|
|
||||||
})
|
|
||||||
controller.present(alert, animated: true, completion: nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = sem.wait(timeout: .distantFuture)
|
|
||||||
return password
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class PasswordStore {
|
class PasswordStore {
|
||||||
static let shared = PasswordStore()
|
static let shared = PasswordStore()
|
||||||
let storeURL = URL(fileURLWithPath: "\(Globals.repositoryPath)")
|
let storeURL = URL(fileURLWithPath: "\(Globals.repositoryPath)")
|
||||||
|
|
@ -223,16 +130,16 @@ class PasswordStore {
|
||||||
let keyPath = Globals.pgpPublicKeyPath
|
let keyPath = Globals.pgpPublicKeyPath
|
||||||
self.publicKey = importKey(from: keyPath)
|
self.publicKey = importKey(from: keyPath)
|
||||||
if self.publicKey == nil {
|
if self.publicKey == nil {
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import the public PGP key."])
|
throw AppError.KeyImportError
|
||||||
}
|
}
|
||||||
case .secret:
|
case .secret:
|
||||||
let keyPath = Globals.pgpPrivateKeyPath
|
let keyPath = Globals.pgpPrivateKeyPath
|
||||||
self.privateKey = importKey(from: keyPath)
|
self.privateKey = importKey(from: keyPath)
|
||||||
if self.privateKey == nil {
|
if self.privateKey == nil {
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import the private PGP key."])
|
throw AppError.KeyImportError
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot import key: unknown PGP key type."])
|
throw AppError.UnknownError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -329,9 +236,7 @@ class PasswordStore {
|
||||||
Utils.removeFileIfExists(at: tempStoreURL)
|
Utils.removeFileIfExists(at: tempStoreURL)
|
||||||
do {
|
do {
|
||||||
let credentialProvider = try credential.credentialProvider()
|
let credentialProvider = try credential.credentialProvider()
|
||||||
let options: [String: Any] = [
|
let options = [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
|
||||||
GTRepositoryCloneOptionsCredentialProvider: credentialProvider,
|
|
||||||
]
|
|
||||||
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock)
|
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock)
|
||||||
let fm = FileManager.default
|
let fm = FileManager.default
|
||||||
if fm.fileExists(atPath: storeURL.path) {
|
if fm.fileExists(atPath: storeURL.path) {
|
||||||
|
|
@ -352,16 +257,14 @@ class PasswordStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
func pullRepository(credential: GitCredential, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
func pullRepository(credential: GitCredential, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
||||||
if storeRepository == nil {
|
guard let repository = storeRepository else {
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "Git Repository is not set."])
|
throw AppError.RepositoryNotSetError
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
let credentialProvider = try credential.credentialProvider()
|
let credentialProvider = try credential.credentialProvider()
|
||||||
let options: [String: Any] = [
|
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
|
||||||
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider
|
|
||||||
]
|
|
||||||
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
||||||
try storeRepository!.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock)
|
try repository.pull(repository.currentBranch(), from: remote, withOptions: options, progress: transferProgressBlock)
|
||||||
} catch {
|
} catch {
|
||||||
credential.delete()
|
credential.delete()
|
||||||
throw(error)
|
throw(error)
|
||||||
|
|
@ -429,21 +332,18 @@ class PasswordStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRecentCommits(count: Int) -> [GTCommit] {
|
func getRecentCommits(count: Int) throws -> [GTCommit] {
|
||||||
guard storeRepository != nil else {
|
guard let repository = storeRepository else {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
var commits = [GTCommit]()
|
var commits = [GTCommit]()
|
||||||
do {
|
let enumerator = try GTEnumerator(repository: repository)
|
||||||
let enumerator = try GTEnumerator(repository: storeRepository!)
|
if let sha = try repository.headReference().targetOID.sha {
|
||||||
try enumerator.pushSHA(storeRepository!.headReference().targetOID.sha!)
|
try enumerator.pushSHA(sha)
|
||||||
for _ in 0 ..< count {
|
}
|
||||||
let commit = try enumerator.nextObject(withSuccess: nil)
|
for _ in 0 ..< count {
|
||||||
commits.append(commit)
|
let commit = try enumerator.nextObject(withSuccess: nil)
|
||||||
}
|
commits.append(commit)
|
||||||
} catch {
|
|
||||||
print(error)
|
|
||||||
return commits
|
|
||||||
}
|
}
|
||||||
return commits
|
return commits
|
||||||
}
|
}
|
||||||
|
|
@ -464,7 +364,6 @@ class PasswordStore {
|
||||||
do {
|
do {
|
||||||
if !withDir {
|
if !withDir {
|
||||||
passwordEntityFetch.predicate = NSPredicate(format: "isDir = false")
|
passwordEntityFetch.predicate = NSPredicate(format: "isDir = false")
|
||||||
|
|
||||||
}
|
}
|
||||||
let fetchedPasswordEntities = try context.fetch(passwordEntityFetch) as! [PasswordEntity]
|
let fetchedPasswordEntities = try context.fetch(passwordEntityFetch) as! [PasswordEntity]
|
||||||
return fetchedPasswordEntities.sorted { $0.name!.caseInsensitiveCompare($1.name!) == .orderedAscending }
|
return fetchedPasswordEntities.sorted { $0.name!.caseInsensitiveCompare($1.name!) == .orderedAscending }
|
||||||
|
|
@ -511,11 +410,14 @@ class PasswordStore {
|
||||||
|
|
||||||
|
|
||||||
func getLatestUpdateInfo(filename: String) -> String {
|
func getLatestUpdateInfo(filename: String) -> String {
|
||||||
guard let blameHunks = try? storeRepository?.blame(withFile: filename, options: nil).hunks,
|
guard let repository = storeRepository else {
|
||||||
let latestCommitTime = blameHunks?.map({
|
return "Unknown"
|
||||||
|
}
|
||||||
|
guard let blameHunks = try? repository.blame(withFile: filename, options: nil).hunks,
|
||||||
|
let latestCommitTime = blameHunks.map({
|
||||||
$0.finalSignature?.time?.timeIntervalSince1970 ?? 0
|
$0.finalSignature?.time?.timeIntervalSince1970 ?? 0
|
||||||
}).max() else {
|
}).max() else {
|
||||||
return "unknown"
|
return "Unknown"
|
||||||
}
|
}
|
||||||
let lastCommitDate = Date(timeIntervalSince1970: latestCommitTime)
|
let lastCommitDate = Date(timeIntervalSince1970: latestCommitTime)
|
||||||
let currentDate = Date()
|
let currentDate = Date()
|
||||||
|
|
@ -528,7 +430,7 @@ class PasswordStore {
|
||||||
dateComponentsFormatter.unitsStyle = .full
|
dateComponentsFormatter.unitsStyle = .full
|
||||||
dateComponentsFormatter.maximumUnitCount = 2
|
dateComponentsFormatter.maximumUnitCount = 2
|
||||||
dateComponentsFormatter.includesApproximationPhrase = true
|
dateComponentsFormatter.includesApproximationPhrase = true
|
||||||
autoFormattedDifference = (dateComponentsFormatter.string(from: diffDate)?.appending(" ago"))!
|
autoFormattedDifference = dateComponentsFormatter.string(from: diffDate)!.appending(" ago")
|
||||||
}
|
}
|
||||||
return autoFormattedDifference
|
return autoFormattedDifference
|
||||||
}
|
}
|
||||||
|
|
@ -537,21 +439,22 @@ class PasswordStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func gitAdd(path: String) throws {
|
private func gitAdd(path: String) throws {
|
||||||
if let repo = storeRepository {
|
guard let repository = storeRepository else {
|
||||||
try repo.index().addFile(path)
|
throw AppError.RepositoryNotSetError
|
||||||
try repo.index().write()
|
|
||||||
}
|
}
|
||||||
|
try repository.index().addFile(path)
|
||||||
|
try repository.index().write()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func gitRm(path: String) throws {
|
private func gitRm(path: String) throws {
|
||||||
if let repo = storeRepository {
|
guard let repository = storeRepository else {
|
||||||
if FileManager.default.fileExists(atPath: storeURL.appendingPathComponent(path).path) {
|
throw AppError.RepositoryNotSetError
|
||||||
try FileManager.default.removeItem(at: storeURL.appendingPathComponent(path))
|
|
||||||
}
|
|
||||||
try repo.index().removeFile(path)
|
|
||||||
try repo.index().write()
|
|
||||||
}
|
}
|
||||||
|
if FileManager.default.fileExists(atPath: storeURL.appendingPathComponent(path).path) {
|
||||||
|
try FileManager.default.removeItem(at: storeURL.appendingPathComponent(path))
|
||||||
|
}
|
||||||
|
try repository.index().removeFile(path)
|
||||||
|
try repository.index().write()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func deleteDirectoryTree(at url: URL) throws {
|
private func deleteDirectoryTree(at url: URL) throws {
|
||||||
|
|
@ -586,39 +489,39 @@ class PasswordStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func gitCommit(message: String) throws -> GTCommit? {
|
private func gitCommit(message: String) throws -> GTCommit? {
|
||||||
if let repo = storeRepository {
|
guard let repository = storeRepository else {
|
||||||
let newTree = try repo.index().writeTree()
|
throw AppError.RepositoryNotSetError
|
||||||
let headReference = try repo.headReference()
|
|
||||||
let commitEnum = try GTEnumerator(repository: repo)
|
|
||||||
try commitEnum.pushSHA(headReference.targetOID.sha!)
|
|
||||||
let parent = commitEnum.nextObject() as! GTCommit
|
|
||||||
let signature = gitSignatureForNow
|
|
||||||
let commit = try repo.createCommit(with: newTree, message: message, author: signature, committer: signature, parents: [parent], updatingReferenceNamed: headReference.name)
|
|
||||||
return commit
|
|
||||||
}
|
}
|
||||||
return nil
|
let newTree = try repository.index().writeTree()
|
||||||
|
let headReference = try repository.headReference()
|
||||||
|
let commitEnum = try GTEnumerator(repository: repository)
|
||||||
|
try commitEnum.pushSHA(headReference.targetOID.sha!)
|
||||||
|
let parent = commitEnum.nextObject() as! GTCommit
|
||||||
|
let signature = gitSignatureForNow
|
||||||
|
let commit = try repository.createCommit(with: newTree, message: message, author: signature, committer: signature, parents: [parent], updatingReferenceNamed: headReference.name)
|
||||||
|
return commit
|
||||||
}
|
}
|
||||||
|
|
||||||
private func getLocalBranch(withName branchName: String) -> GTBranch? {
|
private func getLocalBranch(withName branchName: String) throws -> GTBranch? {
|
||||||
do {
|
guard let repository = storeRepository else {
|
||||||
let reference = GTBranch.localNamePrefix().appending(branchName)
|
throw AppError.RepositoryNotSetError
|
||||||
let branches = try storeRepository!.branches(withPrefix: reference)
|
|
||||||
return branches[0]
|
|
||||||
} catch {
|
|
||||||
print(error)
|
|
||||||
}
|
}
|
||||||
return nil
|
let reference = GTBranch.localNamePrefix().appending(branchName)
|
||||||
|
let branches = try repository.branches(withPrefix: reference)
|
||||||
|
return branches.first
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushRepository(credential: GitCredential, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
func pushRepository(credential: GitCredential, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
||||||
|
guard let repository = storeRepository else {
|
||||||
|
throw AppError.RepositoryNotSetError
|
||||||
|
}
|
||||||
do {
|
do {
|
||||||
let credentialProvider = try credential.credentialProvider()
|
let credentialProvider = try credential.credentialProvider()
|
||||||
let options: [String: Any] = [
|
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
|
||||||
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider,
|
if let masterBranch = try getLocalBranch(withName: "master") {
|
||||||
]
|
let remote = try GTRemote(name: "origin", in: repository)
|
||||||
let masterBranch = getLocalBranch(withName: "master")!
|
try repository.push(masterBranch, to: remote, withOptions: options, progress: transferProgressBlock)
|
||||||
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
}
|
||||||
try storeRepository?.push(masterBranch, to: remote, withOptions: options, progress: transferProgressBlock)
|
|
||||||
} catch {
|
} catch {
|
||||||
credential.delete()
|
credential.delete()
|
||||||
throw(error)
|
throw(error)
|
||||||
|
|
@ -627,7 +530,7 @@ class PasswordStore {
|
||||||
|
|
||||||
private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
|
private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
|
||||||
guard !passwordExisted(password: password) else {
|
guard !passwordExisted(password: password) else {
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 2, userInfo: [NSLocalizedDescriptionKey: "Cannot add password: password duplicated."])
|
throw AppError.PasswordDuplicatedError
|
||||||
}
|
}
|
||||||
|
|
||||||
var passwordURL = password.url!
|
var passwordURL = password.url!
|
||||||
|
|
@ -815,7 +718,7 @@ class PasswordStore {
|
||||||
guard let firstLocalCommit = localCommits.last,
|
guard let firstLocalCommit = localCommits.last,
|
||||||
firstLocalCommit.parents.count == 1,
|
firstLocalCommit.parents.count == 1,
|
||||||
let newHead = firstLocalCommit.parents.first else {
|
let newHead = firstLocalCommit.parents.first else {
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "Cannot decide how to reset."])
|
throw AppError.GitResetError
|
||||||
}
|
}
|
||||||
try self.storeRepository?.reset(to: newHead, resetType: GTRepositoryResetType.hard)
|
try self.storeRepository?.reset(to: newHead, resetType: GTRepositoryResetType.hard)
|
||||||
self.setAllSynced()
|
self.setAllSynced()
|
||||||
|
|
@ -843,14 +746,14 @@ class PasswordStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func getLocalCommits() throws -> [GTCommit]? {
|
private func getLocalCommits() throws -> [GTCommit]? {
|
||||||
// get the remote origin/master branch
|
guard let repository = storeRepository else {
|
||||||
guard let remoteBranches = try storeRepository?.remoteBranches(),
|
throw AppError.RepositoryNotSetError
|
||||||
let index = remoteBranches.index(where: { $0.shortName == "master" })
|
|
||||||
else {
|
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "Cannot find remote branch origin/master."])
|
|
||||||
}
|
}
|
||||||
let remoteMasterBranch = remoteBranches[index]
|
// get the remote origin/master branch
|
||||||
//print("remoteMasterBranch \(remoteMasterBranch)")
|
guard let index = try repository.remoteBranches().index(where: { $0.shortName == "master" }) else {
|
||||||
|
throw AppError.RepositoryRemoteMasterNotFoundError
|
||||||
|
}
|
||||||
|
let remoteMasterBranch = try repository.remoteBranches()[index]
|
||||||
|
|
||||||
// get a list of local commits
|
// get a list of local commits
|
||||||
return try storeRepository?.localCommitsRelative(toRemoteBranch: remoteMasterBranch)
|
return try storeRepository?.localCommitsRelative(toRemoteBranch: remoteMasterBranch)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue