offline_access
scope included, it returns a that can be used to request a new user token, without forcing the user to perform authentication again.
Credentials Manager
Auth0.Android provides a utility class to streamline the process of storing and renewing credentials. You can access theaccessToken
or idToken
properties from the Credentials instance. This is the preferred method to manage user credentials.
Credential Managers are included as part of the Auth0.Android SDK. If this is not yet part of your dependencies, follow the instructions in Auth0.Android.
There are two classes you can use to manage credentials:
CredentialsManager
stores data in plain text.SecureCredentialsManager
encrypts the data before storing it, using a combination of RSA and AES algorithms along with Android KeyStore.
Using the CredentialsManager class
Set up the CredentialsManager
Create a new instance by passing anAuthenticationAPIClient
and a Storage
implementation.
Current state of the authentication
Stored credentials are considered valid if they have not expired or can be refreshed. Check if a user has already logged in.Retrieving credentials
Because the credentials may need to be refreshed against Auth0 Servers, this method is asynchronous. Pass a callback implementation where you want to receive the credentials. Credentials returned by this method upon success are always valid.accessToken
has expired, the manager automatically uses the refreshToken
and renews the credentials for you. New credentials will be stored for future access.
Save new credentials
You can save the credentials obtained during authentication in the manager.Using the SecureCredentialsManager class
Set up the SecureCredentialsManager
Create a new instance by passing a valid androidContext
, an AuthenticationAPIClient
and a Storage
implementation.
Pre-authenticate the user
This class provides optional functionality for additional authentication using the device’s configured lock screen.If Lock Screen Security is set to something different than PIN, Pattern, Password or Fingerprint, this feature isn’t available.
Activity
context, a request code, and two optional strings to use as title and description for the lock screen.
Also define a request code constant so that the request code coming from the activity result can be matched up.
onActivityResult
method of the activity passed before as first parameter. If the feature was not enabled, lock screen authentication will be skipped.
After checking that the received request code matches the one used in the configuration step, redirect the received parameters to the manager to finish the authentication. The credentials will yield to the original callback.
The
onActivityResult
method was deprecated on 2021 in favor of the new Activity Result APIs. However, the Auth0.Android SDK is not compatible with these contracts. You can continue to use the deprecated methods safely.Handling usage exceptions
If an unexpected event occurs while trying to save or retrieve the Credentials, aCredentialsManagerException
will be thrown. These are some of the failure scenarios you can expect:
- The Credentials to be stored are invalid (for example, some of the following fields are not defined:
access_token
,id_token
orexpires_at
). - The stored Credentials have expired but there is no refresh token available to renew them automatically.
- The device’s lock screen security settings have changed (for example the security PIN code changed). Even when
hasCredentials
returns true, the encryption keys will be deemed invalid. UntilsaveCredentials
is called again, it won’t be possible to decrypt any previously existing content because they previously used keys are not the same as the new ones. - The device is not compatible with some of the cryptography algorithms required by the
SecureCredentialsManager
class. This is considered a catastrophic event and is the only exception that will prevent you from using this implementation. This scenario happens when the OEM has modified the Android ROM of the device, removing some of the algorithms officially included in every Android distribution. Nevertheless, you can check if this is the case in the exception instance itself by calling theisDeviceIncompatible
method. By doing so you can decide the fallback implementation for storing the credentials, such as using the regularCredentialsManager
class.