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

@ -1,5 +1,5 @@
github "SVProgressHUD/SVProgressHUD" github "SVProgressHUD/SVProgressHUD"
github "radex/SwiftyUserDefaults" github "radex/SwiftyUserDefaults"
github "libgit2/objective-git" github "mssun/objective-git" "master"
github "mssun/SwiftPasscodeLock" "master" github "zahlz/SwiftPasscodeLock" "master"
github "bitserf/FavIcon" github "bitserf/FavIcon"

View file

@ -329,42 +329,12 @@
</constraints> </constraints>
</tableViewCellContentView> </tableViewCellContentView>
</tableViewCell> </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> </cells>
</tableViewSection> </tableViewSection>
<tableViewSection id="s6v-aZ-kaD"> <tableViewSection id="s6v-aZ-kaD">
<cells> <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"> <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"/> <autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="skE-NA-SBb" id="xec-0B-cTD"> <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"/> <rect key="frame" x="0.0" y="0.0" width="381" height="43.666666666666664"/>
@ -407,7 +377,7 @@
</barButtonItem> </barButtonItem>
<barButtonItem key="rightBarButtonItem" style="done" systemItem="save" id="sgQ-zB-rxv"> <barButtonItem key="rightBarButtonItem" style="done" systemItem="save" id="sgQ-zB-rxv">
<connections> <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> </connections>
</barButtonItem> </barButtonItem>
</navigationItem> </navigationItem>
@ -415,8 +385,8 @@
<connections> <connections>
<outlet property="authenticationTableViewCell" destination="skE-NA-SBb" id="Tc1-FF-nhM"/> <outlet property="authenticationTableViewCell" destination="skE-NA-SBb" id="Tc1-FF-nhM"/>
<outlet property="gitRepositoryURLTextField" destination="EVT-VU-sCi" id="2EH-Uq-tbU"/> <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"/> <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> </connections>
</tableViewController> </tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="7c1-c7-Qyp" userLabel="First Responder" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="7c1-c7-Qyp" userLabel="First Responder" sceneMemberID="firstResponder"/>
@ -452,7 +422,7 @@
</subviews> </subviews>
</tableViewCellContentView> </tableViewCellContentView>
<connections> <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> </connections>
</tableViewCell> </tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" textLabel="844-Ya-Uiz" style="IBUITableViewCellStyleDefault" id="ahw-Sp-Et2"> <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" textLabel="844-Ya-Uiz" style="IBUITableViewCellStyleDefault" id="ahw-Sp-Et2">
@ -472,7 +442,7 @@
</subviews> </subviews>
</tableViewCellContentView> </tableViewCellContentView>
<connections> <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> </connections>
</tableViewCell> </tableViewCell>
</cells> </cells>

View file

@ -13,9 +13,11 @@ class GitServerSettingTableViewController: UITableViewController {
@IBOutlet weak var gitRepositoryURLTextField: UITextField! @IBOutlet weak var gitRepositoryURLTextField: UITextField!
@IBOutlet weak var usernameTextField: UITextField! @IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField! // @IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var authenticationTableViewCell: UITableViewCell! @IBOutlet weak var authenticationTableViewCell: UITableViewCell!
// var password: String?
var authenticationMethod = Defaults[.gitRepositoryAuthenticationMethod] var authenticationMethod = Defaults[.gitRepositoryAuthenticationMethod]
@ -25,7 +27,7 @@ class GitServerSettingTableViewController: UITableViewController {
gitRepositoryURLTextField.text = url.absoluteString gitRepositoryURLTextField.text = url.absoluteString
} }
usernameTextField.text = Defaults[.gitRepositoryUsername] usernameTextField.text = Defaults[.gitRepositoryUsername]
passwordTextField.text = Defaults[.gitRepositoryPassword] // passwordTextField.text = Defaults[.gitRepositoryPassword]
authenticationTableViewCell.detailTextLabel?.text = authenticationMethod authenticationTableViewCell.detailTextLabel?.text = authenticationMethod
} }
@ -68,7 +70,28 @@ class GitServerSettingTableViewController: UITableViewController {
return true 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 let controller = segue.source as? UITableViewController {
if controller.tableView.indexPathForSelectedRow == IndexPath(row: 0, section:0) { if controller.tableView.indexPathForSelectedRow == IndexPath(row: 0, section:0) {
authenticationMethod = "Password" authenticationMethod = "Password"

View file

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

View file

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

View file

@ -12,6 +12,7 @@ import CoreData
import UIKit import UIKit
import SwiftyUserDefaults import SwiftyUserDefaults
import ObjectiveGit import ObjectiveGit
import SVProgressHUD
struct GitCredential { struct GitCredential {
@ -23,15 +24,53 @@ struct GitCredential {
var credential: Credential var credential: Credential
func credentialProvider() throws -> GTCredentialProvider { func credentialProvider() throws -> GTCredentialProvider {
return GTCredentialProvider { (_, _, _) -> (GTCredential) in return GTCredentialProvider { (_, _, _) -> (GTCredential?) in
let credential: GTCredential? var credential: GTCredential? = nil
switch self.credential { switch self.credential {
case let .http(userName, password): 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): case let .ssh(userName, password, publicKeyFile, privateKeyFile):
credential = try? GTCredential(userName: userName, publicKeyURL: publicKeyFile, privateKeyURL: privateKeyFile, passphrase: password) 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 newTree = addEntryToGTTree(fileData: fileData, filename: filename)
let headReference = try storeRepository!.headReference() let headReference = try storeRepository!.headReference()
let commitEnum = try GTEnumerator(repository: storeRepository!) let commitEnum = try GTEnumerator(repository: storeRepository!)
try commitEnum.pushSHA(headReference.targetOID.sha) try commitEnum.pushSHA(headReference.targetOID.sha!)
let parent = commitEnum.nextObject() as! GTCommit let parent = commitEnum.nextObject() as! GTCommit
progressBlock(0.5) progressBlock(0.5)
let commit = try storeRepository!.createCommit(with: newTree, message: message, parents: [parent], updatingReferenceNamed: headReference.name) 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 newTree = removeEntryFromGTTree(filename: filename)
let headReference = try storeRepository!.headReference() let headReference = try storeRepository!.headReference()
let commitEnum = try GTEnumerator(repository: storeRepository!) let commitEnum = try GTEnumerator(repository: storeRepository!)
try commitEnum.pushSHA(headReference.targetOID.sha) try commitEnum.pushSHA(headReference.targetOID.sha!)
let parent = commitEnum.nextObject() as! GTCommit let parent = commitEnum.nextObject() as! GTCommit
progressBlock(0.5) progressBlock(0.5)
let commit = try storeRepository!.createCommit(with: newTree, message: message, parents: [parent], updatingReferenceNamed: headReference.name) let commit = try storeRepository!.createCommit(with: newTree, message: message, parents: [parent], updatingReferenceNamed: headReference.name)