Remove imported keys from iTunes Documents (fix #97)

This commit is contained in:
Yishi Lin 2017-06-14 20:22:15 +08:00
parent fb5f689540
commit 3b562da607
4 changed files with 53 additions and 17 deletions

View file

@ -165,7 +165,7 @@ class GitServerSettingTableViewController: UITableViewController {
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
var urlActionTitle = "Download from URL" var urlActionTitle = "Download from URL"
var armorActionTitle = "ASCII-Armor Encrypted Key" var armorActionTitle = "ASCII-Armor Encrypted Key"
var fileActionTitle = "Use Imported Keys" var fileActionTitle = "iTunes File Sharing"
if SharedDefaults[.gitSSHKeySource] == "url" { if SharedDefaults[.gitSSHKeySource] == "url" {
urlActionTitle = "\(urlActionTitle)" urlActionTitle = "\(urlActionTitle)"
@ -184,16 +184,21 @@ class GitServerSettingTableViewController: UITableViewController {
optionMenu.addAction(urlAction) optionMenu.addAction(urlAction)
optionMenu.addAction(armorAction) optionMenu.addAction(armorAction)
if passwordStore.gitSSHKeyExists() { if passwordStore.gitSSHKeyExists(inFileSharing: true) {
// might keys updated via iTunes, or downloaded/pasted inside the app // might keys updated via iTunes, or downloaded/pasted inside the app
fileActionTitle.append(" (Import)")
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
self.passwordStore.gitSSHKeyImportFromFileSharing()
SharedDefaults[.gitSSHKeySource] = "file" SharedDefaults[.gitSSHKeySource] = "file"
SVProgressHUD.showSuccess(withStatus: "Imported")
SVProgressHUD.dismiss(withDelay: 1)
} }
optionMenu.addAction(fileAction) optionMenu.addAction(fileAction)
} else { } else {
let fileAction = UIAlertAction(title: "iTunes File Sharing", style: .default) { _ in fileActionTitle.append(" (Tips)")
let title = "Import via iTunes File Sharing" let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
let message = "Copy your private key from your computer to Pass for iOS with the name \"ssh_key\" (without quotes)." let title = "Tips"
let message = "Copy your private key to Pass with the name \"ssh_key\" (without quotes) via iTunes. Then come back and click \"iTunes File Sharing\" to finish."
Utils.alert(title: title, message: message, controller: self) Utils.alert(title: title, message: message, controller: self)
} }
optionMenu.addAction(fileAction) optionMenu.addAction(fileAction)

View file

@ -96,16 +96,16 @@ class SettingsTableViewController: UITableViewController {
private func saveImportedPGPKey() { private func saveImportedPGPKey() {
// load keys // load keys
SharedDefaults[.pgpKeySource] = "file" SharedDefaults[.pgpKeySource] = "file"
SVProgressHUD.setDefaultMaskType(.black) SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.setDefaultStyle(.light) SVProgressHUD.setDefaultStyle(.light)
SVProgressHUD.show(withStatus: "Fetching PGP Key") SVProgressHUD.show(withStatus: "Fetching PGP Key")
passwordStore.pgpKeyImportFromFileSharing()
DispatchQueue.global(qos: .userInitiated).async { [unowned self] in DispatchQueue.global(qos: .userInitiated).async { [unowned self] in
do { do {
try self.passwordStore.initPGPKeys() try self.passwordStore.initPGPKeys()
DispatchQueue.main.async { DispatchQueue.main.async {
self.pgpKeyTableViewCell.detailTextLabel?.text = self.passwordStore.pgpKeyID self.pgpKeyTableViewCell.detailTextLabel?.text = self.passwordStore.pgpKeyID
SVProgressHUD.showSuccess(withStatus: "Success") SVProgressHUD.showSuccess(withStatus: "Imported")
SVProgressHUD.dismiss(withDelay: 1) SVProgressHUD.dismiss(withDelay: 1)
} }
} catch { } catch {
@ -237,7 +237,7 @@ class SettingsTableViewController: UITableViewController {
let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
var urlActionTitle = "Download from URL" var urlActionTitle = "Download from URL"
var armorActionTitle = "ASCII-Armor Encrypted Key" var armorActionTitle = "ASCII-Armor Encrypted Key"
var fileActionTitle = "Use Imported Keys" var fileActionTitle = "iTunes File Sharing"
if SharedDefaults[.pgpKeySource] == "url" { if SharedDefaults[.pgpKeySource] == "url" {
urlActionTitle = "\(urlActionTitle)" urlActionTitle = "\(urlActionTitle)"
@ -256,7 +256,8 @@ class SettingsTableViewController: UITableViewController {
optionMenu.addAction(urlAction) optionMenu.addAction(urlAction)
optionMenu.addAction(armorAction) optionMenu.addAction(armorAction)
if passwordStore.pgpKeyExists() { if passwordStore.pgpKeyExists(inFileSharing: true) {
fileActionTitle.append(" (Import)")
let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
// passphrase related // passphrase related
let savePassphraseAlert = UIAlertController(title: "Passphrase", message: "Do you want to save the passphrase for later decryption?", preferredStyle: UIAlertControllerStyle.alert) let savePassphraseAlert = UIAlertController(title: "Passphrase", message: "Do you want to save the passphrase for later decryption?", preferredStyle: UIAlertControllerStyle.alert)
@ -285,9 +286,10 @@ class SettingsTableViewController: UITableViewController {
} }
optionMenu.addAction(fileAction) optionMenu.addAction(fileAction)
} else { } else {
let fileAction = UIAlertAction(title: "iTunes File Sharing", style: .default) { _ in fileActionTitle.append(" (Tips)")
let title = "Import via iTunes File Sharing" let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in
let message = "Copy your public and private key from your computer to Pass for iOS with the name \"gpg_key.pub\" and \"gpg_key\" (without quotes)." let title = "Tips"
let message = "Copy your public and private keys to Pass with names \"gpg_key.pub\" and \"gpg_key\" (without quotes) via iTunes. Then come back and click \"iTunes File Sharing\" to finish."
Utils.alert(title: title, message: message, controller: self) Utils.alert(title: title, message: message, controller: self)
} }
optionMenu.addAction(fileAction) optionMenu.addAction(fileAction)

View file

@ -33,6 +33,11 @@ public class Globals {
public static let gitSSHPrivateKeyURL = URL(fileURLWithPath: gitSSHPrivateKeyPath) public static let gitSSHPrivateKeyURL = URL(fileURLWithPath: gitSSHPrivateKeyPath)
public static let repositoryPath = libraryPath + "/password-store" public static let repositoryPath = libraryPath + "/password-store"
public static let iTunesFileSharingPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
public static let iTunesFileSharingPGPPublic = iTunesFileSharingPath + "/gpg_key.pub"
public static let iTunesFileSharingPGPPrivate = iTunesFileSharingPath + "/gpg_key"
public static let iTunesFileSharingSSHPrivate = iTunesFileSharingPath + "/ssh_key"
public static let passwordDefaultLength = ["Random": (min: 4, max: 64, def: 16), public static let passwordDefaultLength = ["Random": (min: 4, max: 64, def: 16),
"Apple": (min: 15, max: 15, def: 15)] "Apple": (min: 15, max: 15, def: 15)]

View file

@ -110,7 +110,6 @@ public class PasswordStore {
return size return size
} }
private init() { private init() {
// File migration to group // File migration to group
print(Globals.documentPath) print(Globals.documentPath)
@ -852,11 +851,36 @@ public class PasswordStore {
Utils.removeKeychain(name: ".gitSSHPrivateKeyPassphrase") Utils.removeKeychain(name: ".gitSSHPrivateKeyPassphrase")
} }
public func gitSSHKeyExists() -> Bool { public func gitSSHKeyExists(inFileSharing: Bool = false) -> Bool {
return fm.fileExists(atPath: Globals.gitSSHPrivateKeyPath) if inFileSharing == false {
return fm.fileExists(atPath: Globals.gitSSHPrivateKeyPath)
} else {
return fm.fileExists(atPath: Globals.iTunesFileSharingSSHPrivate)
}
} }
public func pgpKeyExists() -> Bool { public func pgpKeyExists(inFileSharing: Bool = false) -> Bool {
return fm.fileExists(atPath: Globals.pgpPublicKeyPath) && fm.fileExists(atPath: Globals.pgpPrivateKeyPath) if inFileSharing == false {
return fm.fileExists(atPath: Globals.pgpPublicKeyPath) && fm.fileExists(atPath: Globals.pgpPrivateKeyPath)
} else {
return fm.fileExists(atPath: Globals.iTunesFileSharingPGPPublic) && fm.fileExists(atPath: Globals.iTunesFileSharingPGPPrivate)
}
}
public func gitSSHKeyImportFromFileSharing() {
do {
try fm.moveItem(atPath: Globals.iTunesFileSharingSSHPrivate, toPath: Globals.gitSSHPrivateKeyPath)
} catch {
print(error)
}
}
public func pgpKeyImportFromFileSharing() {
do {
try fm.moveItem(atPath: Globals.iTunesFileSharingPGPPublic, toPath: Globals.pgpPublicKeyPath)
try fm.moveItem(atPath: Globals.iTunesFileSharingPGPPrivate, toPath: Globals.pgpPrivateKeyPath)
} catch {
print(error)
}
} }
} }