Use constants for otp_* and other related strings

This commit is contained in:
Danny Moesch 2018-11-14 21:29:25 +01:00 committed by Bob Sun
parent 60c509dba0
commit 1f57305203
2 changed files with 37 additions and 22 deletions

View file

@ -156,7 +156,7 @@ public class Password {
} }
// get secret data // 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), let secretData = MF_Base32Codec.data(fromBase32String: secretString),
!secretData.isEmpty else { !secretData.isEmpty else {
// Missing / Invalid otp secret // Missing / Invalid otp secret
@ -164,19 +164,19 @@ public class Password {
} }
// get type // get type
guard let type = getAdditionValue(withKey: "otp_type")?.lowercased(), guard let type = getAdditionValue(withKey: Constants.OTP_TYPE)?.lowercased(),
(type == "totp" || type == "hotp") else { (type == Constants.TOTP || type == Constants.HOTP) else {
// Missing / Invalid otp type // Missing/Invalid OTP type
return return
} }
// get algorithm (optional) // get algorithm (optional)
var algorithm = Generator.Algorithm.sha1 var algorithm = Generator.Algorithm.sha1
if let algoString = getAdditionValue(withKey: "otp_algorithm") { if let algoString = getAdditionValue(withKey: Constants.OTP_ALGORITHM) {
switch algoString.lowercased() { switch algoString.lowercased() {
case "sha256": case Constants.SHA256:
algorithm = .sha256 algorithm = .sha256
case "sha512": case Constants.SHA512:
algorithm = .sha512 algorithm = .sha512
default: default:
algorithm = .sha1 algorithm = .sha1
@ -184,12 +184,12 @@ public class Password {
} }
// construct the token // construct the token
if type == "totp" { if type == Constants.TOTP {
// HOTP // HOTP
// default: 6 digits, 30 seconds // default: 6 digits, 30 seconds
guard let digits = Int(getAdditionValue(withKey: "otp_digits") ?? "6"), guard let digits = Int(getAdditionValue(withKey: Constants.OTP_DIGITS) ?? Constants.DEFAULT_DIGITS),
let period = Double(getAdditionValue(withKey: "otp_period") ?? "30.0") else { let period = Double(getAdditionValue(withKey: Constants.OTP_PERIOD) ?? Constants.DEFAULT_PERIOD) else {
// Invalid otp_digits or otp_period. // Invalid OTP digits or OTP period.
return return
} }
guard let generator = Generator( guard let generator = Generator(
@ -204,9 +204,9 @@ public class Password {
} else { } else {
// HOTP // HOTP
// default: 6 digits // default: 6 digits
guard let digits = Int(getAdditionValue(withKey: "otp_digits") ?? "6"), guard let digits = Int(getAdditionValue(withKey: Constants.OTP_DIGITS) ?? Constants.DEFAULT_DIGITS),
let counter = UInt64(getAdditionValue(withKey: "otp_counter") ?? "") else { let counter = UInt64(getAdditionValue(withKey: Constants.OTP_COUNTER) ?? Constants.DEFAULT_COUNTER) else {
// Invalid otp_digits or otp_counter. // Invalid OTP digits or OTP counter.
return return
} }
guard let generator = Generator( guard let generator = Generator(

View file

@ -8,23 +8,38 @@
public struct Constants { 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 = [ public static let OTP_KEYWORDS = [
"otp_secret", OTP_SECRET,
"otp_type", OTP_TYPE,
"otp_algorithm", OTP_ALGORITHM,
"otp_period", OTP_PERIOD,
"otp_digits", OTP_DIGITS,
"otp_counter", OTP_COUNTER,
"otpauth", 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 BLANK = " "
static let MULTILINE_WITH_LINE_BREAK_INDICATOR = "|" static let MULTILINE_WITH_LINE_BREAK_INDICATOR = "|"
static let MULTILINE_WITH_LINE_BREAK_SEPARATOR = "\n" static let MULTILINE_WITH_LINE_BREAK_SEPARATOR = "\n"
static let MULTILINE_WITHOUT_LINE_BREAK_INDICATOR = ">" static let MULTILINE_WITHOUT_LINE_BREAK_INDICATOR = ">"
static let MULTILINE_WITHOUT_LINE_BREAK_SEPARATOR = BLANK static let MULTILINE_WITHOUT_LINE_BREAK_SEPARATOR = BLANK
static let OTPAUTH = "otpauth"
static let OTPAUTH_URL_START = "\(OTPAUTH)://" static let OTPAUTH_URL_START = "\(OTPAUTH)://"
static let PASSWORD_KEYWORD = "password" static let PASSWORD_KEYWORD = "password"
static let USERNAME_KEYWORD = "username" static let USERNAME_KEYWORD = "username"