use runtime password filling

This commit is contained in:
Bob Sun 2017-02-17 20:14:01 +08:00
parent a3a09beebf
commit 3bbd4c47df
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
6 changed files with 90 additions and 51 deletions

View file

@ -13,9 +13,11 @@ class GitServerSettingTableViewController: UITableViewController {
@IBOutlet weak var gitRepositoryURLTextField: UITextField!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
// @IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var authenticationTableViewCell: UITableViewCell!
// var password: String?
var authenticationMethod = Defaults[.gitRepositoryAuthenticationMethod]
@ -25,7 +27,7 @@ class GitServerSettingTableViewController: UITableViewController {
gitRepositoryURLTextField.text = url.absoluteString
}
usernameTextField.text = Defaults[.gitRepositoryUsername]
passwordTextField.text = Defaults[.gitRepositoryPassword]
// passwordTextField.text = Defaults[.gitRepositoryPassword]
authenticationTableViewCell.detailTextLabel?.text = authenticationMethod
}
@ -68,7 +70,28 @@ class GitServerSettingTableViewController: UITableViewController {
return true
}
@IBAction func save(segue: UIStoryboardSegue) {
@IBAction func save(_ sender: Any) {
if authenticationMethod == "Password" {
let alert = UIAlertController(title: "Password", message: "Please fill in the password of your Git account.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in
Defaults[.gitRepositoryPassword] = alert.textFields?.first?.text
if self.shouldPerformSegue(withIdentifier: "saveGitServerSettingSegue", sender: self) {
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
}
}))
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = Defaults[.gitRepositoryPassword]
textField.isSecureTextEntry = true
})
self.present(alert, animated: true, completion: nil)
} else {
if self.shouldPerformSegue(withIdentifier: "saveGitServerSettingSegue", sender: self) {
self.performSegue(withIdentifier: "saveGitServerSettingSegue", sender: self)
}
}
}
@IBAction func saveAuthMethod(segue: UIStoryboardSegue) {
if let controller = segue.source as? UITableViewController {
if controller.tableView.indexPathForSelectedRow == IndexPath(row: 0, section:0) {
authenticationMethod = "Password"

View file

@ -43,21 +43,26 @@ class PasswordRepositorySettingsTableViewController: BasicStaticTableViewControl
if let controller = segue.source as? GitServerSettingTableViewController {
let gitRepostiroyURL = controller.gitRepositoryURLTextField.text!
let username = controller.usernameTextField.text!
let password = controller.passwordTextField.text!
let password = Defaults[.gitRepositoryPassword]
let auth = controller.authenticationMethod
if Defaults[.gitRepositoryURL] == nil || gitRepostiroyURL != Defaults[.gitRepositoryURL]!.absoluteString {
if Defaults[.gitRepositoryURL] == nil ||
Defaults[.gitRepositoryURL]!.absoluteString != gitRepostiroyURL ||
auth != Defaults[.gitRepositoryAuthenticationMethod] ||
username != Defaults[.gitRepositoryUsername] ||
password != Defaults[.gitRepositoryPassword] {
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "Prepare Repository")
var gitCredential: GitCredential
if auth == "Password" {
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password))
gitCredential = GitCredential(credential: GitCredential.Credential.http(userName: username, password: password!))
} else {
gitCredential = GitCredential(credential: GitCredential.Credential.ssh(userName: username, password: Defaults[.gitRepositorySSHPrivateKeyPassphrase]!, publicKeyFile: Globals.sshPublicKeyURL, privateKeyFile: Globals.sshPrivateKeyURL))
}
DispatchQueue.global(qos: .userInitiated).async {
let dispatchQueue = DispatchQueue.global(qos: .userInitiated)
dispatchQueue.async {
do {
try PasswordStore.shared.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!,
credential: gitCredential,
@ -79,6 +84,7 @@ class PasswordRepositorySettingsTableViewController: BasicStaticTableViewControl
Defaults[.gitRepositoryUsername] = username
Defaults[.gitRepositoryPassword] = password
Defaults[.gitRepositoryAuthenticationMethod] = auth
Defaults[.gitRepositoryPasswordAttempts] = 0
SVProgressHUD.showSuccess(withStatus: "Done")
SVProgressHUD.dismiss(withDelay: 1)
}