bitwarden-mobile/src/App/Pages/Vault/AutofillCiphersPageViewMode...

249 lines
9.6 KiB
C#
Raw Normal View History

2022-04-26 17:21:17 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Controls;
2019-05-17 20:46:31 +02:00
using Bit.App.Models;
2019-05-17 19:14:26 +02:00
using Bit.App.Resources;
2019-05-30 05:35:34 +02:00
using Bit.App.Utilities;
2019-05-17 19:14:26 +02:00
using Bit.Core;
using Bit.Core.Abstractions;
2019-05-17 20:46:31 +02:00
using Bit.Core.Enums;
using Bit.Core.Exceptions;
2019-05-17 19:14:26 +02:00
using Bit.Core.Models.View;
using Bit.Core.Utilities;
using Xamarin.CommunityToolkit.ObjectModel;
2019-05-17 19:14:26 +02:00
using Xamarin.Forms;
namespace Bit.App.Pages
{
public class AutofillCiphersPageViewModel : BaseViewModel
{
private readonly IPlatformUtilsService _platformUtilsService;
2019-05-17 20:46:31 +02:00
private readonly IDeviceActionService _deviceActionService;
2019-05-17 19:14:26 +02:00
private readonly ICipherService _cipherService;
2019-06-08 18:18:49 +02:00
private readonly IStateService _stateService;
private readonly IPasswordRepromptService _passwordRepromptService;
private readonly IMessagingService _messagingService;
private readonly ILogger _logger;
2019-05-17 19:14:26 +02:00
private bool _showNoData;
2019-05-17 19:14:26 +02:00
private bool _showList;
2019-05-17 20:58:42 +02:00
private string _noDataText;
2019-06-08 18:18:49 +02:00
private bool _websiteIconsEnabled;
2019-05-17 19:14:26 +02:00
public AutofillCiphersPageViewModel()
{
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
2019-05-17 20:46:31 +02:00
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
2019-06-08 18:18:49 +02:00
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
_passwordRepromptService = ServiceContainer.Resolve<IPasswordRepromptService>("passwordRepromptService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
_logger = ServiceContainer.Resolve<ILogger>("logger");
2019-05-17 19:14:26 +02:00
GroupedItems = new ObservableRangeCollection<IGroupingsPageListItem>();
2019-05-17 19:14:26 +02:00
CipherOptionsCommand = new Command<CipherView>(CipherOptionsAsync);
2022-04-26 17:21:17 +02:00
AccountSwitchingOverlayViewModel = new AccountSwitchingOverlayViewModel(_stateService, _messagingService, _logger)
{
AllowAddAccountRow = false
};
2019-05-17 19:14:26 +02:00
}
2019-05-17 21:24:15 +02:00
public string Name { get; set; }
public string Uri { get; set; }
2019-05-17 19:14:26 +02:00
public Command CipherOptionsCommand { get; set; }
public bool LoadedOnce { get; set; }
public ObservableRangeCollection<IGroupingsPageListItem> GroupedItems { get; set; }
public AccountSwitchingOverlayViewModel AccountSwitchingOverlayViewModel { get; }
public bool ShowNoData
{
get => _showNoData;
set => SetProperty(ref _showNoData, value);
}
2019-05-17 19:14:26 +02:00
public bool ShowList
{
get => _showList;
set => SetProperty(ref _showList, value);
}
2019-05-17 20:58:42 +02:00
public string NoDataText
{
get => _noDataText;
set => SetProperty(ref _noDataText, value);
}
2019-06-08 18:18:49 +02:00
public bool WebsiteIconsEnabled
{
get => _websiteIconsEnabled;
set => SetProperty(ref _websiteIconsEnabled, value);
}
2019-05-17 20:58:42 +02:00
2019-05-17 19:14:26 +02:00
public void Init(AppOptions appOptions)
{
Uri = appOptions?.Uri;
2019-05-17 21:24:15 +02:00
string name = null;
if (Uri?.StartsWith(Constants.AndroidAppProtocol) ?? false)
2019-05-17 19:14:26 +02:00
{
2019-05-17 21:24:15 +02:00
name = Uri.Substring(Constants.AndroidAppProtocol.Length);
2019-05-17 19:14:26 +02:00
}
2019-05-31 14:30:18 +02:00
else
{
name = CoreHelpers.GetDomain(Uri);
}
if (string.IsNullOrWhiteSpace(name))
2019-05-17 19:14:26 +02:00
{
2019-05-17 21:24:15 +02:00
name = "--";
2019-05-17 19:14:26 +02:00
}
2019-05-17 21:24:15 +02:00
Name = name;
PageTitle = string.Format(AppResources.ItemsForUri, Name ?? "--");
NoDataText = string.Format(AppResources.NoItemsForUri, Name ?? "--");
2019-05-17 19:14:26 +02:00
}
public async Task LoadAsync()
{
LoadedOnce = true;
2019-05-17 19:14:26 +02:00
ShowList = false;
ShowNoData = false;
WebsiteIconsEnabled = !(await _stateService.GetDisableFaviconAsync()).GetValueOrDefault();
2019-05-17 20:58:42 +02:00
var groupedItems = new List<GroupingsPageListGroup>();
2019-05-17 21:24:15 +02:00
var ciphers = await _cipherService.GetAllDecryptedByUrlAsync(Uri, null);
2019-05-17 19:14:26 +02:00
var matching = ciphers.Item1?.Select(c => new GroupingsPageListItem { Cipher = c }).ToList();
2019-05-31 17:36:44 +02:00
var hasMatching = matching?.Any() ?? false;
if (matching?.Any() ?? false)
2019-05-17 20:58:42 +02:00
{
groupedItems.Add(
2019-05-31 17:36:44 +02:00
new GroupingsPageListGroup(matching, AppResources.MatchingItems, matching.Count, false, true));
2019-05-17 20:58:42 +02:00
}
var fuzzy = ciphers.Item2?.Select(c =>
new GroupingsPageListItem { Cipher = c, FuzzyAutofill = true }).ToList();
if (fuzzy?.Any() ?? false)
2019-05-17 20:58:42 +02:00
{
groupedItems.Add(
2019-05-31 17:36:44 +02:00
new GroupingsPageListGroup(fuzzy, AppResources.PossibleMatchingItems, fuzzy.Count, false,
!hasMatching));
2019-05-17 20:58:42 +02:00
}
// TODO: refactor this
if (Device.RuntimePlatform == Device.Android
||
GroupedItems.Any())
{
var items = new List<IGroupingsPageListItem>();
foreach (var itemGroup in groupedItems)
{
items.Add(new GroupingsPageHeaderListItem(itemGroup.Name, itemGroup.ItemCount));
items.AddRange(itemGroup);
}
GroupedItems.ReplaceRange(items);
}
else
{
// HACK: we need this on iOS, so that it doesn't crash when adding coming from an empty list
var first = true;
var items = new List<IGroupingsPageListItem>();
foreach (var itemGroup in groupedItems)
{
if (!first)
{
items.Add(new GroupingsPageHeaderListItem(itemGroup.Name, itemGroup.ItemCount));
}
else
{
first = false;
}
items.AddRange(itemGroup);
}
if (groupedItems.Any())
{
GroupedItems.ReplaceRange(new List<IGroupingsPageListItem> { new GroupingsPageHeaderListItem(groupedItems[0].Name, groupedItems[0].ItemCount) });
GroupedItems.AddRange(items);
}
else
{
GroupedItems.Clear();
}
}
2019-05-17 20:58:42 +02:00
ShowList = groupedItems.Any();
ShowNoData = !ShowList;
2019-05-17 19:14:26 +02:00
}
2019-05-17 20:46:31 +02:00
public async Task SelectCipherAsync(CipherView cipher, bool fuzzy)
2019-05-17 19:14:26 +02:00
{
if (cipher == null)
2019-06-14 14:05:28 +02:00
{
return;
}
if (_deviceActionService.SystemMajorVersion() < 21)
2019-05-17 20:46:31 +02:00
{
await AppHelpers.CipherListOptions(Page, cipher, _passwordRepromptService);
2019-05-17 20:46:31 +02:00
}
else
{
if (cipher.Reprompt != CipherRepromptType.None && !await _passwordRepromptService.ShowPasswordPromptAsync())
{
return;
}
2019-05-17 20:46:31 +02:00
var autofillResponse = AppResources.Yes;
if (fuzzy)
2019-05-17 20:46:31 +02:00
{
var options = new List<string> { AppResources.Yes };
if (cipher.Type == CipherType.Login &&
Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.None)
2019-05-17 20:46:31 +02:00
{
options.Add(AppResources.YesAndSave);
}
autofillResponse = await _deviceActionService.DisplayAlertAsync(null,
2019-05-17 21:24:15 +02:00
string.Format(AppResources.BitwardenAutofillServiceMatchConfirm, Name), AppResources.No,
2019-05-17 20:46:31 +02:00
options.ToArray());
}
if (autofillResponse == AppResources.YesAndSave && cipher.Type == CipherType.Login)
2019-05-17 20:46:31 +02:00
{
var uris = cipher.Login?.Uris?.ToList();
if (uris == null)
2019-05-17 20:46:31 +02:00
{
uris = new List<LoginUriView>();
}
uris.Add(new LoginUriView
{
2019-05-17 21:24:15 +02:00
Uri = Uri,
2019-05-17 20:46:31 +02:00
Match = null
});
cipher.Login.Uris = uris;
try
{
await _deviceActionService.ShowLoadingAsync(AppResources.Saving);
await _cipherService.SaveWithServerAsync(await _cipherService.EncryptAsync(cipher));
await _deviceActionService.HideLoadingAsync();
}
catch (ApiException e)
2019-05-17 20:46:31 +02:00
{
await _deviceActionService.HideLoadingAsync();
if (e?.Error != null)
2019-10-22 22:37:40 +02:00
{
await _platformUtilsService.ShowDialogAsync(e.Error.GetSingleMessage(),
AppResources.AnErrorHasOccurred);
}
2019-05-17 20:46:31 +02:00
}
}
if (autofillResponse == AppResources.Yes || autofillResponse == AppResources.YesAndSave)
2019-05-17 20:46:31 +02:00
{
_deviceActionService.Autofill(cipher);
}
}
2019-05-17 19:14:26 +02:00
}
private async void CipherOptionsAsync(CipherView cipher)
{
if ((Page as BaseContentPage).DoOnce())
2019-05-17 19:14:26 +02:00
{
await AppHelpers.CipherListOptions(Page, cipher, _passwordRepromptService);
2019-05-17 19:14:26 +02:00
}
}
}
}