Fix bugs about getting next HOTP
- correctly increase the otpauth counter now - fix bugs about re-initializing password
This commit is contained in:
parent
c769c4126b
commit
4eba8f7800
2 changed files with 37 additions and 25 deletions
|
|
@ -386,11 +386,8 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// increase HOTP counter
|
// copy HOTP to pasteboard (will update counter)
|
||||||
password!.increaseHotpCounter()
|
if let plainPassword = password!.getNextHotp() {
|
||||||
|
|
||||||
// copy HOTP to pasteboard
|
|
||||||
if let plainPassword = password!.getOtp() {
|
|
||||||
Utils.copyToPasteboard(textToCopy: plainPassword)
|
Utils.copyToPasteboard(textToCopy: plainPassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,21 +61,20 @@ class Password {
|
||||||
private func initEverything(name: String, plainText: String) {
|
private func initEverything(name: String, plainText: String) {
|
||||||
self.name = name
|
self.name = name
|
||||||
self.plainText = plainText
|
self.plainText = plainText
|
||||||
|
self.additions.removeAll()
|
||||||
|
self.additionKeys.removeAll()
|
||||||
|
|
||||||
// get password and additional fields
|
// get password and additional fields
|
||||||
let plainTextSplit = plainText.characters.split(maxSplits: 1, omittingEmptySubsequences: false) {
|
let plainTextSplit = plainText.characters.split(maxSplits: 1, omittingEmptySubsequences: false) {
|
||||||
$0 == "\n" || $0 == "\r\n"
|
$0 == "\n" || $0 == "\r\n"
|
||||||
}.map(String.init)
|
}.map(String.init)
|
||||||
guard plainTextSplit.count > 0 else {
|
self.password = plainTextSplit.first ?? ""
|
||||||
return;
|
|
||||||
}
|
|
||||||
self.password = plainTextSplit[0]
|
|
||||||
if plainTextSplit.count == 2 {
|
if plainTextSplit.count == 2 {
|
||||||
(self.additions, self.additionKeys) = Password.getAdditionFields(from: plainTextSplit[1])
|
(self.additions, self.additionKeys) = Password.getAdditionFields(from: plainTextSplit[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// check whether the first line of the plainText looks like an otp entry
|
// check whether the first line of the plainText looks like an otp entry
|
||||||
let (key, value) = Password.getKeyValuePair(from: plainTextSplit[0])
|
let (key, value) = Password.getKeyValuePair(from: self.password)
|
||||||
if Password.otpKeywords.contains(key ?? "") {
|
if Password.otpKeywords.contains(key ?? "") {
|
||||||
firstLineIsOTPField = true
|
firstLineIsOTPField = true
|
||||||
self.additions[key!] = value
|
self.additions[key!] = value
|
||||||
|
|
@ -269,21 +268,6 @@ class Password {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// it is guaranteed that it is a HOTP password when we call this
|
|
||||||
func increaseHotpCounter() {
|
|
||||||
var lines : [String] = []
|
|
||||||
self.plainText.enumerateLines() { line, _ in
|
|
||||||
let (key, value) = Password.getKeyValuePair(from: line)
|
|
||||||
if key == "otp_counter", let newValue = UInt64(value)?.advanced(by: 1) {
|
|
||||||
let newLine = "\(key!): \(newValue)"
|
|
||||||
lines.append(newLine)
|
|
||||||
} else {
|
|
||||||
lines.append(line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.updatePassword(name: self.name, plainText: lines.joined(separator: "\n"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the description and the password strings
|
// return the description and the password strings
|
||||||
func getOtpStrings() -> (description: String, otp: String)? {
|
func getOtpStrings() -> (description: String, otp: String)? {
|
||||||
guard let token = self.otpToken else {
|
guard let token = self.otpToken else {
|
||||||
|
|
@ -313,6 +297,37 @@ class Password {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return the password strings
|
||||||
|
// it is guaranteed that it is a HOTP password when we call this
|
||||||
|
func getNextHotp() -> String? {
|
||||||
|
// increase the counter
|
||||||
|
otpToken = otpToken?.updatedToken()
|
||||||
|
|
||||||
|
// replace old HOTP settings with the new otpauth
|
||||||
|
var newOtpauth = try! otpToken?.toURL().absoluteString
|
||||||
|
newOtpauth?.append("&secret=")
|
||||||
|
newOtpauth?.append(MF_Base32Codec.base32String(from: otpToken?.generator.secret))
|
||||||
|
|
||||||
|
var lines : [String] = []
|
||||||
|
self.plainText.enumerateLines() { line, _ in
|
||||||
|
let (key, _) = Password.getKeyValuePair(from: line)
|
||||||
|
if !Password.otpKeywords.contains(key ?? "") {
|
||||||
|
lines.append(line)
|
||||||
|
} else if key == "otpauth" && newOtpauth != nil {
|
||||||
|
lines.append(newOtpauth!)
|
||||||
|
// set to nil to prevent duplication
|
||||||
|
newOtpauth = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if newOtpauth != nil {
|
||||||
|
lines.append(newOtpauth!)
|
||||||
|
}
|
||||||
|
self.updatePassword(name: self.name, plainText: lines.joined(separator: "\n"))
|
||||||
|
|
||||||
|
// get and return the password
|
||||||
|
return self.otpToken?.currentPassword
|
||||||
|
}
|
||||||
|
|
||||||
static func LooksLikeOTP(line: String) -> Bool {
|
static func LooksLikeOTP(line: String) -> Bool {
|
||||||
let (key, _) = getKeyValuePair(from: line)
|
let (key, _) = getKeyValuePair(from: line)
|
||||||
return Password.otpKeywords.contains(key ?? "")
|
return Password.otpKeywords.contains(key ?? "")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue