From 773d60180ff0f721cca145e6c06d6101ad43f54a Mon Sep 17 00:00:00 2001 From: lalwani Date: Tue, 17 Dec 2024 08:58:50 -0800 Subject: [PATCH] Switching to use startActivity with flags instead of startActivityForResult --- .../com/uber/sdk2/auth/AuthProviding.kt | 3 -- .../uber/sdk2/auth/internal/AuthActivity.kt | 48 +++++++++++-------- .../uber/sdk2/auth/internal/AuthProvider.kt | 4 -- .../auth/internal/sso/UniversalSsoLink.kt | 21 ++------ .../kotlin/com/uber/sdk2/auth/sso/SsoLink.kt | 3 -- 5 files changed, 32 insertions(+), 47 deletions(-) diff --git a/authentication/src/main/kotlin/com/uber/sdk2/auth/AuthProviding.kt b/authentication/src/main/kotlin/com/uber/sdk2/auth/AuthProviding.kt index 88b8896..af20cbb 100644 --- a/authentication/src/main/kotlin/com/uber/sdk2/auth/AuthProviding.kt +++ b/authentication/src/main/kotlin/com/uber/sdk2/auth/AuthProviding.kt @@ -35,7 +35,4 @@ interface AuthProviding { /** Handles the authentication code received from the SSO flow via deeplink. */ fun handleAuthCode(authCode: String) - - /** Checks if the authentication is in progress. */ - fun isAuthInProgress(): Boolean } diff --git a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt index 796c019..e93acb3 100644 --- a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt +++ b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthActivity.kt @@ -49,8 +49,9 @@ class AuthActivity : AppCompatActivity() { authProvider = AuthProvider(this, authContext) } - private fun init() { + private fun startAuth() { authProvider?.let { + authStarted = true lifecycleScope.launch(Dispatchers.Main) { when (val authResult = it.authenticate()) { is AuthResult.Success -> { @@ -76,32 +77,39 @@ class AuthActivity : AppCompatActivity() { override fun onResume() { super.onResume() - - if (!authStarted) { - init() - authStarted = true - return - } - // Check if the intent has the auth code. This happens when user has authenticated using custom - // tabs - intent?.data?.let { - val authCode = it.getQueryParameter(KEY_AUTHENTICATION_CODE) - if (!authCode.isNullOrEmpty()) { - authProvider?.handleAuthCode(authCode) - } else { - // If the intent does not have the auth code, then the user denied the authentication - val error = it.getQueryParameter(KEY_ERROR) ?: CANCELED - finishAuthWithError(error) - } - } + // Check if the intent has the auth code. + intent?.data?.let { handleResponse(it) } ?: run { - if (authProvider?.isAuthInProgress() == true) { + // if intent does not have auth code and auth has not started then start the auth flow + if (!authStarted) { + startAuth() return } + // otherwise finish the auth flow with error finishAuthWithError() } } + private fun handleResponse(uri: Uri) { + // This happens when user has authenticated using custom tabs + val authCode = + if (uri.getQueryParameter(KEY_AUTHENTICATION_CODE).isNullOrEmpty() == false) { + uri.getQueryParameter(KEY_AUTHENTICATION_CODE) + } else if (uri.fragment?.isNotEmpty() == true) { + // This happens when user has authenticated using 1p app + Uri.Builder().encodedQuery(uri.fragment).build().getQueryParameter(KEY_AUTHENTICATION_CODE) + } else { + "" + } + if (!authCode.isNullOrEmpty()) { + authProvider?.handleAuthCode(authCode) + } else { + // If the intent does not have the auth code, then the user denied the authentication + val error = uri.getQueryParameter(KEY_ERROR) ?: CANCELED + finishAuthWithError(error) + } + } + private fun finishAuthWithError(error: String = CANCELED) { // If the intent does not have the auth code, then the user has cancelled the authentication intent.putExtra("EXTRA_ERROR", error) diff --git a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthProvider.kt b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthProvider.kt index be0189e..4a6ba05 100644 --- a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthProvider.kt +++ b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/AuthProvider.kt @@ -117,8 +117,4 @@ class AuthProvider( override fun handleAuthCode(authCode: String) { ssoLink.handleAuthCode(authCode) } - - override fun isAuthInProgress(): Boolean { - return ssoLink.isAuthInProgress() - } } diff --git a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/sso/UniversalSsoLink.kt b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/sso/UniversalSsoLink.kt index 76116d1..607d426 100644 --- a/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/sso/UniversalSsoLink.kt +++ b/authentication/src/main/kotlin/com/uber/sdk2/auth/internal/sso/UniversalSsoLink.kt @@ -24,7 +24,6 @@ package com.uber.sdk2.auth.internal.sso import android.content.Intent import android.net.Uri import androidx.activity.result.ActivityResult -import androidx.activity.result.contract.ActivityResultContracts import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity.RESULT_CANCELED @@ -60,17 +59,6 @@ internal class UniversalSsoLink( ) : SsoLink { @VisibleForTesting val resultDeferred = CompletableDeferred() - private var isAuthInProgress: Boolean = false - - private val launcher = - activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> - try { - resultDeferred.complete(handleResult(result)) - } catch (e: AuthException) { - resultDeferred.completeExceptionally(e) - } - isAuthInProgress = true - } override suspend fun execute(optionalQueryParams: Map): String { val uri = @@ -96,7 +84,9 @@ internal class UniversalSsoLink( val intent = Intent() intent.`package` = packageName intent.data = uri - launcher.launch(intent) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP + intent.putExtra(CALLING_PACKAGE, activity.packageName) + activity.startActivity(intent) } ?: loadCustomtab(getSecureWebviewUri(uri)) } is AuthDestination.InApp -> loadCustomtab(getSecureWebviewUri(uri)) @@ -129,10 +119,6 @@ internal class UniversalSsoLink( } } - override fun isAuthInProgress(): Boolean { - return isAuthInProgress - } - private fun loadCustomtab(uri: Uri) { customTabsLauncher.launch(uri) } @@ -143,5 +129,6 @@ internal class UniversalSsoLink( private const val EXTRA_CODE_RECEIVED = "CODE_RECEIVED" private const val EXTRA_ERROR = "ERROR" + private const val CALLING_PACKAGE = "CALLING_APPLICATION_ID" } } diff --git a/authentication/src/main/kotlin/com/uber/sdk2/auth/sso/SsoLink.kt b/authentication/src/main/kotlin/com/uber/sdk2/auth/sso/SsoLink.kt index d2790b7..933afb2 100644 --- a/authentication/src/main/kotlin/com/uber/sdk2/auth/sso/SsoLink.kt +++ b/authentication/src/main/kotlin/com/uber/sdk2/auth/sso/SsoLink.kt @@ -31,7 +31,4 @@ interface SsoLink { /** Handles the authentication code received from the SSO flow via deeplink. */ fun handleAuthCode(authCode: String) - - /** Checks if the authentication is in progress. */ - fun isAuthInProgress(): Boolean }