From 1f57305203b0e2b4e6121cb360a015b94d9723f7 Mon Sep 17 00:00:00 2001 From: Danny Moesch Date: Wed, 14 Nov 2018 21:29:25 +0100 Subject: [PATCH] Use constants for otp_* and other related strings --- passKit/Models/Password.swift | 28 ++++++++++++++-------------- passKit/Parser/Constants.swift | 31 +++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/passKit/Models/Password.swift b/passKit/Models/Password.swift index 6525d1e..007aa5f 100644 --- a/passKit/Models/Password.swift +++ b/passKit/Models/Password.swift @@ -156,7 +156,7 @@ public class Password { } // get secret data - guard let secretString = getAdditionValue(withKey: "otp_secret"), + guard let secretString = getAdditionValue(withKey: Constants.OTP_SECRET), let secretData = MF_Base32Codec.data(fromBase32String: secretString), !secretData.isEmpty else { // Missing / Invalid otp secret @@ -164,19 +164,19 @@ public class Password { } // get type - guard let type = getAdditionValue(withKey: "otp_type")?.lowercased(), - (type == "totp" || type == "hotp") else { - // Missing / Invalid otp type + guard let type = getAdditionValue(withKey: Constants.OTP_TYPE)?.lowercased(), + (type == Constants.TOTP || type == Constants.HOTP) else { + // Missing/Invalid OTP type return } // get algorithm (optional) var algorithm = Generator.Algorithm.sha1 - if let algoString = getAdditionValue(withKey: "otp_algorithm") { + if let algoString = getAdditionValue(withKey: Constants.OTP_ALGORITHM) { switch algoString.lowercased() { - case "sha256": + case Constants.SHA256: algorithm = .sha256 - case "sha512": + case Constants.SHA512: algorithm = .sha512 default: algorithm = .sha1 @@ -184,12 +184,12 @@ public class Password { } // construct the token - if type == "totp" { + if type == Constants.TOTP { // HOTP // default: 6 digits, 30 seconds - guard let digits = Int(getAdditionValue(withKey: "otp_digits") ?? "6"), - let period = Double(getAdditionValue(withKey: "otp_period") ?? "30.0") else { - // Invalid otp_digits or otp_period. + guard let digits = Int(getAdditionValue(withKey: Constants.OTP_DIGITS) ?? Constants.DEFAULT_DIGITS), + let period = Double(getAdditionValue(withKey: Constants.OTP_PERIOD) ?? Constants.DEFAULT_PERIOD) else { + // Invalid OTP digits or OTP period. return } guard let generator = Generator( @@ -204,9 +204,9 @@ public class Password { } else { // HOTP // default: 6 digits - guard let digits = Int(getAdditionValue(withKey: "otp_digits") ?? "6"), - let counter = UInt64(getAdditionValue(withKey: "otp_counter") ?? "") else { - // Invalid otp_digits or otp_counter. + guard let digits = Int(getAdditionValue(withKey: Constants.OTP_DIGITS) ?? Constants.DEFAULT_DIGITS), + let counter = UInt64(getAdditionValue(withKey: Constants.OTP_COUNTER) ?? Constants.DEFAULT_COUNTER) else { + // Invalid OTP digits or OTP counter. return } guard let generator = Generator( diff --git a/passKit/Parser/Constants.swift b/passKit/Parser/Constants.swift index 11786ea..96cfd19 100644 --- a/passKit/Parser/Constants.swift +++ b/passKit/Parser/Constants.swift @@ -8,23 +8,38 @@ public struct Constants { + static let OTP_SECRET = "otp_secret" + static let OTP_TYPE = "otp_type" + static let OTP_ALGORITHM = "otp_algorithm" + static let OTP_PERIOD = "otp_period" + static let OTP_DIGITS = "otp_digits" + static let OTP_COUNTER = "otp_counter" + static let OTPAUTH = "otpauth" + public static let OTP_KEYWORDS = [ - "otp_secret", - "otp_type", - "otp_algorithm", - "otp_period", - "otp_digits", - "otp_counter", - "otpauth", + OTP_SECRET, + OTP_TYPE, + OTP_ALGORITHM, + OTP_PERIOD, + OTP_DIGITS, + OTP_COUNTER, + OTPAUTH, ] + static let TOTP = "totp" + static let HOTP = "hotp" + static let SHA256 = "sha256" + static let SHA512 = "sha512" + static let DEFAULT_DIGITS = "6" + static let DEFAULT_PERIOD = "30.0" + static let DEFAULT_COUNTER = "" + static let BLANK = " " static let MULTILINE_WITH_LINE_BREAK_INDICATOR = "|" static let MULTILINE_WITH_LINE_BREAK_SEPARATOR = "\n" static let MULTILINE_WITHOUT_LINE_BREAK_INDICATOR = ">" static let MULTILINE_WITHOUT_LINE_BREAK_SEPARATOR = BLANK - static let OTPAUTH = "otpauth" static let OTPAUTH_URL_START = "\(OTPAUTH)://" static let PASSWORD_KEYWORD = "password" static let USERNAME_KEYWORD = "username"