Add more password name checks.
This commit is contained in:
parent
49180d74e6
commit
b9ef596bfb
5 changed files with 56 additions and 16 deletions
|
|
@ -39,10 +39,7 @@ class AddPasswordTableViewController: PasswordEditorTableViewController {
|
|||
}
|
||||
|
||||
// check name
|
||||
guard nameCell?.getContent()?.isEmpty == false else {
|
||||
let alertTitle = "Cannot Add Password"
|
||||
let alertMessage = "Please fill in the name."
|
||||
Utils.alert(title: alertTitle, message: alertMessage, controller: self, completion: nil)
|
||||
guard checkName() == true else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -57,9 +54,7 @@ class AddPasswordTableViewController: PasswordEditorTableViewController {
|
|||
plainText.append("\n")
|
||||
plainText.append(additionsString)
|
||||
}
|
||||
let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())!
|
||||
let name = URL(string: encodedName)!.lastPathComponent
|
||||
let url = URL(string: encodedName)!.appendingPathExtension("gpg")
|
||||
let (name, url) = getNameURL()
|
||||
password = Password(name: name, url: url, plainText: plainText)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,12 +27,8 @@ class EditPasswordTableViewController: PasswordEditorTableViewController {
|
|||
|
||||
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
|
||||
if identifier == "saveEditPasswordSegue" {
|
||||
if let name = nameCell?.getContent(),
|
||||
let path = name.stringByAddingPercentEncodingForRFC3986(),
|
||||
let _ = URL(string: path) {
|
||||
return true
|
||||
} else {
|
||||
Utils.alert(title: "Cannot Save", message: "Password name is invalid.", controller: self, completion: nil)
|
||||
// check name
|
||||
guard checkName() == true else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -47,9 +43,7 @@ class EditPasswordTableViewController: PasswordEditorTableViewController {
|
|||
plainText.append("\n")
|
||||
plainText.append(additionsString)
|
||||
}
|
||||
let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())!
|
||||
let name = URL(string: encodedName)!.lastPathComponent
|
||||
let url = URL(string: encodedName)!.appendingPathExtension("gpg")
|
||||
let (name, url) = getNameURL()
|
||||
if password!.plainText != plainText || password!.url!.path != url.path {
|
||||
password!.updatePassword(name: name, url: url, plainText: plainText)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -261,4 +261,45 @@ class PasswordEditorTableViewController: UITableViewController, FillPasswordTabl
|
|||
tableData[additionsSection][0][PasswordEditorCellKey.content] = additionsCell?.getContent()
|
||||
}
|
||||
}
|
||||
|
||||
func getNameURL() -> (String, URL) {
|
||||
let encodedName = (nameCell?.getContent()?.stringByAddingPercentEncodingForRFC3986())!
|
||||
let name = URL(string: encodedName)!.lastPathComponent
|
||||
let url = URL(string: encodedName)!.appendingPathExtension("gpg")
|
||||
return (name, url)
|
||||
}
|
||||
|
||||
func checkName() -> Bool {
|
||||
// the name field should not be empty
|
||||
guard let name = nameCell?.getContent(), name.isEmpty == false else {
|
||||
Utils.alert(title: "Cannot Save", message: "Please fill in the name.", controller: self, completion: nil)
|
||||
return false
|
||||
}
|
||||
|
||||
// the name should not start with /
|
||||
guard name.hasPrefix("/") == false else {
|
||||
Utils.alert(title: "Cannot Save", message: "Please remove the prefix \"/\" from your password name.", controller: self, completion: nil)
|
||||
return false
|
||||
}
|
||||
|
||||
// the name field should be a valid url
|
||||
guard let path = name.stringByAddingPercentEncodingForRFC3986(),
|
||||
var passwordURL = URL(string: path) else {
|
||||
Utils.alert(title: "Cannot Save", message: "Password name is invalid.", controller: self, completion: nil)
|
||||
return false
|
||||
}
|
||||
|
||||
// check whether we can parse the filename (be consistent with PasswordStore::addPasswordEntities)
|
||||
var previousURLLength = Int.max
|
||||
while passwordURL.path != "." {
|
||||
passwordURL = passwordURL.deletingLastPathComponent()
|
||||
if passwordURL.absoluteString.count >= previousURLLength {
|
||||
Utils.alert(title: "Cannot Save", message: "Cannot parse the filename. Please check and simplify the password name.", controller: self, completion: nil)
|
||||
return false
|
||||
}
|
||||
previousURLLength = passwordURL.absoluteString.count
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue