-
Notifications
You must be signed in to change notification settings - Fork 393
@W-22699714: [Android] Improve error handling at code exchange #2932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
JohnsonEricAtSalesforce
wants to merge
2
commits into
forcedotcom:dev
Choose a base branch
from
JohnsonEricAtSalesforce:feature/w-22699714_android-improve-error-handling-at-code-exchange
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,7 @@ import com.salesforce.androidsdk.R.string.cannot_use_another_apps_login_qr_code | |
| import com.salesforce.androidsdk.R.string.sf__biometric_opt_in_title | ||
| import com.salesforce.androidsdk.R.string.sf__generic_authentication_error_title | ||
| import com.salesforce.androidsdk.R.string.sf__jwt_authentication_error | ||
| import com.salesforce.androidsdk.R.string.sf__app_blocked_error | ||
| import com.salesforce.androidsdk.R.string.sf__lightning_url_code_exchange_error | ||
| import com.salesforce.androidsdk.R.string.sf__login_with_biometric | ||
| import com.salesforce.androidsdk.R.string.sf__screen_lock_error | ||
|
|
@@ -125,6 +126,7 @@ import com.salesforce.androidsdk.app.Features.FEATURE_WELCOME_DISCOVERY_LOGIN | |
| import com.salesforce.androidsdk.app.SalesforceSDKManager | ||
| import com.salesforce.androidsdk.app.SalesforceSDKManager.Theme.DARK | ||
| import com.salesforce.androidsdk.auth.HttpAccess | ||
| import com.salesforce.androidsdk.auth.OAuth2.CLIENT_BLOCKED_ERROR | ||
| import com.salesforce.androidsdk.auth.OAuth2.OAuthFailedException | ||
| import com.salesforce.androidsdk.auth.OAuth2.TokenEndpointResponse | ||
| import com.salesforce.androidsdk.auth.OAuth2.swapJWTForTokens | ||
|
|
@@ -584,6 +586,8 @@ open class LoginActivity : FragmentActivity() { | |
| ) | ||
|
|
||
| viewModel.clearCookies() | ||
| val isClientBlocked = e is OAuthFailedException | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also recognize CLIENT_BLOCKED_RETRY_ERROR. |
||
| && e.tokenErrorResponse.error == CLIENT_BLOCKED_ERROR | ||
| val isLightningTokenEndpointFailure = e is OAuthFailedException | ||
| && e.tokenErrorResponse.error == "unsupported_grant_type" | ||
| && viewModel.selectedServer.value?.contains(".lightning.") == true | ||
|
|
@@ -592,11 +596,12 @@ open class LoginActivity : FragmentActivity() { | |
| } | ||
| // Displays the error in a toast, clears cookies and reloads the login page | ||
| runOnUiThread { | ||
| if (isLightningTokenEndpointFailure) { | ||
| makeText(this, getString(sf__lightning_url_code_exchange_error), LENGTH_LONG).show() | ||
| } else { | ||
| makeText(this, "$error : $errorDesc", LENGTH_LONG).show() | ||
| val toastMessage = when { | ||
| isClientBlocked -> getString(sf__app_blocked_error) | ||
| isLightningTokenEndpointFailure -> getString(sf__lightning_url_code_exchange_error) | ||
| else -> "$error : $errorDesc" | ||
| } | ||
| makeText(this, toastMessage, LENGTH_LONG).show() | ||
| viewModel.reloadWebView() | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 226 additions & 0 deletions
226
libs/test/SalesforceSDKTest/src/com/salesforce/androidsdk/auth/LoginActivityAuthErrorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| /* | ||
| * Copyright (c) 2025-present, salesforce.com, inc. | ||
| * All rights reserved. | ||
| * Redistribution and use of this software in source and binary forms, with or | ||
| * without modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * - Redistributions of source code must retain the above copyright notice, this | ||
| * list of conditions and the following disclaimer. | ||
| * - Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * - Neither the name of salesforce.com, inc. nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software without | ||
| * specific prior written permission of salesforce.com, inc. | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.salesforce.androidsdk.auth | ||
|
|
||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.IntentFilter | ||
| import androidx.test.core.app.ActivityScenario.launch | ||
| import androidx.test.core.app.ApplicationProvider.getApplicationContext | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.salesforce.androidsdk.accounts.UserAccount | ||
| import com.salesforce.androidsdk.auth.OAuth2.CLIENT_BLOCKED_ERROR | ||
| import com.salesforce.androidsdk.auth.OAuth2.OAuthFailedException | ||
| import com.salesforce.androidsdk.ui.LoginActivity | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import io.mockk.mockkStatic | ||
| import io.mockk.unmockkAll | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| /** | ||
| * Test subclass that exposes the protected onAuthFlowError for testing. | ||
| */ | ||
| class TestLoginActivity : LoginActivity() { | ||
| public override fun onAuthFlowError(error: String, errorDesc: String?, e: Throwable?) { | ||
| super.onAuthFlowError(error, errorDesc, e) | ||
| } | ||
|
|
||
| override fun onAuthFlowSuccess(userAccount: UserAccount) { | ||
| // No-op for tests | ||
| } | ||
| } | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class LoginActivityAuthErrorTest { | ||
|
|
||
| private companion object { | ||
| const val AUTHENTICATION_FAILED_INTENT = "com.salesforce.auth.intent.AUTHENTICATION_ERROR" | ||
| const val HTTP_ERROR_RESPONSE_CODE_INTENT = "com.salesforce.auth.intent.HTTP_RESPONSE_CODE" | ||
| const val RESPONSE_ERROR_INTENT = "com.salesforce.auth.intent.RESPONSE_ERROR" | ||
| const val RESPONSE_ERROR_DESCRIPTION_INTENT = "com.salesforce.auth.intent.RESPONSE_ERROR_DESCRIPTION" | ||
| } | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| OAuth2.TIMESTAMP_FORMAT | ||
| mockkStatic(OAuth2::class) | ||
| } | ||
|
|
||
| @After | ||
| fun teardown() { | ||
| unmockkAll() | ||
| } | ||
|
|
||
| @Test | ||
| fun onAuthFlowError_givenClientBlocked_broadcastsWithCorrectExtras() { | ||
| val tokenErrorResponse = mockk<OAuth2.TokenErrorResponse>(relaxed = true) | ||
| tokenErrorResponse.error = CLIENT_BLOCKED_ERROR | ||
| tokenErrorResponse.errorDescription = "App is blocked by admin" | ||
| val oauthException = OAuthFailedException(tokenErrorResponse, 403) | ||
|
|
||
| every { | ||
| OAuth2.exchangeCode(any(), any(), any(), any(), any(), any()) | ||
| } throws oauthException | ||
|
|
||
| val latch = CountDownLatch(1) | ||
| var receivedIntent: Intent? = null | ||
|
|
||
| val receiver = object : BroadcastReceiver() { | ||
| override fun onReceive(context: Context?, intent: Intent?) { | ||
| receivedIntent = intent | ||
| latch.countDown() | ||
| } | ||
| } | ||
|
|
||
| val context: Context = getApplicationContext() | ||
| context.registerReceiver( | ||
| receiver, | ||
| IntentFilter(AUTHENTICATION_FAILED_INTENT), | ||
| Context.RECEIVER_EXPORTED | ||
| ) | ||
|
|
||
| try { | ||
| launch<TestLoginActivity>( | ||
| Intent(context, TestLoginActivity::class.java) | ||
| ).use { activityScenario -> | ||
| activityScenario.onActivity { activity -> | ||
| activity.viewModel.onWebServerFlowComplete( | ||
| "test_code", | ||
| { error, errorDesc, e -> activity.onAuthFlowError(error, errorDesc, e) }, | ||
| { }, | ||
| ) | ||
| } | ||
|
|
||
| assertTrue("Broadcast should be received within 5 seconds", latch.await(5, TimeUnit.SECONDS)) | ||
| assertNotNull(receivedIntent) | ||
| assertEquals(403, receivedIntent!!.getIntExtra(HTTP_ERROR_RESPONSE_CODE_INTENT, 0)) | ||
| assertEquals(CLIENT_BLOCKED_ERROR, receivedIntent!!.getStringExtra(RESPONSE_ERROR_INTENT)) | ||
| assertEquals("App is blocked by admin", receivedIntent!!.getStringExtra(RESPONSE_ERROR_DESCRIPTION_INTENT)) | ||
| } | ||
| } finally { | ||
| context.unregisterReceiver(receiver) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun onAuthFlowError_givenClientBlocked_showsAppBlockedToast() { | ||
| val tokenErrorResponse = mockk<OAuth2.TokenErrorResponse>(relaxed = true) | ||
| tokenErrorResponse.error = CLIENT_BLOCKED_ERROR | ||
| tokenErrorResponse.errorDescription = "App is blocked" | ||
| val oauthException = OAuthFailedException(tokenErrorResponse, 403) | ||
|
|
||
| every { | ||
| OAuth2.exchangeCode(any(), any(), any(), any(), any(), any()) | ||
| } throws oauthException | ||
|
|
||
| launch<TestLoginActivity>( | ||
| Intent(getApplicationContext(), TestLoginActivity::class.java) | ||
| ).use { activityScenario -> | ||
| activityScenario.onActivity { activity -> | ||
| activity.viewModel.onWebServerFlowComplete( | ||
| "test_code", | ||
| { error, errorDesc, e -> activity.onAuthFlowError(error, errorDesc, e) }, | ||
| { }, | ||
| ) | ||
| } | ||
|
|
||
| // Allow time for the coroutine + runOnUiThread to complete | ||
| Thread.sleep(500) | ||
|
|
||
| activityScenario.onActivity { activity -> | ||
| val expectedMessage = activity.getString( | ||
| com.salesforce.androidsdk.R.string.sf__app_blocked_error | ||
| ) | ||
| assertEquals( | ||
| "This app has been blocked. Contact your administrator for assistance.", | ||
| expectedMessage | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun onAuthFlowError_givenGenericOAuthError_broadcastsWithCorrectExtras() { | ||
| val tokenErrorResponse = mockk<OAuth2.TokenErrorResponse>(relaxed = true) | ||
| tokenErrorResponse.error = "invalid_grant" | ||
| tokenErrorResponse.errorDescription = "Expired authorization code" | ||
| val oauthException = OAuthFailedException(tokenErrorResponse, 400) | ||
|
|
||
| every { | ||
| OAuth2.exchangeCode(any(), any(), any(), any(), any(), any()) | ||
| } throws oauthException | ||
|
|
||
| val latch = CountDownLatch(1) | ||
| var receivedIntent: Intent? = null | ||
|
|
||
| val receiver = object : BroadcastReceiver() { | ||
| override fun onReceive(context: Context?, intent: Intent?) { | ||
| receivedIntent = intent | ||
| latch.countDown() | ||
| } | ||
| } | ||
|
|
||
| val context: Context = getApplicationContext() | ||
| context.registerReceiver( | ||
| receiver, | ||
| IntentFilter(AUTHENTICATION_FAILED_INTENT), | ||
| Context.RECEIVER_EXPORTED | ||
| ) | ||
|
|
||
| try { | ||
| launch<TestLoginActivity>( | ||
| Intent(context, TestLoginActivity::class.java) | ||
| ).use { activityScenario -> | ||
| activityScenario.onActivity { activity -> | ||
| activity.viewModel.onWebServerFlowComplete( | ||
| "test_code", | ||
| { error, errorDesc, e -> activity.onAuthFlowError(error, errorDesc, e) }, | ||
| { }, | ||
| ) | ||
| } | ||
|
|
||
| assertTrue("Broadcast should be received within 5 seconds", latch.await(5, TimeUnit.SECONDS)) | ||
| assertNotNull(receivedIntent) | ||
| assertEquals(400, receivedIntent!!.getIntExtra(HTTP_ERROR_RESPONSE_CODE_INTENT, 0)) | ||
| assertEquals("invalid_grant", receivedIntent!!.getStringExtra(RESPONSE_ERROR_INTENT)) | ||
| assertEquals("Expired authorization code", receivedIntent!!.getStringExtra(RESPONSE_ERROR_DESCRIPTION_INTENT)) | ||
| } | ||
| } finally { | ||
| context.unregisterReceiver(receiver) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The server returns user_blocked error when the app failed app attestation.
The error returned will probably change (soon) to reflect that - but our message here could already be more descriptive.