Skip to content

Commit

Permalink
Fix Android WebAuth callback for system browser flows (#15187)
Browse files Browse the repository at this point in the history
* Fix Android WebAuth callback for system browser

This adds a flag to track if the web authentication flow was launched via custom tabs or the fallback, system browser.

We were assuming it was always via custom tabs, and so the intermediate activity would be started which is not necessary or designed to compatible with the flow using the system browser.

This changes the callback activity to properly route back to the intermediate activity for custom tabs initialized flows, and to just call the OnResume callback for other flows (system browser).

* Auto-format source code

---------

Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
  • Loading branch information
2 people authored and rmarinho committed May 30, 2023
1 parent 707c9e0 commit 97d65d7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ partial class WebAuthenticatorImplementation : IWebAuthenticator, IPlatformWebAu
Uri currentRedirectUri = null;
WebAuthenticatorOptions currentOptions = null;

internal bool AuthenticatingWithCustomTabs { get; private set; } = false;

public bool OnResumeCallback(Intent intent)
{
// If we aren't waiting on a task, don't handle the url
Expand Down Expand Up @@ -70,7 +72,11 @@ public async Task<WebAuthenticatorResult> AuthenticateAsync(WebAuthenticatorOpti
tcsResponse = new TaskCompletionSource<WebAuthenticatorResult>();
currentRedirectUri = callbackUrl;

if (!(await StartCustomTabsActivity(url)))
// Try to start with custom tabs if the system supports it and we resolve it
AuthenticatingWithCustomTabs = await StartCustomTabsActivity(url);

// Fall back to using the system browser if necessary
if (!AuthenticatingWithCustomTabs)
{
// Fall back to opening the system-registered browser if necessary
var urlOriginalString = url.OriginalString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ static IPlatformWebAuthenticatorCallback AsPlatformCallback(this IWebAuthenticat
return platform;
}

#if ANDROID
internal static bool IsAuthenticatingWithCustomTabs(this IWebAuthenticator webAuthenticator)
=> (webAuthenticator as WebAuthenticatorImplementation)?.AuthenticatingWithCustomTabs ?? false;
#endif

/// <summary>
/// Begin an authentication flow by navigating to the specified url and waiting for a callback/redirect to the callbackUrl scheme.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// start the intermediate activity again with flags to close the custom tabs
var intent = new Intent(this, typeof(WebAuthenticatorIntermediateActivity));
intent.SetData(Intent.Data);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
StartActivity(intent);
// Check how we launched the flow initially
if (WebAuthenticator.Default.IsAuthenticatingWithCustomTabs())
{
// start the intermediate activity again with flags to close the custom tabs
var intent = new Intent(this, typeof(WebAuthenticatorIntermediateActivity));
intent.SetData(Intent.Data);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
StartActivity(intent);
}
else
{
// No intermediate activity if we returned from a system browser
// intent since there's no custom tab instance to clean up
WebAuthenticator.Default.OnResume(Intent);
}

// finish this activity
Finish();
Expand Down

0 comments on commit 97d65d7

Please sign in to comment.