2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Bit.App.Abstractions;
|
2016-06-22 06:54:51 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Bit.App.Models.Api;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
using Bit.App.Resources;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-07-30 20:28:13 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-08-21 02:19:10 +03:00
|
|
|
|
using PushNotification.Plugin.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
|
|
|
|
{
|
2016-06-28 02:53:31 +03:00
|
|
|
|
public class LoginPage : ExtendedContentPage
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private ICryptoService _cryptoService;
|
|
|
|
|
private IAuthService _authService;
|
2017-02-04 09:12:25 +03:00
|
|
|
|
private ITokenService _tokenService;
|
2016-08-24 05:43:17 +03:00
|
|
|
|
private IDeviceInfoService _deviceInfoService;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private IAppIdService _appIdService;
|
|
|
|
|
private IUserDialogs _userDialogs;
|
2016-07-01 03:08:34 +03:00
|
|
|
|
private ISyncService _syncService;
|
2016-07-30 20:28:13 +03:00
|
|
|
|
private ISettings _settings;
|
2016-08-04 07:06:09 +03:00
|
|
|
|
private IGoogleAnalyticsService _googleAnalyticsService;
|
2016-08-21 02:19:10 +03:00
|
|
|
|
private IPushNotification _pushNotification;
|
2016-08-06 18:57:05 +03:00
|
|
|
|
private readonly string _email;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
|
2016-08-06 18:57:05 +03:00
|
|
|
|
public LoginPage(string email = null)
|
2016-08-09 02:00:36 +03:00
|
|
|
|
: base(updateActivity: false)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-08-06 18:57:05 +03:00
|
|
|
|
_email = email;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_cryptoService = Resolver.Resolve<ICryptoService>();
|
|
|
|
|
_authService = Resolver.Resolve<IAuthService>();
|
2017-02-04 09:12:25 +03:00
|
|
|
|
_tokenService = Resolver.Resolve<ITokenService>();
|
2016-08-24 05:43:17 +03:00
|
|
|
|
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_appIdService = Resolver.Resolve<IAppIdService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
2016-07-01 03:08:34 +03:00
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
2016-07-30 20:28:13 +03:00
|
|
|
|
_settings = Resolver.Resolve<ISettings>();
|
2016-08-04 07:06:09 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-08-21 02:19:10 +03:00
|
|
|
|
_pushNotification = Resolver.Resolve<IPushNotification>();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Init();
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
public FormEntryCell PasswordCell { get; set; }
|
|
|
|
|
public FormEntryCell EmailCell { get; set; }
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +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-09 05:29:24 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.MasterPassword, isPassword: true,
|
2016-07-03 07:27:10 +03:00
|
|
|
|
useLabelAsPlaceholder: true, imageSource: "lock", containerPadding: padding);
|
2016-06-28 05:17:49 +03:00
|
|
|
|
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
|
2016-07-03 07:27:10 +03:00
|
|
|
|
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope",
|
|
|
|
|
containerPadding: padding);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var lastLoginEmail = _settings.GetValueOrDefault(Constants.LastLoginEmail, string.Empty);
|
2016-08-06 18:57:05 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(_email))
|
|
|
|
|
{
|
|
|
|
|
EmailCell.Entry.Text = _email;
|
|
|
|
|
}
|
|
|
|
|
else if(!string.IsNullOrWhiteSpace(lastLoginEmail))
|
2016-07-30 20:28:13 +03:00
|
|
|
|
{
|
|
|
|
|
EmailCell.Entry.Text = lastLoginEmail;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
PasswordCell.Entry.ReturnType = Enums.ReturnType.Go;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
var table = new ExtendedTableView
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Intent = TableIntent.Settings,
|
2016-07-23 07:40:17 +03:00
|
|
|
|
EnableScrolling = false,
|
2016-06-24 06:03:00 +03:00
|
|
|
|
HasUnevenRows = true,
|
2016-07-23 07:40:17 +03:00
|
|
|
|
EnableSelection = true,
|
|
|
|
|
NoFooter = true,
|
|
|
|
|
VerticalOptions = LayoutOptions.Start,
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Root = new TableRoot
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
new TableSection()
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
EmailCell,
|
|
|
|
|
PasswordCell
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-08-18 07:20:21 +03:00
|
|
|
|
var forgotPasswordButton = new ExtendedButton
|
2016-06-24 06:03:00 +03:00
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Text = AppResources.GetPasswordHint,
|
2016-07-23 07:40:17 +03:00
|
|
|
|
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
|
|
|
|
|
Margin = new Thickness(15, 0, 15, 25),
|
2016-08-18 07:20:21 +03:00
|
|
|
|
Command = new Command(async () => await ForgotPasswordAsync()),
|
|
|
|
|
Uppercase = false,
|
|
|
|
|
BackgroundColor = Color.Transparent
|
2016-07-23 07:40:17 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var layout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
Children = { table, forgotPasswordButton },
|
2016-08-18 15:43:55 +03:00
|
|
|
|
Spacing = Device.OnPlatform(iOS: 0, Android: 10, WinPhone: 0)
|
2016-07-23 07:40:17 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var scrollView = new ScrollView { Content = layout };
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.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-24 06:03:00 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-07-23 07:40:17 +03:00
|
|
|
|
var loginToolbarItem = new ToolbarItem(AppResources.LogIn, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
await LogIn();
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
ToolbarItems.Add(loginToolbarItem);
|
|
|
|
|
Title = AppResources.Bitwarden;
|
2016-07-23 07:40:17 +03:00
|
|
|
|
Content = scrollView;
|
2016-11-26 00:54:33 +03:00
|
|
|
|
NavigationPage.SetBackButtonTitle(this, AppResources.LogIn);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2017-02-15 08:28:05 +03:00
|
|
|
|
PasswordCell.InitEvents();
|
|
|
|
|
EmailCell.InitEvents();
|
|
|
|
|
|
|
|
|
|
PasswordCell.Entry.Completed += Entry_Completed;
|
2016-07-23 22:32:11 +03:00
|
|
|
|
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
|
2016-07-30 20:28:13 +03:00
|
|
|
|
|
2016-08-06 20:14:58 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(_email))
|
2016-07-30 20:28:13 +03:00
|
|
|
|
{
|
2016-08-06 20:14:58 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-19 07:27:37 +03:00
|
|
|
|
PasswordCell.Entry.FocusWithDelay();
|
2016-08-06 20:14:58 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-19 07:27:37 +03:00
|
|
|
|
EmailCell.Entry.FocusWithDelay();
|
2016-08-06 20:14:58 +03:00
|
|
|
|
}
|
2016-07-30 20:28:13 +03:00
|
|
|
|
}
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
|
|
|
|
PasswordCell.Dispose();
|
|
|
|
|
EmailCell.Dispose();
|
|
|
|
|
PasswordCell.Entry.Completed -= Entry_Completed;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 06:06:33 +03:00
|
|
|
|
private async void Entry_Completed(object sender, EventArgs e)
|
2016-06-24 06:03:00 +03:00
|
|
|
|
{
|
2016-06-24 06:06:33 +03:00
|
|
|
|
await LogIn();
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-07-23 22:32:11 +03:00
|
|
|
|
|
2016-07-23 07:40:17 +03:00
|
|
|
|
private async Task ForgotPasswordAsync()
|
|
|
|
|
{
|
|
|
|
|
await Navigation.PushAsync(new PasswordHintPage());
|
|
|
|
|
}
|
2016-05-03 00:50:16 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private async Task LogIn()
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
|
|
|
|
AppResources.EmailAddress), AppResources.Ok);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
|
|
|
|
AppResources.MasterPassword), AppResources.Ok);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-08-14 04:43:15 +03:00
|
|
|
|
var normalizedEmail = EmailCell.Entry.Text.ToLower();
|
|
|
|
|
|
|
|
|
|
var key = _cryptoService.MakeKeyFromPassword(PasswordCell.Entry.Text, normalizedEmail);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
|
|
|
|
|
var request = new TokenRequest
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-08-14 04:43:15 +03:00
|
|
|
|
Email = normalizedEmail,
|
2016-06-24 06:03:00 +03:00
|
|
|
|
MasterPasswordHash = _cryptoService.HashPasswordBase64(key, PasswordCell.Entry.Text),
|
2016-08-24 05:43:17 +03:00
|
|
|
|
Device = new DeviceRequest(_appIdService, _deviceInfoService)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.LoggingIn, MaskType.Black);
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var response = await _authService.TokenPostAsync(request);
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
if(!response.Succeeded)
|
2016-06-22 06:54:51 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
|
|
|
|
|
return;
|
2016-06-22 06:54:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 17:39:07 +03:00
|
|
|
|
if(response.Result.TwoFactorProviders != null && response.Result.TwoFactorProviders.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
_googleAnalyticsService.TrackAppEvent("LoggedIn To Two-step");
|
|
|
|
|
await Navigation.PushAsync(new LoginTwoFactorPage(request.Email, request.MasterPasswordHash, key));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_cryptoService.Key = key;
|
2017-02-04 09:12:25 +03:00
|
|
|
|
_tokenService.Token = response.Result.AccessToken;
|
|
|
|
|
_tokenService.RefreshToken = response.Result.RefreshToken;
|
|
|
|
|
_authService.UserId = _tokenService.TokenUserId;
|
|
|
|
|
_authService.Email = _tokenService.TokenEmail;
|
2016-08-06 06:58:31 +03:00
|
|
|
|
_settings.AddOrUpdateValue(Constants.LastLoginEmail, _authService.Email);
|
2016-08-04 07:32:37 +03:00
|
|
|
|
_googleAnalyticsService.TrackAppEvent("LoggedIn");
|
2016-06-24 06:03:00 +03:00
|
|
|
|
|
2016-08-21 02:19:10 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.Android)
|
|
|
|
|
{
|
|
|
|
|
_pushNotification.Register();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-07 07:40:24 +03:00
|
|
|
|
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
|
2017-02-06 17:39:07 +03:00
|
|
|
|
Application.Current.MainPage = new MainPage();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|