From 9ab41c5de6ab83bfc7b5792afefd1563ffe1f608 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 17 May 2019 15:24:15 -0400 Subject: [PATCH] populate add page from autofill --- src/App/Pages/Vault/AddEditPage.xaml.cs | 20 +++++++++++++ src/App/Pages/Vault/AddEditPageViewModel.cs | 16 +++++++++-- .../Pages/Vault/AutofillCiphersPage.xaml.cs | 5 ++-- .../Vault/AutofillCiphersPageViewModel.cs | 28 ++++++++++--------- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/App/Pages/Vault/AddEditPage.xaml.cs b/src/App/Pages/Vault/AddEditPage.xaml.cs index 92f4e3461..569c94a49 100644 --- a/src/App/Pages/Vault/AddEditPage.xaml.cs +++ b/src/App/Pages/Vault/AddEditPage.xaml.cs @@ -9,15 +9,21 @@ namespace Bit.App.Pages { private AddEditPageViewModel _vm; private readonly AppOptions _appOptions; + private bool _fromAutofill; public AddEditPage( string cipherId = null, CipherType? type = null, string folderId = null, string collectionId = null, + string name = null, + string uri = null, + bool fromAutofill = false, AppOptions appOptions = null) { _appOptions = appOptions; + _fromAutofill = fromAutofill; + FromAutofillFramework = _appOptions?.FromAutofillFramework ?? false; InitializeComponent(); _vm = BindingContext as AddEditPageViewModel; _vm.Page = this; @@ -25,6 +31,8 @@ namespace Bit.App.Pages _vm.FolderId = folderId; _vm.CollectionIds = collectionId != null ? new HashSet(new List { collectionId }) : null; _vm.Type = type; + _vm.DefaultName = name ?? appOptions?.SaveName; + _vm.DefaultUri = uri ?? appOptions?.Uri; _vm.Init(); SetActivityIndicator(); if(!_vm.EditMode) @@ -41,6 +49,8 @@ namespace Bit.App.Pages _ownershipPicker.ItemDisplayBinding = new Binding("Key"); } + public bool FromAutofillFramework { get; set; } + protected override async void OnAppearing() { base.OnAppearing(); @@ -77,6 +87,16 @@ namespace Bit.App.Pages base.OnDisappearing(); } + protected override bool OnBackButtonPressed() + { + if(FromAutofillFramework) + { + Application.Current.MainPage = new TabsPage(); + return true; + } + return base.OnBackButtonPressed(); + } + private async void PasswordHistory_Tapped(object sender, System.EventArgs e) { if(DoOnce()) diff --git a/src/App/Pages/Vault/AddEditPageViewModel.cs b/src/App/Pages/Vault/AddEditPageViewModel.cs index b0a4167d0..49b2a518f 100644 --- a/src/App/Pages/Vault/AddEditPageViewModel.cs +++ b/src/App/Pages/Vault/AddEditPageViewModel.cs @@ -142,6 +142,8 @@ namespace Bit.App.Pages public string FolderId { get; set; } public CipherType? Type { get; set; } public HashSet CollectionIds { get; set; } + public string DefaultName { get; set; } + public string DefaultUri { get; set; } public List> TypeOptions { get; set; } public List> CardBrandOptions { get; set; } public List> CardExpMonthOptions { get; set; } @@ -300,6 +302,7 @@ namespace Bit.App.Pages { Cipher = new CipherView { + Name = DefaultName, OrganizationId = OrganizationId, FolderId = FolderId, Type = Type.GetValueOrDefault(CipherType.Login), @@ -308,7 +311,7 @@ namespace Bit.App.Pages Identity = new IdentityView(), SecureNote = new SecureNoteView() }; - Cipher.Login.Uris = new List { new LoginUriView() }; + Cipher.Login.Uris = new List { new LoginUriView { Uri = DefaultUri } }; Cipher.SecureNote.Type = SecureNoteType.Generic; TypeSelectedIndex = TypeOptions.FindIndex(k => k.Value == Cipher.Type); @@ -398,7 +401,16 @@ namespace Bit.App.Pages _platformUtilsService.ShowToast("success", null, EditMode ? AppResources.ItemUpdated : AppResources.NewItemCreated); _messagingService.Send(EditMode ? "editedCipher" : "addedCipher"); - await Page.Navigation.PopModalAsync(); + + if((Page as AddEditPage).FromAutofillFramework) + { + // Close and go back to app + _deviceActionService.CloseAutofill(); + } + else + { + await Page.Navigation.PopModalAsync(); + } return true; } catch(ApiException e) diff --git a/src/App/Pages/Vault/AutofillCiphersPage.xaml.cs b/src/App/Pages/Vault/AutofillCiphersPage.xaml.cs index 5b331848f..153ea59d1 100644 --- a/src/App/Pages/Vault/AutofillCiphersPage.xaml.cs +++ b/src/App/Pages/Vault/AutofillCiphersPage.xaml.cs @@ -49,11 +49,12 @@ namespace Bit.App.Pages } if(_appOptions.FillType.HasValue && _appOptions.FillType != CipherType.Login) { - var pageForOther = new AddEditPage(type: _appOptions.FillType); + var pageForOther = new AddEditPage(type: _appOptions.FillType, fromAutofill: true); await Navigation.PushModalAsync(new NavigationPage(pageForOther)); return; } - var pageForLogin = new AddEditPage(null, CipherType.Login); + var pageForLogin = new AddEditPage(null, CipherType.Login, uri: _vm.Uri, name: _vm.Name, + fromAutofill: true); await Navigation.PushModalAsync(new NavigationPage(pageForLogin)); } diff --git a/src/App/Pages/Vault/AutofillCiphersPageViewModel.cs b/src/App/Pages/Vault/AutofillCiphersPageViewModel.cs index 14fefee59..fc57dd2da 100644 --- a/src/App/Pages/Vault/AutofillCiphersPageViewModel.cs +++ b/src/App/Pages/Vault/AutofillCiphersPageViewModel.cs @@ -23,8 +23,6 @@ namespace Bit.App.Pages private readonly ICipherService _cipherService; private AppOptions _appOptions; - private string _name; - private string _uri; private bool _showList; private string _noDataText; @@ -38,6 +36,8 @@ namespace Bit.App.Pages CipherOptionsCommand = new Command(CipherOptionsAsync); } + public string Name { get; set; } + public string Uri { get; set; } public Command CipherOptionsCommand { get; set; } public ExtendedObservableCollection GroupedItems { get; set; } @@ -56,25 +56,27 @@ namespace Bit.App.Pages public void Init(AppOptions appOptions) { _appOptions = appOptions; - _uri = appOptions.Uri; - if(_uri.StartsWith(Constants.AndroidAppProtocol)) + Uri = appOptions.Uri; + string name = null; + if(Uri.StartsWith(Constants.AndroidAppProtocol)) { - _name = _uri.Substring(Constants.AndroidAppProtocol.Length); + name = Uri.Substring(Constants.AndroidAppProtocol.Length); } - else if(!Uri.TryCreate(_uri, UriKind.Absolute, out Uri uri) || - !DomainName.TryParseBaseDomain(uri.Host, out _name)) + else if(!System.Uri.TryCreate(Uri, UriKind.Absolute, out Uri uri) || + !DomainName.TryParseBaseDomain(uri.Host, out name)) { - _name = "--"; + name = "--"; } - PageTitle = string.Format(AppResources.ItemsForUri, _name ?? "--"); - NoDataText = string.Format(AppResources.NoItemsForUri, _name ?? "--"); + Name = name; + PageTitle = string.Format(AppResources.ItemsForUri, Name ?? "--"); + NoDataText = string.Format(AppResources.NoItemsForUri, Name ?? "--"); } public async Task LoadAsync() { ShowList = false; var groupedItems = new List(); - var ciphers = await _cipherService.GetAllDecryptedByUrlAsync(_uri, null); + var ciphers = await _cipherService.GetAllDecryptedByUrlAsync(Uri, null); var matching = ciphers.Item1?.Select(c => new GroupingsPageListItem { Cipher = c }).ToList(); if(matching?.Any() ?? false) { @@ -109,7 +111,7 @@ namespace Bit.App.Pages options.Add(AppResources.YesAndSave); } autofillResponse = await _deviceActionService.DisplayAlertAsync(null, - string.Format(AppResources.BitwardenAutofillServiceMatchConfirm, _name), AppResources.No, + string.Format(AppResources.BitwardenAutofillServiceMatchConfirm, Name), AppResources.No, options.ToArray()); } if(autofillResponse == AppResources.YesAndSave && cipher.Type == CipherType.Login) @@ -121,7 +123,7 @@ namespace Bit.App.Pages } uris.Add(new LoginUriView { - Uri = _uri, + Uri = Uri, Match = null }); cipher.Login.Uris = uris;