Package com.iizix.server.user.mfa
Class MFAUser
java.lang.Object
com.iizix.server.user.mfa.MFAUser
The class used to provide an MFA authentication using a TOTP based QR code. The authentication software will use this QR code to set-up the users' account. The sign in, the user will have to provide a six digit code that is valid during a certain period of time, generally 60 seconds, with a time drift of +/-30 seconds. Oftentimes, allow codes valid for 2 time periods before/after to pass as valid.
- Nested Class SummaryNested ClassesModifier and TypeClassDescription- static enumEnum to define valid hashing algorithms.
- Field SummaryFieldsModifier and TypeFieldDescription- static StringThe default time provider host: "pool.ntp.org".- static intDefault timeout in milliseconds: 3_000 (or 3 seconds).- static final intThe default number of digits: 6.- final intThe drift period, default +/- 30 seconds.- static final intThe default number of characters in the secret: 32.
- Constructor SummaryConstructors
- Method SummaryModifier and TypeMethodDescription- generateQRCodeData- (ErrorCorrectionLevel errorCorrectionLevel) Generate the QR code data with relevant information with specified error correction level.Generate the secret with the- secretLengthspecified, default- SECRET_LENGTH = 32.Gets or generate the secret with the- secretLengthspecified, default- SECRET_LENGTH = 32, if not previously done.Sets the algorithm to use when creating the QR code.- setDigits- (int digits) Sets the number of digits to use for the code.- setPeriod- (int period) Sets the drift period in +/- seconds to use, default is- PERIOD = 30.- setTimeProvider- (String timeProviderHost, int timeoutMilliseconds) Before validating a code, the time provider host and the timeout value in milliseconds can be set.- boolean- validateCode- (String code) Validates a code.
- Field Details- SECRET_LENGTHpublic static final int SECRET_LENGTHThe default number of characters in the secret: 32.- See Also:
 
- DIGITSpublic static final int DIGITSThe default number of digits: 6.- See Also:
 
- PERIODpublic final int PERIODThe drift period, default +/- 30 seconds.- See Also:
 
- DEFAULT_TIME_PROVIDER_HOSTThe default time provider host: "pool.ntp.org".
- DEFAULT_TIMEOUT_MILLISECONDSpublic static int DEFAULT_TIMEOUT_MILLISECONDSDefault timeout in milliseconds: 3_000 (or 3 seconds).
 
- Constructor Details- MFAUserConstructor of the instance for a six digit code.- Parameters:
- appName- The application name as the issuer, in localized form (should never change).
- account- The account name is generally something to identify uniquely the user, generally an email address.
 
 
- Method Details- setDigitsSets the number of digits to use for the code.- Setting how many digits long the generated codes are: - Most TOTP implementations generate codes that are 6 digits long, but codes can have a length of any positive non-zero integer. - Parameters:
- digits- The requested number of digits (default 6, minimum 4 and maximum 8 is recommended).
- Returns:
- "this" instance for concatenation.
- Throws:
- IllegalArgumentException- If digits range is not within 4 to 8.
 
- setPeriodSets the drift period in +/- seconds to use, default is- PERIOD = 30.- Setting the time period and discrepancy: - The one time password codes generated in the authenticator apps only last for a certain time period before they are re-generated, and most implementations of TOTP allow room for codes that have recently expired, or will only "become valid" soon in the future to be accepted as valid, to allow for a small time drift between the server and the authenticator app (discrepancy). - Parameters:
- period- The requested period in seconds (range is 10 to 60).
- Returns:
- "this" instance for concatenation.
- Throws:
- IllegalArgumentException- If period range is not within 10 to 60 seconds.
 
- setAlgorithmSets the algorithm to use when creating the QR code.- Parameters:
- alg- The hashing algorithm.
- Returns:
- "this" instance for concatenation.
- Throws:
- NullPointerExecption- If- algis- null.
 
- generateSecretGenerate the secret with the- secretLengthspecified, default- SECRET_LENGTH = 32. The secret is only generated once, so there is no point in calling this method many times, nothing will happen.- Returns:
- "this" instance for concatenation.
 
- getSecretGets or generate the secret with the- secretLengthspecified, default- SECRET_LENGTH = 32, if not previously done.- Returns:
- The secret.
 
- generateQRCodeDatapublic BarcodeURN generateQRCodeData- (ErrorCorrectionLevel errorCorrectionLevel) throws BarcodeException Generate the QR code data with relevant information with specified error correction level.- Parameters:
- errorCorrectionLevel-- nullfor default L = Low = ~7% error correction level.
- Returns:
- The generated barcode URN from where e.g. an SVG can be extracted.
- Throws:
- IllegalStateException- If the secret is missing (has not been generated).
- BarcodeException- If there are missing or invalid parameters assigned in this builder encountered during barcode encoding.
 
- setTimeProviderBefore validating a code, the time provider host and the timeout value in milliseconds can be set. If this is not done, the default values are used:- timeProviderHost = DEFAULT_TIME_PROVIDER_HOST timeoutMilliseconds = DEFAULT_TIMEOUT_MILLISECONDS - Parameters:
- timeProviderHost- The time provider host.
- timeoutMilliseconds- The timeout value in milliseconds (must be over 500 milliseconds).
- Returns:
- "this" instance for concatenation.
- Throws:
- IllegalArgumentException- If the- timeoutMillisecondsvalue is less than 500 milliseconds or larger than 60_000 milliseconds (1 minute).
- NullPointerException- If the- timeProviderHostis- null.
 
- validateCodeValidates a code.- Parameters:
- code- The code.
- Returns:
- The valid code flag.
- Throws:
- UnknownHostException- In case the- timeProviderHostis an unknown host.