Skip to content

Commit

Permalink
update switchBrowserChallenge
Browse files Browse the repository at this point in the history
  • Loading branch information
p3dr0rv committed Jan 24, 2025
1 parent ec3fd80 commit ebfaabc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
Expand All @@ -49,14 +48,11 @@
import com.microsoft.identity.common.internal.fido.LegacyFido2ApiManager;
import com.microsoft.identity.common.internal.providers.oauth2.AuthorizationActivity;
import com.microsoft.identity.common.internal.providers.oauth2.WebViewAuthorizationFragment;
import com.microsoft.identity.common.internal.ui.browser.BrowserSelector;
import com.microsoft.identity.common.internal.ui.browser.CustomTabsManager;
import com.microsoft.identity.common.internal.ui.webview.certbasedauth.AbstractSmartcardCertBasedAuthChallengeHandler;
import com.microsoft.identity.common.internal.ui.webview.certbasedauth.AbstractCertBasedAuthChallengeHandler;
import com.microsoft.identity.common.internal.ui.webview.certbasedauth.CertBasedAuthFactory;
import com.microsoft.identity.common.internal.ui.webview.challengehandlers.SwitchBrowserChallenge;
import com.microsoft.identity.common.internal.ui.webview.challengehandlers.SwitchBrowserHandler;
import com.microsoft.identity.common.java.browser.Browser;
import com.microsoft.identity.common.internal.ui.webview.challengehandlers.NonceRedirectHandler;
import com.microsoft.identity.common.java.constants.FidoConstants;
import com.microsoft.identity.common.java.flighting.CommonFlight;
Expand All @@ -80,10 +76,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

Expand Down Expand Up @@ -118,6 +111,9 @@ public class AzureActiveDirectoryWebViewClient extends OAuth2WebViewClient {
private final CertBasedAuthFactory mCertBasedAuthFactory;
private AbstractCertBasedAuthChallengeHandler mCertBasedAuthChallengeHandler;

private final SwitchBrowserHandler mSwitchBrowserHandler;


private HashMap<String, String> mRequestHeaders;

public AzureActiveDirectoryWebViewClient(@NonNull final Activity activity,
Expand All @@ -127,6 +123,7 @@ public AzureActiveDirectoryWebViewClient(@NonNull final Activity activity,
super(activity, completionCallback, pageLoadedCallback);
mRedirectUrl = redirectUrl;
mCertBasedAuthFactory = new CertBasedAuthFactory(activity);
mSwitchBrowserHandler= new SwitchBrowserHandler(activity);
}

/**
Expand Down Expand Up @@ -220,7 +217,7 @@ else if (isRedirectUrl(formattedURL)) {
final Uri formattedUri = Uri.parse(formattedURL);
if (isSwitchBrowserRequest(formattedUri)) {
Logger.info(methodTag,"Request to switch browser.");
return processSwitchBrowserRequest(view, formattedUri);
return processSwitchBrowserRequest(formattedUri);
} else {
Logger.info(methodTag,"It is a redirect request.");
processRedirectUrl(view, url);
Expand Down Expand Up @@ -341,7 +338,7 @@ private boolean isHeaderForwardingRequiredUri(@NonNull final String url) {

// This function is only called when the client received a redirect that starts with the apps
// redirect uri.
protected void processRedirectUrl(@NonNull final WebView view, @NonNull final String url) {
private void processRedirectUrl(@NonNull final WebView view, @NonNull final String url) {
final String methodTag = TAG + ":processRedirectUrl";

Logger.info(methodTag, "It is pointing to redirect. Final url can be processed to get the code or error.");
Expand All @@ -359,16 +356,13 @@ protected void processRedirectUrl(@NonNull final WebView view, @NonNull final St
*
* @param uri The URI of the request.
*/
protected boolean processSwitchBrowserRequest(@NonNull final WebView view, @NonNull final Uri uri) {
final Context context = view.getContext();
final CustomTabsManager ctManager = new CustomTabsManager(context);
final SwitchBrowserChallenge challenge = SwitchBrowserChallenge.constructFromUri(uri);
final Browser browser = new BrowserSelector(context).select(Collections.emptyList(), null);
if (browser != null && challenge != null) {
final SwitchBrowserHandler handler = new SwitchBrowserHandler(context,ctManager, browser);
return handler.processChallenge(challenge);
private boolean processSwitchBrowserRequest(@NonNull final Uri uri) {
final SwitchBrowserChallenge switchBrowserChallenge =
SwitchBrowserChallenge.constructFromRedirectUri(uri);
if (switchBrowserChallenge == null) {
return false;
}
return false;
return mSwitchBrowserHandler.processChallenge(switchBrowserChallenge);
}

private void processWebsiteRequest(@NonNull final WebView view, @NonNull final String url) {
Expand Down Expand Up @@ -669,6 +663,7 @@ public void onDestroy() {
mCertBasedAuthChallengeHandler.cleanUp();
}
mCertBasedAuthFactory.onDestroy();
mSwitchBrowserHandler.unbind();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ data class SwitchBrowserChallenge(
private val TAG = SwitchBrowserChallenge::class.simpleName

@JvmStatic
fun constructFromUri(redirectUri: Uri): SwitchBrowserChallenge? {
fun constructFromRedirectUri(redirectUri: Uri): SwitchBrowserChallenge? {
val methodTag = "${TAG}:constructFromUri"

val actionUri = redirectUri.getQueryParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,52 @@
// THE SOFTWARE.
package com.microsoft.identity.common.internal.ui.webview.challengehandlers

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.microsoft.identity.common.internal.ui.browser.AndroidBrowserSelector
import com.microsoft.identity.common.internal.ui.browser.CustomTabsManager
import com.microsoft.identity.common.java.browser.Browser
import com.microsoft.identity.common.java.browser.IBrowserSelector
import com.microsoft.identity.common.java.ui.BrowserDescriptor
import com.microsoft.identity.common.logging.Logger

class SwitchBrowserHandler(
private val activity: Activity,
private val context: Context,
private val customTabsManager: CustomTabsManager,
private val browser: Browser
private val browserSelector: IBrowserSelector
) : IChallengeHandler<SwitchBrowserChallenge, Boolean> {


constructor( activity: Activity) : this(
activity,
activity.applicationContext,
CustomTabsManager(activity.applicationContext),
AndroidBrowserSelector(activity.applicationContext)
)

companion object {
private val TAG = SwitchBrowserHandler::class.simpleName
}

/**
* Process difference kinds of challenge request.
*
* @param challenge challenge request
* @param switchBrowserChallenge challenge request
* @return GenericResponse
*/
override fun processChallenge(challenge: SwitchBrowserChallenge): Boolean {
override fun processChallenge(switchBrowserChallenge: SwitchBrowserChallenge): Boolean {
val methodTag = "$TAG:processChallenge"

val browser = browserSelector.selectBrowser(
BrowserDescriptor.getBrowserSafeListForSwitchBrowser(),
null
)
if (browser == null) {
Logger.warn(methodTag, "No browser found for SwitchBrowserChallenge.")
return false
}

val browserIntent: Intent
if (browser.isCustomTabsServiceSupported) {
Logger.info(methodTag, "CustomTabsService is supported.")
Expand All @@ -62,13 +83,12 @@ class SwitchBrowserHandler(
browserIntent = Intent(Intent.ACTION_VIEW)
}
browserIntent.setPackage(browser.packageName)
browserIntent.setData(challenge.uri)
context.startActivity(browserIntent)
browserIntent.setData(switchBrowserChallenge.uri)
activity.startActivity(browserIntent)
return true
}


//if (mCustomTabManager != null) {
// mCustomTabManager.unbind();
// }
fun unbind() {
customTabsManager.unbind()
}
}

0 comments on commit ebfaabc

Please sign in to comment.