Package sk.iway.iwcm.system.googleauth
Class GoogleAuthenticator
java.lang.Object
sk.iway.iwcm.system.googleauth.GoogleAuthenticator
- All Implemented Interfaces:
IGoogleAuthenticator
GoogleAuthenticator.java
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks a verification code against a secret key using the current time.boolean
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.This method generates a new set of credentials including: Secret key. Validation code. A list of scratch codes.createCredentials
(String userName) This method generates a new set of credentials invoking the#createCredentials
method with no arguments.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.
-
Field Details
-
RNG_ALGORITHM
The system property to specify the random number generator algorithm to use.- Since:
- 0.5.0
- See Also:
-
RNG_ALGORITHM_PROVIDER
The system property to specify the random number generator provider to use.- Since:
- 0.5.0
- See Also:
-
SCRATCH_CODE_MODULUS
public static final int SCRATCH_CODE_MODULUSModulus used to truncate the scratch code.
-
-
Constructor Details
-
GoogleAuthenticator
public GoogleAuthenticator() -
GoogleAuthenticator
-
-
Method Details
-
createCredentials
Description copied from interface:IGoogleAuthenticator
This method generates a new set of credentials including:- Secret key.
- Validation code.
- A list of scratch codes.
- Specified by:
createCredentials
in interfaceIGoogleAuthenticator
- Returns:
- secret key
-
createCredentials
Description copied from interface:IGoogleAuthenticator
This method generates a new set of credentials invoking the#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.- Specified by:
createCredentials
in interfaceIGoogleAuthenticator
- Parameters:
userName
- the user name.- Returns:
- secret key
-
getTotpPassword
Description copied from interface:IGoogleAuthenticator
This method generates the current TOTP password.- Specified by:
getTotpPassword
in interfaceIGoogleAuthenticator
- Parameters:
secret
- the encoded secret key.- Returns:
- the current TOTP password.
-
getTotpPassword
Description copied from interface:IGoogleAuthenticator
This method generates the TOTP password at the specified time.- Specified by:
getTotpPassword
in interfaceIGoogleAuthenticator
- Parameters:
secret
- The encoded secret key.time
- The time to use to calculate the password.- Returns:
- the TOTP password at the specified time.
-
getTotpPasswordOfUser
Description copied from interface:IGoogleAuthenticator
This method generates the current TOTP password.- Specified by:
getTotpPasswordOfUser
in interfaceIGoogleAuthenticator
- Parameters:
userName
- The user whose password must be created.- Returns:
- the current TOTP password.
-
getTotpPasswordOfUser
Description copied from interface:IGoogleAuthenticator
This method generates the TOTP password at the specified time.- Specified by:
getTotpPasswordOfUser
in interfaceIGoogleAuthenticator
- Parameters:
userName
- The user whose password must be created.time
- The time to use to calculate the password.- Returns:
- the TOTP password at the specified time.
-
authorize
Description copied from interface:IGoogleAuthenticator
Checks a verification code against a secret key using the current time.- Specified by:
authorize
in interfaceIGoogleAuthenticator
- Parameters:
secret
- the encoded secret key.verificationCode
- the verification code.- Returns:
true
if the validation code is valid,false
otherwise.- Throws:
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.- See Also:
-
authorize
public boolean authorize(String secret, int verificationCode, long time) throws GoogleAuthenticatorException Description copied from interface:IGoogleAuthenticator
Checks a verification code against a secret key using the specified time. The algorithm also checks in a time window whose size determined by thewindowSize
property of this class. The default value of 30 seconds recommended by RFC 6238 is used for the interval size.- Specified by:
authorize
in interfaceIGoogleAuthenticator
- Parameters:
secret
- The encoded secret key.verificationCode
- The verification code.time
- The time to use to calculate the TOTP password..- Returns:
true
if the validation code is valid,false
otherwise.- Throws:
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.
-
authorizeUser
public boolean authorizeUser(String userName, int verificationCode) throws GoogleAuthenticatorException Description copied from interface:IGoogleAuthenticator
This method validates a verification code of the specified user whose private key is retrieved from the configured credential repository using the current time. This method delegates the validation to theIGoogleAuthenticator.authorizeUser(String, int, long)
.- Specified by:
authorizeUser
in interfaceIGoogleAuthenticator
- Parameters:
userName
- The user whose verification code is to be validated.verificationCode
- The validation code.- Returns:
true
if the validation code is valid,false
otherwise.- Throws:
GoogleAuthenticatorException
- if an unexpected error occurs.- See Also:
-
authorizeUser
public boolean authorizeUser(String userName, int verificationCode, long time) throws GoogleAuthenticatorException Description copied from interface:IGoogleAuthenticator
This method validates a verification code of the specified user whose private key is retrieved from the configured credential repository. This method delegates the validation to theIGoogleAuthenticator.authorize(String, int, long)
method.- Specified by:
authorizeUser
in interfaceIGoogleAuthenticator
- Parameters:
userName
- The user whose verification code is to be validated.verificationCode
- The validation code.time
- The time to use to calculate the TOTP password.- Returns:
true
if the validation code is valid,false
otherwise.- Throws:
GoogleAuthenticatorException
- if an unexpected error occurs.- See Also:
-
getCredentialRepository
This method loads the first available ICredentialRepository registered using the Java service loader API.- Specified by:
getCredentialRepository
in interfaceIGoogleAuthenticator
- Returns:
- the first registered ICredentialRepository or
null
if none is found.
-
setCredentialRepository
Description copied from interface:IGoogleAuthenticator
This method sets the credential repository used by this instance. Ifnull
is passed to this method, no credential repository will be used, nor discovered using the ServiceLoader API.- Specified by:
setCredentialRepository
in interfaceIGoogleAuthenticator
- Parameters:
repository
- The credential repository to use, ornull
to disable this feature.
-