התכונה 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 לסיסמאות 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 מספק חוויית משתמש עקבית ואחידה בכל שיטות האימות.
אפשרויות העברה:
| תרחיש שימוש | המלצה |
|---|---|
| שמירת סיסמה וכניסה באמצעות סיסמה שמורה | משתמשים באפשרות הסיסמה מהמדריך כניסת משתמשים באמצעות Credential Manager. בהמשך מפורטים השלבים לשמירת סיסמאות ולאימות. |
| כניסה באמצעות חשבון Google | פועלים לפי ההוראות במדריך שילוב של Credential Manager עם "כניסה באמצעות חשבון Google". |
| הצגת רמז למספר הטלפון למשתמשים | משתמשים ב-Phone Number Hint API מ-Google Identity Services. |
כניסה באמצעות סיסמאות דרך Credential Manager
כדי להשתמש ב-Credential Manager API, צריך לבצע את השלבים שמפורטים בקטע דרישות מוקדמות במדריך של Credential Manager, ולוודא שמבצעים את הפעולות הבאות:
- הוספת יחסי התלות הנדרשים
- שמירת מחלקות בקובץ ProGuard
- הוספת תמיכה ב-Digital Asset Links (השלב הזה כבר אמור להיות מיושם אם אתם משתמשים ב-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