2016-06-26 03:54:17 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Controls;
|
|
|
|
|
using Bit.App.Models.Api;
|
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
2016-06-28 02:53:31 +03:00
|
|
|
|
public class RegisterPage : ExtendedContentPage
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
|
|
|
|
private ICryptoService _cryptoService;
|
|
|
|
|
private IUserDialogs _userDialogs;
|
|
|
|
|
private IAccountsApiRepository _accountsApiRepository;
|
2016-08-04 07:32:37 +03:00
|
|
|
|
private IGoogleAnalyticsService _googleAnalyticsService;
|
2016-08-06 18:57:05 +03:00
|
|
|
|
private HomePage _homePage;
|
2016-06-26 03:54:17 +03:00
|
|
|
|
|
2016-08-06 18:57:05 +03:00
|
|
|
|
public RegisterPage(HomePage homePage)
|
2016-08-09 02:00:36 +03:00
|
|
|
|
: base(updateActivity: false)
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
2016-08-06 18:57:05 +03:00
|
|
|
|
_homePage = homePage;
|
2016-06-26 03:54:17 +03:00
|
|
|
|
_cryptoService = Resolver.Resolve<ICryptoService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_accountsApiRepository = Resolver.Resolve<IAccountsApiRepository>();
|
2016-08-04 07:32:37 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-06-26 03:54:17 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FormEntryCell EmailCell { get; set; }
|
|
|
|
|
public FormEntryCell PasswordCell { get; set; }
|
|
|
|
|
public FormEntryCell ConfirmPasswordCell { get; set; }
|
|
|
|
|
public FormEntryCell PasswordHintCell { get; set; }
|
2017-02-17 08:16:09 +03:00
|
|
|
|
public StackLayout StackLayout { get; set; }
|
|
|
|
|
public Label PasswordLabel { get; set; }
|
|
|
|
|
public Label HintLabel { get; set; }
|
2016-06-26 03:54:17 +03:00
|
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2016-07-23 22:32:11 +03:00
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
|
|
|
|
|
|
2016-08-14 04:43:15 +03:00
|
|
|
|
var padding = Device.OnPlatform(
|
|
|
|
|
iOS: new Thickness(15, 20),
|
|
|
|
|
Android: new Thickness(15, 8),
|
|
|
|
|
WinPhone: new Thickness(15, 20));
|
2016-07-03 07:27:10 +03:00
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
PasswordHintCell = new FormEntryCell(AppResources.MasterPasswordHint, useLabelAsPlaceholder: true,
|
2016-08-13 07:30:41 +03:00
|
|
|
|
imageSource: "lightbulb", containerPadding: padding);
|
2016-11-26 00:54:33 +03:00
|
|
|
|
ConfirmPasswordCell = new FormEntryCell(AppResources.RetypeMasterPassword, isPassword: true,
|
2016-08-17 02:20:41 +03:00
|
|
|
|
nextElement: PasswordHintCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock",
|
|
|
|
|
containerPadding: padding);
|
2016-11-09 05:29:24 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.MasterPassword, isPassword: true,
|
2016-08-17 02:20:41 +03:00
|
|
|
|
nextElement: ConfirmPasswordCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock",
|
|
|
|
|
containerPadding: padding);
|
2016-07-04 09:45:32 +03:00
|
|
|
|
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
|
2016-08-17 02:20:41 +03:00
|
|
|
|
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope",
|
|
|
|
|
containerPadding: padding);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
|
|
|
|
|
PasswordHintCell.Entry.ReturnType = Enums.ReturnType.Done;
|
|
|
|
|
|
2016-07-04 09:45:32 +03:00
|
|
|
|
var table = new FormTableView
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
2017-04-20 21:18:16 +03:00
|
|
|
|
new TableSection(" ")
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
|
|
|
|
EmailCell,
|
2016-07-04 09:45:32 +03:00
|
|
|
|
PasswordCell
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-17 08:16:09 +03:00
|
|
|
|
PasswordLabel = new Label
|
2016-07-04 09:45:32 +03:00
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Text = AppResources.MasterPasswordDescription,
|
2016-07-04 09:45:32 +03:00
|
|
|
|
LineBreakMode = LineBreakMode.WordWrap,
|
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
|
2016-07-05 05:31:15 +03:00
|
|
|
|
Style = (Style)Application.Current.Resources["text-muted"],
|
|
|
|
|
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 25)
|
2016-07-04 09:45:32 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var table2 = new FormTableView
|
|
|
|
|
{
|
2016-07-05 05:31:15 +03:00
|
|
|
|
NoHeader = true,
|
2016-07-04 09:45:32 +03:00
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
2017-04-20 21:18:16 +03:00
|
|
|
|
new TableSection(" ")
|
2016-06-28 05:17:49 +03:00
|
|
|
|
{
|
2016-06-26 03:54:17 +03:00
|
|
|
|
ConfirmPasswordCell,
|
|
|
|
|
PasswordHintCell
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-17 08:16:09 +03:00
|
|
|
|
HintLabel = new Label
|
2016-07-04 09:45:32 +03:00
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Text = AppResources.MasterPasswordHintDescription,
|
2016-07-04 09:45:32 +03:00
|
|
|
|
LineBreakMode = LineBreakMode.WordWrap,
|
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
|
2016-07-05 05:31:15 +03:00
|
|
|
|
Style = (Style)Application.Current.Resources["text-muted"],
|
|
|
|
|
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 25)
|
2016-07-04 09:45:32 +03:00
|
|
|
|
};
|
|
|
|
|
|
2017-02-17 08:16:09 +03:00
|
|
|
|
StackLayout = new StackLayout
|
2016-07-04 09:45:32 +03:00
|
|
|
|
{
|
2017-02-17 08:16:09 +03:00
|
|
|
|
Children = { table, PasswordLabel, table2, HintLabel },
|
2016-07-05 05:31:15 +03:00
|
|
|
|
Spacing = 0
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-04 09:45:32 +03:00
|
|
|
|
var scrollView = new ScrollView
|
|
|
|
|
{
|
2017-02-17 08:16:09 +03:00
|
|
|
|
Content = StackLayout
|
2016-07-04 09:45:32 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
var loginToolbarItem = new ToolbarItem(AppResources.Submit, null, async () =>
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
|
|
|
|
await Register();
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
2016-07-04 09:45:32 +03:00
|
|
|
|
table.RowHeight = table2.RowHeight = table2.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = table2.EstimatedRowHeight = table2.EstimatedRowHeight = 70;
|
2016-11-26 00:54:33 +03:00
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel, () =>
|
2016-07-23 22:32:11 +03:00
|
|
|
|
{
|
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", false);
|
|
|
|
|
}));
|
2016-06-26 03:54:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ToolbarItems.Add(loginToolbarItem);
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Title = AppResources.CreateAccount;
|
2016-07-04 09:45:32 +03:00
|
|
|
|
Content = scrollView;
|
2016-06-26 03:54:17 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2016-07-23 22:32:11 +03:00
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
|
2017-02-15 08:28:05 +03:00
|
|
|
|
EmailCell.InitEvents();
|
|
|
|
|
PasswordCell.InitEvents();
|
|
|
|
|
PasswordHintCell.InitEvents();
|
|
|
|
|
ConfirmPasswordCell.InitEvents();
|
|
|
|
|
PasswordHintCell.Entry.Completed += Entry_Completed;
|
2017-02-17 08:16:09 +03:00
|
|
|
|
StackLayout.LayoutChanged += Layout_LayoutChanged;
|
2016-08-19 07:27:37 +03:00
|
|
|
|
EmailCell.Entry.FocusWithDelay();
|
2016-06-26 03:54:17 +03:00
|
|
|
|
}
|
2017-02-15 08:28:05 +03:00
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
|
|
|
|
EmailCell.Dispose();
|
|
|
|
|
PasswordCell.Dispose();
|
|
|
|
|
PasswordHintCell.Dispose();
|
|
|
|
|
ConfirmPasswordCell.Dispose();
|
|
|
|
|
PasswordHintCell.Entry.Completed -= Entry_Completed;
|
2017-02-17 08:16:09 +03:00
|
|
|
|
StackLayout.LayoutChanged -= Layout_LayoutChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Layout_LayoutChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PasswordLabel.WidthRequest = StackLayout.Bounds.Width - PasswordLabel.Bounds.Left * 2;
|
|
|
|
|
HintLabel.WidthRequest = StackLayout.Bounds.Width - HintLabel.Bounds.Left * 2;
|
2017-02-15 08:28:05 +03:00
|
|
|
|
}
|
2016-06-26 03:54:17 +03:00
|
|
|
|
|
|
|
|
|
private async void Entry_Completed(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
await Register();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task Register()
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-06 18:57:05 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred,
|
2016-07-05 06:20:16 +03:00
|
|
|
|
string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress), AppResources.Ok);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-06 18:57:05 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred,
|
2016-10-04 06:20:17 +03:00
|
|
|
|
string.Format(AppResources.ValidationFieldRequired, AppResources.MasterPassword), AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-11 05:04:00 +03:00
|
|
|
|
if(PasswordCell.Entry.Text.Length < 8)
|
2016-10-04 06:20:17 +03:00
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, AppResources.MasterPasswordLengthValMessage,
|
|
|
|
|
AppResources.Ok);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ConfirmPasswordCell.Entry.Text != PasswordCell.Entry.Text)
|
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, AppResources.MasterPasswordLengthValMessage,
|
|
|
|
|
AppResources.Ok);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 04:43:15 +03:00
|
|
|
|
var normalizedEmail = EmailCell.Entry.Text.ToLower();
|
|
|
|
|
var key = _cryptoService.MakeKeyFromPassword(PasswordCell.Entry.Text, normalizedEmail);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
var request = new RegisterRequest
|
|
|
|
|
{
|
2016-08-14 04:43:15 +03:00
|
|
|
|
Email = normalizedEmail,
|
2016-06-26 03:54:17 +03:00
|
|
|
|
MasterPasswordHash = _cryptoService.HashPasswordBase64(key, PasswordCell.Entry.Text),
|
2016-08-17 02:20:41 +03:00
|
|
|
|
MasterPasswordHint = !string.IsNullOrWhiteSpace(PasswordHintCell.Entry.Text)
|
|
|
|
|
? PasswordHintCell.Entry.Text : null
|
2016-06-26 03:54:17 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.CreatingAccount, MaskType.Black);
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var response = await _accountsApiRepository.PostRegisterAsync(request);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
if(!response.Succeeded)
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message,
|
|
|
|
|
AppResources.Ok);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 07:32:37 +03:00
|
|
|
|
_googleAnalyticsService.TrackAppEvent("Registered");
|
2016-08-14 04:43:15 +03:00
|
|
|
|
await _homePage.DismissRegisterAndLoginAsync(normalizedEmail);
|
2016-06-26 03:54:17 +03:00
|
|
|
|
}
|
2016-07-04 09:45:32 +03:00
|
|
|
|
|
|
|
|
|
private class FormTableView : ExtendedTableView
|
|
|
|
|
{
|
|
|
|
|
public FormTableView()
|
|
|
|
|
{
|
|
|
|
|
Intent = TableIntent.Settings;
|
|
|
|
|
EnableScrolling = false;
|
|
|
|
|
HasUnevenRows = true;
|
2016-07-23 07:40:17 +03:00
|
|
|
|
EnableSelection = true;
|
2016-07-04 09:45:32 +03:00
|
|
|
|
VerticalOptions = LayoutOptions.Start;
|
2016-07-05 05:31:15 +03:00
|
|
|
|
NoFooter = true;
|
2016-07-04 09:45:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 03:54:17 +03:00
|
|
|
|
}
|
|
|
|
|
}
|