Skip to content

Commit 03e67d7

Browse files
@W-22699714: [Android] Improve error handling at code exchange (#2932)
1 parent bf1da6b commit 03e67d7

4 files changed

Lines changed: 163 additions & 56 deletions

File tree

libs/SalesforceSDK/res/values/sf__strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<string name="sf__managed_app_error">Authentication only allowed from managed device.</string>
1212
<string name="sf__jwt_authentication_error">JWT authentication error. Please try again.</string>
1313
<string name="sf__lightning_url_code_exchange_error">Lightning URLs are not supported for OAuth code exchange. Use your My Domain URL instead.</string>
14+
<string name="sf__app_blocked_error">This app could not be verified. Contact support.</string>
1415

1516
<!-- SSL errors -->
1617
<string name="sf__ssl_error">SSL error: %s.</string>

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import com.salesforce.androidsdk.R.color.sf__background
106106
import com.salesforce.androidsdk.R.color.sf__background_dark
107107
import com.salesforce.androidsdk.R.drawable.sf__action_back
108108
import com.salesforce.androidsdk.R.string.cannot_use_another_apps_login_qr_code
109+
import com.salesforce.androidsdk.R.string.sf__app_blocked_error
109110
import com.salesforce.androidsdk.R.string.sf__biometric_opt_in_title
110111
import com.salesforce.androidsdk.R.string.sf__generic_authentication_error_title
111112
import com.salesforce.androidsdk.R.string.sf__jwt_authentication_error
@@ -125,6 +126,8 @@ import com.salesforce.androidsdk.app.Features.FEATURE_WELCOME_DISCOVERY_LOGIN
125126
import com.salesforce.androidsdk.app.SalesforceSDKManager
126127
import com.salesforce.androidsdk.app.SalesforceSDKManager.Theme.DARK
127128
import com.salesforce.androidsdk.auth.HttpAccess
129+
import com.salesforce.androidsdk.auth.OAuth2.CLIENT_BLOCKED_ERROR
130+
import com.salesforce.androidsdk.auth.OAuth2.CLIENT_BLOCKED_RETRY_ERROR
128131
import com.salesforce.androidsdk.auth.OAuth2.OAuthFailedException
129132
import com.salesforce.androidsdk.auth.OAuth2.TokenEndpointResponse
130133
import com.salesforce.androidsdk.auth.OAuth2.swapJWTForTokens
@@ -228,7 +231,7 @@ open class LoginActivity : FragmentActivity() {
228231
}
229232

230233
// Private variables
231-
private var baseUserAgentString = "";
234+
private var baseUserAgentString = ""
232235
private var wasBackgrounded = false
233236
private var accountAuthenticatorResponse: AccountAuthenticatorResponse? = null
234237
private var accountAuthenticatorResult: Bundle? = null
@@ -584,6 +587,9 @@ open class LoginActivity : FragmentActivity() {
584587
)
585588

586589
viewModel.clearCookies()
590+
val isClientBlocked = e is OAuthFailedException
591+
&& (e.tokenErrorResponse.error == CLIENT_BLOCKED_ERROR
592+
|| e.tokenErrorResponse.error == CLIENT_BLOCKED_RETRY_ERROR)
587593
val isLightningTokenEndpointFailure = e is OAuthFailedException
588594
&& e.tokenErrorResponse.error == "unsupported_grant_type"
589595
&& viewModel.selectedServer.value?.contains(".lightning.") == true
@@ -592,11 +598,12 @@ open class LoginActivity : FragmentActivity() {
592598
}
593599
// Displays the error in a toast, clears cookies and reloads the login page
594600
runOnUiThread {
595-
if (isLightningTokenEndpointFailure) {
596-
makeText(this, getString(sf__lightning_url_code_exchange_error), LENGTH_LONG).show()
597-
} else {
598-
makeText(this, "$error : $errorDesc", LENGTH_LONG).show()
601+
val message = when {
602+
isClientBlocked -> getString(sf__app_blocked_error)
603+
isLightningTokenEndpointFailure -> getString(sf__lightning_url_code_exchange_error)
604+
else -> "$error : $errorDesc"
599605
}
606+
makeText(this, message, LENGTH_LONG).show()
600607
viewModel.reloadWebView()
601608
}
602609
}
@@ -936,12 +943,12 @@ open class LoginActivity : FragmentActivity() {
936943
// Set welcome discovery feature flag if applicable
937944
if (isLoginWithWelcomeDiscovery(intent)) {
938945
SalesforceSDKManager.getInstance()
939-
.registerUsedAppFeature(FEATURE_WELCOME_DISCOVERY_LOGIN);
946+
.registerUsedAppFeature(FEATURE_WELCOME_DISCOVERY_LOGIN)
940947
}
941948
else {
942949
SalesforceSDKManager.getInstance().unregisterUsedAppFeature(
943950
FEATURE_WELCOME_DISCOVERY_LOGIN
944-
);
951+
)
945952
}
946953

947954
// Re-apply user agent to WebView
@@ -1125,7 +1132,7 @@ open class LoginActivity : FragmentActivity() {
11251132
loginHint = uri.getQueryParameter(SALESFORCE_WELCOME_DISCOVERY_MOBILE_CALLBACK_URL_QUERY_PARAMETER_KEY_LOGIN_HINT) ?: return false,
11261133
loginHost = uri.getQueryParameter(SALESFORCE_WELCOME_DISCOVERY_MOBILE_CALLBACK_URL_QUERY_PARAMETER_KEY_MY_DOMAIN)?.toUri()?.host ?: return false
11271134
)
1128-
return true
1135+
true
11291136
} else false
11301137
}
11311138

@@ -1534,7 +1541,7 @@ open class LoginActivity : FragmentActivity() {
15341541
/**
15351542
* Determines if the provided URL has the Salesforce Welcome Discovery
15361543
* path.
1537-
* @param url The URL to examine for the Salesforce Welcome Discovery
1544+
* @param uri The URL to examine for the Salesforce Welcome Discovery
15381545
* path
15391546
* @return Boolean true if the URL has the Salesforce Welcome Discovery
15401547
* path or false otherwise
@@ -1549,7 +1556,7 @@ open class LoginActivity : FragmentActivity() {
15491556
* Determines if the provided URL has the Salesforce Welcome Discovery
15501557
* path and parameters for mobile callback. The client id (consumer
15511558
* key) of the URL must match the boot config's consumer key.
1552-
* @param url The URL to examine for the Salesforce Welcome Discovery
1559+
* @param uri The URL to examine for the Salesforce Welcome Discovery
15531560
* path and parameters for mobile callback
15541561
* @return Boolean true if the URL has the Salesforce Welcome Discovery
15551562
* path and parameters for mobile callback and matches the boot config's
@@ -1575,7 +1582,7 @@ open class LoginActivity : FragmentActivity() {
15751582
* Determines if the provided URL has the Salesforce Welcome Discovery
15761583
* path and parameters for mobile callback. The client id (consumer
15771584
* key) of the URL must match the boot config's consumer key.
1578-
* @param url The URL to examine for the Salesforce Welcome Discovery
1585+
* @param uri The URL to examine for the Salesforce Welcome Discovery
15791586
* path and parameters for mobile callback
15801587
* @return Boolean true if the URL has the Salesforce Welcome Discovery
15811588
* path and parameters for mobile callback and matches the boot config's
@@ -1654,7 +1661,7 @@ open class LoginActivity : FragmentActivity() {
16541661
* Activity result callback for the "Login for Admin" custom tab.
16551662
*/
16561663
@VisibleForTesting
1657-
internal inner class AdminCustomTabActivityResult : ActivityResultCallback<ActivityResult> {
1664+
internal class AdminCustomTabActivityResult : ActivityResultCallback<ActivityResult> {
16581665
override fun onActivityResult(result: ActivityResult) {
16591666
// Intentional no-op: keep the existing WebView visible on cancel.
16601667
}

0 commit comments

Comments
 (0)