Skip to content

Commit e5789e4

Browse files
neelanshsahaiNeelansh Sahai
andauthored
Add restore credential snippets (#489)
Co-authored-by: Neelansh Sahai <[email protected]>
1 parent 7eb5acd commit e5789e4

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.identity.credentialmanager
18+
19+
import android.content.Context
20+
import androidx.credentials.ClearCredentialStateRequest
21+
import androidx.credentials.ClearCredentialStateRequest.Companion.TYPE_CLEAR_RESTORE_CREDENTIAL
22+
import androidx.credentials.CreateRestoreCredentialRequest
23+
import androidx.credentials.CredentialManager
24+
import androidx.credentials.GetCredentialRequest
25+
import androidx.credentials.GetRestoreCredentialOption
26+
27+
class RestoreCredentialsFunctions(
28+
private val context: Context,
29+
private val credentialManager: CredentialManager,
30+
) {
31+
suspend fun createRestoreKey(
32+
createRestoreRequest: CreateRestoreCredentialRequest
33+
) {
34+
// [START android_identity_restore_cred_create]
35+
val credentialManager = CredentialManager.create(context)
36+
37+
// On a successful authentication create a Restore Key
38+
// Pass in the context and CreateRestoreCredentialRequest object
39+
val response = credentialManager.createCredential(context, createRestoreRequest)
40+
// [END android_identity_restore_cred_create]
41+
}
42+
43+
suspend fun getRestoreKey(
44+
fetchAuthenticationJson: () -> String,
45+
) {
46+
// [START android_identity_restore_cred_get]
47+
// Fetch the Authentication JSON from server
48+
val authenticationJson = fetchAuthenticationJson()
49+
50+
// Create the GetRestoreCredentialRequest object
51+
val options = GetRestoreCredentialOption(authenticationJson)
52+
val getRequest = GetCredentialRequest(listOf(options))
53+
54+
// The restore key can be fetched in two scenarios to
55+
// 1. On the first launch of app on the device, fetch the Restore Key
56+
// 2. In the onRestore callback (if the app implements the Backup Agent)
57+
val response = credentialManager.getCredential(context, getRequest)
58+
// [END android_identity_restore_cred_get]
59+
}
60+
61+
suspend fun deleteRestoreKey() {
62+
// [START android_identity_restore_cred_delete]
63+
// Create a ClearCredentialStateRequest object
64+
val clearRequest = ClearCredentialStateRequest(TYPE_CLEAR_RESTORE_CREDENTIAL)
65+
66+
// On user log-out, clear the restore key
67+
val response = credentialManager.clearCredentialState(clearRequest)
68+
// [END android_identity_restore_cred_delete]
69+
}
70+
}

0 commit comments

Comments
 (0)