सिंगल टैप करके पासकी बनाने और बायोमेट्रिक प्रॉम्प्ट की मदद से साइन-इन करने की सुविधा इंटिग्रेट करें

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
    ),
)

क्रेडेंशियल देने वाली कंपनियों को allowedAuthenticator इंस्टेंस में allowedAuthenticator प्रॉपर्टी को साफ़ तौर पर सेट करना चाहिए.BiometricPromptData अगर इस प्रॉपर्टी को सेट नहीं किया जाता है, तो डिफ़ॉल्ट रूप से इसकी वैल्यू 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.authenticationError
        // 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 से जुड़ी गड़बड़ी का मैसेज भी शामिल होगा, जिसे यूज़र इंटरफ़ेस (यूआई) पर दिखाया जा सकता है.

इसी तरह, 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...