在 Android 15 上,Credential Manager 支持通过单点触控流程进行凭据创建和检索。在此流程中,正在创建或正在使用的凭据信息直接显示在生物识别提示中,并提供更多选项的入口。这种简化的流程创建了更高效、更流畅的凭据创建和检索流程。
要求
- 用户设备已设置生物识别功能,且用户允许其用于应用身份验证。
- 对于登录流程,即使该账号有多个凭据(例如通行密钥和密码)可用,此功能仅针对单账号场景启用。
在通行密钥创建流程中启用单点触控
此方法的创建步骤与现有凭据创建流程匹配。在您的 BeginCreatePublicKeyCredentialRequest
中,如果请求是针对通行密钥的,请使用 handleCreatePasskeyQuery()
处理请求。
is BeginCreatePublicKeyCredentialRequest -> {
Log.i(TAG, "Request is passkey type")
return handleCreatePasskeyQuery(request, passwordCount, passkeyCount)
}
在您的 handleCreatePasskeyQuery()
中,在 CreateEntry
类中包含 BiometricPromptData
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 retrieveProviderCreateCredentialRequest
或 retrieveProviderGetCredentialRequest
。这些方法返回包含提供商所需元数据的对象。例如,在处理通行密钥创建条目选择时,请按如下所示更新您的代码
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.authenitcationError
// Process the error
}
// Other logic...
}
此示例包含有关生物识别流程成功的信息。它还包含关于凭据的其他信息。如果流程失败,请使用 biometricPromptResult.authenticationError
下的错误代码进行判断。作为 biometricPromptResult.authenticationError.errorCode
返回的错误代码与 androidx.biometric 库中定义的错误代码相同,例如 androidx.biometric.BiometricPrompt.NO_SPACE、androidx.biometric.BiometricPrompt.UNABLE_TO_PROCESS、androidx.biometric.BiometricPrompt.ERROR_TIMEOUT 等。 authenticationError
还将包含与 errorCode
相关联的错误消息,可在 UI 上显示。
类似地,在 retrieveProviderGetCredentialRequest
期间提取元数据。检查您的生物识别流程是否为 null
。如果是,请配置您自己的生物识别功能进行身份验证。这类似于获取操作的实现方式
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.authenitcationError
// Process the error
}
// Other logic...