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;
|
2017-05-30 21:13:53 +03:00
|
|
|
|
using Bit.App.Utilities;
|
2016-06-26 03:54:17 +03:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2017-05-30 21:13:53 +03:00
|
|
|
|
var padding = Helpers.OnPlatform(
|
2016-08-14 04:43:15 +03:00
|
|
|
|
iOS: new Thickness(15, 20),
|
|
|
|
|
Android: new Thickness(15, 8),
|
2017-12-11 07:26:58 +03:00
|
|
|
|
Windows: new Thickness(10, 8));
|
2016-07-03 07:27:10 +03:00
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
PasswordHintCell = new FormEntryCell(AppResources.MasterPasswordHint, useLabelAsPlaceholder: true,
|
2017-10-03 05:15:13 +03:00
|
|
|
|
imageSource: "lightbulb.png", containerPadding: padding);
|
2016-11-26 00:54:33 +03:00
|
|
|
|
ConfirmPasswordCell = new FormEntryCell(AppResources.RetypeMasterPassword, isPassword: true,
|
2017-10-03 05:15:13 +03:00
|
|
|
|
nextElement: PasswordHintCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock.png",
|
2016-08-17 02:20:41 +03:00
|
|
|
|
containerPadding: padding);
|
2016-11-09 05:29:24 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.MasterPassword, isPassword: true,
|
2017-10-03 05:15:13 +03:00
|
|
|
|
nextElement: ConfirmPasswordCell.Entry, useLabelAsPlaceholder: true, imageSource: "lock.png",
|
2016-08-17 02:20:41 +03:00
|
|
|
|
containerPadding: padding);
|
2016-07-04 09:45:32 +03:00
|
|
|
|
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
|
2017-10-03 05:15:13 +03:00
|
|
|
|
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope.png",
|
2016-08-17 02:20:41 +03:00
|
|
|
|
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-11-21 06:39:49 +03:00
|
|
|
|
new TableSection(Helpers.GetEmptyTableSectionTitle())
|
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-11-21 06:39:49 +03:00
|
|
|
|
new TableSection(Helpers.GetEmptyTableSectionTitle())
|
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
|
|
|
|
};
|
|
|
|
|
|
2017-12-11 23:51:16 +03:00
|
|
|
|
var loginToolbarItem = new ToolbarItem(AppResources.Submit, Helpers.ToolbarImage("ion_chevron_right.png"), async () =>
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
|
|
|
|
await Register();
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
2017-12-13 00:03:25 +03:00
|
|
|
|
if(Device.RuntimePlatform == Device.iOS)
|
2016-06-26 03:54:17 +03:00
|
|
|
|
{
|
2017-05-27 21:52:37 +03:00
|
|
|
|
table.RowHeight = table2.RowHeight = -1;
|
|
|
|
|
table.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);
|
2017-06-01 05:47:19 +03:00
|
|
|
|
var encKey = _cryptoService.MakeEncKey(key);
|
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)
|
2017-06-01 05:47:19 +03:00
|
|
|
|
? PasswordHintCell.Entry.Text : null,
|
|
|
|
|
Key = encKey.EncryptedString
|
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
|
|
|
|
}
|
|
|
|
|
}
|