Polish and simplify PasswordStore model class

This commit is contained in:
Bob Sun 2017-04-30 16:16:52 -05:00
parent fb42da4013
commit eba79da0e6
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
5 changed files with 230 additions and 168 deletions

View file

@ -16,7 +16,7 @@ class CommitLogsTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(updateCommitLogs), name: .passwordStoreUpdated, object: nil)
commits = passwordStore.getRecentCommits(count: 20)
commits = getCommitLogs()
self.tableView.estimatedRowHeight = 50
self.tableView.rowHeight = UITableViewAutomaticDimension
}
@ -41,8 +41,17 @@ class CommitLogsTableViewController: UITableViewController {
return cell
}
func updateCommitLogs () {
commits = passwordStore.getRecentCommits(count: 20)
func updateCommitLogs() {
commits = getCommitLogs()
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 []
}
}
}

View 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
}
}