Class Totp
-
Method Summary
Modifier and TypeMethodDescriptionstatic final StringgenerateCode(String key, int codeDigits, Duration time) Generates a TOTP (Time-Based One-Time Password) code.static StringgenerateCode(String key, int codeDigits, Duration time, Duration startTime) Generates a TOTP (Time-Based One-Time Password) code.static final StringGenerate a secret key, for Multi-Factor Authentication.static StringgenerateSecretKey(int length) Generate a secret key, for Multi-Factor Authentication.static final booleanvalidateCode(String key, String code, int codeDigits, Duration time) Validate the givencodewith the given time frame.static booleanvalidateCode(String key, String code, int codeDigits, Duration time, Duration startTime) Validate the givencodewith the given time frame.
-
Method Details
-
generateSecretKey
Generate a secret key, for Multi-Factor Authentication.This will generate a base32-encoded string, of
length. This will only contain characters from [A-Z2-7]. The generated string is created securely, and sufficiently random for cryptographic purposes.This string returned by this function can be used for a user as their shared secret to generate and verify TOTP codes.
Secret keys with length between 16 and 256 are allowed. By default the length will be 32.
-
generateSecretKey
Generate a secret key, for Multi-Factor Authentication.Returns
generateSecretKey(32) -
generateCode
Generates a TOTP (Time-Based One-Time Password) code.This code is generated from a secret
key, at the specifiedtime. The code will be of lengthcodeDigits.The
keyshould be a base32-encoded string, with a length between 16 and 256. ThecodeDigitsparameter should be at least 6 characters, and at most be 16 characters long. Supplying acodeDigitsoutside of this boundary will result in an exception being thrown.The specified time will be the time the code is generated. This ensures that the TOTP algorithm generates a different code for each time window, where the width of a window is 30 seconds.
The
startTimeis optional and is used to define an offset. This offset will be subtracted from the actualtime. It can be used to define a starting point. -
generateCode
Generates a TOTP (Time-Based One-Time Password) code.Returns
generateCode(key, codeDigits, time, Duration.ofSeconds(0)) -
validateCode
public static boolean validateCode(String key, String code, int codeDigits, Duration time, Duration startTime) Validate the givencodewith the given time frame.Here the
keyis the secret key attached to theUser, thecodeis the TOTP code the user has entered, which is expected to be of lengthcodeDigits. This length is configured inAuthService#setMfaCodeLength().The
timespecifies the time window for which the code is valid. When this function executes, the code will be generated for the time frame the passedtimefalls in, and in the previous window. Each window has a width of 30 seconds. Meaning that at most a user has 1 minute to enter the code (if they submit it immediately at the start of the first time frame). Or at least 30 seconds (if they submit it at the end of the first time frame).Time frames start either immediately on the minute, or halfway. This means that for the times:
- 12:52:12, the start time frame will be 12:52:00
- 12:52:48, the start time frame will be 12:52:30
The
startTimeis optional and is used to define an offset. This offset will be subtracted from the actualtime. It can be used to define a starting point. -
validateCode
Validate the givencodewith the given time frame.Returns
validateCode(key, code, codeDigits, time, Duration.ofSeconds(0))
-