Skip to content

Commit abaf21e

Browse files
authored
Merge pull request #2733 from brandonpage/allow-browser-auth
Revert 13.0 Advanced Auth behavior when useWebServerAuthentication is false.
2 parents af3d85a + 00231ae commit abaf21e

2 files changed

Lines changed: 25 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
@@ -143,6 +144,7 @@ import com.salesforce.androidsdk.util.SalesforceSDKLogger.e
143144
import com.salesforce.androidsdk.util.SalesforceSDKLogger.w
144145
import com.salesforce.androidsdk.util.UriFragmentParser
145146
import kotlinx.coroutines.CoroutineScope
147+
import kotlinx.coroutines.Dispatchers
146148
import kotlinx.coroutines.Dispatchers.Default
147149
import kotlinx.coroutines.Dispatchers.IO
148150
import kotlinx.coroutines.launch
@@ -316,11 +318,18 @@ open class LoginActivity : FragmentActivity() {
316318
}
317319
} else {
318320
with(SalesforceSDKManager.getInstance()) {
319-
if (useWebServerAuthentication) {
320-
// Fetch well known config and load in custom tab if required.
321-
fetchAuthenticationConfiguration {
322-
if (isBrowserLoginEnabled) {
321+
// Fetch well known config and load in custom tab if required.
322+
fetchAuthenticationConfiguration {
323+
if (isBrowserLoginEnabled) {
324+
if (useWebServerAuthentication) {
323325
viewModel.loginUrl.value?.let { url -> loadLoginPageInCustomTab(url, customTabLauncher) }
326+
} else {
327+
/* Reload the webview now that isBrowserLoginEnabled has been set
328+
to true so that we generate an authorization URL with PKCE values. */
329+
lifecycleScope.launch(Dispatchers.Main) {
330+
viewModel.reloadWebView()
331+
viewModel.loginUrl.value?.let { url -> loadLoginPageInCustomTab(url, customTabLauncher) }
332+
}
324333
}
325334
}
326335
}
@@ -923,25 +932,19 @@ open class LoginActivity : FragmentActivity() {
923932
// Show loading while we PKCE and/or create user account.
924933
viewModel.authFinished.value = true
925934

926-
// Determine if presence of override parameters require the user agent flow.
927-
val overrideWithUserAgentFlow = viewModel.isUsingFrontDoorBridge
928-
&& viewModel.frontdoorBridgeCodeVerifier == null
929935
when {
930-
SalesforceSDKManager.getInstance().useWebServerAuthentication
931-
&& !overrideWithUserAgentFlow ->
932-
936+
viewModel.useWebServerFlow ->
933937
viewModel.onWebServerFlowComplete(
934938
params["code"],
935939
::onAuthFlowError,
936-
::onAuthFlowSuccess
940+
::onAuthFlowSuccess,
937941
)
938-
939942
else ->
940943
CoroutineScope(Default).launch {
941944
viewModel.onAuthFlowComplete(
942945
TokenEndpointResponse(params),
943946
::onAuthFlowError,
944-
::onAuthFlowSuccess
947+
::onAuthFlowSuccess,
945948
)
946949
}
947950
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ 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+
// Browser based authentication requires the Web Server flow for PKCE security.
150+
(useWebServerAuthentication || isBrowserLoginEnabled)
151+
// QR Code login may require User Agent flow.
152+
&& !(isUsingFrontDoorBridge && frontdoorBridgeCodeVerifier == null)
153+
}
154+
147155
/**
148156
* Setting this option to true will enable a mode where only a custom tab will be shown. The first server will be
149157
* launched in a custom tab immediately and the user will not be able to switch servers. The LoginActivity is
@@ -309,12 +317,11 @@ open class LoginViewModel(val bootConfig: BootConfig) : ViewModel() {
309317
else -> additionalParameters
310318
}
311319

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

316323
val authorizationUrl = OAuth2.getAuthorizationUrl(
317-
SalesforceSDKManager.getInstance().useWebServerAuthentication,
324+
useWebServerFlow,
318325
SalesforceSDKManager.getInstance().useHybridAuthentication,
319326
URI(server),
320327
clientId,

0 commit comments

Comments
 (0)