אחת מהשיטות להגנה על מידע רגיש או תוכן פרימיום באפליקציה היא לבקש אימות ביומטרי, כמו שימוש בזיהוי פנים או בזיהוי טביעת אצבע. במדריך הזה מוסבר איך לתמוך בתהליכי כניסה ביומטריים באפליקציה.
ככלל, מומלץ להשתמש ב-Credential Manager לכניסה הראשונית למכשיר. לאחר מכן אפשר לבצע את ההרשאות מחדש באמצעות אחת מההנחיות הביומטריות, או 'מנהל פרטי הכניסה'. היתרון של השימוש ב-Biometric Prompt הוא שיש בו יותר אפשרויות להתאמה אישית, בעוד ש-Credential Manager מציע הטמעה אחת בשני התהליכים.
הצהרה על סוגי האימות שהאפליקציה תומכת בהם
כדי להגדיר את סוגי האימות שהאפליקציה תומכת בהם, צריך להשתמש ב
BiometricManager.Authenticators
גרפי. המערכת מאפשרת להצהיר על סוגי האימות הבאים:
BIOMETRIC_STRONG
- אימות באמצעות זיהוי ביומטרי מסוג 3, כפי שמוגדר בדף הגדרת התאימות ל-Android.
BIOMETRIC_WEAK
- אימות באמצעות ביומטריה ברמה 2, כפי שמוגדר ב תאימות ל-Android הגדרה
DEVICE_CREDENTIAL
- אימות באמצעות פרטי כניסה של נעילת מסך – קוד האימות, קו ביטול הנעילה או הסיסמה של המשתמש.
כדי להתחיל להשתמש בכלי אימות, המשתמש צריך ליצור קוד אימות. קו ביטול נעילה או סיסמה. אם למשתמש עדיין אין חשבון, תופיע בקשה ליצור חשבון בתהליך ההרשמה הביומטרי.
כדי להגדיר את סוגי האימות הביומטרי שהאפליקציה תקבל, מעבירים את סוג האימות או שילוב של סוגי אימות בביטים לשיטה setAllowedAuthenticators()
. קטע הקוד הבא מראה איך לתמוך באימות באמצעות
פרטי כניסה ביומטריים ברמה 3 או פרטי כניסה לנעילת מסך.
// 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()
// 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, עליך לספק את קבוצת המאמתים שהאפליקציה
מקבל. הכוונה הזו מבקשת מהמשתמש לרשום פרטי כניסה לאימות שהאפליקציה מקבלת.
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 ) } }
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.
כדי להוסיף לאפליקציה אימות ביומטרי באמצעות ספריית Biometric:
בקובץ
build.gradle
של מודול האפליקציה, מוסיפים תלות בספרייהandroidx.biometric
.בפעילות או בחלק שמארחים את תיבת הדו-שיח של הכניסה הביומטרית, מציגים את תיבת הדו-שיח באמצעות הלוגיקה שמופיעה בקטע הקוד הבא:
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) } }
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
להצפנת נתונים. בכל דוגמה נעשה שימוש בשיטות הבאות:
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) }
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); }
אימות באמצעות פרטי כניסה ביומטריים בלבד
אם האפליקציה משתמשת במפתח סודי שמחייב פרטים ביומטריים כדי לבטל את הנעילה, המשתמש חייב לאמת את פרטי הכניסה הביומטריים שלו בכל פעם לפני האפליקציה ניגש למפתח.
כדי להצפין מידע רגיש רק אחרי שהמשתמש מבצע אימות באמצעות בפרטי כניסה ביומטריים, מבצעים את השלבים הבאים:
ליצור מפתח שמשתמש
KeyGenParameterSpec
תצורה: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())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());הפעלת תהליך עבודה של אימות ביומטרי שכולל צופן:
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)) }
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)); });
בקריאות החזרה (callbacks) של האימות הביומטרי, משתמשים במפתח הסודי כדי להצפין את המידע הרגיש:
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)) }@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)); }
אימות באמצעות מידע ביומטרי או פרטי כניסה למסך הנעילה
אפשר להשתמש במפתח סודי שמאפשר אימות באמצעות פרטי כניסה ביומטריים או פרטי כניסה למסך הנעילה (קוד אימות, קו ביטול נעילה או סיסמה). מתי הגדרת המפתח הזה, קביעת פרק זמן לתוקף. במהלך פרק הזמן הזה, האפליקציה יכולה לבצע כמה פעולות קריפטוגרפיות בלי שהמשתמש יצטרך לבצע אימות מחדש.
כדי להצפין מידע רגיש אחרי שהמשתמש מבצע אימות באמצעות פרטי כניסה ביומטריים או באמצעות מסך הנעילה, מבצעים את הפעולות הבאות:
ליצור מפתח שמשתמש
KeyGenParameterSpec
תצורה: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())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());בתקופה של
VALIDITY_DURATION_SECONDS
לאחר המשתמש מתבצע אימות והצפנה של המידע הרגיש: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) }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); } }
אימות באמצעות מפתחות לאימות לשימוש חד-פעמי
אפשר לספק תמיכה למפתחות אימות לשימוש במופע של
BiometricPrompt
מפתח כזה
מחייבת את המשתמש להציג פרטי כניסה ביומטריים או מכשיר
פרטי כניסה בכל פעם שהאפליקציה צריכה לגשת לנתונים
. מפתחות לאימות בכל שימוש יכולים להיות שימושיים בעסקאות בעלות גבוהה, כמו ביצוע תשלום גדול או עדכון של רשומות רפואיות של אדם.
כדי לשייך אובייקט BiometricPrompt
למפתח אימות לשימוש, צריך להוסיף קוד
כמו:
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()
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 מוצגות שתי גרסאות של אותה תיבת דו-שיח. לגרסה אחת נדרש תג מפורש פעולה מצד המשתמש, והגרסה השנייה לא עושה זאת.
בקטע הקוד הבא מוסבר איך להציג תיבת דו-שיח שלא לדרוש פעולה מפורשת מצד המשתמש כדי להשלים את תהליך האימות:
// 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()
// 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, אפשר לעיין במאמרים הבאים: במשאבי אנוש.