2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
2016-05-13 07:11:32 +03:00
|
|
|
|
using System.Collections.Generic;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using System.Linq;
|
2016-05-03 09:08:50 +03:00
|
|
|
|
using Acr.UserDialogs;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
2016-05-10 06:25:37 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Bit.App.Models;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
using Bit.App.Resources;
|
2016-05-07 01:49:01 +03:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
2016-08-25 07:23:24 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
namespace Bit.App.Pages
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
public class VaultAddLoginPage : ExtendedContentPage
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private const string AddedLoginAlertKey = "addedSiteAlert";
|
2016-08-25 07:23:24 +03:00
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
private readonly ILoginService _loginService;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private readonly IFolderService _folderService;
|
|
|
|
|
private readonly IUserDialogs _userDialogs;
|
|
|
|
|
private readonly IConnectivity _connectivity;
|
2016-08-05 02:35:56 +03:00
|
|
|
|
private readonly IGoogleAnalyticsService _googleAnalyticsService;
|
2016-08-25 07:23:24 +03:00
|
|
|
|
private readonly ISettings _settings;
|
2017-02-10 05:43:03 +03:00
|
|
|
|
private readonly IAppInfoService _appInfoService;
|
2017-01-31 03:26:39 +03:00
|
|
|
|
private readonly string _defaultUri;
|
|
|
|
|
private readonly string _defaultName;
|
2017-02-06 07:55:46 +03:00
|
|
|
|
private readonly bool _fromAutofill;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
2017-02-06 07:55:46 +03:00
|
|
|
|
public VaultAddLoginPage(string defaultUri = null, string defaultName = null, bool fromAutofill = false)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2017-01-31 03:26:39 +03:00
|
|
|
|
_defaultUri = defaultUri;
|
|
|
|
|
_defaultName = defaultName;
|
2017-02-06 07:55:46 +03:00
|
|
|
|
_fromAutofill = fromAutofill;
|
2017-01-31 03:26:39 +03:00
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
_loginService = Resolver.Resolve<ILoginService>();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
_folderService = Resolver.Resolve<IFolderService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-08-05 02:35:56 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-08-25 07:23:24 +03:00
|
|
|
|
_settings = Resolver.Resolve<ISettings>();
|
2017-02-10 05:43:03 +03:00
|
|
|
|
_appInfoService = Resolver.Resolve<IAppInfoService>();
|
2016-05-10 01:16:42 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}
|
2016-05-03 01:35:01 +03:00
|
|
|
|
|
2016-07-07 07:27:29 +03:00
|
|
|
|
public FormEntryCell PasswordCell { get; private set; }
|
2017-02-15 08:28:05 +03:00
|
|
|
|
public FormEntryCell UsernameCell { get; private set; }
|
|
|
|
|
public FormEntryCell UriCell { get; private set; }
|
|
|
|
|
public FormEntryCell NameCell { get; private set; }
|
|
|
|
|
public FormEditorCell NotesCell { get; private set; }
|
|
|
|
|
public FormPickerCell FolderCell { get; private set; }
|
|
|
|
|
public ExtendedTextCell GenerateCell { get; private set; }
|
2016-07-07 07:27:29 +03:00
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2017-05-03 02:41:10 +03:00
|
|
|
|
NotesCell = new FormEditorCell(height: 180);
|
2017-02-15 08:28:05 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.Password, isPassword: true, nextElement: NotesCell.Editor,
|
2016-11-08 06:46:15 +03:00
|
|
|
|
useButton: true);
|
|
|
|
|
PasswordCell.Button.Image = "eye";
|
2016-11-08 08:21:36 +03:00
|
|
|
|
PasswordCell.Entry.DisableAutocapitalize = true;
|
|
|
|
|
PasswordCell.Entry.Autocorrect = false;
|
2016-11-09 05:29:24 +03:00
|
|
|
|
PasswordCell.Entry.FontFamily = Device.OnPlatform(iOS: "Courier", Android: "monospace", WinPhone: "Courier");
|
2016-11-08 06:46:15 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
UsernameCell = new FormEntryCell(AppResources.Username, nextElement: PasswordCell.Entry);
|
|
|
|
|
UsernameCell.Entry.DisableAutocapitalize = true;
|
|
|
|
|
UsernameCell.Entry.Autocorrect = false;
|
2016-06-14 03:03:16 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
UriCell = new FormEntryCell(AppResources.URI, Keyboard.Url, nextElement: UsernameCell.Entry);
|
2017-01-31 03:26:39 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(_defaultUri))
|
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
UriCell.Entry.Text = _defaultUri;
|
2017-01-31 03:26:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
NameCell = new FormEntryCell(AppResources.Name, nextElement: UriCell.Entry);
|
2017-01-31 03:26:39 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(_defaultName))
|
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
NameCell.Entry.Text = _defaultName;
|
2017-01-31 03:26:39 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
var folderOptions = new List<string> { AppResources.FolderNone };
|
2016-07-27 02:21:57 +03:00
|
|
|
|
var folders = _folderService.GetAllAsync().GetAwaiter().GetResult()
|
|
|
|
|
.OrderBy(f => f.Name?.Decrypt()).ToList();
|
2016-05-12 00:30:09 +03:00
|
|
|
|
foreach(var folder in folders)
|
2016-05-03 01:35:01 +03:00
|
|
|
|
{
|
2016-05-13 07:11:32 +03:00
|
|
|
|
folderOptions.Add(folder.Name.Decrypt());
|
2016-05-03 01:35:01 +03:00
|
|
|
|
}
|
2017-02-15 08:28:05 +03:00
|
|
|
|
FolderCell = new FormPickerCell(AppResources.Folder, folderOptions.ToArray());
|
2016-05-13 07:11:32 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
GenerateCell = new ExtendedTextCell
|
2016-07-07 07:27:29 +03:00
|
|
|
|
{
|
2016-11-26 00:42:52 +03:00
|
|
|
|
Text = AppResources.GeneratePassword,
|
2016-07-07 07:27:29 +03:00
|
|
|
|
ShowDisclousure = true
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-26 00:42:52 +03:00
|
|
|
|
var favoriteCell = new ExtendedSwitchCell { Text = AppResources.Favorite };
|
2016-06-15 05:36:37 +03:00
|
|
|
|
|
2016-05-24 06:48:34 +03:00
|
|
|
|
var table = new ExtendedTableView
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
2016-05-12 07:09:06 +03:00
|
|
|
|
Intent = TableIntent.Settings,
|
2016-05-24 06:48:34 +03:00
|
|
|
|
EnableScrolling = true,
|
2016-05-12 00:30:09 +03:00
|
|
|
|
HasUnevenRows = true,
|
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
new TableSection(AppResources.LoginInformation)
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
NameCell,
|
|
|
|
|
UriCell,
|
|
|
|
|
UsernameCell,
|
2016-07-07 07:27:29 +03:00
|
|
|
|
PasswordCell,
|
2017-02-15 08:28:05 +03:00
|
|
|
|
GenerateCell
|
2016-05-13 04:30:02 +03:00
|
|
|
|
},
|
2017-04-20 21:18:16 +03:00
|
|
|
|
new TableSection(" ")
|
2016-06-15 05:36:37 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
FolderCell,
|
2016-06-15 05:36:37 +03:00
|
|
|
|
favoriteCell
|
|
|
|
|
},
|
2016-05-12 07:09:06 +03:00
|
|
|
|
new TableSection(AppResources.Notes)
|
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
NotesCell
|
2016-05-12 00:30:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-12 07:09:06 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
2016-05-24 06:48:34 +03:00
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = 70;
|
2016-05-12 07:09:06 +03:00
|
|
|
|
}
|
2016-11-08 07:45:46 +03:00
|
|
|
|
else if(Device.OS == TargetPlatform.Android)
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Button.WidthRequest = 40;
|
|
|
|
|
}
|
2016-05-12 07:09:06 +03:00
|
|
|
|
|
2016-05-12 00:30:09 +03:00
|
|
|
|
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(NameCell.Entry.Text))
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
|
|
|
|
AppResources.Name), AppResources.Ok);
|
2016-05-12 00:30:09 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var login = new Login
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
Uri = UriCell.Entry.Text?.Encrypt(),
|
|
|
|
|
Name = NameCell.Entry.Text?.Encrypt(),
|
|
|
|
|
Username = UsernameCell.Entry.Text?.Encrypt(),
|
2016-07-07 07:27:29 +03:00
|
|
|
|
Password = PasswordCell.Entry.Text?.Encrypt(),
|
2017-02-15 08:28:05 +03:00
|
|
|
|
Notes = NotesCell.Editor.Text?.Encrypt(),
|
2016-06-15 05:36:37 +03:00
|
|
|
|
Favorite = favoriteCell.On
|
2016-05-12 00:30:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
if(FolderCell.Picker.SelectedIndex > 0)
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
login.FolderId = folders.ElementAt(FolderCell.Picker.SelectedIndex - 1).Id;
|
2016-05-12 00:30:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-26 00:42:52 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.Saving, MaskType.Black);
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var saveTask = await _loginService.SaveAsync(login);
|
2016-05-12 00:30:09 +03:00
|
|
|
|
|
|
|
|
|
_userDialogs.HideLoading();
|
2016-08-17 02:20:41 +03:00
|
|
|
|
if(saveTask.Succeeded)
|
2016-07-20 01:46:39 +03:00
|
|
|
|
{
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PopForDeviceAsync();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
_userDialogs.Toast(AppResources.NewLoginCreated);
|
2017-02-06 07:55:46 +03:00
|
|
|
|
if(_fromAutofill)
|
|
|
|
|
{
|
|
|
|
|
_googleAnalyticsService.TrackExtensionEvent("CreatedLogin");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_googleAnalyticsService.TrackAppEvent("CreatedLogin");
|
|
|
|
|
}
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
2016-08-17 02:20:41 +03:00
|
|
|
|
else if(saveTask.Errors.Count() > 0)
|
2016-07-20 01:46:39 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await _userDialogs.AlertAsync(saveTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
2016-07-31 01:16:09 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2016-05-12 00:30:09 +03:00
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Title = AppResources.AddLogin;
|
2016-05-24 06:48:34 +03:00
|
|
|
|
Content = table;
|
2016-05-12 00:30:09 +03:00
|
|
|
|
ToolbarItems.Add(saveToolBarItem);
|
2016-05-14 08:34:42 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
2016-11-26 00:42:52 +03:00
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel));
|
2016-05-14 08:34:42 +03:00
|
|
|
|
}
|
2016-07-31 00:29:04 +03:00
|
|
|
|
}
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|
2016-07-31 00:29:04 +03:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2016-05-12 00:30:09 +03:00
|
|
|
|
if(!_connectivity.IsConnected)
|
2016-05-07 01:49:01 +03:00
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
}
|
2016-08-25 07:23:24 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
PasswordCell.InitEvents();
|
|
|
|
|
UsernameCell.InitEvents();
|
|
|
|
|
UriCell.InitEvents();
|
|
|
|
|
NameCell.InitEvents();
|
|
|
|
|
NotesCell.InitEvents();
|
|
|
|
|
FolderCell.InitEvents();
|
|
|
|
|
PasswordCell.Button.Clicked += PasswordButton_Clicked;
|
|
|
|
|
GenerateCell.Tapped += GenerateCell_Tapped;
|
|
|
|
|
|
2017-02-10 05:43:03 +03:00
|
|
|
|
if(!_fromAutofill && !_settings.GetValueOrDefault(AddedLoginAlertKey, false))
|
2016-08-25 07:23:24 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
_settings.AddOrUpdateValue(AddedLoginAlertKey, true);
|
2017-02-10 05:43:03 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
DisplayAlert(AppResources.BitwardenAppExtension, AppResources.BitwardenAppExtensionAlert,
|
|
|
|
|
AppResources.Ok);
|
|
|
|
|
}
|
|
|
|
|
else if(Device.OS == TargetPlatform.Android && !_appInfoService.AutofillServiceEnabled)
|
|
|
|
|
{
|
|
|
|
|
DisplayAlert(AppResources.BitwardenAutofillService, AppResources.BitwardenAutofillServiceAlert,
|
|
|
|
|
AppResources.Ok);
|
|
|
|
|
}
|
2016-08-25 07:23:24 +03:00
|
|
|
|
}
|
2017-05-03 02:41:10 +03:00
|
|
|
|
|
|
|
|
|
NameCell.Entry.FocusWithDelay();
|
2016-05-07 01:49:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
|
|
|
|
PasswordCell.Dispose();
|
|
|
|
|
UsernameCell.Dispose();
|
|
|
|
|
UriCell.Dispose();
|
|
|
|
|
NameCell.Dispose();
|
|
|
|
|
NotesCell.Dispose();
|
|
|
|
|
FolderCell.Dispose();
|
|
|
|
|
PasswordCell.Button.Clicked -= PasswordButton_Clicked;
|
|
|
|
|
GenerateCell.Tapped -= GenerateCell_Tapped;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 06:46:15 +03:00
|
|
|
|
private void PasswordButton_Clicked(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Entry.InvokeToggleIsPassword();
|
|
|
|
|
PasswordCell.Button.Image = "eye" + (!PasswordCell.Entry.IsPasswordFromToggled ? "_slash" : string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 06:50:22 +03:00
|
|
|
|
private async void GenerateCell_Tapped(object sender, EventArgs e)
|
2016-07-07 07:27:29 +03:00
|
|
|
|
{
|
|
|
|
|
var page = new ToolsPasswordGeneratorPage((password) =>
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Entry.Text = password;
|
2016-11-26 00:42:52 +03:00
|
|
|
|
_userDialogs.Toast(AppResources.PasswordGenerated);
|
2017-02-06 07:55:46 +03:00
|
|
|
|
}, _fromAutofill);
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PushForDeviceAsync(page);
|
2016-07-07 07:27:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void AlertNoConnection()
|
2016-05-07 01:49:01 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
|
|
|
|
|
AppResources.Ok);
|
2016-05-12 00:30:09 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|