The library provides the ICredentialRepository interface to bridge the gap between the library and your application's user storage. This allows you to use user names directly in API calls instead of manually passing secret keys.
Implementation Requirements
You must implement two methods in your ICredentialRepository implementation:
String getSecretKey(String userName)void saveUserCredentials(String userName, ...)
Registering the Repository
You can register your repository in two ways:
- Per-instance: Use the
credentialRepository property on the IGoogleAuthenticator interface. - ServiceLoader: Place a file named
com.warrenstrange.googleauth.ICredentialRepository in the META-INF/services package containing the fully qualified name of your provider class.
Usage with Repository
Once configured, you can use user-centric methods:
createCredentials(userName): Creates credentials and automatically saves them via the repository.authorizeUser(userName, code): Retrieves the secret key for the user from the repository and validates the code.
// Setup with a repository
GoogleAuthenticator gAuth = new GoogleAuthenticator();
// (Assuming repository is configured via ServiceLoader or property)
// Create and store credentials for 'Bob'
final GoogleAuthenticatorKey key = gAuth.createCredentials("Bob");
// Verify a code for 'Bob'
boolean isCodeValid = gAuth.authorizeUser("Bob", code);