Skip to content

Commit 89400d3

Browse files
@W-21933885: [MSDK Android] App Attestation Implementation (Add Dependency Injection To OAuth2 And IDPAuthCodeHelper For Testing)
- Add dependency injection for SalesforceSDKManager to OAuth2.getAuthorizationUrl() and getBrandedLoginPath() - Create backward-compatible overloads that default to SalesforceSDKManager.getInstance() - Update IDPAuthCodeHelper.getAuthorizationPathForSP() to accept and pass injected SDKManager - Fix IDPAuthCodeHelperTest to properly mock the 11-parameter OAuth2.getAuthorizationUrl() method signature - Add OAuth2.TIMESTAMP_FORMAT access before mockkStatic() to force class initialization - Update OAuth2Test to test new overloaded methods with SalesforceSDKManager parameter All IDPAuthCodeHelperTest tests now pass (10/10, 100% success rate).
1 parent 55d370f commit 89400d3

4 files changed

Lines changed: 249 additions & 49 deletions

File tree

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

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ public static URI getAuthorizationUrl(
289289
null,
290290
displayType,
291291
codeChallenge,
292-
addlParams
292+
addlParams,
293+
SalesforceSDKManager.getInstance()
293294
);
294295
}
295296

@@ -326,12 +327,63 @@ public static URI getAuthorizationUrl(
326327
String displayType,
327328
String codeChallenge,
328329
Map<String, String> addlParams) {
330+
return getAuthorizationUrl(
331+
useWebServerAuthentication,
332+
useHybridAuthentication,
333+
loginServer,
334+
clientId,
335+
callbackUrl,
336+
scopes,
337+
loginHint,
338+
displayType,
339+
codeChallenge,
340+
addlParams,
341+
SalesforceSDKManager.getInstance()
342+
);
343+
}
344+
345+
/**
346+
* Builds the URL to the authorization web page for this login server.
347+
* You need not provide the 'refresh_token' scope, as it is provided automatically.
348+
*
349+
* @param useWebServerAuthentication True to use web server flow, False to use user agent flow
350+
* @param useHybridAuthentication True to use "hybrid" flow
351+
* @param loginServer Base protocol and server to use (e.g. https://login.salesforce.com).
352+
* @param clientId OAuth client ID.
353+
* @param callbackUrl OAuth callback URL or redirect URL.
354+
* @param scopes A list of OAuth scopes to request (e.g. {"visualforce", "api"}). If null,
355+
* the default OAuth scope is provided.
356+
* @param loginHint When applicable, the Salesforce Welcome Login hint
357+
* @param displayType OAuth display type. If null, the default of 'touch' is used.
358+
* @param codeChallenge Code challenge to use when using web server flow
359+
* @param addlParams Any additional parameters that may be
360+
* added to the request. When using
361+
* Salesforce Mobile App Attestation, the
362+
* "attestation" parameter should be added
363+
* to this map.
364+
* @param sdkManager The SalesforceSDKManager instance to use. This parameter is
365+
* intended for testing purposes only.
366+
* @return A URL to start the OAuth flow in a web browser/view.
367+
* @see <a href="https://help.salesforce.com/apex/HTViewHelpDoc?language=en&id=remoteaccess_oauth_scopes.htm">RemoteAccess OAuth Scopes</a>
368+
*/
369+
public static URI getAuthorizationUrl(
370+
boolean useWebServerAuthentication,
371+
boolean useHybridAuthentication,
372+
URI loginServer,
373+
String clientId,
374+
String callbackUrl,
375+
String[] scopes,
376+
String loginHint,
377+
String displayType,
378+
String codeChallenge,
379+
Map<String, String> addlParams,
380+
SalesforceSDKManager sdkManager) {
329381
final StringBuilder sb = new StringBuilder(loginServer.toString());
330382

331383
final String responseType = useWebServerAuthentication
332384
? CODE
333385
: useHybridAuthentication ? HYBRID_TOKEN : TOKEN;
334-
sb.append(OAUTH_AUTH_PATH).append(getBrandedLoginPath());
386+
sb.append(OAUTH_AUTH_PATH).append(getBrandedLoginPath(sdkManager));
335387
sb.append(OAUTH_DISPLAY_PARAM).append(displayType == null ? TOUCH : displayType);
336388
sb.append(AND).append(RESPONSE_TYPE).append(EQUAL).append(responseType);
337389
sb.append(AND).append(CLIENT_ID).append(EQUAL).append(Uri.encode(clientId));
@@ -342,7 +394,7 @@ public static URI getAuthorizationUrl(
342394
sb.append(AND).append(LOGIN_HINT).append(EQUAL).append(Uri.encode(loginHint));
343395
}
344396
sb.append(AND).append(REDIRECT_URI).append(EQUAL).append(callbackUrl);
345-
sb.append(AND).append(DEVICE_ID).append(EQUAL).append(SalesforceSDKManager.getInstance().getDeviceId());
397+
sb.append(AND).append(DEVICE_ID).append(EQUAL).append(sdkManager.getDeviceId());
346398
if (useWebServerAuthentication) {
347399
sb.append(AND).append(CODE_CHALLENGE).append(EQUAL).append(Uri.encode(codeChallenge));
348400
}
@@ -355,8 +407,8 @@ public static URI getAuthorizationUrl(
355407
return URI.create(sb.toString());
356408
}
357409

358-
private static String getBrandedLoginPath() {
359-
String brandedLoginPath = SalesforceSDKManager.getInstance().getLoginBrand();
410+
private static String getBrandedLoginPath(SalesforceSDKManager sdkManager) {
411+
String brandedLoginPath = sdkManager.getLoginBrand();
360412
if (brandedLoginPath == null || brandedLoginPath.trim().isEmpty()) {
361413
brandedLoginPath = EMPTY_STRING;
362414
} else {

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,18 @@ internal class IDPAuthCodeHelper @VisibleForTesting internal constructor(
105105

106106
/**
107107
* Compute relative path of authorization url for SP
108+
* @param salesforceSDKManager The Salesforce SDK manager instance. This
109+
* parameter is intended for testing purposes only. Defaults to the
110+
* singleton instance for production use
108111
* @return authorization relative path
109112
*/
110113
@VisibleForTesting
111-
internal suspend fun getAuthorizationPathForSP(): String? {
114+
internal suspend fun getAuthorizationPathForSP(
115+
salesforceSDKManager: SalesforceSDKManager = SalesforceSDKManager.getInstance()
116+
): String? {
112117
SalesforceSDKLogger.d(TAG, "Getting authorization url")
113-
val context = SalesforceSDKManager.getInstance().appContext
114-
val useHybridAuthentication = SalesforceSDKManager.getInstance().useHybridAuthentication
118+
val context = salesforceSDKManager.appContext
119+
val useHybridAuthentication = salesforceSDKManager.useHybridAuthentication
115120

116121
// Add Salesforce Mobile App Attestation parameter to authorization URL if applicable.
117122
val additionalParams = appAttestationClient?.run {
@@ -127,9 +132,11 @@ internal class IDPAuthCodeHelper @VisibleForTesting internal constructor(
127132
spConfig.oauthClientId,
128133
spConfig.oauthCallbackUrl,
129134
spConfig.oauthScopes,
135+
null, // Login Hint
130136
context.getString(R.string.oauth_display_type),
131137
codeChallenge,
132-
additionalParams
138+
additionalParams,
139+
salesforceSDKManager
133140
)
134141

135142
return authorizationUri?.let {

0 commit comments

Comments
 (0)