Move parts of the documentation into the TokenBuilder class

This commit is contained in:
Danny Moesch 2018-12-02 16:55:35 +01:00 committed by Bob Sun
parent 2e744a760f
commit 469451b7b7
2 changed files with 31 additions and 23 deletions

View file

@ -121,29 +121,19 @@ public class Password {
return additions.first(where: { toLowercase($0.title) == toLowercase(key) })?.content return additions.first(where: { toLowercase($0.title) == toLowercase(key) })?.content
} }
/* /// Set the OTP token if we are able to construct a valid one.
Set otpType and otpToken, if we are able to construct a valid token. ///
/// Example of TOTP otpauth:
Example of TOTP otpauth ///
(Key Uri Format: https://github.com/google/google-authenticator/wiki/Key-Uri-Format) /// otpauth://totp/totp-secret?secret=AAAAAAAAAAAAAAAA&issuer=totp-secret
otpauth://totp/totp-secret?secret=AAAAAAAAAAAAAAAA&issuer=totp-secret ///
/// See also [Key Uri Format](https://github.com/google/google-authenticator/wiki/Key-Uri-Format).
Example of TOTP fields [Legacy, lower priority] ///
otp_secret: secretsecretsecretsecretsecretsecret /// In case no otpauth is given in the password file, try to construct the token from separate fields using a
otp_type: totp /// `TokenBuilder`. This means that tokens provided as otpauth have higher priority.
otp_algorithm: sha1 (default: sha1, optional) ///
otp_period: 30 (default: 30, optional)
otp_digits: 6 (default: 6, optional)
Example of HOTP fields [Legacy, lower priority]
otp_secret: secretsecretsecretsecretsecretsecret
otp_type: hotp
otp_counter: 1
otp_digits: 6 (default: 6, optional)
*/
private func updateOtpToken() { private func updateOtpToken() {
// 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: Constants.OTPAUTH, caseSensitive: true) { if var otpauthString = getAdditionValue(withKey: Constants.OTPAUTH, caseSensitive: true) {
if !otpauthString.hasPrefix("\(Constants.OTPAUTH):") { if !otpauthString.hasPrefix("\(Constants.OTPAUTH):") {
otpauthString = "\(Constants.OTPAUTH):\(otpauthString)" otpauthString = "\(Constants.OTPAUTH):\(otpauthString)"
@ -153,7 +143,8 @@ public class Password {
return return
} }
} }
// Construct OTP token from separate fields provided in the password file.
otpToken = TokenBuilder() otpToken = TokenBuilder()
.usingName(name) .usingName(name)
.usingSecret(getAdditionValue(withKey: Constants.OTP_SECRET)) .usingSecret(getAdditionValue(withKey: Constants.OTP_SECRET))

View file

@ -9,6 +9,23 @@
import Base32 import Base32
import OneTimePassword import OneTimePassword
/// Help building an OTP token from given data.
///
/// There is currently support TOTP and HOTP tokens:
///
/// * Necessary TOTP data
/// * secret: `secretsecretsecretsecretsecretsecret`
/// * type: `totp`
/// * algorithm: `sha1` (default: `sha1`, optional)
/// * period: `30` (default: `30`, optional)
/// * digits: `6` (default: `6`, optional)
///
/// * Necessary HOTP data
/// * secret: `secretsecretsecretsecretsecretsecret`
/// * type: `hotp`
/// * counter: `1`
/// * digits: `6` (default: `6`, optional)
///
class TokenBuilder { class TokenBuilder {
private var name: String = "" private var name: String = ""