הצגת תיבת דו-שיח לאימות ביומטרי

אחת מהשיטות להגנה על מידע רגיש או על תוכן פרימיום באפליקציה היא לבקש אימות ביומטרי, כמו זיהוי פנים או זיהוי טביעת אצבע. במדריך הזה מוסבר איך לתמוך בתהליכי התחברות ביומטריים באפליקציה.

ככלל, מומלץ להשתמש ב-Credential Manager לכניסה ראשונית למכשיר. אפשר לבצע הרשאות מחדש באמצעות הנחיה ביומטרית או באמצעות מנהל האישורים. היתרון בשימוש בהנחיה ביומטרית הוא שהיא מציעה יותר אפשרויות להתאמה אישית, בעוד שמנהל האישורים מציע הטמעה יחידה בשני התהליכים.

הצהרה על סוגי האימות שהאפליקציה תומכת בהם

כדי להגדיר את סוגי האימות שהאפליקציה תומכת בהם, משתמשים בממשק BiometricManager.Authenticators. המערכת מאפשרת לכם להצהיר על סוגי האימות הבאים:

BIOMETRIC_STRONG
אימות באמצעות נתונים ביומטריים ברמה 3, כפי שמוגדר בדף הגדרת התאימות של Android.
BIOMETRIC_WEAK
אימות באמצעות נתונים ביומטריים מסוג 2, כפי שמוגדר בדף הגדרת התאימות של Android.
DEVICE_CREDENTIAL
אימות באמצעות פרטי כניסה לנעילת המסך – קוד האימות, קו ביטול הנעילה או הסיסמה של המשתמש.

כדי להתחיל להשתמש באמצעי אימות, המשתמש צריך ליצור קוד אימות, קו ביטול נעילה או סיסמה. אם למשתמש אין עדיין אמצעי אימות ביומטרי, הוא יקבל הנחיות ליצירת אמצעי כזה.

כדי להגדיר את סוגי האימות הביומטרי שהאפליקציה מקבלת, מעבירים סוג אימות או שילוב של סוגים באמצעות פעולת OR ברמת הביטים אל השיטה setAllowedAuthenticators(). קטע הקוד הבא מראה איך לתמוך באימות באמצעות נתונים ביומטריים מסוג Class 3 או פרטי כניסה לביטול נעילת המסך.

Kotlin

// Lets the user authenticate using either a Class 3 biometric or
// their lock screen credential (PIN, pattern, or password).
promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
        .build()

Java

// Lets user authenticate using either a Class 3 biometric or
// their lock screen credential (PIN, pattern, or password).
promptInfo = new BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setAllowedAuthenticators(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)
        .build();

השילובים הבאים של סוגי אמצעי אימות לא נתמכים ב-Android 10 (רמת API‏ 29) ובגרסאות קודמות: DEVICE_CREDENTIAL ו-BIOMETRIC_STRONG | DEVICE_CREDENTIAL. כדי לבדוק אם יש קוד אימות, קו ביטול נעילה או סיסמה ב-Android מגרסה 10 ומטה, משתמשים בשיטה KeyguardManager.isDeviceSecure().

בדיקה אם יש אימות ביומטרי

אחרי שמחליטים אילו רכיבי אימות האפליקציה תומכת בהם, בודקים אם הרכיבים האלה זמינים. כדי לעשות זאת, מעבירים את אותו שילוב בינארי של סוגים שהצהרתם עליהם באמצעות השיטה setAllowedAuthenticators() לשיטה canAuthenticate(). אם יש צורך, מפעילים את פעולת הכוונה ACTION_BIOMETRIC_ENROLL. ב-intent extra, מציינים את קבוצת אמצעי האימות שהאפליקציה מקבלת. הכוונה הזו מציגה למשתמש בקשה לרשום פרטי כניסה לאמצעי אימות שהאפליקציה מקבלת.

Kotlin

val biometricManager = BiometricManager.from(this)
when (biometricManager.canAuthenticate(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)) {
    BiometricManager.BIOMETRIC_SUCCESS ->
        Log.d("MY_APP_TAG", "App can authenticate using biometrics.")
    BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
        Log.e("MY_APP_TAG", "No biometric features available on this device.")
    BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->
        Log.e("MY_APP_TAG", "Biometric features are currently unavailable.")
    BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
        // Prompts the user to create credentials that your app accepts.
        val enrollIntent = Intent(Settings.ACTION_BIOMETRIC_ENROLL).apply {
            putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
                BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
        }
        startActivityForResult(enrollIntent, REQUEST_CODE)
    }
}

Java

BiometricManager biometricManager = BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)) {
    case BiometricManager.BIOMETRIC_SUCCESS:
        Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
        break;
    case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
        Log.e("MY_APP_TAG", "No biometric features available on this device.");
        break;
    case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
        Log.e("MY_APP_TAG", "Biometric features are currently unavailable.");
        break;
    case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
        // Prompts the user to create credentials that your app accepts.
        final Intent enrollIntent = new Intent(Settings.ACTION_BIOMETRIC_ENROLL);
        enrollIntent.putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
                BIOMETRIC_STRONG | DEVICE_CREDENTIAL);
        startActivityForResult(enrollIntent, REQUEST_CODE);
        break;
}

קביעה של אופן האימות של המשתמש

אחרי שהמשתמש עובר אימות, אפשר לבדוק אם האימות בוצע באמצעות פרטי כניסה למכשיר או פרטים ביומטריים על ידי קריאה ל-getAuthenticationType().

הצגת בקשת ההתחברות

כדי להציג הנחיה של המערכת שמבקשת מהמשתמש לבצע אימות באמצעות פרטי כניסה ביומטריים, משתמשים בספרייה הביומטרית. הדיאלוג הזה מסופק על ידי המערכת והוא עקבי בכל האפליקציות שמשתמשות בו, וכך הוא יוצר חוויית משתמש מהימנה יותר. דוגמה לתיבת דו-שיח מוצגת באיור 1.

צילום מסך שבו מוצגת תיבת דו-שיח
איור 1. תיבת דו-שיח של המערכת שבה מתבקש אימות ביומטרי.

כדי להוסיף אימות ביומטרי לאפליקציה באמצעות הספרייה הביומטרית, צריך לבצע את השלבים הבאים:

  1. בקובץ build.gradle של מודול האפליקציה, מוסיפים תלות בספרייה.androidx.biometric

  2. בפעילות או בקטע שמארחים את תיבת הדו-שיח של הכניסה הביומטרית, מציגים את תיבת הדו-שיח באמצעות הלוגיקה שמוצגת בקטע הקוד הבא:

    Kotlin

    private lateinit var executor: Executor
    private lateinit var biometricPrompt: BiometricPrompt
    private lateinit var promptInfo: BiometricPrompt.PromptInfo
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        executor = ContextCompat.getMainExecutor(this)
        biometricPrompt = BiometricPrompt(this, executor,
                object : BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationError(errorCode: Int,
                    errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                Toast.makeText(applicationContext,
                    "Authentication error: $errString", Toast.LENGTH_SHORT)
                    .show()
            }
    
            override fun onAuthenticationSucceeded(
                    result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                Toast.makeText(applicationContext,
                    "Authentication succeeded!", Toast.LENGTH_SHORT)
                    .show()
            }
    
            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                Toast.makeText(applicationContext, "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show()
            }
        })
    
        promptInfo = BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric login for my app")
                .setSubtitle("Log in using your biometric credential")
                .setNegativeButtonText("Use account password")
                .build()
    
        // Prompt appears when user clicks "Log in".
        // Consider integrating with the keystore to unlock cryptographic operations,
        // if needed by your app.
        val biometricLoginButton =
                findViewById<Button>(R.id.biometric_login)
        biometricLoginButton.setOnClickListener {
            biometricPrompt.authenticate(promptInfo)
        }
    }

    Java

    private Executor executor;
    private BiometricPrompt biometricPrompt;
    private BiometricPrompt.PromptInfo promptInfo;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        executor = ContextCompat.getMainExecutor(this);
        biometricPrompt = new BiometricPrompt(MainActivity.this,
                executor, new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode,
                    @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(getApplicationContext(),
                    "Authentication error: " + errString, Toast.LENGTH_SHORT)
                    .show();
            }
    
            @Override
            public void onAuthenticationSucceeded(
                    @NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                Toast.makeText(getApplicationContext(),
                    "Authentication succeeded!", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                Toast.makeText(getApplicationContext(), "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show();
            }
        });
    
        promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric login for my app")
                .setSubtitle("Log in using your biometric credential")
                .setNegativeButtonText("Use account password")
                .build();
    
        // Prompt appears when user clicks "Log in".
        // Consider integrating with the keystore to unlock cryptographic operations,
        // if needed by your app.
        Button biometricLoginButton = findViewById(R.id.biometric_login);
        biometricLoginButton.setOnClickListener(view -> {
                biometricPrompt.authenticate(promptInfo);
        });
    }

שימוש בפתרון קריפטוגרפי שתלוי באימות

כדי להגן עוד יותר על מידע רגיש באפליקציה, אפשר לשלב קריפטוגרפיה בתהליך העבודה של אימות ביומטרי באמצעות מופע של CryptoObject. המסגרת תומכת באובייקטים הקריפטוגרפיים הבאים: Signature, Cipher ו- Mac.

אחרי שהמשתמש עובר אימות בהצלחה באמצעות בקשה ביומטרית, האפליקציה יכולה לבצע פעולה קריפטוגרפית. לדוגמה, אם אתם מבצעים אימות באמצעות אובייקט Cipher, האפליקציה שלכם יכולה לבצע הצפנה ופענוח באמצעות אובייקט SecretKey.

בקטעים הבאים מופיעות דוגמאות לשימוש באובייקט Cipher ובאובייקט SecretKey להצפנת נתונים. בכל דוגמה נעשה שימוש בשיטות הבאות:

Kotlin

private fun generateSecretKey(keyGenParameterSpec: KeyGenParameterSpec) {
    val keyGenerator = KeyGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
    keyGenerator.init(keyGenParameterSpec)
    keyGenerator.generateKey()
}

private fun getSecretKey(): SecretKey {
    val keyStore = KeyStore.getInstance("AndroidKeyStore")

    // Before the keystore can be accessed, it must be loaded.
    keyStore.load(null)
    return keyStore.getKey(KEY_NAME, null) as SecretKey
}

private fun getCipher(): Cipher {
    return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
            + KeyProperties.BLOCK_MODE_CBC + "/"
            + KeyProperties.ENCRYPTION_PADDING_PKCS7)
}

Java

private void generateSecretKey(KeyGenParameterSpec keyGenParameterSpec) {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    keyGenerator.init(keyGenParameterSpec);
    keyGenerator.generateKey();
}

private SecretKey getSecretKey() {
    KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");

    // Before the keystore can be accessed, it must be loaded.
    keyStore.load(null);
    return ((SecretKey)keyStore.getKey(KEY_NAME, null));
}

private Cipher getCipher() {
    return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
            + KeyProperties.BLOCK_MODE_CBC + "/"
            + KeyProperties.ENCRYPTION_PADDING_PKCS7);
}

אימות באמצעות פרטים ביומטריים בלבד

אם האפליקציה שלכם משתמשת במפתח סודי שנדרשים פרטים ביומטריים כדי לבטל את הנעילה שלו, המשתמש צריך לאמת את הפרטים הביומטריים שלו בכל פעם לפני שהאפליקציה ניגשת למפתח.

כדי להצפין מידע רגיש רק אחרי שהמשתמש מאמת את עצמו באמצעות פרטי כניסה ביומטריים, צריך לבצע את השלבים הבאים:

  1. יוצרים מפתח באמצעות הגדרות התצורה הבאות של KeyGenParameterSpec:

    Kotlin

    generateSecretKey(KeyGenParameterSpec.Builder(
            KEY_NAME,
            KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
            .setUserAuthenticationRequired(true)
            // Invalidate the keys if the user has registered a new biometric
            // credential, such as a new fingerprint. Can call this method only
            // on Android 7.0 (API level 24) or higher. The variable
            // "invalidatedByBiometricEnrollment" is true by default.
            .setInvalidatedByBiometricEnrollment(true)
            .build())

    Java

    generateSecretKey(new KeyGenParameterSpec.Builder(
            KEY_NAME,
            KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
            .setUserAuthenticationRequired(true)
            // Invalidate the keys if the user has registered a new biometric
            // credential, such as a new fingerprint. Can call this method only
            // on Android 7.0 (API level 24) or higher. The variable
            // "invalidatedByBiometricEnrollment" is true by default.
            .setInvalidatedByBiometricEnrollment(true)
            .build());
  2. התחלת תהליך עבודה של אימות ביומטרי שמשלב צופן:

    Kotlin

    biometricLoginButton.setOnClickListener {
        // Exceptions are unhandled within this snippet.
        val cipher = getCipher()
        val secretKey = getSecretKey()
        cipher.init(Cipher.ENCRYPT_MODE, secretKey)
        biometricPrompt.authenticate(promptInfo,
                BiometricPrompt.CryptoObject(cipher))
    }

    Java

    biometricLoginButton.setOnClickListener(view -> {
        // Exceptions are unhandled within this snippet.
        Cipher cipher = getCipher();
        SecretKey secretKey = getSecretKey();
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        biometricPrompt.authenticate(promptInfo,
                new BiometricPrompt.CryptoObject(cipher));
    });
  3. בפונקציות הקריאה החוזרת של האימות הביומטרי, משתמשים במפתח הסודי כדי להצפין את המידע הרגיש:

    Kotlin

    override fun onAuthenticationSucceeded(
            result: BiometricPrompt.AuthenticationResult) {
        val encryptedInfo: ByteArray = result.cryptoObject.cipher?.doFinal(
            // plaintext-string text is whatever data the developer would like
            // to encrypt. It happens to be plain-text in this example, but it
            // can be anything
                plaintext-string.toByteArray(Charset.defaultCharset())
        )
        Log.d("MY_APP_TAG", "Encrypted information: " +
                Arrays.toString(encryptedInfo))
    }

    Java

    @Override
    public void onAuthenticationSucceeded(
            @NonNull BiometricPrompt.AuthenticationResult result) {
        // NullPointerException is unhandled; use Objects.requireNonNull().
        byte[] encryptedInfo = result.getCryptoObject().getCipher().doFinal(
            // plaintext-string text is whatever data the developer would like
            // to encrypt. It happens to be plain-text in this example, but it
            // can be anything
                plaintext-string.getBytes(Charset.defaultCharset()));
        Log.d("MY_APP_TAG", "Encrypted information: " +
                Arrays.toString(encryptedInfo));
    }

אימות באמצעות מידע ביומטרי או פרטי כניסה לנעילת המסך

אפשר להשתמש במפתח סודי שמאפשר אימות באמצעות פרטים ביומטריים או פרטים לביטול נעילה (קוד אימות, קו ביטול נעילה או סיסמה). כשמגדירים את המפתח הזה, צריך לציין תקופת תוקף. במהלך פרק הזמן הזה, האפליקציה שלך יכולה לבצע כמה פעולות קריפטוגרפיות בלי שהמשתמש יצטרך לבצע אימות מחדש.

כדי להצפין מידע רגיש אחרי שהמשתמש מאמת את הזהות שלו באמצעות פרטים ביומטריים או פרטי כניסה למסך הנעילה, צריך לבצע את השלבים הבאים:

  1. יוצרים מפתח באמצעות הגדרות התצורה הבאות של KeyGenParameterSpec:

    Kotlin

    generateSecretKey(KeyGenParameterSpec.Builder(
        KEY_NAME,
        KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
        .setUserAuthenticationRequired(true)
        .setUserAuthenticationParameters(VALIDITY_DURATION_SECONDS,
                ALLOWED_AUTHENTICATORS)
        .build())

    Java

    generateSecretKey(new KeyGenParameterSpec.Builder(
        KEY_NAME,
        KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
        .setUserAuthenticationRequired(true)
        .setUserAuthenticationParameters(VALIDITY_DURATION_SECONDS,
                ALLOWED_AUTHENTICATORS)
        .build());
  2. במהלך תקופה של VALIDITY_DURATION_SECONDS אחרי שהמשתמש מאמת את עצמו, מצפינים את המידע הרגיש:

    Kotlin

    private fun encryptSecretInformation() {
        // Exceptions are unhandled for getCipher() and getSecretKey().
        val cipher = getCipher()
        val secretKey = getSecretKey()
        try {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey)
            val encryptedInfo: ByteArray = cipher.doFinal(
                // plaintext-string text is whatever data the developer would
                // like to encrypt. It happens to be plain-text in this example,
                // but it can be anything
                    plaintext-string.toByteArray(Charset.defaultCharset()))
            Log.d("MY_APP_TAG", "Encrypted information: " +
                    Arrays.toString(encryptedInfo))
        } catch (e: InvalidKeyException) {
            Log.e("MY_APP_TAG", "Key is invalid.")
        } catch (e: UserNotAuthenticatedException) {
            Log.d("MY_APP_TAG", "The key's validity timed out.")
            biometricPrompt.authenticate(promptInfo)
        }

    Java

    private void encryptSecretInformation() {
        // Exceptions are unhandled for getCipher() and getSecretKey().
        Cipher cipher = getCipher();
        SecretKey secretKey = getSecretKey();
        try {
            // NullPointerException is unhandled; use Objects.requireNonNull().
            ciper.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedInfo = cipher.doFinal(
                // plaintext-string text is whatever data the developer would
                // like to encrypt. It happens to be plain-text in this example,
                // but it can be anything
                    plaintext-string.getBytes(Charset.defaultCharset()));
        } catch (InvalidKeyException e) {
            Log.e("MY_APP_TAG", "Key is invalid.");
        } catch (UserNotAuthenticatedException e) {
            Log.d("MY_APP_TAG", "The key's validity timed out.");
            biometricPrompt.authenticate(promptInfo);
        }
    }

אימות באמצעות מפתחות auth-per-use

אתם יכולים לספק תמיכה במפתחות אימות לכל שימוש במופע של BiometricPrompt. כדי להשתמש במפתח כזה, המשתמש צריך להציג אישור ביומטרי או אישור של המכשיר בכל פעם שהאפליקציה שלכם צריכה לגשת לנתונים שמוגנים על ידי המפתח הזה. מפתחות אימות לכל שימוש יכולים להיות שימושיים לעסקאות עם ערך גבוה, כמו ביצוע תשלום גדול או עדכון רשומות בריאותיות של אדם מסוים.

כדי לשייך אובייקט BiometricPrompt למפתח הרשאה לשימוש, מוסיפים קוד שדומה לקוד הבא:

Kotlin

val authPerOpKeyGenParameterSpec =
        KeyGenParameterSpec.Builder("myKeystoreAlias", key-purpose)
    // Accept either a biometric credential or a device credential.
    // To accept only one type of credential, include only that type as the
    // second argument.
    .setUserAuthenticationParameters(0 /* duration */,
            KeyProperties.AUTH_BIOMETRIC_STRONG or
            KeyProperties.AUTH_DEVICE_CREDENTIAL)
    .build()

Java

KeyGenParameterSpec authPerOpKeyGenParameterSpec =
        new KeyGenParameterSpec.Builder("myKeystoreAlias", key-purpose)
    // Accept either a biometric credential or a device credential.
    // To accept only one type of credential, include only that type as the
    // second argument.
    .setUserAuthenticationParameters(0 /* duration */,
            KeyProperties.AUTH_BIOMETRIC_STRONG |
            KeyProperties.AUTH_DEVICE_CREDENTIAL)
    .build();

אימות ללא פעולה מפורשת מצד המשתמש

כברירת מחדל, המערכת דורשת מהמשתמשים לבצע פעולה ספציפית, כמו לחיצה על לחצן, אחרי שהיא מקבלת את האישורים הביומטריים שלהם. ההגדרה הזו מומלצת אם האפליקציה מציגה את תיבת הדו-שיח כדי לאשר פעולה רגישה או פעולה שקשורה לסיכון גבוה, כמו ביצוע רכישה.

אם האפליקציה מציגה תיבת דו-שיח של אימות ביומטרי לפעולה עם סיכון נמוך יותר, אפשר לספק למערכת רמז לכך שהמשתמש לא צריך לאשר את האימות. ההצעה הזו מאפשרת למשתמשים לצפות בתוכן באפליקציה שלכם מהר יותר אחרי שהם מבצעים אימות מחדש באמצעות שיטה פסיבית, כמו זיהוי פנים או זיהוי קשתית. כדי לספק את הרמז הזה, מעבירים את false לשיטה setConfirmationRequired().

בתמונה 2 מוצגות שתי גרסאות של אותו דו-שיח. בגרסה אחת נדרשת פעולה מפורשת של המשתמש, ובגרסה השנייה לא.

צילום מסך של תיבת דו-שיח צילום מסך של תיבת דו-שיח
איור 2. אימות פנים ללא אישור המשתמש (למעלה) ועם אישור המשתמש (למטה).

קטע הקוד הבא מראה איך להציג תיבת דו-שיח שלא דורשת פעולת משתמש מפורשת כדי להשלים את תהליך האימות:

Kotlin

// Lets the user authenticate without performing an action, such as pressing a
// button, after their biometric credential is accepted.
promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .setConfirmationRequired(false)
        .build()

Java

// Lets the user authenticate without performing an action, such as pressing a
// button, after their biometric credential is accepted.
promptInfo = new BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .setConfirmationRequired(false)
        .build();

אפשרות לחזרה לפרטי כניסה לא ביומטריים

אם רוצים שהאפליקציה תאפשר אימות באמצעות נתונים ביומטריים או פרטי כניסה למכשיר, אפשר להצהיר שהאפליקציה תומכת בפרטי כניסה למכשיר על ידי הוספת DEVICE_CREDENTIAL לקבוצת הערכים שמעבירים אל setAllowedAuthenticators().

אם האפליקציה שלכם משתמשת כרגע ב-createConfirmDeviceCredentialIntent() או ב-setDeviceCredentialAllowed() כדי לספק את היכולת הזו, עליכם לעבור לשימוש ב-setAllowedAuthenticators().

מקורות מידע נוספים

למידע נוסף על אימות ביומטרי ב-Android, אפשר לעיין במקורות המידע הבאים.

פוסטים בבלוג