From 8566f5c00a9aa0804cd6b5ed9f1bea3a7e6729a5 Mon Sep 17 00:00:00 2001 From: Federico Maccaroni Date: Mon, 13 May 2024 14:26:37 -0300 Subject: [PATCH] PM-8051 Fix WebAuthenticator getting the Window properly on iOS with the workaround to use Share app window when coming from iOS extension (#3239) --- src/Core/Pages/Accounts/LoginPage.xaml.cs | 1 + .../LoginPasswordlessRequestPage.xaml.cs | 1 + src/Core/Pages/Accounts/RegisterPage.xaml.cs | 7 +++++-- src/Core/Pages/Accounts/TwoFactorPage.xaml.cs | 1 + .../Pages/Accounts/TwoFactorPageViewModel.cs | 17 ++++++++++++++++- src/Core/Pages/CaptchaProtectedViewModel.cs | 11 +++++++++++ .../CredentialProviderViewController.cs | 10 ++++++---- 7 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/Core/Pages/Accounts/LoginPage.xaml.cs b/src/Core/Pages/Accounts/LoginPage.xaml.cs index 17a41cbaf..182cd2bbd 100644 --- a/src/Core/Pages/Accounts/LoginPage.xaml.cs +++ b/src/Core/Pages/Accounts/LoginPage.xaml.cs @@ -25,6 +25,7 @@ namespace Bit.App.Pages _broadcasterService = ServiceContainer.Resolve(); _vm = BindingContext as LoginPageViewModel; _vm.Page = this; + _vm.FromIosExtension = _appOptions?.IosExtension ?? false; _vm.StartTwoFactorAction = () => MainThread.BeginInvokeOnMainThread(async () => await StartTwoFactorAsync()); _vm.LogInSuccessAction = () => MainThread.BeginInvokeOnMainThread(async () => await LogInSuccessAsync()); _vm.LogInWithDeviceAction = () => StartLoginWithDeviceAsync().FireAndForget(); diff --git a/src/Core/Pages/Accounts/LoginPasswordlessRequestPage.xaml.cs b/src/Core/Pages/Accounts/LoginPasswordlessRequestPage.xaml.cs index f1f67ccdb..c1492e761 100644 --- a/src/Core/Pages/Accounts/LoginPasswordlessRequestPage.xaml.cs +++ b/src/Core/Pages/Accounts/LoginPasswordlessRequestPage.xaml.cs @@ -19,6 +19,7 @@ namespace Bit.App.Pages _vm.Email = email; _vm.AuthRequestType = authRequestType; _vm.AuthingWithSso = authingWithSso; + _vm.FromIosExtension = _appOptions?.IosExtension ?? false; _vm.StartTwoFactorAction = () => MainThread.BeginInvokeOnMainThread(async () => await StartTwoFactorAsync()); _vm.LogInSuccessAction = () => MainThread.BeginInvokeOnMainThread(async () => await LogInSuccessAsync()); _vm.UpdateTempPasswordAction = () => MainThread.BeginInvokeOnMainThread(async () => await UpdateTempPasswordAsync()); diff --git a/src/Core/Pages/Accounts/RegisterPage.xaml.cs b/src/Core/Pages/Accounts/RegisterPage.xaml.cs index d1b273031..6e88fa84b 100644 --- a/src/Core/Pages/Accounts/RegisterPage.xaml.cs +++ b/src/Core/Pages/Accounts/RegisterPage.xaml.cs @@ -1,4 +1,6 @@ -namespace Bit.App.Pages +using Bit.App.Models; + +namespace Bit.App.Pages { public partial class RegisterPage : BaseContentPage { @@ -6,11 +8,12 @@ private bool _inputFocused; - public RegisterPage(HomePage homePage) + public RegisterPage(HomePage homePage, AppOptions appOptions = null) { InitializeComponent(); _vm = BindingContext as RegisterPageViewModel; _vm.Page = this; + _vm.FromIosExtension = appOptions?.IosExtension ?? false; _vm.RegistrationSuccess = () => MainThread.BeginInvokeOnMainThread(async () => await RegistrationSuccessAsync(homePage)); _vm.CloseAction = async () => { diff --git a/src/Core/Pages/Accounts/TwoFactorPage.xaml.cs b/src/Core/Pages/Accounts/TwoFactorPage.xaml.cs index 342eefd79..b6b53e8ea 100644 --- a/src/Core/Pages/Accounts/TwoFactorPage.xaml.cs +++ b/src/Core/Pages/Accounts/TwoFactorPage.xaml.cs @@ -28,6 +28,7 @@ namespace Bit.App.Pages _vm = BindingContext as TwoFactorPageViewModel; _vm.Page = this; _vm.AuthingWithSso = authingWithSso ?? false; + _vm.FromIosExtension = _appOptions?.IosExtension ?? false; _vm.StartSetPasswordAction = () => MainThread.BeginInvokeOnMainThread(async () => await StartSetPasswordAsync()); _vm.TwoFactorAuthSuccessAction = () => diff --git a/src/Core/Pages/Accounts/TwoFactorPageViewModel.cs b/src/Core/Pages/Accounts/TwoFactorPageViewModel.cs index 4f1ec58e4..f3bad034c 100644 --- a/src/Core/Pages/Accounts/TwoFactorPageViewModel.cs +++ b/src/Core/Pages/Accounts/TwoFactorPageViewModel.cs @@ -11,6 +11,13 @@ using Bit.Core.Models.Request; using Bit.Core.Resources.Localization; using Bit.Core.Utilities; using Newtonsoft.Json; +using Bit.Core.Services; + +#if IOS +using WebAuthenticator = Bit.Core.Utilities.MAUI.WebAuthenticator; +using WebAuthenticatorResult = Bit.Core.Utilities.MAUI.WebAuthenticatorResult; +using WebAuthenticatorOptions = Bit.Core.Utilities.MAUI.WebAuthenticatorOptions; +#endif namespace Bit.App.Pages { @@ -136,6 +143,7 @@ namespace Bit.App.Pages nameof(ShowTryAgain), }); } + public ICommand SubmitCommand { get; } public ICommand MoreCommand { get; } public ICommand AuthenticateWithDuoFramelessCommand { get; } @@ -261,7 +269,11 @@ namespace Bit.App.Pages authResult = await WebAuthenticator.AuthenticateAsync(new WebAuthenticatorOptions { Url = new Uri(url), - CallbackUrl = new Uri(Constants.DuoCallback) + CallbackUrl = new Uri(Constants.DuoCallback), +#if IOS + ShouldUseSharedApplicationKeyWindow = FromIosExtension +#endif + }); } catch (TaskCanceledException) @@ -348,6 +360,9 @@ namespace Bit.App.Pages Url = new Uri(url), CallbackUrl = new Uri(callbackUri), PrefersEphemeralWebBrowserSession = true, +#if IOS + ShouldUseSharedApplicationKeyWindow = FromIosExtension +#endif }; authResult = await WebAuthenticator.AuthenticateAsync(options); } diff --git a/src/Core/Pages/CaptchaProtectedViewModel.cs b/src/Core/Pages/CaptchaProtectedViewModel.cs index 28935bd20..c44391cb5 100644 --- a/src/Core/Pages/CaptchaProtectedViewModel.cs +++ b/src/Core/Pages/CaptchaProtectedViewModel.cs @@ -6,6 +6,12 @@ using Bit.App.Utilities; using Bit.Core.Abstractions; using Microsoft.Maui.Authentication; +#if IOS +using WebAuthenticator = Bit.Core.Utilities.MAUI.WebAuthenticator; +using WebAuthenticatorResult = Bit.Core.Utilities.MAUI.WebAuthenticatorResult; +using WebAuthenticatorOptions = Bit.Core.Utilities.MAUI.WebAuthenticatorOptions; +#endif + namespace Bit.App.Pages { public abstract class CaptchaProtectedViewModel : BaseViewModel @@ -16,6 +22,8 @@ namespace Bit.App.Pages protected abstract IPlatformUtilsService platformUtilsService { get; } protected string _captchaToken = null; + public bool FromIosExtension { get; set; } + protected async Task HandleCaptchaAsync(string captchaSiteKey, bool needsCaptcha, Func onSuccess) { if (!needsCaptcha) @@ -61,6 +69,9 @@ namespace Bit.App.Pages Url = new Uri(url), CallbackUrl = new Uri(callbackUri), PrefersEphemeralWebBrowserSession = false, +#if IOS + ShouldUseSharedApplicationKeyWindow = FromIosExtension +#endif }; authResult = await WebAuthenticator.AuthenticateAsync(options); } diff --git a/src/iOS.Autofill/CredentialProviderViewController.cs b/src/iOS.Autofill/CredentialProviderViewController.cs index 501b508d0..c9e62c70a 100644 --- a/src/iOS.Autofill/CredentialProviderViewController.cs +++ b/src/iOS.Autofill/CredentialProviderViewController.cs @@ -617,8 +617,9 @@ namespace Bit.iOS.Autofill private void LaunchRegisterFlow() { - var registerPage = new RegisterPage(null); - var app = new App.App(new AppOptions { IosExtension = true }); + var appOptions = new AppOptions { IosExtension = true }; + var registerPage = new RegisterPage(null, appOptions); + var app = new App.App(appOptions); ThemeManager.SetTheme(app.Resources); ThemeManager.ApplyResourcesTo(registerPage); if (registerPage.BindingContext is RegisterPageViewModel vm) @@ -696,8 +697,9 @@ namespace Bit.iOS.Autofill private void LaunchTwoFactorFlow(bool authingWithSso) { - var twoFactorPage = new TwoFactorPage(authingWithSso); - var app = new App.App(new AppOptions { IosExtension = true }); + var appOptions = new AppOptions { IosExtension = true }; + var twoFactorPage = new TwoFactorPage(authingWithSso, appOptions); + var app = new App.App(); ThemeManager.SetTheme(app.Resources); ThemeManager.ApplyResourcesTo(twoFactorPage); if (twoFactorPage.BindingContext is TwoFactorPageViewModel vm)