Skip to content

Commit 04b7691

Browse files
Dev Instant Login PTOn Spike.
1 parent 9ed0a67 commit 04b7691

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

libs/SalesforceSDK/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
android:theme="@style/SalesforceSDK"
7575
android:exported="false" />
7676

77+
<!-- Dev ui testing activity -->
78+
<activity android:name="com.salesforce.androidsdk.test.TestAuthentication"
79+
android:excludeFromRecents="true"
80+
android:theme="@style/SalesforceSDK"
81+
android:exported="true" />
82+
7783
<!-- Receiver in SP app for IDP-SP login flows -->
7884
<receiver android:name="com.salesforce.androidsdk.auth.idp.SPReceiver"
7985
android:exported="true"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.salesforce.androidsdk.test
2+
3+
import android.content.Intent
4+
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
5+
import android.os.Bundle
6+
import android.util.Log
7+
import androidx.appcompat.app.AppCompatActivity
8+
import com.salesforce.androidsdk.accounts.UserAccountBuilder
9+
import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_DEFAULT
10+
import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_FIRST_LOGIN
11+
import com.salesforce.androidsdk.accounts.UserAccountManager.USER_SWITCH_TYPE_LOGIN
12+
import com.salesforce.androidsdk.app.SalesforceSDKManager
13+
import com.salesforce.androidsdk.rest.ClientManager.AccMgrAuthTokenProvider
14+
import com.salesforce.androidsdk.util.test.TestCredentials
15+
16+
class TestAuthentication : AppCompatActivity() {
17+
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
21+
val creds = intent.getStringExtra("creds") ?: return
22+
TestCredentials.init(creds, this)
23+
24+
val account = UserAccountBuilder.getInstance()
25+
.refreshToken(TestCredentials.REFRESH_TOKEN)
26+
.instanceServer(TestCredentials.INSTANCE_URL)
27+
.idUrl(TestCredentials.IDENTITY_URL)
28+
.orgId(TestCredentials.ORG_ID)
29+
.userId(TestCredentials.USER_ID)
30+
.communityUrl(TestCredentials.COMMUNITY_URL)
31+
.accountName(TestCredentials.ACCOUNT_NAME)
32+
.clientId(TestCredentials.CLIENT_ID)
33+
.photoUrl(TestCredentials.PHOTO_URL)
34+
.language(TestCredentials.LANGUAGE)
35+
.locale(TestCredentials.LOCALE)
36+
.loginServer(TestCredentials.LOGIN_URL)
37+
.authToken("nothing yet")
38+
.firstName("user")
39+
.lastName("test")
40+
.displayName("test user")
41+
.username("username")
42+
.build()
43+
44+
val authTokenProvider = AccMgrAuthTokenProvider(
45+
SalesforceSDKManager.getInstance().clientManager,
46+
TestCredentials.INSTANCE_URL, null, TestCredentials.REFRESH_TOKEN
47+
)
48+
authTokenProvider.newAuthToken
49+
account.downloadProfilePhoto()
50+
51+
val userAccountManager = SalesforceSDKManager.getInstance().userAccountManager
52+
// Send User Switch Intent, create user and switch to user.
53+
val numAuthenticatedUsers = userAccountManager.authenticatedUsers?.size ?: 0
54+
val userSwitchType = when {
55+
// We've already authenticated the first user, so there should be one
56+
numAuthenticatedUsers == 1 -> USER_SWITCH_TYPE_FIRST_LOGIN
57+
58+
// Otherwise we're logging in with an additional user
59+
numAuthenticatedUsers > 1 -> USER_SWITCH_TYPE_LOGIN
60+
61+
// This should never happen but if it does, pass in the "unknown" value
62+
else -> USER_SWITCH_TYPE_DEFAULT
63+
}
64+
userAccountManager.sendUserSwitchIntent(userSwitchType, null)
65+
userAccountManager.createAccount(account)
66+
userAccountManager.switchToUser(account)
67+
68+
startActivity(Intent(this, SalesforceSDKManager.getInstance().mainActivityClass).apply {
69+
setPackage(SalesforceSDKManager.getInstance().appContext.packageName)
70+
flags = FLAG_ACTIVITY_NEW_TASK
71+
})
72+
}
73+
}

libs/SalesforceSDK/src/com/salesforce/androidsdk/util/test/TestCredentials.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.salesforce.androidsdk.util.JSONObjectHelper;
3434
import com.salesforce.androidsdk.util.ResourceReaderHelper;
3535

36+
import org.json.JSONException;
3637
import org.json.JSONObject;
3738

3839
/**
@@ -84,4 +85,29 @@ public static void init(Context ctx) {
8485
throw new RuntimeException("Failed to read test_credentials.json", e);
8586
}
8687
}
87-
}
88+
89+
public static void init(String creds, Context ctx) {
90+
try {
91+
JSONObject json = new JSONObject(creds);
92+
API_VERSION = ApiVersionStrings.getVersionNumber(ctx);
93+
ACCOUNT_TYPE = ctx.getString(R.string.account_type);
94+
ORG_ID = json.getString("organization_id");
95+
USERNAME = json.getString("username");
96+
ACCOUNT_NAME = json.getString("display_name");
97+
USER_ID = json.getString("user_id");
98+
LOGIN_URL = json.getString("test_login_domain");
99+
INSTANCE_URL = json.getString("instance_url");
100+
COMMUNITY_URL = json.optString("community_url", INSTANCE_URL /* in case the test_credentials.json was obtained for a user / org without community setup */);
101+
IDENTITY_URL = json.getString("identity_url");
102+
CLIENT_ID = json.getString("test_client_id");
103+
REFRESH_TOKEN = json.getString("refresh_token");
104+
PHOTO_URL = json.getString("photo_url");
105+
LANGUAGE = json.optString("language", "en_US");
106+
LOCALE = json.optString("locale", "en_US");
107+
108+
} catch (JSONException e) {
109+
throw new RuntimeException(e);
110+
}
111+
112+
}
113+
}

0 commit comments

Comments
 (0)