Skip to content

Commit 8f281b8

Browse files
authored
Merge pull request #2708 from wmathurin/master
Cherry picking fixes for 13.0.1
2 parents 96e5dd6 + 9bdc641 commit 8f281b8

5 files changed

Lines changed: 57 additions & 40 deletions

File tree

libs/SalesforceSDK/src/com/salesforce/androidsdk/auth/AuthenticationUtilities.kt

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ import com.salesforce.androidsdk.analytics.SalesforceAnalyticsManager
4242
import com.salesforce.androidsdk.app.Features.FEATURE_BIOMETRIC_AUTH
4343
import com.salesforce.androidsdk.app.Features.FEATURE_SCREEN_LOCK
4444
import com.salesforce.androidsdk.app.SalesforceSDKManager
45-
import com.salesforce.androidsdk.auth.OAuth2.IdServiceResponse
4645
import com.salesforce.androidsdk.auth.OAuth2.TokenEndpointResponse
4746
import com.salesforce.androidsdk.auth.OAuth2.addAuthorizationHeader
47+
import com.salesforce.androidsdk.auth.OAuth2.callIdentityService
4848
import com.salesforce.androidsdk.auth.OAuth2.revokeRefreshToken
4949
import com.salesforce.androidsdk.config.RuntimeConfig.getRuntimeConfig
5050
import com.salesforce.androidsdk.push.PushMessaging.register
@@ -55,8 +55,10 @@ import com.salesforce.androidsdk.security.ScreenLockManager
5555
import com.salesforce.androidsdk.util.SalesforceSDKLogger.e
5656
import com.salesforce.androidsdk.util.SalesforceSDKLogger.w
5757
import kotlinx.coroutines.CoroutineScope
58+
import kotlinx.coroutines.Dispatchers.Default
5859
import kotlinx.coroutines.Dispatchers.IO
5960
import kotlinx.coroutines.launch
61+
import kotlinx.coroutines.withContext
6062
import okhttp3.HttpUrl
6163
import okhttp3.Interceptor
6264
import okhttp3.Request.Builder
@@ -83,7 +85,7 @@ private const val TAG = "AuthenticationUtilities"
8385
* * Creates an Account.
8486
* * Checks for any CA/ECA settings such as Screen Lock or Biometric Authentication.
8587
*/
86-
internal fun onAuthFlowComplete(
88+
internal suspend fun onAuthFlowComplete(
8789
tokenResponse: TokenEndpointResponse,
8890
loginServer: String,
8991
consumerKey: String,
@@ -113,14 +115,17 @@ internal fun onAuthFlowComplete(
113115
return
114116
}
115117

116-
var userIdentity: IdServiceResponse? = null
117-
runCatching {
118-
userIdentity = OAuth2.callIdentityService(
119-
HttpAccess.DEFAULT,
120-
tokenResponse.idUrlWithInstance,
121-
tokenResponse.authToken,
122-
)
123-
}
118+
val userIdentity = runCatching {
119+
withContext(Default) {
120+
callIdentityService(
121+
HttpAccess.DEFAULT,
122+
tokenResponse.idUrlWithInstance,
123+
tokenResponse.authToken,
124+
)
125+
}
126+
}.onFailure { throwable ->
127+
w(TAG, "Cannot fetch user identity due to an error.", throwable)
128+
}.getOrNull()
124129

125130
val mustBeManagedApp = userIdentity?.customPermissions?.optBoolean(MUST_BE_MANAGED_APP_PERM) ?: false
126131
if (mustBeManagedApp && !getRuntimeConfig(context).isManagedApp) {
@@ -231,21 +236,21 @@ internal fun onAuthFlowComplete(
231236
// Screen lock required by mobile policy
232237
if (userIdentity?.screenLockTimeout?.compareTo(0) == 1) {
233238
SalesforceSDKManager.getInstance().registerUsedAppFeature(FEATURE_SCREEN_LOCK)
234-
val timeoutInMills = (userIdentity?.screenLockTimeout ?: 0) * 1000 * 60
239+
val timeoutInMills = userIdentity.screenLockTimeout * 1000 * 60
235240
(SalesforceSDKManager.getInstance().screenLockManager as ScreenLockManager?)?.storeMobilePolicy(
236241
account,
237-
userIdentity?.screenLock ?: false,
242+
userIdentity.screenLock,
238243
timeoutInMills
239244
)
240245
}
241246

242247
// Biometric authorization required by mobile policy
243248
if (userIdentity?.biometricAuth == true) {
244249
SalesforceSDKManager.getInstance().registerUsedAppFeature(FEATURE_BIOMETRIC_AUTH)
245-
val timeoutInMills = (userIdentity?.biometricAuthTimeout ?: 0) * 60 * 1000
250+
val timeoutInMills = userIdentity.biometricAuthTimeout * 60 * 1000
246251
(SalesforceSDKManager.getInstance().biometricAuthenticationManager as BiometricAuthenticationManager?)?.storeMobilePolicy(
247252
account,
248-
userIdentity?.biometricAuth ?: false,
253+
userIdentity.biometricAuth,
249254
timeoutInMills
250255
)
251256
}
@@ -307,7 +312,7 @@ private fun HttpUrl.isSalesforceUrl(): Boolean {
307312
// List from https://help.salesforce.com/s/articleView?language=en_US&id=sf.domain_name_url_formats.htm&type=5
308313
val salesforceHosts = listOf(".salesforce.com", ".force.com", ".sfdcopens.com", ".site.com", ".lightning.com",
309314
".salesforce-sites.com", ".force-user-content.com", ".salesforce-experience.com", ".salesforce-scrt.com")
310-
return salesforceHosts.map { host.endsWith(it) }.any() { it }
315+
return salesforceHosts.map { host.endsWith(it) }.any { it }
311316
}
312317

313318
private fun addAccount(account: UserAccount?) {

libs/SalesforceSDK/src/com/salesforce/androidsdk/auth/NativeLoginManager.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ import com.salesforce.androidsdk.security.BiometricAuthenticationManager.Compani
7575
import com.salesforce.androidsdk.security.SalesforceKeyGenerator.getRandom128ByteKey
7676
import com.salesforce.androidsdk.security.SalesforceKeyGenerator.getSHA256Hash
7777
import com.salesforce.androidsdk.util.SalesforceSDKLogger
78-
import kotlinx.coroutines.Dispatchers
78+
import kotlinx.coroutines.Dispatchers.Default
79+
import kotlinx.coroutines.runBlocking
7980
import kotlinx.coroutines.withContext
8081
import okhttp3.MediaType.Companion.toMediaTypeOrNull
8182
import okhttp3.RequestBody
@@ -196,22 +197,24 @@ internal class NativeLoginManager(
196197
val tokenEndpointResponse = TokenEndpointResponse(tokenResponse.rawResponse)
197198
tokenResponse.consumeQuietly()
198199

199-
return withContext(Dispatchers.IO) {
200+
return withContext(Default) {
200201
suspendCoroutine { continuation ->
201-
onAuthFlowComplete(
202-
tokenResponse = tokenEndpointResponse,
203-
loginServer = loginUrl,
204-
consumerKey = clientId,
205-
onAuthFlowError = { error, errorDesc, e ->
206-
SalesforceSDKLogger.e(TAG, "$error: $errorDesc", e)
207-
continuation.resume(UnknownError)
208-
},
209-
onAuthFlowSuccess = { userAccount ->
210-
SalesforceSDKLogger.d(TAG, "onAuthFlowSuccess $userAccount")
211-
continuation.resume(Success)
212-
},
213-
nativeLogin = true,
214-
)
202+
runBlocking {
203+
onAuthFlowComplete(
204+
tokenResponse = tokenEndpointResponse,
205+
loginServer = loginUrl,
206+
consumerKey = clientId,
207+
onAuthFlowError = { error, errorDesc, e ->
208+
SalesforceSDKLogger.e(TAG, "$error: $errorDesc", e)
209+
continuation.resume(UnknownError)
210+
},
211+
onAuthFlowSuccess = { userAccount ->
212+
SalesforceSDKLogger.d(TAG, "onAuthFlowSuccess $userAccount")
213+
continuation.resume(Success)
214+
},
215+
nativeLogin = true,
216+
)
217+
}
215218
}
216219
}
217220
}

libs/SalesforceSDK/src/com/salesforce/androidsdk/auth/idp/SPAuthCodeHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal class SPAuthCodeHelper private constructor (
8383
return tokenResponse
8484
}
8585

86-
private fun completeLogin(tokenResponse: TokenEndpointResponse) {
86+
private suspend fun completeLogin(tokenResponse: TokenEndpointResponse) {
8787
onAuthFlowComplete(
8888
tokenResponse = tokenResponse,
8989
loginServer = loginUrl,

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import androidx.compose.ui.graphics.toArgb
9797
import androidx.core.content.ContextCompat
9898
import androidx.core.content.ContextCompat.getMainExecutor
9999
import androidx.core.net.toUri
100+
import androidx.core.view.WindowCompat
100101
import androidx.fragment.app.FragmentActivity
101102
import com.salesforce.androidsdk.R.color.sf__background
102103
import com.salesforce.androidsdk.R.color.sf__background_dark
@@ -137,6 +138,7 @@ import com.salesforce.androidsdk.util.SalesforceSDKLogger.e
137138
import com.salesforce.androidsdk.util.SalesforceSDKLogger.w
138139
import com.salesforce.androidsdk.util.UriFragmentParser
139140
import kotlinx.coroutines.CoroutineScope
141+
import kotlinx.coroutines.Dispatchers.Default
140142
import kotlinx.coroutines.Dispatchers.IO
141143
import kotlinx.coroutines.launch
142144
import org.json.JSONObject
@@ -519,7 +521,7 @@ open class LoginActivity : FragmentActivity() {
519521
* @param errorDesc The error description
520522
* @param e The exception
521523
*/
522-
protected fun onAuthFlowError(
524+
protected open fun onAuthFlowError(
523525
error: String,
524526
errorDesc: String?,
525527
e: Throwable? = null,
@@ -884,11 +886,13 @@ open class LoginActivity : FragmentActivity() {
884886
)
885887

886888
else ->
887-
viewModel.onAuthFlowComplete(
888-
TokenEndpointResponse(params),
889-
::onAuthFlowError,
890-
::onAuthFlowSuccess
891-
)
889+
CoroutineScope(Default).launch {
890+
viewModel.onAuthFlowComplete(
891+
TokenEndpointResponse(params),
892+
::onAuthFlowError,
893+
::onAuthFlowSuccess
894+
)
895+
}
892896
}
893897
}
894898
}
@@ -914,6 +918,10 @@ open class LoginActivity : FragmentActivity() {
914918

915919
viewModel.dynamicBackgroundColor.value = validateAndExtractBackgroundColor(result)
916920
?: return@evaluateJavascript
921+
922+
// Ensure Status Bar Icons are readable no matter which OS theme is used.
923+
val useLightIcons = viewModel.dynamicBackgroundTheme.value == DARK
924+
WindowCompat.getInsetsController(window, window.decorView).isAppearanceLightStatusBars = useLightIcons
917925
}.also {
918926
if (!viewModel.authFinished.value) {
919927
viewModel.loading.value = false

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import com.salesforce.androidsdk.auth.OAuth2.TokenEndpointResponse
5454
import com.salesforce.androidsdk.auth.OAuth2.exchangeCode
5555
import com.salesforce.androidsdk.auth.OAuth2.getFrontdoorUrl
5656
import com.salesforce.androidsdk.auth.defaultBuildAccountName
57+
import com.salesforce.androidsdk.auth.onAuthFlowComplete
5758
import com.salesforce.androidsdk.config.BootConfig
5859
import com.salesforce.androidsdk.security.SalesforceKeyGenerator.getRandom128ByteKey
5960
import com.salesforce.androidsdk.security.SalesforceKeyGenerator.getSHA256Hash
@@ -240,7 +241,7 @@ open class LoginViewModel(val bootConfig: BootConfig) : ViewModel() {
240241
* Called when the webview portion of the User Agent flow or the code exchange
241242
* portion of the Web Server is finished to create and the user.
242243
*/
243-
internal fun onAuthFlowComplete(
244+
internal suspend fun onAuthFlowComplete(
244245
tr: TokenEndpointResponse,
245246
onAuthFlowError: (error: String, errorDesc: String?, e: Throwable?) -> Unit,
246247
onAuthFlowSuccess: (userAccount: UserAccount) -> Unit,
@@ -249,7 +250,7 @@ open class LoginViewModel(val bootConfig: BootConfig) : ViewModel() {
249250
// if the user tries to add another user right away.
250251
clearCookies()
251252
authCodeForJwtFlow = null
252-
com.salesforce.androidsdk.auth.onAuthFlowComplete(
253+
onAuthFlowComplete(
253254
tokenResponse = tr,
254255
loginServer = selectedServer.value ?: "", // This will never actually be null.
255256
consumerKey = clientId,

0 commit comments

Comments
 (0)