Skip to content

Commit dc3b459

Browse files
committed
doc: Updated the Examples.md file with the new CredentialsManagerExceptions (#946)
1 parent 1c0b25e commit dc3b459

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

EXAMPLES.md

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ if (DPoP.isNonceRequiredError(response)) {
280280
}
281281
```
282282

283+
When using DPoP with `CredentialsManager` or `SecureCredentialsManager`, the `AuthenticationAPIClient` passed to the credentials manager **must** also have DPoP enabled. Otherwise, token refresh requests will be sent without the DPoP proof and the SDK will throw a `CredentialsManagerException.DPOP_NOT_CONFIGURED` error.
284+
285+
```kotlin
286+
287+
val auth0 = Auth0.getInstance("YOUR_CLIENT_ID", "YOUR_DOMAIN")
288+
val apiClient = AuthenticationAPIClient(auth0).useDPoP(context) // DPoP enabled
289+
val storage = SharedPreferencesStorage(context)
290+
val manager = CredentialsManager(apiClient, storage)
291+
292+
WebAuthProvider
293+
.useDPoP()
294+
.login(auth0)
295+
.start(context, callback)
296+
```
297+
283298
On logout, you should call `DPoP.clearKeyPair()` to delete the user's key pair from the Keychain.
284299

285300
```kotlin
@@ -293,7 +308,7 @@ WebAuthProvider.logout(account)
293308

294309
})
295310
```
296-
> [!NOTE]
311+
> [!NOTE]
297312
> DPoP is supported only on Android version 6.0 (API level 23) and above. Trying to use DPoP in any older versions will result in an exception.
298313
299314
## Authentication API
@@ -1662,11 +1677,21 @@ val auth0 = Auth0.getInstance("YOUR_CLIENT_ID", "YOUR_DOMAIN")
16621677
val apiClient = AuthenticationAPIClient(auth0).useDPoP(this)
16631678
val storage = SharedPreferencesStorage(this)
16641679
val manager = SecureCredentialsManager(apiClient, this, auth0, storage)
1680+
```
1681+
1682+
Similarly, for `CredentialsManager`:
16651683

1684+
```kotlin
1685+
val auth0 = Auth0.getInstance("YOUR_CLIENT_ID", "YOUR_DOMAIN")
1686+
val apiClient = AuthenticationAPIClient(auth0).useDPoP(this)
1687+
val storage = SharedPreferencesStorage(this)
1688+
val manager = CredentialsManager(apiClient, storage)
16661689
```
16671690

1691+
> [!IMPORTANT]
1692+
> When credentials are DPoP-bound, the SDK validates the DPoP key state before each token refresh. If the DPoP key pair is lost, the SDK will throw `CredentialsManagerException.DPOP_KEY_MISSING` and the user must re-authenticate. If the key pair has changed since the credentials were saved, the SDK will throw `CredentialsManagerException.DPOP_KEY_MISMATCH`. If the `AuthenticationAPIClient` was not configured with `useDPoP()`, the SDK will throw `CredentialsManagerException.DPOP_NOT_CONFIGURED`.
16681693
1669-
> [!NOTE]
1694+
> [!NOTE]
16701695
> DPoP is supported only on Android version 6.0 (API level 23) and above. Trying to use DPoP in any older versions will result in an exception.
16711696
16721697

@@ -2587,24 +2612,42 @@ In the event that something happened while trying to save or retrieve the creden
25872612
- Tokens have expired but no `refresh_token` is available to perform a refresh credentials request.
25882613
- Device's Lock Screen security settings have changed (e.g. the PIN code was changed). Even when `hasCredentials` returns true, the encryption keys will be deemed invalid and until `saveCredentials` is called again it won't be possible to decrypt any previously existing content, since they keys used back then are not the same as the new ones.
25892614
- Device is not compatible with some of the algorithms required by the `SecureCredentialsManager` class. This is considered a catastrophic event and might happen when the OEM has modified the Android ROM removing some of the officially included algorithms. Nevertheless, it can be checked in the exception instance itself by calling `isDeviceIncompatible`. By doing so you can decide the fallback for storing the credentials, such as using the regular `CredentialsManager`.
2615+
- **DPoP key pair lost** — The DPoP key pair is no longer available in the Android KeyStore. The stored credentials are cleared and re-authentication is required.
2616+
- **DPoP key pair mismatch** — The DPoP key pair exists but is different from the one used when the credentials were saved. The stored credentials are cleared and re-authentication is required.
2617+
- **DPoP not configured** — The stored credentials are DPoP-bound but the `AuthenticationAPIClient` used by the credentials manager was not configured with `useDPoP(context)`. The developer needs to call `AuthenticationAPIClient(auth0).useDPoP(context)` and pass the configured client to the credentials manager.
25902618

2591-
You can access the `code` property of the `CredentialsManagerException` to understand why the operation with `CredentialsManager` has failed and the `message` property of the `CredentialsManagerException` would give you a description of the exception.
2619+
You can access the `code` property of the `CredentialsManagerException` to understand why the operation with `CredentialsManager` has failed and the `message` property of the `CredentialsManagerException` would give you a description of the exception.
25922620

2593-
Starting from version `3.0.0` you can even pass the exception to a `when` expression and handle the exception accordingly in your app's logic as shown in the below code snippet:
2621+
Starting from version `3.0.0` you can even pass the exception to a `when` expression and handle the exception accordingly in your app's logic as shown in the below code snippet:
25942622

25952623
```kotlin
25962624
when(credentialsManagerException) {
2597-
CredentialsManagerException.NO_CREDENTIALS - > {
2625+
CredentialsManagerException.NO_CREDENTIALS -> {
25982626
// handle no credentials scenario
25992627
}
26002628

2601-
CredentialsManagerException.NO_REFRESH_TOKEN - > {
2629+
CredentialsManagerException.NO_REFRESH_TOKEN -> {
26022630
// handle no refresh token scenario
26032631
}
26042632

2605-
CredentialsManagerException.STORE_FAILED - > {
2633+
CredentialsManagerException.STORE_FAILED -> {
26062634
// handle store failed scenario
26072635
}
2636+
2637+
CredentialsManagerException.DPOP_KEY_MISSING -> {
2638+
// DPoP key was lost
2639+
// Clear local state and prompt user to re-authenticate
2640+
}
2641+
2642+
CredentialsManagerException.DPOP_KEY_MISMATCH -> {
2643+
// DPoP key exists but doesn't match the one used at login (key rotation)
2644+
// Clear local state and prompt user to re-authenticate
2645+
}
2646+
2647+
CredentialsManagerException.DPOP_NOT_CONFIGURED -> {
2648+
// Developer forgot to call useDPoP() on the AuthenticationAPIClient
2649+
// passed to the credentials manager. Fix the client configuration.
2650+
}
26082651
// ... similarly for other error codes
26092652
}
26102653
```

0 commit comments

Comments
 (0)