From 1dc027cf491f5c88675d5592ec8ae618edd6c58c Mon Sep 17 00:00:00 2001 From: Vincent Salucci <26154748+vincentsalucci@users.noreply.github.com> Date: Mon, 13 Apr 2020 11:32:23 -0500 Subject: [PATCH] [Autofill] Apply locked autofill flow to logged out state (#827) * Initial commit: apply locked auto-fill flow to log out auto-fill * Alphabetized imports * Removed unnecessary else conditional * Fix for talkback slider control (#828) * Initial commit: apply locked auto-fill flow to log out auto-fill * Alphabetized imports * Removed unnecessary else conditional * Fixed variable init order Co-authored-by: Matt Portune <59324545+mportune-bw@users.noreply.github.com> --- src/App/App.xaml.cs | 2 +- src/App/Pages/Accounts/HomePage.xaml.cs | 11 +++-- src/App/Pages/Accounts/LockPage.xaml.cs | 2 +- src/App/Pages/Accounts/LoginPage.xaml.cs | 42 ++++++++++++++++++- src/App/Pages/Accounts/LoginPageViewModel.cs | 8 ++-- src/App/Pages/Accounts/TwoFactorPage.xaml.cs | 32 +++++++++++++- .../Pages/Accounts/TwoFactorPageViewModel.cs | 4 +- 7 files changed, 88 insertions(+), 13 deletions(-) diff --git a/src/App/App.xaml.cs b/src/App/App.xaml.cs index 303f31908..117bc6c43 100644 --- a/src/App/App.xaml.cs +++ b/src/App/App.xaml.cs @@ -304,7 +304,7 @@ namespace Bit.App } else { - Current.MainPage = new HomePage(); + Current.MainPage = new HomePage(_appOptions); } } diff --git a/src/App/Pages/Accounts/HomePage.xaml.cs b/src/App/Pages/Accounts/HomePage.xaml.cs index ce072c963..67cc3b2f4 100644 --- a/src/App/Pages/Accounts/HomePage.xaml.cs +++ b/src/App/Pages/Accounts/HomePage.xaml.cs @@ -1,4 +1,5 @@ -using Bit.App.Utilities; +using Bit.App.Models; +using Bit.App.Utilities; using Bit.Core.Abstractions; using Bit.Core.Utilities; using System; @@ -9,12 +10,14 @@ namespace Bit.App.Pages { public partial class HomePage : BaseContentPage { + private readonly AppOptions _appOptions; private IMessagingService _messagingService; - public HomePage() + public HomePage(AppOptions appOptions = null) { _messagingService = ServiceContainer.Resolve("messagingService"); _messagingService.Send("showStatusBar", false); + _appOptions = appOptions; InitializeComponent(); _logo.Source = !ThemeManager.UsingLightTheme ? "logo_white.png" : "logo.png"; } @@ -22,7 +25,7 @@ namespace Bit.App.Pages public async Task DismissRegisterPageAndLogInAsync(string email) { await Navigation.PopModalAsync(); - await Navigation.PushModalAsync(new NavigationPage(new LoginPage(email))); + await Navigation.PushModalAsync(new NavigationPage(new LoginPage(email, _appOptions))); } protected override void OnAppearing() @@ -35,7 +38,7 @@ namespace Bit.App.Pages { if (DoOnce()) { - Navigation.PushModalAsync(new NavigationPage(new LoginPage())); + Navigation.PushModalAsync(new NavigationPage(new LoginPage(null, _appOptions))); } } diff --git a/src/App/Pages/Accounts/LockPage.xaml.cs b/src/App/Pages/Accounts/LockPage.xaml.cs index 040a24a62..0383b6698 100644 --- a/src/App/Pages/Accounts/LockPage.xaml.cs +++ b/src/App/Pages/Accounts/LockPage.xaml.cs @@ -106,7 +106,7 @@ namespace Bit.App.Pages Application.Current.MainPage = new NavigationPage(new AddEditPage(appOptions: _appOptions)); return; } - else if (_appOptions.Uri != null) + if (_appOptions.Uri != null) { Application.Current.MainPage = new NavigationPage(new AutofillCiphersPage(_appOptions)); return; diff --git a/src/App/Pages/Accounts/LoginPage.xaml.cs b/src/App/Pages/Accounts/LoginPage.xaml.cs index aa6c623b5..06300545b 100644 --- a/src/App/Pages/Accounts/LoginPage.xaml.cs +++ b/src/App/Pages/Accounts/LoginPage.xaml.cs @@ -1,6 +1,9 @@ -using Bit.Core.Abstractions; +using Bit.App.Models; +using Bit.Core; +using Bit.Core.Abstractions; using Bit.Core.Utilities; using System; +using System.Threading.Tasks; using Xamarin.Forms; namespace Bit.App.Pages @@ -8,15 +11,21 @@ namespace Bit.App.Pages public partial class LoginPage : BaseContentPage { private readonly IMessagingService _messagingService; + private readonly IStorageService _storageService; private readonly LoginPageViewModel _vm; + private readonly AppOptions _appOptions; - public LoginPage(string email = null) + public LoginPage(string email = null, AppOptions appOptions = null) { + _storageService = ServiceContainer.Resolve("storageService"); _messagingService = ServiceContainer.Resolve("messagingService"); _messagingService.Send("showStatusBar", true); + _appOptions = appOptions; InitializeComponent(); _vm = BindingContext as LoginPageViewModel; _vm.Page = this; + _vm.StartTwoFactorAction = () => Device.BeginInvokeOnMainThread(async () => await StartTwoFactorAsync()); + _vm.LoggedInAction = () => Device.BeginInvokeOnMainThread(async () => await LoggedInAsync()); _vm.Email = email; MasterPasswordEntry = _masterPassword; if (Device.RuntimePlatform == Device.Android) @@ -68,5 +77,34 @@ namespace Bit.App.Pages await Navigation.PopModalAsync(); } } + + private async Task StartTwoFactorAsync() + { + var page = new TwoFactorPage(); + await Navigation.PushModalAsync(new NavigationPage(page)); + } + + private async Task LoggedInAsync() + { + if (_appOptions != null) + { + if (_appOptions.FromAutofillFramework && _appOptions.SaveType.HasValue) + { + Application.Current.MainPage = new NavigationPage(new AddEditPage(appOptions: _appOptions)); + return; + } + if (_appOptions.Uri != null) + { + Application.Current.MainPage = new NavigationPage(new AutofillCiphersPage(_appOptions)); + return; + } + } + var previousPage = await _storageService.GetAsync(Constants.PreviousPageKey); + if (previousPage != null) + { + await _storageService.RemoveAsync(Constants.PreviousPageKey); + } + Application.Current.MainPage = new TabsPage(_appOptions, previousPage); + } } } diff --git a/src/App/Pages/Accounts/LoginPageViewModel.cs b/src/App/Pages/Accounts/LoginPageViewModel.cs index d6c8893fc..326647f14 100644 --- a/src/App/Pages/Accounts/LoginPageViewModel.cs +++ b/src/App/Pages/Accounts/LoginPageViewModel.cs @@ -4,6 +4,7 @@ using Bit.Core; using Bit.Core.Abstractions; using Bit.Core.Exceptions; using Bit.Core.Utilities; +using System; using System.Threading.Tasks; using Xamarin.Forms; @@ -65,6 +66,8 @@ namespace Bit.App.Pages public Command TogglePasswordCommand { get; } public string ShowPasswordIcon => ShowPassword ? "" : ""; public bool RememberEmail { get; set; } + public Action StartTwoFactorAction { get; set; } + public Action LoggedInAction { get; set; } public async Task InitAsync() { @@ -121,15 +124,14 @@ namespace Bit.App.Pages await _deviceActionService.HideLoadingAsync(); if (response.TwoFactor) { - var page = new TwoFactorPage(); - await Page.Navigation.PushModalAsync(new NavigationPage(page)); + StartTwoFactorAction?.Invoke(); } else { var disableFavicon = await _storageService.GetAsync(Constants.DisableFaviconKey); await _stateService.SaveAsync(Constants.DisableFaviconKey, disableFavicon.GetValueOrDefault()); var task = Task.Run(async () => await _syncService.FullSyncAsync(true)); - Application.Current.MainPage = new TabsPage(); + LoggedInAction?.Invoke(); } } catch (ApiException e) diff --git a/src/App/Pages/Accounts/TwoFactorPage.xaml.cs b/src/App/Pages/Accounts/TwoFactorPage.xaml.cs index 50db79f1c..0e93e2009 100644 --- a/src/App/Pages/Accounts/TwoFactorPage.xaml.cs +++ b/src/App/Pages/Accounts/TwoFactorPage.xaml.cs @@ -1,4 +1,6 @@ using Bit.App.Controls; +using Bit.App.Models; +using Bit.Core; using Bit.Core.Abstractions; using Bit.Core.Utilities; using System; @@ -11,18 +13,23 @@ namespace Bit.App.Pages { private readonly IBroadcasterService _broadcasterService; private readonly IMessagingService _messagingService; + private readonly IStorageService _storageService; + private readonly AppOptions _appOptions; private TwoFactorPageViewModel _vm; private bool _inited; - public TwoFactorPage() + public TwoFactorPage(AppOptions appOptions = null) { InitializeComponent(); SetActivityIndicator(); + _appOptions = appOptions; + _storageService = ServiceContainer.Resolve("storageService"); _broadcasterService = ServiceContainer.Resolve("broadcasterService"); _messagingService = ServiceContainer.Resolve("messagingService"); _vm = BindingContext as TwoFactorPageViewModel; _vm.Page = this; + _vm.TwoFactorAction = () => Device.BeginInvokeOnMainThread(async () => await TwoFactorAuthAsync()); DuoWebView = _duoWebView; if (Device.RuntimePlatform == Device.Android) { @@ -151,5 +158,28 @@ namespace Bit.App.Pages } } } + + private async Task TwoFactorAuthAsync() + { + if (_appOptions != null) + { + if (_appOptions.FromAutofillFramework && _appOptions.SaveType.HasValue) + { + Application.Current.MainPage = new NavigationPage(new AddEditPage(appOptions: _appOptions)); + return; + } + if (_appOptions.Uri != null) + { + Application.Current.MainPage = new NavigationPage(new AutofillCiphersPage(_appOptions)); + return; + } + } + var previousPage = await _storageService.GetAsync(Constants.PreviousPageKey); + if (previousPage != null) + { + await _storageService.RemoveAsync(Constants.PreviousPageKey); + } + Application.Current.MainPage = new TabsPage(_appOptions, previousPage); + } } } diff --git a/src/App/Pages/Accounts/TwoFactorPageViewModel.cs b/src/App/Pages/Accounts/TwoFactorPageViewModel.cs index 61ba7b5a7..f716bd79a 100644 --- a/src/App/Pages/Accounts/TwoFactorPageViewModel.cs +++ b/src/App/Pages/Accounts/TwoFactorPageViewModel.cs @@ -6,6 +6,7 @@ using Bit.Core.Enums; using Bit.Core.Exceptions; using Bit.Core.Models.Request; using Bit.Core.Utilities; +using System; using System.Linq; using System.Net; using System.Threading.Tasks; @@ -88,6 +89,7 @@ namespace Bit.App.Pages }); } public Command SubmitCommand { get; } + public Action TwoFactorAction { get; set; } public void Init() { @@ -209,7 +211,7 @@ namespace Bit.App.Pages _broadcasterService.Unsubscribe(nameof(TwoFactorPage)); var disableFavicon = await _storageService.GetAsync(Constants.DisableFaviconKey); await _stateService.SaveAsync(Constants.DisableFaviconKey, disableFavicon.GetValueOrDefault()); - Application.Current.MainPage = new TabsPage(); + TwoFactorAction?.Invoke(); } catch (ApiException e) {