התכונה Smart Lock לסיסמאות הוצאה משימוש ב-2022, ועכשיו היא מוּסרת מ-Google Play Services Auth SDK (com.google.android.gms:play-services-auth
) . כדי להפחית את מספרם של שינויי התוכנה שעלולים לגרום לכשל בשילובים קיימים, היכולות של Smart Lock בכל האפליקציות הקיימות בחנות Play ימשיכו לפעול כראוי. לגרסאות חדשות של האפליקציה ששולבו עם ערכת ה-SDK המעודכנת (com.google.android.gms:play-services-auth:21.0.0
) לא תהיה יותר גישה ל-Smart Lock for Password API וה-build שלהן ייכשל. כל המפתחים צריכים להעביר את הפרויקטים שלהם ל-Android ולהשתמש ב-Credential Manager בהקדם האפשרי.
היתרונות של המעבר ל-Credential Manager API
Credential Manager מציע ממשק API מאוחד ופשוט שמאפשר תמיכה בתכונות ובשיטות מודרניות, תוך שיפור חוויית האימות של המשתמשים:
- מערכת Credential Manager תומכת באופן מלא בשמירה ובאימות של סיסמאות. כלומר, לא תהיה הפרעה למשתמשים בזמן שהאפליקציה שלכם תעבור מ-Smart Lock ל-Credential Manager.
- Credential Manager משלב תמיכה במספר שיטות כניסה, כולל מפתחות גישה ושיטות כניסה מאוחדות כמו כניסה באמצעות חשבון Google, כדי לשפר את האבטחה ולאפשר המרה אם אתם מתכננים לתמוך בשיטות האלה בעתיד.
- החל מ-Android 14, מנהל פרטי הכניסה תומך בספקי סיסמאות ומפתחות גישה של צד שלישי.
- Credential Manager מספק חוויית משתמש עקבית ואחידה בכל שיטות האימות.
אפשרויות העברה:
תרחיש לדוגמה | המלצה |
---|---|
שמירת הסיסמה וכניסה באמצעות הסיסמה השמורה | משתמשים באפשרות הסיסמה מהמדריך כניסת משתמשים באמצעות Credential Manager. בהמשך מפורטים השלבים המפורטים לשמירת סיסמה ולאימות. |
כניסה באמצעות חשבון Google | פועלים לפי ההוראות במדריך אינטגרציה של Credential Manager עם 'כניסה באמצעות חשבון Google'. |
הצגת הנחיה למשתמשים לגבי מספר הטלפון | משתמשים ב-Phone Number Hint API מ-Google Identity Services. |
כניסה באמצעות סיסמאות באמצעות Credential Manager
כדי להשתמש ב-Credential Manager API, צריך לבצע את השלבים שמפורטים בקטע דרישות מוקדמות במדריך של Credential Manager, ולוודא ש:
- הוספת יחסי התלות הנדרשים
- שימור כיתות בקובץ ProGuard
- מוסיפים תמיכה בקישורים לנכסים דיגיטליים (השלב הזה אמור להיות מיושם כבר אם אתם משתמשים ב-Smart Lock לסיסמאות)
- הגדרת Credential Manager
- ציון השדות של פרטי הכניסה
- שמירה של סיסמה של משתמש
כניסה באמצעות סיסמאות שמורות
כדי לאחזר את אפשרויות הסיסמה שמשויכות לחשבון המשתמש:
1. איך מפעילים את הסיסמה ואת אפשרות האימות
// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()
2. משתמשים באפשרויות שאוחזרו מהשלב הקודם כדי ליצור את בקשת הכניסה.
val getCredRequest = GetCredentialRequest(
listOf(getPasswordOption)
)
3. הפעלת תהליך הכניסה
fun launchSignInFlow() {
coroutineScope.launch {
try {
// Attempt to retrieve the credential from the Credential Manager.
val result = credentialManager.getCredential(
// Use an activity-based context to avoid undefined system UI
// launching behavior.
context = activityContext,
request = getCredRequest
)
// Process the successfully retrieved credential.
handleSignIn(result)
} catch (e: GetCredentialException) {
// Handle any errors that occur during the credential retrieval
// process.
handleFailure(e)
}
}
}
private fun handleSignIn(result: GetCredentialResponse) {
// Extract the credential from the response.
val credential = result.credential
// Determine the type of credential and handle it accordingly.
when (credential) {
is PasswordCredential -> {
val username = credential.id
val password = credential.password
// Use the extracted username and password to perform
// authentication.
}
else -> {
// Handle unrecognized credential types.
Log.e(TAG, "Unexpected type of credential")
}
}
}
private fun handleFailure(e: GetCredentialException) {
// Handle specific credential retrieval errors.
when (e) {
is GetCredentialCancellationException -> {
/* This exception is thrown when the user intentionally cancels
the credential retrieval operation. Update the application's state
accordingly. */
}
is GetCredentialCustomException -> {
/* This exception is thrown when a custom error occurs during the
credential retrieval flow. Refer to the documentation of the
third-party SDK used to create the GetCredentialRequest for
handling this exception. */
}
is GetCredentialInterruptedException -> {
/* This exception is thrown when an interruption occurs during the
credential retrieval flow. Determine whether to retry the
operation or proceed with an alternative authentication method. */
}
is GetCredentialProviderConfigurationException -> {
/* This exception is thrown when there is a mismatch in
configurations for the credential provider. Verify that the
provider dependency is included in the manifest and that the
required system services are enabled. */
}
is GetCredentialUnknownException -> {
/* This exception is thrown when the credential retrieval
operation fails without providing any additional details. Handle
the error appropriately based on the application's context. */
}
is GetCredentialUnsupportedException -> {
/* This exception is thrown when the device does not support the
Credential Manager feature. Inform the user that credential-based
authentication is unavailable and guide them to an alternative
authentication method. */
}
is NoCredentialException -> {
/* This exception is thrown when there are no viable credentials
available for the user. Prompt the user to sign up for an account
or provide an alternative authentication method. Upon successful
authentication, store the login information using
androidx.credentials.CredentialManager.createCredential to
facilitate easier sign-in the next time. */
}
else -> {
// Handle unexpected exceptions.
Log.w(TAG, "Unexpected exception type: ${e::class.java.name}")
}
}
}
מקורות מידע נוספים
- חומר עזר לדוגמה בנושא Credential Manager
- Codelab בנושא Credential Manager
- כניסה של המשתמש באמצעות Credential Manager