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() {
|
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)
|
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: "Erase", style: UIAlertActionStyle.destructive, handler: { _ in
|
alert.addAction(UIAlertAction(title: "Overwrite", style: UIAlertActionStyle.destructive, handler: { _ in
|
||||||
// perform segue only after a successful clone
|
// perform segue only after a successful clone
|
||||||
self.cloneAndSegueIfSuccess()
|
self.cloneAndSegueIfSuccess()
|
||||||
}))
|
}))
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import ObjectiveGit
|
||||||
|
|
||||||
public struct GitCredential {
|
public struct GitCredential {
|
||||||
private var credential: Credential
|
private var credential: Credential
|
||||||
|
private let passwordStore = PasswordStore.shared
|
||||||
|
|
||||||
public enum Credential {
|
public enum Credential {
|
||||||
case http(userName: String)
|
case http(userName: String)
|
||||||
|
|
@ -31,11 +32,11 @@ public struct GitCredential {
|
||||||
|
|
||||||
switch self.credential {
|
switch self.credential {
|
||||||
case let .http(userName):
|
case let .http(userName):
|
||||||
var newPassword = Utils.getPasswordFromKeychain(name: "gitPassword")
|
var newPassword = self.passwordStore.gitPassword
|
||||||
if newPassword == nil || attempts != 0 {
|
if newPassword == nil || attempts != 0 {
|
||||||
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
|
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
|
||||||
newPassword = requestedPassword
|
newPassword = requestedPassword
|
||||||
Utils.addPasswordToKeychain(name: "gitPassword", password: newPassword)
|
self.passwordStore.gitPassword = newPassword
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -44,11 +45,12 @@ public struct GitCredential {
|
||||||
lastPassword = newPassword
|
lastPassword = newPassword
|
||||||
credential = try? GTCredential(userName: userName, password: newPassword!)
|
credential = try? GTCredential(userName: userName, password: newPassword!)
|
||||||
case let .ssh(userName, privateKeyFile):
|
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 newPassword == nil || attempts != 0 {
|
||||||
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
|
if let requestedPassword = requestGitPassword(self.credential, lastPassword) {
|
||||||
newPassword = requestedPassword
|
newPassword = requestedPassword
|
||||||
Utils.addPasswordToKeychain(name: "gitSSHKeyPassphrase", password: newPassword)
|
self.passwordStore.gitSSHPrivateKeyPassphrase = newPassword
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -56,7 +58,6 @@ public struct GitCredential {
|
||||||
attempts += 1
|
attempts += 1
|
||||||
lastPassword = newPassword
|
lastPassword = newPassword
|
||||||
credential = try? GTCredential(userName: userName, publicKeyURL: nil, privateKeyURL: privateKeyFile, passphrase: newPassword!)
|
credential = try? GTCredential(userName: userName, publicKeyURL: nil, privateKeyURL: privateKeyFile, passphrase: newPassword!)
|
||||||
print(privateKeyFile)
|
|
||||||
}
|
}
|
||||||
return credential
|
return credential
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -291,6 +291,8 @@ public 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)
|
||||||
|
self.gitPassword = nil
|
||||||
|
self.gitSSHPrivateKeyPassphrase = nil
|
||||||
do {
|
do {
|
||||||
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
|
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
|
||||||
let options = [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
|
let options = [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
|
||||||
|
|
@ -302,6 +304,11 @@ public class PasswordStore {
|
||||||
storeRepository = try GTRepository(url: storeURL)
|
storeRepository = try GTRepository(url: storeURL)
|
||||||
} catch {
|
} catch {
|
||||||
credential.delete()
|
credential.delete()
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
SharedDefaults[.lastSyncedTime] = nil
|
||||||
|
self.deleteCoreData(entityName: "PasswordEntity")
|
||||||
|
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
|
||||||
|
}
|
||||||
throw(error)
|
throw(error)
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue