Fix the logic of git clone
- erase git password and ssh passphrase before cloning - erase core data after a failed cloning
This commit is contained in:
parent
aa4ff7ce47
commit
6d118eab7e
3 changed files with 15 additions and 7 deletions
|
|
@ -176,8 +176,8 @@ class GitServerSettingTableViewController: UITableViewController {
|
|||
}
|
||||
|
||||
if passwordStore.repositoryExisted() {
|
||||
let alert = UIAlertController(title: "Erase Current Password Store Data?", message: "A cloned password store exists. This operation will erase all local data. Data on your remote server will not be affected.", preferredStyle: UIAlertControllerStyle.alert)
|
||||
alert.addAction(UIAlertAction(title: "Erase", style: UIAlertActionStyle.destructive, handler: { _ in
|
||||
let alert = UIAlertController(title: "Overwrite?", message: "This operation will overwrite your current password store data (repository). Data on your remote server will not be affected.", preferredStyle: UIAlertControllerStyle.alert)
|
||||
alert.addAction(UIAlertAction(title: "Overwrite", style: UIAlertActionStyle.destructive, handler: { _ in
|
||||
// perform segue only after a successful clone
|
||||
self.cloneAndSegueIfSuccess()
|
||||
}))
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import ObjectiveGit
|
|||
|
||||
public struct GitCredential {
|
||||
private var credential: Credential
|
||||
private let passwordStore = PasswordStore.shared
|
||||
|
||||
public enum Credential {
|
||||
case http(userName: String)
|
||||
|
|
@ -31,11 +32,11 @@ public struct GitCredential {
|
|||
|
||||
switch self.credential {
|
||||
case let .http(userName):
|
||||
var newPassword = Utils.getPasswordFromKeychain(name: "gitPassword")
|
||||
var newPassword = self.passwordStore.gitPassword
|
||||
if newPassword == nil || attempts != 0 {
|
||||
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
|
||||
newPassword = requestedPassword
|
||||
Utils.addPasswordToKeychain(name: "gitPassword", password: newPassword)
|
||||
self.passwordStore.gitPassword = newPassword
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -44,11 +45,12 @@ public struct GitCredential {
|
|||
lastPassword = newPassword
|
||||
credential = try? GTCredential(userName: userName, password: newPassword!)
|
||||
case let .ssh(userName, privateKeyFile):
|
||||
var newPassword = Utils.getPasswordFromKeychain(name: "gitSSHKeyPassphrase")
|
||||
// remarks: in fact, attempts > 1 never happens even with the wrong passphrase
|
||||
var newPassword = self.passwordStore.gitSSHPrivateKeyPassphrase
|
||||
if newPassword == nil || attempts != 0 {
|
||||
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
|
||||
newPassword = requestedPassword
|
||||
Utils.addPasswordToKeychain(name: "gitSSHKeyPassphrase", password: newPassword)
|
||||
self.passwordStore.gitSSHPrivateKeyPassphrase = newPassword
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -56,7 +58,6 @@ public struct GitCredential {
|
|||
attempts += 1
|
||||
lastPassword = newPassword
|
||||
credential = try? GTCredential(userName: userName, publicKeyURL: nil, privateKeyURL: privateKeyFile, passphrase: newPassword!)
|
||||
print(privateKeyFile)
|
||||
}
|
||||
return credential
|
||||
}
|
||||
|
|
|
|||
|
|
@ -291,6 +291,8 @@ public class PasswordStore {
|
|||
checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws {
|
||||
Utils.removeFileIfExists(at: storeURL)
|
||||
Utils.removeFileIfExists(at: tempStoreURL)
|
||||
self.gitPassword = nil
|
||||
self.gitSSHPrivateKeyPassphrase = nil
|
||||
do {
|
||||
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
|
||||
let options = [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
|
||||
|
|
@ -302,6 +304,11 @@ public class PasswordStore {
|
|||
storeRepository = try GTRepository(url: storeURL)
|
||||
} catch {
|
||||
credential.delete()
|
||||
DispatchQueue.main.async {
|
||||
SharedDefaults[.lastSyncedTime] = nil
|
||||
self.deleteCoreData(entityName: "PasswordEntity")
|
||||
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
|
||||
}
|
||||
throw(error)
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue