|
| 1 | +package com.example.identity.credentialmanager |
| 2 | + |
| 3 | +import android.os.Build.VERSION_CODES |
| 4 | +import android.util.Log |
| 5 | +import androidx.activity.ComponentActivity |
| 6 | +import androidx.annotation.RequiresApi |
| 7 | +import androidx.credentials.CreatePublicKeyCredentialRequest |
| 8 | +import androidx.credentials.provider.BeginCreatePublicKeyCredentialRequest |
| 9 | +import androidx.credentials.provider.BeginGetPublicKeyCredentialOption |
| 10 | +import androidx.credentials.provider.BiometricPromptData |
| 11 | +import androidx.credentials.provider.CallingAppInfo |
| 12 | +import androidx.credentials.provider.PendingIntentHandler |
| 13 | + |
| 14 | +class SingleTap: ComponentActivity() { |
| 15 | + private val x: Any? = null |
| 16 | + private val TAG: String = "" |
| 17 | + |
| 18 | + private fun passkeyCreation( |
| 19 | + request: BeginCreatePublicKeyCredentialRequest, |
| 20 | + passwordCount: Int, |
| 21 | + passkeyCount: Int |
| 22 | + ) { |
| 23 | + val option = null |
| 24 | + val origin = null |
| 25 | + val responseBuilder = null |
| 26 | + val autoSelectEnabled = null |
| 27 | + val allowedAuthenticator = 0 |
| 28 | + |
| 29 | + val y = |
| 30 | + // [START android_identity_single_tap_set_biometric_prompt_data] |
| 31 | + PublicKeyCredentialEntry( |
| 32 | + // other properties... |
| 33 | + |
| 34 | + biometricPromptData = BiometricPromptData( |
| 35 | + allowedAuthenticators = allowedAuthenticator |
| 36 | + ) |
| 37 | + ) |
| 38 | + // [END android_identity_single_tap_set_biometric_prompt_data] |
| 39 | + |
| 40 | + when (x) { |
| 41 | + // [START android_identity_single_tap_pk_creation] |
| 42 | + is BeginCreatePublicKeyCredentialRequest -> { |
| 43 | + Log.i(TAG, "Request is passkey type") |
| 44 | + return handleCreatePasskeyQuery(request, passwordCount, passkeyCount) |
| 45 | + } |
| 46 | + // [END android_identity_single_tap_pk_creation] |
| 47 | + |
| 48 | + // [START android_identity_single_tap_pk_flow] |
| 49 | + is BeginGetPublicKeyCredentialOption -> { |
| 50 | + // ... other logic |
| 51 | + |
| 52 | + populatePasskeyData( |
| 53 | + origin, |
| 54 | + option, |
| 55 | + responseBuilder, |
| 56 | + autoSelectEnabled, |
| 57 | + allowedAuthenticator |
| 58 | + ) |
| 59 | + |
| 60 | + // ... other logic as needed |
| 61 | + } |
| 62 | + // [END android_identity_single_tap_pk_flow] |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private fun handleCreatePasskeyQuery( |
| 67 | + request: BeginCreatePublicKeyCredentialRequest, |
| 68 | + passwordCount: Int, |
| 69 | + passkeyCount: Int |
| 70 | + ) { |
| 71 | + val allowedAuthenticator = 0 |
| 72 | + |
| 73 | + // [START android_identity_single_tap_create_entry] |
| 74 | + val createEntry = CreateEntry( |
| 75 | + // Additional properties... |
| 76 | + biometricPromptData = BiometricPromptData( |
| 77 | + allowedAuthenticators = allowedAuthenticator |
| 78 | + ), |
| 79 | + ) |
| 80 | + // [END android_identity_single_tap_create_entry] |
| 81 | + } |
| 82 | + |
| 83 | + @RequiresApi(VERSION_CODES.M) |
| 84 | + private fun handleCredentialEntrySelection( |
| 85 | + accountId: String = "", |
| 86 | + createPasskey: (String, CallingAppInfo, ByteArray?, String) -> Unit |
| 87 | + ) { |
| 88 | + // [START android_identity_single_tap_handle_credential_entry] |
| 89 | + val createRequest = PendingIntentHandler.retrieveProviderCreateCredentialRequest(intent) |
| 90 | + if (createRequest == null) { |
| 91 | + Log.i(TAG, "request is null") |
| 92 | + setUpFailureResponseAndFinish("Unable to extract request from intent") |
| 93 | + return |
| 94 | + } |
| 95 | + // Other logic... |
| 96 | + |
| 97 | + val biometricPromptResult = createRequest.biometricPromptResult |
| 98 | + |
| 99 | + // Add your logic based on what needs to be done |
| 100 | + // after getting biometrics |
| 101 | + |
| 102 | + if (createRequest.callingRequest is CreatePublicKeyCredentialRequest) { |
| 103 | + val publicKeyRequest: CreatePublicKeyCredentialRequest = |
| 104 | + createRequest.callingRequest as CreatePublicKeyCredentialRequest |
| 105 | + |
| 106 | + if (biometricPromptResult == null) { |
| 107 | + // Do your own authentication flow, if needed |
| 108 | + } |
| 109 | + else if (biometricPromptResult.isSuccessful) { |
| 110 | + createPasskey( |
| 111 | + publicKeyRequest.requestJson, |
| 112 | + createRequest.callingAppInfo, |
| 113 | + publicKeyRequest.clientDataHash, |
| 114 | + accountId |
| 115 | + ) |
| 116 | + } else { |
| 117 | + val error = biometricPromptResult.authenticationError |
| 118 | + // Process the error |
| 119 | + } |
| 120 | + |
| 121 | + // Other logic... |
| 122 | + } |
| 123 | + // [END android_identity_single_tap_handle_credential_entry] |
| 124 | + } |
| 125 | + |
| 126 | + @RequiresApi(VERSION_CODES.M) |
| 127 | + private fun retrieveProviderGetCredentialRequest( |
| 128 | + validatePasskey: (String, String, String, String, String, String, String) -> Unit, |
| 129 | + publicKeyRequest: CreatePublicKeyCredentialRequest, |
| 130 | + origin: String, |
| 131 | + uid: String, |
| 132 | + passkey: PK, |
| 133 | + credId: String, |
| 134 | + privateKey: String, |
| 135 | + ) { |
| 136 | + // [START android_identity_single_tap_get_cred_request] |
| 137 | + val getRequest = |
| 138 | + PendingIntentHandler.retrieveProviderGetCredentialRequest(intent) |
| 139 | + |
| 140 | + if (getRequest == null) { |
| 141 | + Log.i(TAG, "request is null") |
| 142 | + setUpFailureResponseAndFinish("Unable to extract request from intent") |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + // Other logic... |
| 147 | + |
| 148 | + val biometricPromptResult = getRequest.biometricPromptResult |
| 149 | + |
| 150 | + // Add your logic based on what needs to be done |
| 151 | + // after getting biometrics |
| 152 | + |
| 153 | + if (biometricPromptResult == null) |
| 154 | + { |
| 155 | + // Do your own authentication flow, if necessary |
| 156 | + } else if (biometricPromptResult.isSuccessful) { |
| 157 | + |
| 158 | + Log.i(TAG, "The response from the biometricPromptResult was ${biometricPromptResult.authenticationResult?.authenticationType}") |
| 159 | + |
| 160 | + validatePasskey( |
| 161 | + publicKeyRequest.requestJson, |
| 162 | + origin, |
| 163 | + packageName, |
| 164 | + uid, |
| 165 | + passkey.username, |
| 166 | + credId, |
| 167 | + privateKey |
| 168 | + ) |
| 169 | + } else { |
| 170 | + val error = biometricPromptResult.authenticationError |
| 171 | + // Process the error |
| 172 | + } |
| 173 | + |
| 174 | + // Other logic... |
| 175 | + // [END android_identity_single_tap_get_cred_request] |
| 176 | + } |
| 177 | + |
| 178 | + private fun CreateEntry(biometricPromptData: BiometricPromptData) {} |
| 179 | + |
| 180 | + private fun PublicKeyCredentialEntry(biometricPromptData: BiometricPromptData) {} |
| 181 | + |
| 182 | + private fun populatePasskeyData( |
| 183 | + origin: Any?, |
| 184 | + option: Any?, |
| 185 | + responseBuilder: Any?, |
| 186 | + autoSelectEnabled: Any?, |
| 187 | + allowedAuthenticator: Any? |
| 188 | + ) {} |
| 189 | + |
| 190 | + private fun setUpFailureResponseAndFinish(str: String) {} |
| 191 | +} |
| 192 | + |
| 193 | +data class PK( |
| 194 | + val username: String, |
| 195 | +) |
0 commit comments