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

@ -329,42 +329,12 @@
</constraints>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="passwordTableViewCell" rowHeight="52" id="vIU-mk-Lbm">
<rect key="frame" x="0.0" y="139" width="414" height="52"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="vIU-mk-Lbm" id="6VP-wN-g64">
<rect key="frame" x="0.0" y="0.0" width="414" height="51.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Password" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5Jt-ds-Hhi">
<rect key="frame" x="15" y="8" width="391" height="15"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleCaption1"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Password" textAlignment="natural" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="hSW-BQ-SXN">
<rect key="frame" x="15" y="23" width="391" height="21"/>
<nil key="textColor"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" keyboardType="alphabet" keyboardAppearance="alert" secureTextEntry="YES"/>
</textField>
</subviews>
<constraints>
<constraint firstAttribute="trailingMargin" secondItem="hSW-BQ-SXN" secondAttribute="trailing" id="3gM-Hv-ZaR"/>
<constraint firstAttribute="topMargin" secondItem="5Jt-ds-Hhi" secondAttribute="top" id="INe-r1-0Lo"/>
<constraint firstItem="hSW-BQ-SXN" firstAttribute="leading" secondItem="5Jt-ds-Hhi" secondAttribute="leading" id="JRs-qa-035"/>
<constraint firstAttribute="trailingMargin" secondItem="5Jt-ds-Hhi" secondAttribute="trailing" id="ibB-cz-ZJG"/>
<constraint firstItem="hSW-BQ-SXN" firstAttribute="top" secondItem="5Jt-ds-Hhi" secondAttribute="bottom" id="mVE-aV-0yw"/>
<constraint firstItem="5Jt-ds-Hhi" firstAttribute="leading" secondItem="6VP-wN-g64" secondAttribute="leadingMargin" constant="7" id="sMW-lm-4pj"/>
</constraints>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection id="s6v-aZ-kaD">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" accessoryType="disclosureIndicator" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="Gar-Yh-ZKL" detailTextLabel="Tjx-af-c7u" style="IBUITableViewCellStyleValue1" id="skE-NA-SBb">
<rect key="frame" x="0.0" y="227" width="414" height="44"/>
<rect key="frame" x="0.0" y="175" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="skE-NA-SBb" id="xec-0B-cTD">
<rect key="frame" x="0.0" y="0.0" width="381" height="43.666666666666664"/>
@ -407,7 +377,7 @@
</barButtonItem>
<barButtonItem key="rightBarButtonItem" style="done" systemItem="save" id="sgQ-zB-rxv">
<connections>
<segue destination="7K9-cE-9qq" kind="unwind" identifier="saveGitServerSettingSegue" unwindAction="saveGitServerSettingWithSegue:" id="DRY-fA-6Ce"/>
<action selector="save:" destination="ynQ-64-MfA" id="HNL-Da-fXT"/>
</connections>
</barButtonItem>
</navigationItem>
@ -415,8 +385,8 @@
<connections>
<outlet property="authenticationTableViewCell" destination="skE-NA-SBb" id="Tc1-FF-nhM"/>
<outlet property="gitRepositoryURLTextField" destination="EVT-VU-sCi" id="2EH-Uq-tbU"/>
<outlet property="passwordTextField" destination="hSW-BQ-SXN" id="uFS-s5-yfZ"/>
<outlet property="usernameTextField" destination="cxu-dD-J2H" id="RjU-Hf-xyw"/>
<segue destination="7K9-cE-9qq" kind="unwind" identifier="saveGitServerSettingSegue" unwindAction="saveGitServerSettingWithSegue:" id="5UN-sC-xCA"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="7c1-c7-Qyp" userLabel="First Responder" sceneMemberID="firstResponder"/>
@ -452,7 +422,7 @@
</subviews>
</tableViewCellContentView>
<connections>
<segue destination="MfG-VE-Ork" kind="unwind" unwindAction="saveWithSegue:" id="QZM-o4-88Q"/>
<segue destination="MfG-VE-Ork" kind="unwind" unwindAction="saveAuthMethodWithSegue:" id="bKv-RY-c5z"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" textLabel="844-Ya-Uiz" style="IBUITableViewCellStyleDefault" id="ahw-Sp-Et2">
@ -472,7 +442,7 @@
</subviews>
</tableViewCellContentView>
<connections>
<segue destination="MfG-VE-Ork" kind="unwind" unwindAction="saveWithSegue:" id="eBA-wP-36P"/>
<segue destination="MfG-VE-Ork" kind="unwind" unwindAction="saveAuthMethodWithSegue:" id="Cww-6I-bWW"/>
</connections>
</tableViewCell>
</cells>

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

View file

@ -28,6 +28,7 @@ extension DefaultsKeys {
static let gitRepositoryAuthenticationMethod = DefaultsKey<String?>("gitRepositoryAuthenticationMethod")
static let gitRepositoryUsername = DefaultsKey<String?>("gitRepositoryUsername")
static let gitRepositoryPassword = DefaultsKey<String?>("gitRepositoryPassword")
static let gitRepositoryPasswordAttempts = DefaultsKey<Int>("gitRepositoryPasswordAttempts")
static let gitRepositorySSHPublicKeyURL = DefaultsKey<URL?>("gitRepositorySSHPublicKeyURL")
static let gitRepositorySSHPrivateKeyURL = DefaultsKey<URL?>("gitRepositorySSHPrivateKeyURL")
static let gitRepositorySSHPrivateKeyPassphrase = DefaultsKey<String?>("gitRepositorySSHPrivateKeyPassphrase")

View file

@ -12,6 +12,7 @@ import CoreData
import UIKit
import SwiftyUserDefaults
import ObjectiveGit
import SVProgressHUD
struct GitCredential {
@ -23,15 +24,53 @@ struct GitCredential {
var credential: Credential
func credentialProvider() throws -> GTCredentialProvider {
return GTCredentialProvider { (_, _, _) -> (GTCredential) in
let credential: GTCredential?
return GTCredentialProvider { (_, _, _) -> (GTCredential?) in
var credential: GTCredential? = nil
switch self.credential {
case let .http(userName, password):
credential = try? GTCredential(userName: userName, password: password)
print(Defaults[.gitRepositoryPasswordAttempts])
var newPassword: String = password
if Defaults[.gitRepositoryPasswordAttempts] != 0 {
let sem = DispatchSemaphore(value: 0)
DispatchQueue.main.async {
SVProgressHUD.dismiss()
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
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
newPassword = alert.textFields!.first!.text!
Defaults[.gitRepositoryPassword] = newPassword
sem.signal()
}))
if Defaults[.gitRepositoryPasswordAttempts] == 3 {
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
Defaults[.gitRepositoryPasswordAttempts] = -1
sem.signal()
})
}
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = Defaults[.gitRepositoryPassword]
textField.isSecureTextEntry = true
})
topController.present(alert, animated: true, completion: nil)
}
}
let _ = sem.wait(timeout: DispatchTime.distantFuture)
}
if Defaults[.gitRepositoryPasswordAttempts] == -1 {
Defaults[.gitRepositoryPasswordAttempts] = 0
return nil
}
print("usernmae: \(userName)")
print("password: \(newPassword)")
Defaults[.gitRepositoryPasswordAttempts] += 1
credential = try? GTCredential(userName: userName, password: newPassword)
case let .ssh(userName, password, publicKeyFile, privateKeyFile):
credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: password)
}
return credential ?? GTCredential()
return credential
}
}
}
@ -270,7 +309,7 @@ class PasswordStore {
let newTree = addEntryToGTTree(fileData: fileData, filename: filename)
let headReference = try storeRepository!.headReference()
let commitEnum = try GTEnumerator(repository: storeRepository!)
try commitEnum.pushSHA(headReference.targetOID.sha)
try commitEnum.pushSHA(headReference.targetOID.sha!)
let parent = commitEnum.nextObject() as! GTCommit
progressBlock(0.5)
let commit = try storeRepository!.createCommit(with: newTree, message: message, parents: [parent], updatingReferenceNamed: headReference.name)
@ -287,7 +326,7 @@ class PasswordStore {
let newTree = removeEntryFromGTTree(filename: filename)
let headReference = try storeRepository!.headReference()
let commitEnum = try GTEnumerator(repository: storeRepository!)
try commitEnum.pushSHA(headReference.targetOID.sha)
try commitEnum.pushSHA(headReference.targetOID.sha!)
let parent = commitEnum.nextObject() as! GTCommit
progressBlock(0.5)
let commit = try storeRepository!.createCommit(with: newTree, message: message, parents: [parent], updatingReferenceNamed: headReference.name)