From d1393c6d368960484c7cf9feacdc7f37bf66b83a Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Thu, 11 May 2017 22:55:55 -0700 Subject: [PATCH] Polish validation logic of importing pgp key via url --- .../PGPKeySettingTableViewController.swift | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pass/Controllers/PGPKeySettingTableViewController.swift b/pass/Controllers/PGPKeySettingTableViewController.swift index e99222f..c7ab7db 100644 --- a/pass/Controllers/PGPKeySettingTableViewController.swift +++ b/pass/Controllers/PGPKeySettingTableViewController.swift @@ -39,22 +39,26 @@ class PGPKeySettingTableViewController: UITableViewController { override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { if identifier == "savePGPKeySegue" { - guard let pgpPublicKeyURL = URL(string: pgpPublicKeyURLTextField.text!) else { - Utils.alert(title: "Cannot Save", message: "Please set Public Key URL first.", controller: self, completion: nil) - return false - } - guard let pgpPrivateKeyURL = URL(string: pgpPrivateKeyURLTextField.text!) else { - Utils.alert(title: "Cannot Save", message: "Please set Private Key URL first.", controller: self, completion: nil) - return false - } - guard pgpPublicKeyURL.scheme! == "https", pgpPrivateKeyURL.scheme! == "https" else { - Utils.alert(title: "Cannot Save Settings", message: "HTTP connection is not supported.", controller: self, completion: nil) + guard validatePGPKeyURL(input: pgpPublicKeyURLTextField.text) == true, + validatePGPKeyURL(input: pgpPrivateKeyURLTextField.text) == true else { return false } } return true } + private func validatePGPKeyURL(input: String?) -> Bool { + guard let path = input, let url = URL(string: path) else { + Utils.alert(title: "Cannot Save PGP Key", message: "Please set PGP Key URL first.", controller: self, completion: nil) + return false + } + guard let scheme = url.scheme, scheme == "https", scheme == "https" else { + Utils.alert(title: "Cannot Save PGP Key", message: "HTTP connection is not supported.", controller: self, completion: nil) + return false + } + return true + } + @IBAction func save(_ sender: Any) { let alert = UIAlertController(title: "Passphrase", message: "Please fill in the passphrase of your PGP secret key.", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {_ in