Support Steam OTP (#505)

This commit is contained in:
Mingshen Sun 2021-09-06 10:47:04 -07:00 committed by GitHub
parent 06d2ef1d09
commit f2ab400f4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 34 deletions

View file

@ -165,6 +165,7 @@ public class Password {
.usingDigits(getAdditionValue(withKey: Constants.OTP_DIGITS))
.usingPeriod(getAdditionValue(withKey: Constants.OTP_PERIOD))
.usingCounter(getAdditionValue(withKey: Constants.OTP_COUNTER))
.usingRepresentation(getAdditionValue(withKey: Constants.OTP_REPRESENTATION))
.build()
}

View file

@ -6,6 +6,8 @@
// Copyright © 2018 Bob Sun. All rights reserved.
//
import OneTimePassword
public enum Constants {
static let OTP_SECRET = "otp_secret"
static let OTP_TYPE = "otp_type"
@ -13,6 +15,7 @@ public enum Constants {
static let OTP_PERIOD = "otp_period"
static let OTP_DIGITS = "otp_digits"
static let OTP_COUNTER = "otp_counter"
static let OTP_REPRESENTATION = "otp_representation"
static let OTPAUTH = "otpauth"
public static let OTP_KEYWORDS = [
@ -22,6 +25,7 @@ public enum Constants {
OTP_PERIOD,
OTP_DIGITS,
OTP_COUNTER,
OTP_REPRESENTATION,
OTPAUTH,
]
@ -32,6 +36,7 @@ public enum Constants {
static let DEFAULT_DIGITS = 6
static let DEFAULT_PERIOD = 30.0
static let DEFAULT_COUNTER: UInt64? = nil
static let DEFAULT_REPRESENTATION: OneTimePassword.Generator.Representation = .numeric
static let BLANK = " "
static let MULTILINE_WITH_LINE_BREAK_INDICATOR = "|"

View file

@ -34,6 +34,7 @@ class TokenBuilder {
private var digits: Int? = Constants.DEFAULT_DIGITS
private var period: Double? = Constants.DEFAULT_PERIOD
private var counter: UInt64? = Constants.DEFAULT_COUNTER
private var representation: OneTimePassword.Generator.Representation = Constants.DEFAULT_REPRESENTATION
func usingName(_ name: String) -> TokenBuilder {
self.name = name
@ -79,6 +80,18 @@ class TokenBuilder {
return self
}
func usingRepresentation(_ representation: String?) -> TokenBuilder {
switch representation {
case "numeric":
self.representation = .numeric
case "steamguard":
self.representation = .steamguard
default:
self.representation = .numeric
}
return self
}
func build() -> Token? {
guard secret != nil, digits != nil else {
return nil
@ -95,7 +108,7 @@ class TokenBuilder {
}
private func createToken(factor: Generator.Factor) -> Token? {
guard let generator = Generator(factor: factor, secret: secret!, algorithm: algorithm, digits: digits!) else {
guard let generator = Generator(factor: factor, secret: secret!, algorithm: algorithm, digits: digits!, representation: representation) else {
return nil
}
return Token(name: name, issuer: "", generator: generator)