Skip to content

Commit f39e3aa

Browse files
authored
Merge pull request #2678 from brandonpage/remove_loginoptions
Remove LoginOptions and OAuthWebviewHelper.
2 parents 64dbe6e + c90db40 commit f39e3aa

14 files changed

Lines changed: 104 additions & 1260 deletions

File tree

libs/SalesforceSDK/src/com/salesforce/androidsdk/accounts/UserAccountManager.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ public void switchToUser(UserAccount user, int userSwitchType, Bundle extras) {
325325
if (user.equals(curUser)) {
326326
return;
327327
}
328-
final ClientManager cm = new ClientManager(context, accountType,
329-
SalesforceSDKManager.getInstance().getLoginOptions(), true);
328+
final ClientManager cm = new ClientManager(context, accountType, true);
330329
final Account account = cm.getAccountByName(user.getAccountName());
331330
storeCurrentUserInfo(user.getUserId(), user.getOrgId());
332331
cm.peekRestClient(account);
@@ -350,23 +349,17 @@ public void switchToUser(UserAccount user, int userSwitchType, Bundle extras) {
350349
* in ClientManager will return a RestClient instance for the new user.
351350
*/
352351
public void switchToNewUser() {
353-
final Bundle options = SalesforceSDKManager.getInstance().getLoginOptions().asBundle();
354-
switchToNewUserWithOptions(options);
352+
final Bundle options = new Bundle();
353+
final Bundle reply = new Bundle();
354+
final Intent i = new Intent(context, SalesforceSDKManager.getInstance().getLoginActivityClass());
355+
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
356+
options.putBoolean(BiometricAuthenticationManager.SHOW_BIOMETRIC, false);
357+
options.putBoolean(LoginActivity.NEW_USER, true);
358+
i.putExtras(options);
359+
reply.putParcelable(AccountManager.KEY_INTENT, i);
360+
context.startActivity(i);
355361
}
356362

357-
/**
358-
* Kicks off the login flow to switch to a new user with jwt. Once the login
359-
* flow is complete, the context will automatically become the
360-
* new user's context and a call to peekRestClient() or getRestClient()
361-
* in ClientManager will return a RestClient instance for the new user.
362-
*
363-
* @param jwt JWT.
364-
* @param url Instance/My domain URL.
365-
*/
366-
public void switchToNewUser(String jwt, String url) {
367-
final Bundle options = SalesforceSDKManager.getInstance().getLoginOptions(jwt, url).asBundle();
368-
switchToNewUserWithOptions(options);
369-
}
370363

371364
/**
372365
* Logs the current user out.
@@ -697,17 +690,6 @@ public synchronized void refreshToken(UserAccount userAccount) {
697690
}
698691
}
699692

700-
private void switchToNewUserWithOptions(Bundle options) {
701-
final Bundle reply = new Bundle();
702-
final Intent i = new Intent(context, SalesforceSDKManager.getInstance().getLoginActivityClass());
703-
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
704-
options.putBoolean(BiometricAuthenticationManager.SHOW_BIOMETRIC, false);
705-
options.putBoolean(LoginActivity.NEW_USER, true);
706-
i.putExtras(options);
707-
reply.putParcelable(AccountManager.KEY_INTENT, i);
708-
context.startActivity(i);
709-
}
710-
711693
/**
712694
* Create bundle for authenticator service
713695
* - it uses keys understood by authenticator service

libs/SalesforceSDK/src/com/salesforce/androidsdk/app/SalesforceSDKManager.kt

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ import com.salesforce.androidsdk.push.PushService
119119
import com.salesforce.androidsdk.push.PushService.Companion.pushNotificationsRegistrationType
120120
import com.salesforce.androidsdk.push.PushService.PushNotificationReRegistrationType.ReRegistrationOnAppForeground
121121
import com.salesforce.androidsdk.rest.ClientManager
122-
import com.salesforce.androidsdk.rest.ClientManager.LoginOptions
123122
import com.salesforce.androidsdk.rest.RestClient
124123
import com.salesforce.androidsdk.security.BiometricAuthenticationManager
125124
import com.salesforce.androidsdk.security.SalesforceKeyGenerator.getEncryptionKey
@@ -188,9 +187,6 @@ open class SalesforceSDKManager protected constructor(
188187
/** The Android context */
189188
val appContext: Context = context
190189

191-
/** Login options associated with the app */
192-
private var loginOptionsInternal: LoginOptions? = null
193-
194190
/**
195191
* Returns the class for the main activity.
196192
*
@@ -550,42 +546,6 @@ open class SalesforceSDKManager protected constructor(
550546
}
551547
}
552548

553-
/** Login options associated with the app */
554-
open val loginOptions: LoginOptions get() = getLoginOptions(null, null)
555-
556-
/**
557-
* Sets the login options associated with the app.
558-
*
559-
* @param jwt The `jwt`
560-
* @param url The URL
561-
*/
562-
open fun getLoginOptions(
563-
jwt: String?,
564-
url: String?
565-
) = loginOptionsInternal?.apply {
566-
this.jwt = jwt
567-
setUrl(url)
568-
} ?: getBootConfig(
569-
appContext
570-
).let { config ->
571-
when {
572-
isEmpty(jwt) -> LoginOptions(
573-
url,
574-
config.oauthRedirectURI,
575-
config.remoteAccessConsumerKey,
576-
config.oauthScopes
577-
)
578-
579-
else -> LoginOptions(
580-
url,
581-
config.oauthRedirectURI,
582-
config.remoteAccessConsumerKey,
583-
config.oauthScopes,
584-
jwt
585-
)
586-
}.also { loginOptionsInternal = it }
587-
}
588-
589549
/**
590550
* Indicates if the Salesforce Mobile SDK should automatically log out when
591551
* the access token is revoked. When overriding this method to return false,
@@ -995,7 +955,6 @@ open class SalesforceSDKManager protected constructor(
995955
val clientMgr = ClientManager(
996956
appContext,
997957
accountType,
998-
null,
999958
shouldLogoutWhenTokenRevoked()
1000959
)
1001960

@@ -1202,7 +1161,6 @@ open class SalesforceSDKManager protected constructor(
12021161
ClientManager(
12031162
appContext,
12041163
accountType,
1205-
loginOptions,
12061164
true
12071165
)
12081166
}
@@ -1218,7 +1176,6 @@ open class SalesforceSDKManager protected constructor(
12181176
): ClientManager = ClientManager(
12191177
appContext,
12201178
accountType,
1221-
getLoginOptions(jwt, url),
12221179
true
12231180
)
12241181

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@
2626
*/
2727
package com.salesforce.androidsdk.auth
2828

29+
import android.content.Intent
30+
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
2931
import com.salesforce.androidsdk.R.string.sf__generic_authentication_error
3032
import com.salesforce.androidsdk.R.string.sf__generic_authentication_error_title
3133
import com.salesforce.androidsdk.R.string.sf__managed_app_error
3234
import com.salesforce.androidsdk.accounts.UserAccount
3335
import com.salesforce.androidsdk.accounts.UserAccountBuilder
3436
import com.salesforce.androidsdk.accounts.UserAccountManager
37+
import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_DEFAULT
38+
import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_FIRST_LOGIN
39+
import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_LOGIN
3540
import com.salesforce.androidsdk.analytics.EventBuilderHelper.createAndStoreEventSync
41+
import com.salesforce.androidsdk.analytics.SalesforceAnalyticsManager
3642
import com.salesforce.androidsdk.app.Features.FEATURE_BIOMETRIC_AUTH
3743
import com.salesforce.androidsdk.app.Features.FEATURE_SCREEN_LOCK
3844
import com.salesforce.androidsdk.app.SalesforceSDKManager
@@ -46,7 +52,6 @@ import com.salesforce.androidsdk.rest.RestClient.clearCaches
4652
import com.salesforce.androidsdk.security.BiometricAuthenticationManager
4753
import com.salesforce.androidsdk.security.BiometricAuthenticationManager.Companion.isBiometricAuthenticationEnabled
4854
import com.salesforce.androidsdk.security.ScreenLockManager
49-
import com.salesforce.androidsdk.ui.OAuthWebviewHelper.Companion.MUST_BE_MANAGED_APP_PERM
5055
import com.salesforce.androidsdk.util.SalesforceSDKLogger.e
5156
import com.salesforce.androidsdk.util.SalesforceSDKLogger.w
5257
import kotlinx.coroutines.CoroutineScope
@@ -60,6 +65,12 @@ import org.json.JSONObject
6065
import java.net.URI
6166
import java.util.function.Consumer
6267

68+
/**
69+
* Set a custom permission on the connected application with that name
70+
* for the application to be restricted to managed devices
71+
*/
72+
private const val MUST_BE_MANAGED_APP_PERM = "must_be_managed_app"
73+
6374
private const val TAG = "AuthenticationUtilities"
6475

6576
/**
@@ -79,8 +90,10 @@ internal fun onAuthFlowComplete(
7990
onAuthFlowError: (error: String, errorDesc: String?, e: Throwable?) -> Unit,
8091
onAuthFlowSuccess: (userAccount: UserAccount) -> Unit,
8192
buildAccountName: (username: String?, instanceServer: String?) -> String = ::defaultBuildAccountName,
93+
nativeLogin: Boolean = false,
8294
) {
8395
val context = SalesforceSDKManager.getInstance().appContext
96+
val userAccountManager = SalesforceSDKManager.getInstance().userAccountManager
8497
val blockIntegrationUser = SalesforceSDKManager.getInstance().shouldBlockSalesforceIntegrationUser &&
8598
fetchIsSalesforceIntegrationUser(tokenResponse, loginServer)
8699

@@ -124,6 +137,7 @@ internal fun onAuthFlowComplete(
124137
.accountName(buildAccountName(userIdentity?.username, tokenResponse.instanceUrl))
125138
.loginServer(loginServer)
126139
.clientId(consumerKey)
140+
.nativeLogin(nativeLogin)
127141
.build()
128142
account.downloadProfilePhoto()
129143

@@ -141,7 +155,7 @@ internal fun onAuthFlowComplete(
141155
if (existingUsers.contains(account)) {
142156
val duplicateUserAccount = existingUsers.removeAt(existingUsers.indexOf(account))
143157
clearCaches()
144-
UserAccountManager.getInstance().clearCachedCurrentUser()
158+
userAccountManager.clearCachedCurrentUser()
145159

146160
// Revoke existing refresh token
147161
if (account.refreshToken != duplicateUserAccount.refreshToken) {
@@ -172,7 +186,7 @@ internal fun onAuthFlowComplete(
172186
existingUsers.forEach(Consumer { existingUser ->
173187
if (isBiometricAuthenticationEnabled(existingUser)) {
174188
// This is an unexpected logout(s) because we only support one Bio Auth user.
175-
SalesforceSDKManager.getInstance().userAccountManager.signoutUser(
189+
userAccountManager.signoutUser(
176190
existingUser, null, false, OAuth2.LogoutReason.UNEXPECTED
177191
)
178192
}
@@ -183,8 +197,35 @@ internal fun onAuthFlowComplete(
183197
// Save the user account
184198
addAccount(account)
185199

200+
// Init user logging
201+
SalesforceAnalyticsManager.getInstance(account)?.updateLoggingPrefs()
202+
203+
// Send User Switch Intent, create user and switch to user.
204+
val numAuthenticatedUsers = userAccountManager.authenticatedUsers?.size ?: 0
205+
val userSwitchType = when {
206+
// We've already authenticated the first user, so there should be one
207+
numAuthenticatedUsers == 1 -> USER_SWITCH_TYPE_FIRST_LOGIN
208+
209+
// Otherwise we're logging in with an additional user
210+
numAuthenticatedUsers > 1 -> USER_SWITCH_TYPE_LOGIN
211+
212+
// This should never happen but if it does, pass in the "unknown" value
213+
else -> USER_SWITCH_TYPE_DEFAULT
214+
}
215+
userAccountManager.sendUserSwitchIntent(userSwitchType, null)
216+
userAccountManager.createAccount(account)
217+
userAccountManager.switchToUser(account)
218+
186219
// Kickoff the end of the flow before storing mobile policy to prevent launching
187220
// the main activity over/after the screen lock.
221+
with(SalesforceSDKManager.getInstance()) {
222+
appContext.startActivity(Intent(appContext, mainActivityClass).apply {
223+
setPackage(appContext.packageName)
224+
flags = FLAG_ACTIVITY_NEW_TASK
225+
})
226+
}
227+
228+
// Let the calling process resume
188229
onAuthFlowSuccess(account)
189230

190231
// Screen lock required by mobile policy

libs/SalesforceSDK/src/com/salesforce/androidsdk/auth/AuthenticatorService.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ public IBinder onBind(Intent intent) {
105105
}
106106

107107
private static class Authenticator extends AbstractAccountAuthenticator {
108-
109-
private static final String SETTINGS_PACKAGE_NAME = "com.android.settings";
110-
private static final String ANDROID_PACKAGE_NAME = "androidPackageName";
111-
112108
private final Context context;
113109

114110
Authenticator(Context ctx) {
@@ -122,24 +118,16 @@ public Bundle addAccount(AccountAuthenticatorResponse response,
122118
String authTokenType,
123119
String[] requiredFeatures,
124120
Bundle options) {
125-
if (isAddFromSettings(options)) {
126-
options.putAll(SalesforceSDKManager.getInstance().getLoginOptions().asBundle());
127-
}
128121
return makeAuthIntentBundle(response, options);
129122
}
130123

131-
private boolean isAddFromSettings(Bundle options) {
132-
return options.containsKey(ANDROID_PACKAGE_NAME)
133-
&& SETTINGS_PACKAGE_NAME.equals(options.getString(ANDROID_PACKAGE_NAME));
134-
}
135-
136124
@Override
137125
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
138126
String authTokenType, Bundle options) throws NetworkErrorException {
139-
140127
UserAccount originalUserAccount = UserAccountManager.getInstance().buildUserAccount(account);
141-
final Map<String,String> addlParamsMap = SalesforceSDKManager.getInstance().getLoginOptions().getAdditionalParameters();
128+
142129
try {
130+
final Map<String,String> addlParamsMap = originalUserAccount.getAdditionalOauthValues();
143131
final OAuth2.TokenEndpointResponse tr = OAuth2.refreshAuthToken(HttpAccess.DEFAULT,
144132
new URI(originalUserAccount.getLoginServer()), originalUserAccount.getClientId(), originalUserAccount.getRefreshToken(), addlParamsMap);
145133

@@ -172,10 +160,6 @@ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account accoun
172160
}
173161
}
174162

175-
private String decryptUserData(AccountManager mgr, Account account, String key, String encryptionKey) {
176-
return SalesforceSDKManager.decrypt(mgr.getUserData(account, key), encryptionKey);
177-
}
178-
179163
private Bundle makeAuthIntentBundle(AccountAuthenticatorResponse response, Bundle options) {
180164
final Bundle reply = new Bundle();
181165
final Intent i = new Intent(context, SalesforceSDKManager.getInstance().getLoginActivityClass());

0 commit comments

Comments
 (0)