Skip to content

Commit bf00ccb

Browse files
committed
Allow Browser Auth (with Web Server Flow) to be used if app has set useWebServerAuthentication to false.
1 parent 13b5611 commit bf00ccb

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

libs/SalesforceSDK/src/com/salesforce/androidsdk/ui/LoginActivity.kt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import androidx.core.content.ContextCompat.getMainExecutor
102102
import androidx.core.net.toUri
103103
import androidx.core.view.WindowCompat
104104
import androidx.fragment.app.FragmentActivity
105+
import androidx.lifecycle.lifecycleScope
105106
import com.salesforce.androidsdk.R.color.sf__background
106107
import com.salesforce.androidsdk.R.color.sf__background_dark
107108
import com.salesforce.androidsdk.R.color.sf__primary_color
@@ -142,6 +143,7 @@ import com.salesforce.androidsdk.util.SalesforceSDKLogger.e
142143
import com.salesforce.androidsdk.util.SalesforceSDKLogger.w
143144
import com.salesforce.androidsdk.util.UriFragmentParser
144145
import kotlinx.coroutines.CoroutineScope
146+
import kotlinx.coroutines.Dispatchers
145147
import kotlinx.coroutines.Dispatchers.Default
146148
import kotlinx.coroutines.Dispatchers.IO
147149
import kotlinx.coroutines.launch
@@ -293,11 +295,18 @@ open class LoginActivity : FragmentActivity() {
293295
}
294296
} else {
295297
with(SalesforceSDKManager.getInstance()) {
296-
if (useWebServerAuthentication) {
297-
// Fetch well known config and load in custom tab if required.
298-
fetchAuthenticationConfiguration {
299-
if (isBrowserLoginEnabled) {
298+
// Fetch well known config and load in custom tab if required.
299+
fetchAuthenticationConfiguration {
300+
if (isBrowserLoginEnabled) {
301+
if (useWebServerAuthentication) {
300302
viewModel.loginUrl.value?.let { url -> loadLoginPageInCustomTab(url, customTabLauncher) }
303+
} else {
304+
/* Reload the webview now that isBrowserLoginEnabled has been set
305+
to true so that we generate an authorization URL with PKCE values. */
306+
lifecycleScope.launch(Dispatchers.Main) {
307+
viewModel.reloadWebView()
308+
viewModel.loginUrl.value?.let { url -> loadLoginPageInCustomTab(url, customTabLauncher) }
309+
}
301310
}
302311
}
303312
}
@@ -900,25 +909,19 @@ open class LoginActivity : FragmentActivity() {
900909
// Show loading while we PKCE and/or create user account.
901910
viewModel.authFinished.value = true
902911

903-
// Determine if presence of override parameters require the user agent flow.
904-
val overrideWithUserAgentFlow = viewModel.isUsingFrontDoorBridge
905-
&& viewModel.frontdoorBridgeCodeVerifier == null
906912
when {
907-
SalesforceSDKManager.getInstance().useWebServerAuthentication
908-
&& !overrideWithUserAgentFlow ->
909-
913+
viewModel.useWebServerFlow ->
910914
viewModel.onWebServerFlowComplete(
911915
params["code"],
912916
::onAuthFlowError,
913-
::onAuthFlowSuccess
917+
::onAuthFlowSuccess,
914918
)
915-
916919
else ->
917920
CoroutineScope(Default).launch {
918921
viewModel.onAuthFlowComplete(
919922
TokenEndpointResponse(params),
920923
::onAuthFlowError,
921-
::onAuthFlowSuccess
924+
::onAuthFlowSuccess,
922925
)
923926
}
924927
}

libs/SalesforceSDK/src/com/salesforce/androidsdk/ui/LoginViewModel.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ open class LoginViewModel(val bootConfig: BootConfig) : ViewModel() {
144144
protected open val authorizationDisplayType =
145145
SalesforceSDKManager.getInstance().appContext.getString(oauth_display_type)
146146

147+
internal val useWebServerFlow: Boolean
148+
get() = with(SalesforceSDKManager.getInstance()) {
149+
(useWebServerAuthentication || isBrowserLoginEnabled)
150+
&& !(isUsingFrontDoorBridge && frontdoorBridgeCodeVerifier == null)
151+
}
152+
147153
/**
148154
* Setting this option to true will enable a mode where only a custom tab will be shown. The first server will be
149155
* launched in a custom tab immediately and the user will not be able to switch servers. The LoginActivity is
@@ -309,12 +315,11 @@ open class LoginViewModel(val bootConfig: BootConfig) : ViewModel() {
309315
else -> additionalParameters
310316
}
311317

312-
// NB code verifier / code challenge are only used when useWebServerAuthentication is true
313318
val codeVerifier = getRandom128ByteKey().also { codeVerifier = it }
314319
val codeChallenge = getSHA256Hash(codeVerifier)
315320

316321
val authorizationUrl = OAuth2.getAuthorizationUrl(
317-
SalesforceSDKManager.getInstance().useWebServerAuthentication,
322+
useWebServerFlow,
318323
SalesforceSDKManager.getInstance().useHybridAuthentication,
319324
URI(server),
320325
clientId,

0 commit comments

Comments
 (0)