از «Smart Lock برای گذرواژه‌ها» به Credential Manager نقل مکان کنید

Smart Lock برای گذرواژه‌ها که در سال ۲۰۲۲ منسوخ شد، اکنون از کیت توسعه نرم‌افزار احراز هویت خدمات Google Play (com.google.android.gms:play-services-auth) حذف شده است. برای به‌حداقل رساندن تغییرات ناگهانی که ممکن است بر یکپارچه‌سازی‌های فعلی تأثیر بگذارد، قابلیت‌های Smart Lock مربوط به همه برنامه‌های موجود در «فروشگاه Play» همچنان به‌درستی کار خواهد کرد. نسخه‌های جدید برنامه که با کیت توسعه نرم‌افزار به‌روزشده (com.google.android.gms:play-services-auth:21.0.0) ترجمه شده‌اند، دیگر نمی‌توانند به میانای برنامه‌سازی کاربردی «Smart Lock برای گذرواژه‌ها» دسترسی پیدا کنند و به‌درستی ساخته نمی‌شوند. همه توسعه‌دهندگان باید در اسرع وقت پروژه‌های Android خود را منتقل کنند تا از Credential Manager استفاده کنند.

مزایای انتقال به Credential Manager API

‫Credential Manager یک API ساده و یکپارچه ارائه می‌دهد که پشتیبانی از ویژگی‌ها و شیوه‌های مدرن را امکان‌پذیر می‌کند و درعین‌حال تجربه اصالت‌سنجی را برای کاربران شما بهبود می‌بخشد:

  • ‫Credential Manager به‌طور کامل از ذخیره گذرواژه و اصالت‌سنجی پشتیبانی می‌کند. این یعنی وقتی برنامه‌تان از Smart Lock به Credential Manager منتقل می‌شود، هیچ وقفه‌ای برای کاربران شما ایجاد نمی‌شود.
  • ‫Credential Manager پشتیبانی از چندین روش ورود به سیستم را ادغام می‌کند، ازجمله گذرکلید و روش‌های ورود به سیستم فدرال مانند ورود به سیستم با Google، تا امنیت را افزایش دهد و اگر قصد دارید در آینده از هرکدام پشتیبانی کنید، تبدیل را فعال کند.
  • از Android 14، «مدیر اطلاعات کاربری» از ارائه‌دهندگان گذرواژه و گذرکلید طرف سوم پشتیبانی می‌کند.
  • «مدیر اطلاعات کاربری» تجربه کاربری یکپارچه و ثابتی را در همه روش‌های اصالت‌سنجی ارائه می‌دهد.

گزینه‌های انتقال:

مورد استفاده توصیه
ذخیره کردن گذرواژه و ورود به سیستم با گذرواژه ذخیره‌شده از گزینه گذرواژه در راهنمای ورود به سیستم کاربر با Credential Manager استفاده کنید. مراحل دقیق ذخیره گذرواژه و اصالت‌سنجی در ادامه شرح داده شده است.
ورود به سیستم با Google‎ راهنمای ادغام Credential Manager با «ورود به سیستم با Google» را دنبال کنید.
نمایش راهنمای شماره تلفن به کاربران از میانای برنامه‌سازی کاربردی راهنمای شماره تلفن از «سرویس‌های مدیریت هویت‌سنجی Google» استفاده کنید.

ورود به سیستم با گذرواژه بااستفاده از Credential Manager

برای استفاده از Credential Manager API، مراحل ذکرشده در بخش پیش‌نیازهای راهنمای Credential Manager را تکمیل کنید و مطمئن شوید که کارهای زیر را انجام می‌دهید:

ورود به سیستم با گذرواژه‌های ذخیره‌شده

برای بازیابی گزینه‌های گذرواژه مرتبط با حساب کاربر، این مراحل را تکمیل کنید:

۱. مقداردهی اولیه گذرواژه و گزینه اصالت‌سنجی

// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()

۲. از گزینه‌های بازیابی‌شده از مرحله قبلی برای ساختن درخواست ورود به سیستم استفاده کنید

val getCredRequest = GetCredentialRequest(
    listOf(getPasswordOption)
)

۳. راه‌اندازی جریان ورود به سیستم

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}")
        }
    }
}

منابع بیشتر