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

Fix Android WebAuth callback for system browser flows #15187

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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