Skip to content

Commit eb7462e

Browse files
authored
Merge pull request #2779 from wmathurin/zero_scopes_2
Hybrid app inspect scopes on user agent and warn if important ones are missing
2 parents efc05d1 + 62ebdf7 commit eb7462e

4 files changed

Lines changed: 366 additions & 5 deletions

File tree

libs/SalesforceHybrid/src/com/salesforce/androidsdk/phonegap/ui/SalesforceWebViewCookieManager.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,23 @@ import android.net.Uri
3030
import android.os.SystemClock
3131
import android.webkit.CookieManager
3232
import com.salesforce.androidsdk.accounts.UserAccount
33+
import com.salesforce.androidsdk.auth.ScopeParser
3334
import com.salesforce.androidsdk.phonegap.util.SalesforceHybridLogger.d
3435
import com.salesforce.androidsdk.phonegap.util.SalesforceHybridLogger.w
3536
import java.net.URI
3637

3738
class SalesforceWebViewCookieManager {
3839
private val cookieManager = CookieManager.getInstance()
3940

40-
fun setCookies(userAccount: UserAccount) {
41+
fun setCookies(userAccount: UserAccount,
42+
// Optional lambda parameters to facilitate testing
43+
setCookieValue: (String, String?, Boolean, String?, String?) -> Unit = ::setCookieValue,
44+
syncCookies: () -> Unit = ::syncCookies) {
4145
d(TAG, "setCookies for userAccount:${userAccount.toJson()}")
4246

47+
// Warn if expected scopes are missing
48+
userAccount.scope?.let { inspectScopes(it) }
49+
4350
val instanceUrl = userAccount.instanceServer
4451
val lightningDomain = userAccount.lightningDomain
4552
val lightningSid = userAccount.lightningSid
@@ -83,6 +90,30 @@ class SalesforceWebViewCookieManager {
8390
syncCookies()
8491
}
8592

93+
internal fun inspectScopes(scope: String, warn: (String) -> Unit = { w(TAG, it)}) {
94+
val scopeParser = ScopeParser(scope)
95+
96+
// full encompasses all other scopes except for refresh
97+
if (!scopeParser.hasScope("full")) {
98+
if (!scopeParser.hasScope("web")) {
99+
warn("Missing web scope: will not be able to access web content.")
100+
101+
// web encompasses visualforce scope
102+
if (!scopeParser.hasScope("visualforce")) {
103+
warn("Missing visualforce scope: will not be able to access Visualforce pages.")
104+
}
105+
}
106+
107+
if (!scopeParser.hasScope("lightning")) {
108+
warn("Missing lightning scope: will not be able to access Lightning applications.")
109+
}
110+
111+
if (!scopeParser.hasScope("content")) {
112+
warn("Missing content scope: will not be able to access Content resources.")
113+
}
114+
}
115+
}
116+
86117
private fun setCookieValue(
87118
cookieType: String, domain: String?, setDomain: Boolean, name: String?, value: String?
88119
) {

libs/test/SalesforceHybridTest/src/com/salesforce/androidsdk/phonegap/SDKInfoPluginTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ public void testGetSDKInfo() throws NameNotFoundException, JSONException {
7777
Assert.assertEquals("Wrong bootconfig shouldAuthenticate", bootconfig.shouldAuthenticate(), sdkInfoBootConfig.getBoolean("shouldAuthenticate"));
7878
Assert.assertEquals("Wrong bootconfig attemptOfflineLoad", bootconfig.attemptOfflineLoad(), sdkInfoBootConfig.getBoolean("attemptOfflineLoad"));
7979
Assert.assertEquals("Wrong bootconfig isLocal", bootconfig.isLocal(), sdkInfoBootConfig.getBoolean("isLocal"));
80-
List<String> sdkInfoOAuthScopes = toList(sdkInfoBootConfig.getJSONArray("oauthScopes"));
81-
Assert.assertEquals("Wrong bootconfig oauthScopes", 1, sdkInfoOAuthScopes.size());
82-
Assert.assertTrue("Wrong bootconfig oauthScopes", sdkInfoOAuthScopes.contains("api"));
80+
Assert.assertNull("Wrong bootconfig oauthScopes", sdkInfoBootConfig.optJSONArray("oauthScopes"));
8381
Assert.assertEquals("Wrong bootconfig oauthRedirectURI", bootconfig.getOauthRedirectURI(), sdkInfoBootConfig.getString("oauthRedirectURI"));
8482
Assert.assertEquals("Wrong bootconfig remoteAccessConsumerKey", bootconfig.getRemoteAccessConsumerKey(), sdkInfoBootConfig.getString("remoteAccessConsumerKey"));
8583
Assert.assertEquals("Wrong bootconfig startPage", "index.html", sdkInfoBootConfig.optString("startPage"));

0 commit comments

Comments
 (0)