Skip to content

Commit

Permalink
Merge pull request #262 from uber/fix
Browse files Browse the repository at this point in the history
Switching to use startActivity with flags instead of startActivityForResult
  • Loading branch information
lalwani authored Dec 18, 2024
2 parents 5d6b053 + 773d601 commit e664878
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,4 @@ class AuthProvider(
override fun handleAuthCode(authCode: String) {
ssoLink.handleAuthCode(authCode)
}

override fun isAuthInProgress(): Boolean {
return ssoLink.isAuthInProgress()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,17 +59,6 @@ internal class UniversalSsoLink(
) : SsoLink {

@VisibleForTesting val resultDeferred = CompletableDeferred<String>()
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, String>): String {
val uri =
Expand All @@ -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))
Expand Down Expand Up @@ -129,10 +119,6 @@ internal class UniversalSsoLink(
}
}

override fun isAuthInProgress(): Boolean {
return isAuthInProgress
}

private fun loadCustomtab(uri: Uri) {
customTabsLauncher.launch(uri)
}
Expand All @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e664878

Please sign in to comment.