Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally support use custom domain button in webview. #2447

Merged
merged 7 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import android.webkit.CookieManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
Expand Down Expand Up @@ -100,6 +101,7 @@
import java.util.SortedSet;
import java.util.UUID;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.regex.Pattern;

/**
* This class serves as an interface to the various
Expand Down Expand Up @@ -182,7 +184,7 @@ public class SalesforceSDKManager implements LifecycleObserver {

private boolean useHybridAuthentication = true; // hybrid authentication flows ON by default - but app can opt out by calling setUseHybridAuthentication(false)

private boolean shouldInferCustomDomain = false; // Do not detect use of Custom Domain input from login webview but app can opt in by calling setInferCustomDomain(ture)
private Pattern customDomainInferencePattern;

private Theme theme = Theme.SYSTEM_DEFAULT;
private String appName;
Expand Down Expand Up @@ -676,20 +678,25 @@ public synchronized void setUseHybridAuthentication(boolean useHybridAuthenticat
}

/**
* Returns whether the SDK should infer if the user has entered a new login server through
* the "Use Custom Domain" button on the login screen.
* Returns the pattern used to detect the use of "Use Custom Domain" input from login web view.
*
* @return pattern if set or null
*/
public boolean shouldInferCustomDomain() {
return this.shouldInferCustomDomain;
public synchronized Pattern getCustomDomainInferencePattern() {
return customDomainInferencePattern;
}

/**
* Sets whether the SDK should infer if the user has entered a new login server through
* the "Use Custom Domain" button on the login screen.
* @param shouldInferCustomDomain
* Detect use of "Use Custom Domain" input from login web view using the given regex.
* Example for a specific org:
* "^https:\\/\\/mobilesdk\\.my\\.salesforce\\.com\\/\\?startURL=%2Fsetup%2Fsecur%2FRemoteAccessAuthorizationPage\\.apexp"
* For any my domain:
* "^https:\\/\\/[a-zA-Z0-9]+\\.my\\.salesforce\\.com/\\?startURL=%2Fsetup%2Fsecur%2FRemoteAccessAuthorizationPage\\.apexp"
*
* @param pattern regex to use when detecting use of custom domain on login
*/
public synchronized void setShouldInferCustomDomain(boolean shouldInferCustomDomain) {
this.shouldInferCustomDomain = shouldInferCustomDomain;
public synchronized void setCustomDomainInferencePattern(@Nullable Pattern pattern) {
this.customDomainInferencePattern = pattern;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

import okhttp3.Request;
import okhttp3.Response;
Expand Down Expand Up @@ -525,7 +526,10 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
}

// Check if user entered a custom domain
if (SalesforceSDKManager.getInstance().shouldInferCustomDomain() && isNewLoginUrl(uri)) {
String host = uri.getHost();
Pattern customDomainPattern = SalesforceSDKManager.getInstance().getCustomDomainInferencePattern();
if (host != null && !getLoginUrl().contains(host) && customDomainPattern != null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing with iOS, should we have a flag like domainUpdated, or is that achieved by setting the login server which causes getLoginUrl().contains(host) to be true if you are back here a second time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getLoginUrl checks the currently selected login server so it should server the same purpose.

&& customDomainPattern.matcher(uri.toString()).find()) {
try {
String baseUrl = "https://" + uri.getHost();
LoginServerManager serverManager = SalesforceSDKManager.getInstance().getLoginServerManager();
Expand Down Expand Up @@ -574,20 +578,6 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
return authFlowFinished;
}

private boolean isNewLoginUrl(Uri uri) {
String host = uri.getHost();
String query = uri.getQuery();
String path = uri.getPath();

if (host == null || query == null || path == null || getLoginUrl().contains(host)) {
return false;
}

final String myDomainHost = ".my.salesforce.com";
final String loginPath = "startURL=/setup/secur/RemoteAccessAuthorizationPage.apexp";
return host.endsWith(myDomainHost) && query.startsWith(loginPath) && path.equals("/");
}

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
int primError = error.getPrimaryError();
Expand Down