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

171 lines
5.7 KiB
C#
Raw Normal View History

2016-07-23 09:17:11 +03:00
using System;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Resources;
using Xamarin.Forms;
using XLabs.Ioc;
using Acr.UserDialogs;
using System.Threading.Tasks;
using PushNotification.Plugin.Abstractions;
using Bit.App.Models;
2016-07-23 09:17:11 +03:00
namespace Bit.App.Pages
{
public class LoginTwoFactorPage : ExtendedContentPage
{
private IAuthService _authService;
private IUserDialogs _userDialogs;
private ISyncService _syncService;
private IGoogleAnalyticsService _googleAnalyticsService;
private IPushNotification _pushNotification;
private readonly string _email;
private readonly string _masterPasswordHash;
private readonly CryptoKey _key;
public LoginTwoFactorPage(string email, string masterPasswordHash, CryptoKey key)
: base(updateActivity: false)
2016-07-23 09:17:11 +03:00
{
_email = email;
_masterPasswordHash = masterPasswordHash;
_key = key;
2016-07-23 09:17:11 +03:00
_authService = Resolver.Resolve<IAuthService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_syncService = Resolver.Resolve<ISyncService>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
_pushNotification = Resolver.Resolve<IPushNotification>();
2016-07-23 09:17:11 +03:00
Init();
}
public FormEntryCell CodeCell { get; set; }
private void Init()
{
var padding = Device.OnPlatform(
iOS: new Thickness(15, 20),
Android: new Thickness(15, 8),
WinPhone: new Thickness(15, 20));
CodeCell = new FormEntryCell(AppResources.VerificationCode, useLabelAsPlaceholder: true,
imageSource: "lock", containerPadding: padding);
2016-07-23 09:17:11 +03:00
CodeCell.Entry.Keyboard = Keyboard.Numeric;
CodeCell.Entry.ReturnType = Enums.ReturnType.Go;
var table = new ExtendedTableView
{
Intent = TableIntent.Settings,
EnableScrolling = false,
HasUnevenRows = true,
EnableSelection = true,
NoFooter = true,
VerticalOptions = LayoutOptions.Start,
Root = new TableRoot
{
new TableSection(" ")
2016-07-23 09:17:11 +03:00
{
CodeCell
}
}
};
var codeLabel = new Label
{
Text = AppResources.EnterVerificationCode,
2016-07-23 09:17:11 +03:00
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
Style = (Style)Application.Current.Resources["text-muted"],
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 25)
};
var lostAppButton = new ExtendedButton
{
Text = AppResources.Lost2FAApp,
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
Margin = new Thickness(15, 0, 15, 25),
Command = new Command(() => Lost2FAApp()),
Uppercase = false,
BackgroundColor = Color.Transparent
};
2016-07-23 09:17:11 +03:00
var layout = new StackLayout
{
Children = { table, codeLabel, lostAppButton },
2016-07-23 09:17:11 +03:00
Spacing = 0
};
var scrollView = new ScrollView { Content = layout };
if(Device.OS == TargetPlatform.iOS)
{
table.RowHeight = -1;
table.EstimatedRowHeight = 70;
}
var continueToolbarItem = new ToolbarItem(AppResources.Continue, null, async () =>
2016-07-23 09:17:11 +03:00
{
await LogInAsync();
2016-07-23 09:17:11 +03:00
}, ToolbarItemOrder.Default, 0);
ToolbarItems.Add(continueToolbarItem);
Title = AppResources.VerificationCode;
2016-07-23 09:17:11 +03:00
Content = scrollView;
}
protected override void OnAppearing()
{
base.OnAppearing();
CodeCell.InitEvents();
CodeCell.Entry.FocusWithDelay();
CodeCell.Entry.Completed += Entry_Completed;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
CodeCell.Dispose();
CodeCell.Entry.Completed -= Entry_Completed;
2016-07-23 09:17:11 +03:00
}
private void Lost2FAApp()
{
Device.OpenUri(new Uri("https://vault.bitwarden.com/#/recover"));
}
2016-07-23 09:17:11 +03:00
private async void Entry_Completed(object sender, EventArgs e)
{
await LogInAsync();
2016-07-23 09:17:11 +03:00
}
private async Task LogInAsync()
2016-07-23 09:17:11 +03:00
{
if(string.IsNullOrWhiteSpace(CodeCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.VerificationCode), AppResources.Ok);
2016-07-23 09:17:11 +03:00
return;
}
_userDialogs.ShowLoading(AppResources.ValidatingCode, MaskType.Black);
var response = await _authService.TokenPostTwoFactorAsync(CodeCell.Entry.Text, _email, _masterPasswordHash, _key);
2016-07-23 09:17:11 +03:00
_userDialogs.HideLoading();
if(!response.Success)
2016-07-23 09:17:11 +03:00
{
await DisplayAlert(AppResources.AnErrorHasOccurred, response.ErrorMessage, AppResources.Ok);
2016-07-23 09:17:11 +03:00
return;
}
_googleAnalyticsService.TrackAppEvent("LoggedIn From Two-step");
if(Device.OS == TargetPlatform.Android)
{
_pushNotification.Register();
}
2016-07-23 09:17:11 +03:00
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
2016-07-23 09:17:11 +03:00
Application.Current.MainPage = new MainPage();
}
}
}