Introduce class constants for the strings "otpauth" and "otpauth://"

This commit is contained in:
Danny Moesch 2018-03-11 12:57:13 +01:00 committed by Bob Sun
parent caa5ce77a3
commit 69bafeb672

View file

@ -25,6 +25,8 @@ enum PasswordChange: Int {
public class Password { public class Password {
public static let otpKeywords = ["otp_secret", "otp_type", "otp_algorithm", "otp_period", "otp_digits", "otp_counter", "otpauth"] public static let otpKeywords = ["otp_secret", "otp_type", "otp_algorithm", "otp_period", "otp_digits", "otp_counter", "otpauth"]
private static let OTPAUTH = "otpauth"
private static let OTPAUTH_URL_START = "\(OTPAUTH)://"
public var name = "" public var name = ""
public var url: URL? public var url: URL?
@ -169,8 +171,8 @@ public class Password {
// no ": " found, or empty on both sides of ": " // no ": " found, or empty on both sides of ": "
value = line value = line
// otpauth special case // otpauth special case
if value.hasPrefix("otpauth://") { if value.hasPrefix(Password.OTPAUTH_URL_START) {
key = "otpauth" key = Password.OTPAUTH
} }
} else { } else {
if !items[0].isEmpty { if !items[0].isEmpty {
@ -237,9 +239,9 @@ public class Password {
self.otpToken = nil self.otpToken = nil
// get otpauth, if we are able to generate a token, return // get otpauth, if we are able to generate a token, return
if var otpauthString = getAdditionValue(withKey: "otpauth") { if var otpauthString = getAdditionValue(withKey: Password.OTPAUTH) {
if !otpauthString.hasPrefix("otpauth:") { if !otpauthString.hasPrefix("\(Password.OTPAUTH):") {
otpauthString = "otpauth:\(otpauthString)" otpauthString = "\(Password.OTPAUTH):\(otpauthString)"
} }
if let otpauthUrl = URL(string: otpauthString), if let otpauthUrl = URL(string: otpauthString),
let token = Token(url: otpauthUrl) { let token = Token(url: otpauthUrl) {
@ -363,7 +365,7 @@ public class Password {
let (key, _) = Password.getKeyValuePair(from: line) let (key, _) = Password.getKeyValuePair(from: line)
if !Password.otpKeywords.contains(key ?? "") { if !Password.otpKeywords.contains(key ?? "") {
lines.append(line) lines.append(line)
} else if key == "otpauth" && newOtpauth != nil { } else if key == Password.OTPAUTH && newOtpauth != nil {
lines.append(newOtpauth!) lines.append(newOtpauth!)
// set to nil to prevent duplication // set to nil to prevent duplication
newOtpauth = nil newOtpauth = nil