Refactor GitCredential to simplify it and to add tests
This commit is contained in:
parent
56b7b24fce
commit
6044098278
11 changed files with 295 additions and 225 deletions
|
|
@ -6,75 +6,103 @@
|
|||
// Copyright © 2017 Bob Sun. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ObjectiveGit
|
||||
import SVProgressHUD
|
||||
|
||||
public struct GitCredential {
|
||||
private var credential: Credential
|
||||
private let passwordStore = PasswordStore.shared
|
||||
public typealias PasswordProvider = (String, String?) -> String?
|
||||
|
||||
public enum Credential {
|
||||
private let credentialType: CredentialType
|
||||
private let keyStore: KeyStore
|
||||
|
||||
private enum CredentialType {
|
||||
case http(userName: String)
|
||||
case ssh(userName: String, privateKey: String)
|
||||
}
|
||||
|
||||
public init(credential: Credential) {
|
||||
self.credential = credential
|
||||
}
|
||||
|
||||
public func credentialProvider(requestCredentialPassword: @escaping (Credential, String?) -> String?) throws -> GTCredentialProvider {
|
||||
var attempts = 0
|
||||
return GTCredentialProvider { _, _, _ -> (GTCredential?) in
|
||||
var credential: GTCredential?
|
||||
|
||||
switch self.credential {
|
||||
case let .http(userName):
|
||||
if attempts > 3 {
|
||||
// After too many failures (say six), the error message "failed to authenticate ssh session" might be confusing.
|
||||
return nil
|
||||
}
|
||||
var lastPassword = self.passwordStore.gitPassword
|
||||
if lastPassword == nil || attempts != 0 {
|
||||
if let requestedPassword = requestCredentialPassword(self.credential, lastPassword) {
|
||||
if Defaults.isRememberGitCredentialPassphraseOn {
|
||||
self.passwordStore.gitPassword = requestedPassword
|
||||
}
|
||||
lastPassword = requestedPassword
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
attempts += 1
|
||||
credential = try? GTCredential(userName: userName, password: lastPassword!)
|
||||
case let .ssh(userName, privateKey):
|
||||
if attempts > 0 {
|
||||
// The passphrase seems correct, but the previous authentification failed.
|
||||
return nil
|
||||
}
|
||||
var lastPassword = self.passwordStore.gitSSHPrivateKeyPassphrase
|
||||
if lastPassword == nil || attempts != 0 {
|
||||
if let requestedPassword = requestCredentialPassword(self.credential, lastPassword) {
|
||||
if Defaults.isRememberGitCredentialPassphraseOn {
|
||||
self.passwordStore.gitSSHPrivateKeyPassphrase = requestedPassword
|
||||
}
|
||||
lastPassword = requestedPassword
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
attempts += 1
|
||||
credential = try? GTCredential(userName: userName, publicKeyString: nil, privateKeyString: privateKey, passphrase: lastPassword!)
|
||||
var requestPassphraseMessage: String {
|
||||
switch self {
|
||||
case .http:
|
||||
return "FillInGitAccountPassword.".localize()
|
||||
case .ssh:
|
||||
return "FillInSshKeyPassphrase.".localize()
|
||||
}
|
||||
return credential
|
||||
}
|
||||
|
||||
var keyStoreKey: String {
|
||||
switch self {
|
||||
case .http:
|
||||
return Globals.gitPassword
|
||||
case .ssh:
|
||||
return Globals.gitSSHPrivateKeyPassphrase
|
||||
}
|
||||
}
|
||||
|
||||
var allowedAttempts: Int {
|
||||
switch self {
|
||||
case .http:
|
||||
return 4
|
||||
case .ssh:
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func createGTCredential(password: String) throws -> GTCredential {
|
||||
switch self {
|
||||
case let .http(userName):
|
||||
return try GTCredential(userName: userName, password: password)
|
||||
case let .ssh(userName, privateKey):
|
||||
return try GTCredential(userName: userName, publicKeyString: nil, privateKeyString: privateKey, passphrase: password)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static func from(authenticationMethod: GitAuthenticationMethod, userName: String, keyStore: KeyStore) -> Self {
|
||||
switch authenticationMethod {
|
||||
case .password:
|
||||
return Self(credentialType: .http(userName: userName), keyStore: keyStore)
|
||||
case .key:
|
||||
let privateKey: String = keyStore.get(for: SshKey.PRIVATE.getKeychainKey()) ?? ""
|
||||
return Self(credentialType: .ssh(userName: userName, privateKey: privateKey), keyStore: keyStore)
|
||||
}
|
||||
}
|
||||
|
||||
public func getCredentialOptions(passwordProvider: @escaping PasswordProvider = { _, _ in nil }) -> [String: Any] {
|
||||
let credentialProvider = createCredentialProvider(passwordProvider)
|
||||
return [
|
||||
GTRepositoryCloneOptionsCredentialProvider: credentialProvider,
|
||||
GTRepositoryRemoteOptionsCredentialProvider: credentialProvider,
|
||||
]
|
||||
}
|
||||
|
||||
private func createCredentialProvider(_ passwordProvider: @escaping PasswordProvider) -> GTCredentialProvider {
|
||||
var attempts = 1
|
||||
return GTCredentialProvider { _, _, _ -> GTCredential? in
|
||||
if attempts > self.credentialType.allowedAttempts {
|
||||
return nil
|
||||
}
|
||||
guard let password = self.getPassword(attempts: attempts, passwordProvider: passwordProvider) else {
|
||||
return nil
|
||||
}
|
||||
attempts += 1
|
||||
return try? self.credentialType.createGTCredential(password: password)
|
||||
}
|
||||
}
|
||||
|
||||
public func delete() {
|
||||
switch credential {
|
||||
case .http:
|
||||
passwordStore.gitPassword = nil
|
||||
case .ssh:
|
||||
passwordStore.gitSSHPrivateKeyPassphrase = nil
|
||||
keyStore.removeContent(for: credentialType.keyStoreKey)
|
||||
}
|
||||
|
||||
private func getPassword(attempts: Int, passwordProvider: @escaping PasswordProvider) -> String? {
|
||||
let lastPassword: String? = keyStore.get(for: credentialType.keyStoreKey)
|
||||
if lastPassword == nil || attempts != 1 {
|
||||
guard let requestedPassword = passwordProvider(credentialType.requestPassphraseMessage, lastPassword) else {
|
||||
return nil
|
||||
}
|
||||
if Defaults.isRememberGitCredentialPassphraseOn {
|
||||
keyStore.add(string: requestedPassword, for: credentialType.keyStoreKey)
|
||||
}
|
||||
return requestedPassword
|
||||
}
|
||||
return lastPassword
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -173,35 +173,10 @@ public class PasswordStore {
|
|||
|
||||
public func cloneRepository(
|
||||
remoteRepoURL: URL,
|
||||
credential: GitCredential,
|
||||
branchName: String,
|
||||
requestCredentialPassword: @escaping (GitCredential.Credential, String?) -> String?,
|
||||
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
|
||||
checkoutProgressBlock: @escaping (String, UInt, UInt) -> Void
|
||||
) throws {
|
||||
do {
|
||||
let credentialProvider = try credential.credentialProvider(requestCredentialPassword: requestCredentialPassword)
|
||||
let options = [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
|
||||
try cloneRepository(
|
||||
remoteRepoURL: remoteRepoURL,
|
||||
branchName: branchName,
|
||||
transferProgressBlock: transferProgressBlock,
|
||||
checkoutProgressBlock: checkoutProgressBlock,
|
||||
options: options
|
||||
)
|
||||
} catch {
|
||||
credential.delete()
|
||||
throw (error)
|
||||
}
|
||||
}
|
||||
|
||||
public func cloneRepository(
|
||||
remoteRepoURL: URL,
|
||||
branchName: String,
|
||||
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void,
|
||||
checkoutProgressBlock: @escaping (String, UInt, UInt) -> Void,
|
||||
options: [AnyHashable: Any]? = nil,
|
||||
completion: @escaping () -> Void = {}
|
||||
transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void = { _, _ in },
|
||||
checkoutProgressBlock: @escaping (String, UInt, UInt) -> Void = { _, _, _ in }
|
||||
) throws {
|
||||
try? fm.removeItem(at: storeURL)
|
||||
try? fm.removeItem(at: tempStoreURL)
|
||||
|
|
@ -231,7 +206,6 @@ public class PasswordStore {
|
|||
DispatchQueue.main.async {
|
||||
self.updatePasswordEntityCoreData()
|
||||
NotificationCenter.default.post(name: .passwordStoreUpdated, object: nil)
|
||||
completion()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -252,15 +226,12 @@ public class PasswordStore {
|
|||
}
|
||||
|
||||
public func pullRepository(
|
||||
credential: GitCredential,
|
||||
requestCredentialPassword: @escaping (GitCredential.Credential, String?) -> String?,
|
||||
progressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void
|
||||
options: [String: Any],
|
||||
progressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void = { _, _ in }
|
||||
) throws {
|
||||
guard let storeRepository = storeRepository else {
|
||||
throw AppError.RepositoryNotSet
|
||||
}
|
||||
let credentialProvider = try credential.credentialProvider(requestCredentialPassword: requestCredentialPassword)
|
||||
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
|
||||
let remote = try GTRemote(name: "origin", in: storeRepository)
|
||||
try storeRepository.pull(storeRepository.currentBranch(), from: remote, withOptions: options, progress: progressBlock)
|
||||
Defaults.lastSyncedTime = Date()
|
||||
|
|
@ -472,12 +443,13 @@ public class PasswordStore {
|
|||
return branches.first
|
||||
}
|
||||
|
||||
public func pushRepository(credential: GitCredential, requestCredentialPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
|
||||
public func pushRepository(
|
||||
options: [String: Any],
|
||||
transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void = { _, _, _, _ in }
|
||||
) throws {
|
||||
guard let storeRepository = storeRepository else {
|
||||
throw AppError.RepositoryNotSet
|
||||
}
|
||||
let credentialProvider = try credential.credentialProvider(requestCredentialPassword: requestCredentialPassword)
|
||||
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
|
||||
if let branch = try getLocalBranch(withName: Defaults.gitBranchName) {
|
||||
let remote = try GTRemote(name: "origin", in: storeRepository)
|
||||
try storeRepository.push(branch, to: remote, withOptions: options, progress: transferProgressBlock)
|
||||
|
|
@ -741,9 +713,5 @@ public func findGPGID(from url: URL) -> String {
|
|||
}
|
||||
path = path.appendingPathComponent(".gpg-id")
|
||||
|
||||
do {
|
||||
return try String(contentsOf: path).trimmed
|
||||
} catch {
|
||||
return ""
|
||||
}
|
||||
return (try? String(contentsOf: path))?.trimmed ?? ""
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue