Polish credential related logic
This commit is contained in:
parent
649d709369
commit
1311962dc1
7 changed files with 140 additions and 213 deletions
|
|
@ -21,28 +21,10 @@ class GitSSHKeyArmorSettingTableViewController: UITableViewController, UITextVie
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
armorPublicKeyTextView.text = Defaults[.gitSSHPublicKeyArmor]
|
armorPublicKeyTextView.text = Defaults[.gitSSHPublicKeyArmor]
|
||||||
armorPrivateKeyTextView.text = Defaults[.gitSSHPrivateKeyArmor]
|
armorPrivateKeyTextView.text = Defaults[.gitSSHPrivateKeyArmor]
|
||||||
gitSSHPrivateKeyPassphrase = passwordStore.gitSSHPrivateKeyPassphrase
|
|
||||||
|
|
||||||
armorPublicKeyTextView.delegate = self
|
armorPublicKeyTextView.delegate = self
|
||||||
armorPrivateKeyTextView.delegate = self
|
armorPrivateKeyTextView.delegate = self
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createSavePassphraseAlert() -> UIAlertController {
|
|
||||||
let savePassphraseAlert = UIAlertController(title: "Passphrase", message: "Do you want to save the passphrase for later sync?", preferredStyle: UIAlertControllerStyle.alert)
|
|
||||||
savePassphraseAlert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default) { _ in
|
|
||||||
Defaults[.isRememberPassphraseOn] = false
|
|
||||||
Defaults[.gitSSHKeySource] = "armor"
|
|
||||||
self.navigationController!.popViewController(animated: true)
|
|
||||||
})
|
|
||||||
savePassphraseAlert.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.destructive) {_ in
|
|
||||||
Defaults[.isRememberPassphraseOn] = true
|
|
||||||
self.passwordStore.gitSSHPrivateKeyPassphrase = self.gitSSHPrivateKeyPassphrase
|
|
||||||
Defaults[.gitSSHKeySource] = "armor"
|
|
||||||
self.navigationController!.popViewController(animated: true)
|
|
||||||
})
|
|
||||||
return savePassphraseAlert
|
|
||||||
}
|
|
||||||
|
|
||||||
@IBAction func doneButtonTapped(_ sender: Any) {
|
@IBAction func doneButtonTapped(_ sender: Any) {
|
||||||
Defaults[.gitSSHPublicKeyArmor] = armorPublicKeyTextView.text
|
Defaults[.gitSSHPublicKeyArmor] = armorPublicKeyTextView.text
|
||||||
Defaults[.gitSSHPrivateKeyArmor] = armorPrivateKeyTextView.text
|
Defaults[.gitSSHPrivateKeyArmor] = armorPrivateKeyTextView.text
|
||||||
|
|
@ -52,22 +34,13 @@ class GitSSHKeyArmorSettingTableViewController: UITableViewController, UITextVie
|
||||||
} catch {
|
} catch {
|
||||||
Utils.alert(title: "Cannot Save", message: "Cannot Save SSH Key", controller: self, completion: nil)
|
Utils.alert(title: "Cannot Save", message: "Cannot Save SSH Key", controller: self, completion: nil)
|
||||||
}
|
}
|
||||||
let alert = UIAlertController(title: "Passphrase", message: "Please fill in the passphrase of your SSH secret key.", preferredStyle: UIAlertControllerStyle.alert)
|
Defaults[.gitSSHKeySource] = "armor"
|
||||||
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
|
self.navigationController!.popViewController(animated: true)
|
||||||
self.gitSSHPrivateKeyPassphrase = alert.textFields?.first?.text
|
|
||||||
let savePassphraseAlert = self.createSavePassphraseAlert()
|
|
||||||
self.present(savePassphraseAlert, animated: true, completion: nil)
|
|
||||||
}))
|
|
||||||
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
|
||||||
textField.text = self.gitSSHPrivateKeyPassphrase
|
|
||||||
textField.isSecureTextEntry = true
|
|
||||||
})
|
|
||||||
self.present(alert, animated: true, completion: nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
||||||
if text == UIPasteboard.general.string {
|
if text == UIPasteboard.general.string {
|
||||||
// user pastes somethint, get ready to clear in 10s
|
// user pastes something, get ready to clear in 10s
|
||||||
recentPastedText = text
|
recentPastedText = text
|
||||||
DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now() + 10) { [weak weakSelf = self] in
|
DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now() + 10) { [weak weakSelf = self] in
|
||||||
if let pasteboardString = UIPasteboard.general.string,
|
if let pasteboardString = UIPasteboard.general.string,
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@ class GitServerSettingTableViewController: UITableViewController {
|
||||||
@IBOutlet weak var authSSHKeyCell: UITableViewCell!
|
@IBOutlet weak var authSSHKeyCell: UITableViewCell!
|
||||||
@IBOutlet weak var authPasswordCell: UITableViewCell!
|
@IBOutlet weak var authPasswordCell: UITableViewCell!
|
||||||
let passwordStore = PasswordStore.shared
|
let passwordStore = PasswordStore.shared
|
||||||
var password: String?
|
var sshLabel: UILabel? = nil
|
||||||
|
|
||||||
var authenticationMethod = Defaults[.gitAuthenticationMethod]
|
var authenticationMethod = Defaults[.gitAuthenticationMethod]
|
||||||
|
|
||||||
private func checkAuthenticationMethod(method: String) {
|
private func checkAuthenticationMethod(method: String) {
|
||||||
|
|
@ -37,24 +37,22 @@ class GitServerSettingTableViewController: UITableViewController {
|
||||||
sshKeyCheckView.isHidden = true
|
sshKeyCheckView.isHidden = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override func viewWillAppear(_ animated: Bool) {
|
||||||
|
super.viewWillAppear(animated)
|
||||||
|
// Grey out ssh option if ssh_key and ssh_key.pub are not present
|
||||||
|
sshLabel = authSSHKeyCell.subviews[0].subviews[0] as? UILabel
|
||||||
|
sshLabel!.isEnabled = gitSSHKeyExists()
|
||||||
|
|
||||||
|
}
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
if let url = Defaults[.gitURL] {
|
if let url = Defaults[.gitURL] {
|
||||||
gitURLTextField.text = url.absoluteString
|
gitURLTextField.text = url.absoluteString
|
||||||
}
|
}
|
||||||
usernameTextField.text = Defaults[.gitUsername]
|
usernameTextField.text = Defaults[.gitUsername]
|
||||||
password = passwordStore.gitPassword
|
|
||||||
authenticationMethod = Defaults[.gitAuthenticationMethod]
|
authenticationMethod = Defaults[.gitAuthenticationMethod]
|
||||||
|
|
||||||
// Grey out ssh option if ssh_key and ssh_key.pub are not present
|
|
||||||
let sshLabel = authSSHKeyCell.subviews[0].subviews[0] as! UILabel
|
|
||||||
|
|
||||||
sshLabel.isEnabled = gitSSHKeyExists()
|
|
||||||
|
|
||||||
if authenticationMethod == nil || !sshLabel.isEnabled {
|
|
||||||
authenticationMethod = "Password"
|
|
||||||
}
|
|
||||||
|
|
||||||
checkAuthenticationMethod(method: authenticationMethod!)
|
checkAuthenticationMethod(method: authenticationMethod!)
|
||||||
authSSHKeyCell.accessoryType = .detailButton
|
authSSHKeyCell.accessoryType = .detailButton
|
||||||
}
|
}
|
||||||
|
|
@ -108,24 +106,8 @@ class GitServerSettingTableViewController: UITableViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func doClone() {
|
private func doClone() {
|
||||||
if authenticationMethod == "Password" {
|
if self.shouldPerformSegue(withIdentifier: "saveGitServerSettingSegue", sender: self) {
|
||||||
let alert = UIAlertController(title: "Password", message: "Please fill in the password of your Git account.", preferredStyle: UIAlertControllerStyle.alert)
|
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
|
||||||
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
|
|
||||||
self.password = alert.textFields!.first!.text
|
|
||||||
self.passwordStore.gitPassword = self.password
|
|
||||||
if self.shouldPerformSegue(withIdentifier: "saveGitServerSettingSegue", sender: self) {
|
|
||||||
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
|
||||||
textField.text = self.password
|
|
||||||
textField.isSecureTextEntry = true
|
|
||||||
})
|
|
||||||
self.present(alert, animated: true, completion: nil)
|
|
||||||
} else {
|
|
||||||
if self.shouldPerformSegue(withIdentifier: "saveGitServerSettingSegue", sender: self) {
|
|
||||||
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -172,30 +154,8 @@ class GitServerSettingTableViewController: UITableViewController {
|
||||||
|
|
||||||
if (gitSSHKeyExists()) {
|
if (gitSSHKeyExists()) {
|
||||||
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
|
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
|
||||||
let alert = UIAlertController(
|
Defaults[.gitSSHKeySource] = "file"
|
||||||
title: "SSH Key Passphrase",
|
|
||||||
message: "Please fill in the passphrase for your Git Repository SSH key.",
|
|
||||||
preferredStyle: UIAlertControllerStyle.alert
|
|
||||||
)
|
|
||||||
|
|
||||||
alert.addAction(
|
|
||||||
UIAlertAction(
|
|
||||||
title: "OK",
|
|
||||||
style: UIAlertActionStyle.default,
|
|
||||||
handler: {_ in
|
|
||||||
self.passwordStore.gitSSHPrivateKeyPassphrase = alert.textFields!.first!.text!
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
alert.addTextField(
|
|
||||||
configurationHandler: {(textField: UITextField!) in
|
|
||||||
textField.text = self.passwordStore.gitSSHPrivateKeyPassphrase
|
|
||||||
textField.isSecureTextEntry = true
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Defaults[.gitSSHKeySource] = "file"
|
|
||||||
optionMenu.addAction(fileAction)
|
optionMenu.addAction(fileAction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -203,6 +163,7 @@ class GitServerSettingTableViewController: UITableViewController {
|
||||||
let deleteAction = UIAlertAction(title: "Remove Git SSH Keys", style: .destructive) { _ in
|
let deleteAction = UIAlertAction(title: "Remove Git SSH Keys", style: .destructive) { _ in
|
||||||
Utils.removeGitSSHKeys()
|
Utils.removeGitSSHKeys()
|
||||||
Defaults[.gitSSHKeySource] = nil
|
Defaults[.gitSSHKeySource] = nil
|
||||||
|
self.sshLabel!.isEnabled = false
|
||||||
}
|
}
|
||||||
optionMenu.addAction(deleteAction)
|
optionMenu.addAction(deleteAction)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -128,19 +128,38 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
}
|
}
|
||||||
|
|
||||||
private func syncPasswords() {
|
private func syncPasswords() {
|
||||||
|
guard passwordStore.repositoryExisted() else {
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(800)) {
|
||||||
|
Utils.alert(title: "Error", message: "There is no password store right now.", controller: self, completion: nil)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
SVProgressHUD.setDefaultMaskType(.black)
|
SVProgressHUD.setDefaultMaskType(.black)
|
||||||
SVProgressHUD.setDefaultStyle(.light)
|
SVProgressHUD.setDefaultStyle(.light)
|
||||||
SVProgressHUD.show(withStatus: "Sync Password Store")
|
SVProgressHUD.show(withStatus: "Sync Password Store")
|
||||||
let numberOfLocalCommits = self.passwordStore.numberOfLocalCommits()
|
let numberOfLocalCommits = self.passwordStore.numberOfLocalCommits()
|
||||||
|
var gitCredential: GitCredential
|
||||||
|
if Defaults[.gitAuthenticationMethod] == "Password" {
|
||||||
|
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: Defaults[.gitUsername]!, controller: self))
|
||||||
|
} else {
|
||||||
|
gitCredential = GitCredential(
|
||||||
|
credential: GitCredential.Credential.ssh(
|
||||||
|
userName: Defaults[.gitUsername]!,
|
||||||
|
publicKeyFile: Globals.gitSSHPublicKeyURL,
|
||||||
|
privateKeyFile: Globals.gitSSHPrivateKeyURL,
|
||||||
|
controller: self
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
|
||||||
do {
|
do {
|
||||||
try self.passwordStore.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in
|
try self.passwordStore.pullRepository(credential: gitCredential, transferProgressBlock: {(git_transfer_progress, stop) in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
|
SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if numberOfLocalCommits > 0 {
|
if numberOfLocalCommits > 0 {
|
||||||
try self.passwordStore.pushRepository(transferProgressBlock: {(current, total, bytes, stop) in
|
try self.passwordStore.pushRepository(credential: gitCredential, transferProgressBlock: {(current, total, bytes, stop) in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
SVProgressHUD.showProgress(Float(current)/Float(total), status: "Push Remote Repository")
|
SVProgressHUD.showProgress(Float(current)/Float(total), status: "Push Remote Repository")
|
||||||
}
|
}
|
||||||
|
|
@ -148,7 +167,6 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.reloadTableView(parent: nil)
|
self.reloadTableView(parent: nil)
|
||||||
Defaults[.gitPasswordAttempts] = 0
|
|
||||||
SVProgressHUD.showSuccess(withStatus: "Done")
|
SVProgressHUD.showSuccess(withStatus: "Done")
|
||||||
SVProgressHUD.dismiss(withDelay: 1)
|
SVProgressHUD.dismiss(withDelay: 1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,32 +43,7 @@ class SSHKeySettingTableViewController: UITableViewController {
|
||||||
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
|
Utils.alert(title: "Error", message: error.localizedDescription, controller: self, completion: nil)
|
||||||
}
|
}
|
||||||
Defaults[.gitSSHKeySource] = "url"
|
Defaults[.gitSSHKeySource] = "url"
|
||||||
let alert = UIAlertController(
|
self.navigationController!.popViewController(animated: true)
|
||||||
title: "PGP Passphrase",
|
|
||||||
message: "Please fill in the passphrase for your Git Repository SSH key.",
|
|
||||||
preferredStyle: UIAlertControllerStyle.alert
|
|
||||||
)
|
|
||||||
|
|
||||||
alert.addAction(
|
|
||||||
UIAlertAction(
|
|
||||||
title: "OK",
|
|
||||||
style: UIAlertActionStyle.default,
|
|
||||||
handler: {_ in
|
|
||||||
Utils.addPasswordToKeychain(
|
|
||||||
name: "gitSSHPrivateKeyPassphrase",
|
|
||||||
password: alert.textFields!.first!.text!
|
|
||||||
)
|
|
||||||
self.navigationController!.popViewController(animated: true)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
alert.addTextField(
|
|
||||||
configurationHandler: {(textField: UITextField!) in
|
|
||||||
textField.text = self.passwordStore.gitSSHPrivateKeyPassphrase
|
|
||||||
textField.isSecureTextEntry = true
|
|
||||||
})
|
|
||||||
self.present(alert, animated: true, completion: nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,6 @@ class SettingsTableViewController: UITableViewController {
|
||||||
if let controller = segue.source as? GitServerSettingTableViewController {
|
if let controller = segue.source as? GitServerSettingTableViewController {
|
||||||
let gitRepostiroyURL = controller.gitURLTextField.text!
|
let gitRepostiroyURL = controller.gitURLTextField.text!
|
||||||
let username = controller.usernameTextField.text!
|
let username = controller.usernameTextField.text!
|
||||||
let password = controller.password
|
|
||||||
let auth = controller.authenticationMethod
|
let auth = controller.authenticationMethod
|
||||||
|
|
||||||
SVProgressHUD.setDefaultMaskType(.black)
|
SVProgressHUD.setDefaultMaskType(.black)
|
||||||
|
|
@ -106,15 +105,14 @@ class SettingsTableViewController: UITableViewController {
|
||||||
SVProgressHUD.show(withStatus: "Prepare Repository")
|
SVProgressHUD.show(withStatus: "Prepare Repository")
|
||||||
var gitCredential: GitCredential
|
var gitCredential: GitCredential
|
||||||
if auth == "Password" {
|
if auth == "Password" {
|
||||||
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password!, requestGitPassword: requestGitPassword))
|
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, controller: self))
|
||||||
} else {
|
} else {
|
||||||
gitCredential = GitCredential(
|
gitCredential = GitCredential(
|
||||||
credential: GitCredential.Credential.ssh(
|
credential: GitCredential.Credential.ssh(
|
||||||
userName: username,
|
userName: username,
|
||||||
password: Utils.getPasswordFromKeychain(name: "gitSSHPrivateKeyPassphrase") ?? "",
|
|
||||||
publicKeyFile: Globals.gitSSHPublicKeyURL,
|
publicKeyFile: Globals.gitSSHPublicKeyURL,
|
||||||
privateKeyFile: Globals.gitSSHPrivateKeyURL,
|
privateKeyFile: Globals.gitSSHPrivateKeyURL,
|
||||||
requestSSHKeyPassword: self.requestGitPassword
|
controller: self
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -137,7 +135,6 @@ class SettingsTableViewController: UITableViewController {
|
||||||
Defaults[.gitURL] = URL(string: gitRepostiroyURL)
|
Defaults[.gitURL] = URL(string: gitRepostiroyURL)
|
||||||
Defaults[.gitUsername] = username
|
Defaults[.gitUsername] = username
|
||||||
Defaults[.gitAuthenticationMethod] = auth
|
Defaults[.gitAuthenticationMethod] = auth
|
||||||
Defaults[.gitPasswordAttempts] = 0
|
|
||||||
self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitURL]?.host
|
self.passwordRepositoryTableViewCell.detailTextLabel?.text = Defaults[.gitURL]?.host
|
||||||
SVProgressHUD.showSuccess(withStatus: "Done")
|
SVProgressHUD.showSuccess(withStatus: "Done")
|
||||||
SVProgressHUD.dismiss(withDelay: 1)
|
SVProgressHUD.dismiss(withDelay: 1)
|
||||||
|
|
@ -196,32 +193,6 @@ class SettingsTableViewController: UITableViewController {
|
||||||
touchIDSwitch.isOn = false
|
touchIDSwitch.isOn = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func requestGitPassword(message: String) -> String? {
|
|
||||||
let sem = DispatchSemaphore(value: 0)
|
|
||||||
var password: String?
|
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
SVProgressHUD.dismiss()
|
|
||||||
let alert = UIAlertController(title: "Password", message: message, preferredStyle: UIAlertControllerStyle.alert)
|
|
||||||
alert.addTextField(configurationHandler: {(textField: UITextField!) in
|
|
||||||
textField.text = self.passwordStore.gitPassword
|
|
||||||
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()
|
|
||||||
})
|
|
||||||
self.present(alert, animated: true, completion: nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = sem.wait(timeout: .distantFuture)
|
|
||||||
return password
|
|
||||||
}
|
|
||||||
|
|
||||||
func actOnPasswordStoreErasedNotification() {
|
func actOnPasswordStoreErasedNotification() {
|
||||||
setPGPKeyTableViewCellDetailText()
|
setPGPKeyTableViewCellDetailText()
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ extension DefaultsKeys {
|
||||||
static let gitURL = DefaultsKey<URL?>("gitURL")
|
static let gitURL = DefaultsKey<URL?>("gitURL")
|
||||||
static let gitAuthenticationMethod = DefaultsKey<String?>("gitAuthenticationMethod")
|
static let gitAuthenticationMethod = DefaultsKey<String?>("gitAuthenticationMethod")
|
||||||
static let gitUsername = DefaultsKey<String?>("gitUsername")
|
static let gitUsername = DefaultsKey<String?>("gitUsername")
|
||||||
static let gitPasswordAttempts = DefaultsKey<Int>("gitPasswordAttempts")
|
|
||||||
static let gitSSHPublicKeyURL = DefaultsKey<URL?>("gitSSHPublicKeyURL")
|
static let gitSSHPublicKeyURL = DefaultsKey<URL?>("gitSSHPublicKeyURL")
|
||||||
static let gitSSHPrivateKeyURL = DefaultsKey<URL?>("gitSSHPrivateKeyURL")
|
static let gitSSHPrivateKeyURL = DefaultsKey<URL?>("gitSSHPrivateKeyURL")
|
||||||
static let gitSSHKeySource = DefaultsKey<String?>("gitSSHKeySource")
|
static let gitSSHKeySource = DefaultsKey<String?>("gitSSHKeySource")
|
||||||
|
|
|
||||||
|
|
@ -17,50 +17,91 @@ struct GitCredential {
|
||||||
var credential: Credential
|
var credential: Credential
|
||||||
|
|
||||||
enum Credential {
|
enum Credential {
|
||||||
case http(userName: String, password: String, requestGitPassword: ((_ message: String) -> String?)?)
|
case http(userName: String, controller: UIViewController)
|
||||||
case ssh(userName: String, password: String, publicKeyFile: URL, privateKeyFile: URL, requestSSHKeyPassword: ((_ message: String) -> String?)? )
|
case ssh(userName: String, publicKeyFile: URL, privateKeyFile: URL, controller: UIViewController)
|
||||||
}
|
}
|
||||||
|
|
||||||
init(credential: Credential) {
|
init(credential: Credential) {
|
||||||
self.credential = credential
|
self.credential = credential
|
||||||
Defaults[.gitPasswordAttempts] = 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func credentialProvider() throws -> GTCredentialProvider {
|
func credentialProvider() throws -> GTCredentialProvider {
|
||||||
|
var attempts = 0
|
||||||
|
var lastPassword: String? = nil
|
||||||
return GTCredentialProvider { (_, _, _) -> (GTCredential?) in
|
return GTCredentialProvider { (_, _, _) -> (GTCredential?) in
|
||||||
var credential: GTCredential? = nil
|
var credential: GTCredential? = nil
|
||||||
|
|
||||||
switch self.credential {
|
switch self.credential {
|
||||||
case let .http(userName, password, requestGitPassword):
|
case let .http(userName, controller):
|
||||||
var newPassword = password
|
var newPassword = Utils.getPasswordFromKeychain(name: "gitPassword")
|
||||||
if Defaults[.gitPasswordAttempts] != 0 {
|
if newPassword == nil || attempts != 0 {
|
||||||
if let requestGitPasswordCallback = requestGitPassword,
|
if let requestedPassword = self.requestGitPassword(controller, lastPassword) {
|
||||||
let requestedPassword = requestGitPasswordCallback("Please fill in the password of your Git account.") {
|
|
||||||
newPassword = requestedPassword
|
newPassword = requestedPassword
|
||||||
|
Utils.addPasswordToKeychain(name: "gitPassword", password: newPassword)
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Defaults[.gitPasswordAttempts] += 1
|
attempts += 1
|
||||||
credential = try? GTCredential(userName: userName, password: newPassword)
|
lastPassword = newPassword
|
||||||
case let .ssh(userName, password, publicKeyFile, privateKeyFile, requestSSHKeyPassword):
|
credential = try? GTCredential(userName: userName, password: newPassword!)
|
||||||
var newPassword = password
|
case let .ssh(userName, publicKeyFile, privateKeyFile, controller):
|
||||||
if Defaults[.gitPasswordAttempts] != 0 {
|
var newPassword = Utils.getPasswordFromKeychain(name: "gitSSHKeyPassphrase")
|
||||||
if let requestSSHKeyPasswordCallback = requestSSHKeyPassword,
|
if newPassword == nil {
|
||||||
let requestedPassword = requestSSHKeyPasswordCallback("Please fill in the password of your SSH key.") {
|
if let requestedPassword = self.requestGitPassword(controller, nil) {
|
||||||
newPassword = requestedPassword
|
newPassword = requestedPassword
|
||||||
|
Utils.addPasswordToKeychain(name: "gitSSHKeyPassphrase", password: newPassword)
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Defaults[.gitPasswordAttempts] += 1
|
credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: newPassword!)
|
||||||
credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: newPassword)
|
|
||||||
}
|
}
|
||||||
return credential
|
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 {
|
||||||
|
|
@ -69,7 +110,6 @@ class PasswordStore {
|
||||||
let tempStoreURL = URL(fileURLWithPath: "\(Globals.repositoryPath)-temp")
|
let tempStoreURL = URL(fileURLWithPath: "\(Globals.repositoryPath)-temp")
|
||||||
|
|
||||||
var storeRepository: GTRepository?
|
var storeRepository: GTRepository?
|
||||||
var gitCredential: GitCredential?
|
|
||||||
var pgpKeyID: String?
|
var pgpKeyID: String?
|
||||||
var publicKey: PGPKey? {
|
var publicKey: PGPKey? {
|
||||||
didSet {
|
didSet {
|
||||||
|
|
@ -100,6 +140,7 @@ class PasswordStore {
|
||||||
return Utils.getPasswordFromKeychain(name: "pgpKeyPassphrase")
|
return Utils.getPasswordFromKeychain(name: "pgpKeyPassphrase")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var gitPassword: String? {
|
var gitPassword: String? {
|
||||||
set {
|
set {
|
||||||
Utils.addPasswordToKeychain(name: "gitPassword", password: newValue)
|
Utils.addPasswordToKeychain(name: "gitPassword", password: newValue)
|
||||||
|
|
@ -114,7 +155,7 @@ class PasswordStore {
|
||||||
Utils.addPasswordToKeychain(name: "gitSSHPrivateKeyPassphrase", password: newValue)
|
Utils.addPasswordToKeychain(name: "gitSSHPrivateKeyPassphrase", password: newValue)
|
||||||
}
|
}
|
||||||
get {
|
get {
|
||||||
return Utils.getPasswordFromKeychain(name: "gitSSHPrivateKeyPassphrase") ?? ""
|
return Utils.getPasswordFromKeychain(name: "gitSSHPrivateKeyPassphrase")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,32 +188,12 @@ class PasswordStore {
|
||||||
print(error)
|
print(error)
|
||||||
}
|
}
|
||||||
initPGPKeys()
|
initPGPKeys()
|
||||||
initGitCredential()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum SSHKeyType {
|
enum SSHKeyType {
|
||||||
case `public`, secret
|
case `public`, secret
|
||||||
}
|
}
|
||||||
|
|
||||||
public func initGitCredential() {
|
|
||||||
if Defaults[.gitAuthenticationMethod] == "Password" {
|
|
||||||
let httpCredential = GitCredential.Credential.http(userName: Defaults[.gitUsername] ?? "", password: Utils.getPasswordFromKeychain(name: "gitPassword") ?? "", requestGitPassword: nil)
|
|
||||||
gitCredential = GitCredential(credential: httpCredential)
|
|
||||||
} else if Defaults[.gitAuthenticationMethod] == "SSH Key"{
|
|
||||||
gitCredential = GitCredential(
|
|
||||||
credential: GitCredential.Credential.ssh(
|
|
||||||
userName: Defaults[.gitUsername] ?? "",
|
|
||||||
password: gitSSHPrivateKeyPassphrase ?? "",
|
|
||||||
publicKeyFile: Globals.gitSSHPublicKeyURL,
|
|
||||||
privateKeyFile: Globals.gitSSHPrivateKeyURL,
|
|
||||||
requestSSHKeyPassword: nil
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
gitCredential = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public func initGitSSHKey(with armorKey: String, _ keyType: SSHKeyType) throws {
|
public func initGitSSHKey(with armorKey: String, _ keyType: SSHKeyType) throws {
|
||||||
var keyPath = ""
|
var keyPath = ""
|
||||||
switch keyType {
|
switch keyType {
|
||||||
|
|
@ -304,43 +325,47 @@ class PasswordStore {
|
||||||
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
|
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
|
||||||
Utils.removeFileIfExists(at: storeURL)
|
Utils.removeFileIfExists(at: storeURL)
|
||||||
Utils.removeFileIfExists(at: tempStoreURL)
|
Utils.removeFileIfExists(at: tempStoreURL)
|
||||||
|
|
||||||
let credentialProvider = try credential.credentialProvider()
|
|
||||||
let options: [String: Any] = [
|
|
||||||
GTRepositoryCloneOptionsCredentialProvider: credentialProvider,
|
|
||||||
]
|
|
||||||
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock)
|
|
||||||
let fm = FileManager.default
|
|
||||||
do {
|
do {
|
||||||
|
let credentialProvider = try credential.credentialProvider()
|
||||||
|
let options: [String: Any] = [
|
||||||
|
GTRepositoryCloneOptionsCredentialProvider: credentialProvider,
|
||||||
|
]
|
||||||
|
storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: tempStoreURL, options: options, transferProgressBlock:transferProgressBlock)
|
||||||
|
let fm = FileManager.default
|
||||||
if fm.fileExists(atPath: storeURL.path) {
|
if fm.fileExists(atPath: storeURL.path) {
|
||||||
try fm.removeItem(at: storeURL)
|
try fm.removeItem(at: storeURL)
|
||||||
}
|
}
|
||||||
try fm.copyItem(at: tempStoreURL, to: storeURL)
|
try fm.copyItem(at: tempStoreURL, to: storeURL)
|
||||||
try fm.removeItem(at: tempStoreURL)
|
try fm.removeItem(at: tempStoreURL)
|
||||||
|
storeRepository = try GTRepository(url: storeURL)
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
credential.delete()
|
||||||
|
throw(error)
|
||||||
}
|
}
|
||||||
storeRepository = try GTRepository(url: storeURL)
|
|
||||||
gitCredential = credential
|
|
||||||
Defaults[.lastSyncedTime] = Date()
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
Defaults[.lastSyncedTime] = Date()
|
||||||
self.updatePasswordEntityCoreData()
|
self.updatePasswordEntityCoreData()
|
||||||
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
|
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pullRepository(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 gitCredential == nil {
|
if storeRepository == nil {
|
||||||
throw NSError(domain: "me.mssun.pass.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "Git Repository is not set."])
|
throw NSError(domain: "me.mssun.pass.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "Git Repository is not set."])
|
||||||
}
|
}
|
||||||
let credentialProvider = try gitCredential!.credentialProvider()
|
do {
|
||||||
let options: [String: Any] = [
|
let credentialProvider = try credential.credentialProvider()
|
||||||
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider
|
let options: [String: Any] = [
|
||||||
]
|
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider
|
||||||
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
]
|
||||||
try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock)
|
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
||||||
Defaults[.lastSyncedTime] = Date()
|
try storeRepository!.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock)
|
||||||
|
} catch {
|
||||||
|
credential.delete()
|
||||||
|
throw(error)
|
||||||
|
}
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
Defaults[.lastSyncedTime] = Date()
|
||||||
self.setAllSynced()
|
self.setAllSynced()
|
||||||
self.updatePasswordEntityCoreData()
|
self.updatePasswordEntityCoreData()
|
||||||
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
|
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
|
||||||
|
|
@ -583,14 +608,19 @@ class PasswordStore {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pushRepository(transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
func pushRepository(credential: GitCredential, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
||||||
let credentialProvider = try gitCredential!.credentialProvider()
|
do {
|
||||||
let options: [String: Any] = [
|
let credentialProvider = try credential.credentialProvider()
|
||||||
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider,
|
let options: [String: Any] = [
|
||||||
]
|
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider,
|
||||||
let masterBranch = getLocalBranch(withName: "master")!
|
]
|
||||||
let remote = try GTRemote(name: "origin", in: storeRepository!)
|
let masterBranch = getLocalBranch(withName: "master")!
|
||||||
try storeRepository?.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 {
|
||||||
|
credential.delete()
|
||||||
|
throw(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
|
private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue