public final class GoogleAuthenticator extends Object implements IGoogleAuthenticator
time = 0 (the UNIX epoch) and the
URL of a Google-provided QR barcode to let an user load the generated
information into Google Authenticator.
The random number generator used by this class uses the default algorithm and
provider. Users can override them by setting the following system properties
to the algorithm and provider name of their choice:
This class does not store in any way either the generated keys nor the keys
passed during the authorization process.
Java Server side class for Google Authenticator's TOTP generator was inspired
by an author's blog post.| Modifier and Type | Field and Description |
|---|---|
static String |
RNG_ALGORITHM
The system property to specify the random number generator algorithm to use.
|
static String |
RNG_ALGORITHM_PROVIDER
The system property to specify the random number generator provider to use.
|
static int |
SCRATCH_CODE_MODULUS
Modulus used to truncate the scratch code.
|
| Constructor and Description |
|---|
GoogleAuthenticator() |
GoogleAuthenticator(GoogleAuthenticatorConfig config) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
authorize(String secret,
int verificationCode)
Checks a verification code against a secret key using the current time.
|
boolean |
authorize(String secret,
int verificationCode,
long time)
Checks a verification code against a secret key using the specified time.
|
boolean |
authorizeUser(String userName,
int verificationCode)
This method validates a verification code of the specified user whose
private key is retrieved from the configured credential repository using
the current time.
|
boolean |
authorizeUser(String userName,
int verificationCode,
long time)
This method validates a verification code of the specified user whose
private key is retrieved from the configured credential repository.
|
GoogleAuthenticatorKey |
createCredentials()
This method generates a new set of credentials including:
Secret key.
Validation code.
A list of scratch codes.
The user must register this secret on their device.
|
GoogleAuthenticatorKey |
createCredentials(String userName)
This method generates a new set of credentials invoking the
#createCredentials method with no arguments. |
ICredentialRepository |
getCredentialRepository()
This method loads the first available ICredentialRepository
registered using the Java service loader API.
|
int |
getTotpPassword(String secret)
This method generates the current TOTP password.
|
int |
getTotpPassword(String secret,
long time)
This method generates the TOTP password at the specified time.
|
int |
getTotpPasswordOfUser(String userName)
This method generates the current TOTP password.
|
int |
getTotpPasswordOfUser(String userName,
long time)
This method generates the TOTP password at the specified time.
|
void |
setCredentialRepository(ICredentialRepository repository)
This method sets the credential repository used by this instance.
|
public static final String RNG_ALGORITHM
public static final String RNG_ALGORITHM_PROVIDER
public static final int SCRATCH_CODE_MODULUS
public GoogleAuthenticator()
public GoogleAuthenticator(GoogleAuthenticatorConfig config)
public GoogleAuthenticatorKey createCredentials()
IGoogleAuthenticatorcreateCredentials in interface IGoogleAuthenticatorpublic GoogleAuthenticatorKey createCredentials(String userName)
IGoogleAuthenticator#createCredentials method with no arguments. The generated
credentials are then saved using the configured
#ICredentialRepository service.
The user must register this secret on their device.createCredentials in interface IGoogleAuthenticatoruserName - the user name.public int getTotpPassword(String secret)
IGoogleAuthenticatorgetTotpPassword in interface IGoogleAuthenticatorsecret - the encoded secret key.public int getTotpPassword(String secret, long time)
IGoogleAuthenticatorgetTotpPassword in interface IGoogleAuthenticatorsecret - The encoded secret key.time - The time to use to calculate the password.public int getTotpPasswordOfUser(String userName)
IGoogleAuthenticatorgetTotpPasswordOfUser in interface IGoogleAuthenticatoruserName - The user whose password must be created.public int getTotpPasswordOfUser(String userName, long time)
IGoogleAuthenticatorgetTotpPasswordOfUser in interface IGoogleAuthenticatoruserName - The user whose password must be created.time - The time to use to calculate the password.public boolean authorize(String secret, int verificationCode) throws GoogleAuthenticatorException
IGoogleAuthenticatorauthorize in interface IGoogleAuthenticatorsecret - the encoded secret key.verificationCode - the verification code.true if the validation code is valid,
false otherwise.GoogleAuthenticatorException - if a failure occurs during the
calculation of the validation code.
The only failures that should occur
are related with the cryptographic
functions provided by the JCE.IGoogleAuthenticator.authorize(String, int, long)public boolean authorize(String secret, int verificationCode, long time) throws GoogleAuthenticatorException
IGoogleAuthenticatorwindowSize property of this class.
The default value of 30 seconds recommended by RFC 6238 is used for the
interval size.authorize in interface IGoogleAuthenticatorsecret - The encoded secret key.verificationCode - The verification code.time - The time to use to calculate the TOTP password..true if the validation code is valid, false
otherwise.GoogleAuthenticatorException - if a failure occurs during the
calculation of the validation code.
The only failures that should occur
are related with the cryptographic
functions provided by the JCE.public boolean authorizeUser(String userName, int verificationCode) throws GoogleAuthenticatorException
IGoogleAuthenticatorIGoogleAuthenticator.authorizeUser(String, int, long).authorizeUser in interface IGoogleAuthenticatoruserName - The user whose verification code is to be
validated.verificationCode - The validation code.true if the validation code is valid,
false otherwise.GoogleAuthenticatorException - if an unexpected error occurs.IGoogleAuthenticator.authorize(String, int)public boolean authorizeUser(String userName, int verificationCode, long time) throws GoogleAuthenticatorException
IGoogleAuthenticatorIGoogleAuthenticator.authorize(String, int, long) method.authorizeUser in interface IGoogleAuthenticatoruserName - The user whose verification code is to be
validated.verificationCode - The validation code.time - The time to use to calculate the TOTP password.true if the validation code is valid,
false otherwise.GoogleAuthenticatorException - if an unexpected error occurs.IGoogleAuthenticator.authorize(String, int)public ICredentialRepository getCredentialRepository()
getCredentialRepository in interface IGoogleAuthenticatornull
if none is found.public void setCredentialRepository(ICredentialRepository repository)
IGoogleAuthenticatornull is passed to this method, no credential repository will be
used, nor discovered using the ServiceLoader API.setCredentialRepository in interface IGoogleAuthenticatorrepository - The credential repository to use, or null to
disable this feature.Copyright © 2017. All rights reserved.