将一键式通行密钥创建和登录与生物识别提示集成

在 Android 15 中,Credential Manager 支持单次点按即可创建和检索凭据。在此流程中,系统会直接在生物识别提示中显示正在创建或使用的凭据的信息,以及用于进入更多选项的入口点。这种简化流程可创建更高效且更流畅的凭据创建和检索流程。

要求

  • 用户在其设备上设置了生物识别功能,并允许使用生物识别功能对应用进行身份验证。
  • 对于登录流程,此功能仅适用于单账号场景,即使该账号有多个凭据(例如通行密钥和密码)也是如此。

在通行密钥创建流程中启用单次点按

此方法的创建步骤与现有凭据创建流程一致。在 BeginCreatePublicKeyCredentialRequest 中,如果请求是通行密钥请求,请使用 handleCreatePasskeyQuery() 来处理该请求。

is BeginCreatePublicKeyCredentialRequest -> {
    Log.i(TAG, "Request is passkey type")
    return handleCreatePasskeyQuery(request, passwordCount, passkeyCount)
}

handleCreatePasskeyQuery() 中,将 BiometricPromptData 添加到 CreateEntry 类:

val createEntry = CreateEntry(
    // Additional properties...
    biometricPromptData = BiometricPromptData(
        allowedAuthenticators = allowedAuthenticator
    ),
)

凭据提供程序应在 BiometricPromptData 实例中明确设置 allowedAuthenticator 属性。如果未设置此属性,则值默认为 DEVICE_WEAK。根据您的用例需要设置可选的 cryptoObject 属性。

在登录通行密钥流程中启用一键登录

与通行密钥创建流程类似,此流程将遵循处理用户登录的现有设置。在 BeginGetPublicKeyCredentialOption 下,使用 populatePasskeyData() 收集有关身份验证请求的相关信息:

is BeginGetPublicKeyCredentialOption -> {
    // ... other logic

    populatePasskeyData(
        origin,
        option,
        responseBuilder,
        autoSelectEnabled,
        allowedAuthenticator
    )

    // ... other logic as needed
}

CreateEntry 类似,BiometricPromptData 实例会设置为 PublicKeyCredentialEntry 实例。如果未明确设置,则 allowedAuthenticator 默认设为 BIOMETRIC_WEAK

PublicKeyCredentialEntry(
    // other properties...

    biometricPromptData = BiometricPromptData(
        allowedAuthenticators = allowedAuthenticator
    )
)

处理凭据条目选择

在处理通行密钥创建登录期间的通行密钥选择的凭据条目选择时,请根据需要调用 PendingIntentHandler's retrieveProviderCreateCredentialRequestretrieveProviderGetCredentialRequest。这些返回的对象包含提供程序所需的元数据。例如,在处理通行密钥创建条目选择时,请按如下所示更新代码:

val createRequest = PendingIntentHandler.retrieveProviderCreateCredentialRequest(intent)
if (createRequest == null) {
    Log.i(TAG, "request is null")
    setUpFailureResponseAndFinish("Unable to extract request from intent")
    return
}
// Other logic...

val biometricPromptResult = createRequest.biometricPromptResult

// Add your logic based on what needs to be done
// after getting biometrics

if (createRequest.callingRequest is CreatePublicKeyCredentialRequest) {
    val publicKeyRequest: CreatePublicKeyCredentialRequest =
        createRequest.callingRequest as CreatePublicKeyCredentialRequest

    if (biometricPromptResult == null) {
        // Do your own authentication flow, if needed
    }
    else if (biometricPromptResult.isSuccessful) {
        createPasskey(
            publicKeyRequest.requestJson,
            createRequest.callingAppInfo,
            publicKeyRequest.clientDataHash,
            accountId
        )
    } else {
        val error = biometricPromptResult.authenticationError
        // Process the error
    }

    // Other logic...
}

此示例包含有关生物识别流程是否成功的信息。它还包含有关凭据的其他信息。如果流程失败,请使用 biometricPromptResult.authenticationError 下的错误代码来做出决策。作为 biometricPromptResult.authenticationError.errorCode 的一部分返回的错误代码与 androidx.biometric 库中定义的错误代码相同,例如 androidx.biometric.BiometricPrompt.NO_SPACEandroidx.biometric.BiometricPrompt.UNABLE_TO_PROCESSandroidx.biometric.BiometricPrompt.ERROR_TIMEOUT 等。authenticationError 还将包含与 errorCode 关联的错误消息,该消息可显示在界面上。

同样,在 retrieveProviderGetCredentialRequest 期间提取元数据。检查您的生物识别流程是否为 null。如果是,请配置您自己的生物识别信息以进行身份验证。这与插桩 get 操作的方式类似:

val getRequest =
    PendingIntentHandler.retrieveProviderGetCredentialRequest(intent)

if (getRequest == null) {
    Log.i(TAG, "request is null")
    setUpFailureResponseAndFinish("Unable to extract request from intent")
    return
}

// Other logic...

val biometricPromptResult = getRequest.biometricPromptResult

// Add your logic based on what needs to be done
// after getting biometrics

if (biometricPromptResult == null)
{
    // Do your own authentication flow, if necessary
} else if (biometricPromptResult.isSuccessful) {

    Log.i(TAG, "The response from the biometricPromptResult was ${biometricPromptResult.authenticationResult?.authenticationType}")

    validatePasskey(
        publicKeyRequest.requestJson,
        origin,
        packageName,
        uid,
        passkey.username,
        credId,
        privateKey
    )
} else {
    val error = biometricPromptResult.authenticationError
    // Process the error
}

// Other logic...