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
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(

View file

@ -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"