bitwarden-android/src/App/Pages/LoginPage.cs

226 lines
8.4 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Linq;
using Bit.App.Abstractions;
using Bit.App.Controls;
2016-05-02 09:52:09 +03:00
using Bit.App.Models.Api;
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;
using Plugin.Settings.Abstractions;
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
{
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;
private ITokenService _tokenService;
private IDeviceInfoService _deviceInfoService;
2016-06-24 06:03:00 +03:00
private IAppIdService _appIdService;
private IUserDialogs _userDialogs;
private ISyncService _syncService;
private ISettings _settings;
private IGoogleAnalyticsService _googleAnalyticsService;
private IPushNotification _pushNotification;
private readonly string _email;
2016-06-24 06:03:00 +03:00
public LoginPage(string email = null)
: base(updateActivity: false)
2016-05-02 09:52:09 +03:00
{
_email = email;
2016-06-24 06:03:00 +03:00
_cryptoService = Resolver.Resolve<ICryptoService>();
_authService = Resolver.Resolve<IAuthService>();
_tokenService = Resolver.Resolve<ITokenService>();
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
2016-06-24 06:03:00 +03:00
_appIdService = Resolver.Resolve<IAppIdService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_syncService = Resolver.Resolve<ISyncService>();
_settings = Resolver.Resolve<ISettings>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
_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()
{
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
var padding = Device.OnPlatform(
iOS: new Thickness(15, 20),
Android: new Thickness(15, 8),
WinPhone: new Thickness(15, 20));
PasswordCell = new FormEntryCell(AppResources.MasterPassword, isPassword: true,
useLabelAsPlaceholder: true, imageSource: "lock", containerPadding: padding);
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope",
containerPadding: padding);
2016-05-02 09:52:09 +03:00
var lastLoginEmail = _settings.GetValueOrDefault(Constants.LastLoginEmail, string.Empty);
if(!string.IsNullOrWhiteSpace(_email))
{
EmailCell.Entry.Text = _email;
}
else if(!string.IsNullOrWhiteSpace(lastLoginEmail))
{
EmailCell.Entry.Text = lastLoginEmail;
}
2016-06-24 06:03:00 +03:00
PasswordCell.Entry.ReturnType = Enums.ReturnType.Go;
PasswordCell.Entry.Completed += Entry_Completed;
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
var forgotPasswordButton = new ExtendedButton
2016-06-24 06:03:00 +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),
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 },
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;
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel, () =>
{
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;
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();
MessagingCenter.Send(Application.Current, "ShowStatusBar", true);
if(string.IsNullOrWhiteSpace(_email))
{
if(!string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
{
PasswordCell.Entry.FocusWithDelay();
}
else
{
EmailCell.Entry.FocusWithDelay();
}
}
2016-06-24 06:03:00 +03:00
}
2016-05-02 09:52:09 +03:00
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 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))
{
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))
{
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
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
{
Email = normalizedEmail,
2016-06-24 06:03:00 +03:00
MasterPasswordHash = _cryptoService.HashPasswordBase64(key, PasswordCell.Entry.Text),
Device = new DeviceRequest(_appIdService, _deviceInfoService)
2016-05-02 09:52:09 +03:00
};
_userDialogs.ShowLoading(AppResources.LoggingIn, MaskType.Black);
var response = await _authService.TokenPostAsync(request);
2016-06-24 06:03:00 +03:00
_userDialogs.HideLoading();
if(!response.Succeeded)
{
2016-06-24 06:03:00 +03:00
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
return;
}
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;
_tokenService.Token = response.Result.AccessToken;
_tokenService.RefreshToken = response.Result.RefreshToken;
_authService.UserId = _tokenService.TokenUserId;
_authService.Email = _tokenService.TokenEmail;
_settings.AddOrUpdateValue(Constants.LastLoginEmail, _authService.Email);
_googleAnalyticsService.RefreshUserId();
2016-08-04 07:32:37 +03:00
_googleAnalyticsService.TrackAppEvent("LoggedIn");
2016-06-24 06:03:00 +03:00
if(Device.OS == TargetPlatform.Android)
{
_pushNotification.Register();
}
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
Application.Current.MainPage = new MainPage();
2016-05-02 09:52:09 +03:00
}
}
}