diff --git a/pass/Controllers/GitServerSettingTableViewController.swift b/pass/Controllers/GitServerSettingTableViewController.swift index 681bb42..2f9afb8 100644 --- a/pass/Controllers/GitServerSettingTableViewController.swift +++ b/pass/Controllers/GitServerSettingTableViewController.swift @@ -165,7 +165,7 @@ class GitServerSettingTableViewController: UITableViewController { let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) var urlActionTitle = "Download from URL" var armorActionTitle = "ASCII-Armor Encrypted Key" - var fileActionTitle = "Use Imported Keys" + var fileActionTitle = "iTunes File Sharing" if SharedDefaults[.gitSSHKeySource] == "url" { urlActionTitle = "✓ \(urlActionTitle)" @@ -184,16 +184,21 @@ class GitServerSettingTableViewController: UITableViewController { optionMenu.addAction(urlAction) optionMenu.addAction(armorAction) - if passwordStore.gitSSHKeyExists() { + if passwordStore.gitSSHKeyExists(inFileSharing: true) { // might keys updated via iTunes, or downloaded/pasted inside the app + fileActionTitle.append(" (Import)") let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in + self.passwordStore.gitSSHKeyImportFromFileSharing() SharedDefaults[.gitSSHKeySource] = "file" + SVProgressHUD.showSuccess(withStatus: "Imported") + SVProgressHUD.dismiss(withDelay: 1) } optionMenu.addAction(fileAction) } else { - let fileAction = UIAlertAction(title: "iTunes File Sharing", style: .default) { _ in - let title = "Import via iTunes File Sharing" - let message = "Copy your private key from your computer to Pass for iOS with the name \"ssh_key\" (without quotes)." + fileActionTitle.append(" (Tips)") + let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in + 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) } optionMenu.addAction(fileAction) diff --git a/pass/Controllers/SettingsTableViewController.swift b/pass/Controllers/SettingsTableViewController.swift index 10bb361..e2b812d 100644 --- a/pass/Controllers/SettingsTableViewController.swift +++ b/pass/Controllers/SettingsTableViewController.swift @@ -96,16 +96,16 @@ class SettingsTableViewController: UITableViewController { private func saveImportedPGPKey() { // load keys SharedDefaults[.pgpKeySource] = "file" - SVProgressHUD.setDefaultMaskType(.black) SVProgressHUD.setDefaultStyle(.light) SVProgressHUD.show(withStatus: "Fetching PGP Key") + passwordStore.pgpKeyImportFromFileSharing() DispatchQueue.global(qos: .userInitiated).async { [unowned self] in do { try self.passwordStore.initPGPKeys() DispatchQueue.main.async { self.pgpKeyTableViewCell.detailTextLabel?.text = self.passwordStore.pgpKeyID - SVProgressHUD.showSuccess(withStatus: "Success") + SVProgressHUD.showSuccess(withStatus: "Imported") SVProgressHUD.dismiss(withDelay: 1) } } catch { @@ -237,7 +237,7 @@ class SettingsTableViewController: UITableViewController { let optionMenu = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) var urlActionTitle = "Download from URL" var armorActionTitle = "ASCII-Armor Encrypted Key" - var fileActionTitle = "Use Imported Keys" + var fileActionTitle = "iTunes File Sharing" if SharedDefaults[.pgpKeySource] == "url" { urlActionTitle = "✓ \(urlActionTitle)" @@ -256,7 +256,8 @@ class SettingsTableViewController: UITableViewController { optionMenu.addAction(urlAction) optionMenu.addAction(armorAction) - if passwordStore.pgpKeyExists() { + if passwordStore.pgpKeyExists(inFileSharing: true) { + fileActionTitle.append(" (Import)") let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in // passphrase related 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) } else { - let fileAction = UIAlertAction(title: "iTunes File Sharing", style: .default) { _ in - let title = "Import via iTunes File Sharing" - 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)." + fileActionTitle.append(" (Tips)") + let fileAction = UIAlertAction(title: fileActionTitle, style: .default) { _ in + 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) } optionMenu.addAction(fileAction) diff --git a/passKit/Helpers/Globals.swift b/passKit/Helpers/Globals.swift index c798f0a..4546c70 100644 --- a/passKit/Helpers/Globals.swift +++ b/passKit/Helpers/Globals.swift @@ -33,6 +33,11 @@ public class Globals { public static let gitSSHPrivateKeyURL = URL(fileURLWithPath: gitSSHPrivateKeyPath) 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), "Apple": (min: 15, max: 15, def: 15)] diff --git a/passKit/Models/PasswordStore.swift b/passKit/Models/PasswordStore.swift index 941669c..628c5e9 100644 --- a/passKit/Models/PasswordStore.swift +++ b/passKit/Models/PasswordStore.swift @@ -109,7 +109,6 @@ public class PasswordStore { } return size } - private init() { // File migration to group @@ -852,11 +851,36 @@ public class PasswordStore { Utils.removeKeychain(name: ".gitSSHPrivateKeyPassphrase") } - public func gitSSHKeyExists() -> Bool { - return fm.fileExists(atPath: Globals.gitSSHPrivateKeyPath) + public func gitSSHKeyExists(inFileSharing: Bool = false) -> Bool { + if inFileSharing == false { + return fm.fileExists(atPath: Globals.gitSSHPrivateKeyPath) + } else { + return fm.fileExists(atPath: Globals.iTunesFileSharingSSHPrivate) + } } - public func pgpKeyExists() -> Bool { - return fm.fileExists(atPath: Globals.pgpPublicKeyPath) && fm.fileExists(atPath: Globals.pgpPrivateKeyPath) + public func pgpKeyExists(inFileSharing: Bool = false) -> Bool { + 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) + } } }