Skip to content

Commit 9f24247

Browse files
authored
doc: Updated the Examples.md file with the new CredentialsManagerExceptions (#946)
1 parent f54d0d2 commit 9f24247

1 file changed

Lines changed: 50 additions & 7 deletions

File tree

EXAMPLES.md

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

282+
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.
283+
284+
```kotlin
285+
286+
val auth0 = Auth0.getInstance("YOUR_CLIENT_ID", "YOUR_DOMAIN")
287+
val apiClient = AuthenticationAPIClient(auth0).useDPoP(context) // DPoP enabled
288+
val storage = SharedPreferencesStorage(context)
289+
val manager = CredentialsManager(apiClient, storage)
290+
291+
WebAuthProvider
292+
.useDPoP()
293+
.login(auth0)
294+
.start(context, callback)
295+
```
296+
282297
On logout, you should call `DPoP.clearKeyPair()` to delete the user's key pair from the Keychain.
283298

284299
```kotlin
@@ -292,7 +307,7 @@ WebAuthProvider.logout(account)
292307

293308
})
294309
```
295-
> [!NOTE]
310+
> [!NOTE]
296311
> 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.
297312
298313
## Authentication API
@@ -1661,11 +1676,21 @@ val auth0 = Auth0.getInstance("YOUR_CLIENT_ID", "YOUR_DOMAIN")
16611676
val apiClient = AuthenticationAPIClient(auth0).useDPoP(this)
16621677
val storage = SharedPreferencesStorage(this)
16631678
val manager = SecureCredentialsManager(apiClient, this, auth0, storage)
1679+
```
1680+
1681+
Similarly, for `CredentialsManager`:
16641682

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

1690+
> [!IMPORTANT]
1691+
> 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`.
16671692
1668-
> [!NOTE]
1693+
> [!NOTE]
16691694
> 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.
16701695
16711696

@@ -2550,24 +2575,42 @@ In the event that something happened while trying to save or retrieve the creden
25502575
- Tokens have expired but no `refresh_token` is available to perform a refresh credentials request.
25512576
- 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.
25522577
- 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`.
2578+
- **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.
2579+
- **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.
2580+
- **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.
25532581

2554-
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.
2582+
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.
25552583

2556-
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:
2584+
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:
25572585

25582586
```kotlin
25592587
when(credentialsManagerException) {
2560-
CredentialsManagerException.NO_CREDENTIALS - > {
2588+
CredentialsManagerException.NO_CREDENTIALS -> {
25612589
// handle no credentials scenario
25622590
}
25632591

2564-
CredentialsManagerException.NO_REFRESH_TOKEN - > {
2592+
CredentialsManagerException.NO_REFRESH_TOKEN -> {
25652593
// handle no refresh token scenario
25662594
}
25672595

2568-
CredentialsManagerException.STORE_FAILED - > {
2596+
CredentialsManagerException.STORE_FAILED -> {
25692597
// handle store failed scenario
25702598
}
2599+
2600+
CredentialsManagerException.DPOP_KEY_MISSING -> {
2601+
// DPoP key was lost
2602+
// Clear local state and prompt user to re-authenticate
2603+
}
2604+
2605+
CredentialsManagerException.DPOP_KEY_MISMATCH -> {
2606+
// DPoP key exists but doesn't match the one used at login (key rotation)
2607+
// Clear local state and prompt user to re-authenticate
2608+
}
2609+
2610+
CredentialsManagerException.DPOP_NOT_CONFIGURED -> {
2611+
// Developer forgot to call useDPoP() on the AuthenticationAPIClient
2612+
// passed to the credentials manager. Fix the client configuration.
2613+
}
25712614
// ... similarly for other error codes
25722615
}
25732616
```

0 commit comments

Comments
 (0)