整合「輕觸一下密碼金鑰建立流程」和生物特徵辨識提示登入功能

在 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 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 相關聯的錯誤訊息,可顯示在 UI 上。

同樣地,請在 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...