2016-07-23 09:17:11 +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
|
|
|
|
|
{
|
|
|
|
|
public class LoginTwoFactorPage : ExtendedContentPage
|
|
|
|
|
{
|
|
|
|
|
private ICryptoService _cryptoService;
|
|
|
|
|
private IAuthService _authService;
|
2016-08-24 05:43:17 +03:00
|
|
|
|
private IDeviceInfoService _deviceInfoService;
|
2016-07-23 09:17:11 +03:00
|
|
|
|
private IAppIdService _appIdService;
|
|
|
|
|
private IUserDialogs _userDialogs;
|
|
|
|
|
private ISyncService _syncService;
|
|
|
|
|
|
|
|
|
|
public LoginTwoFactorPage()
|
2016-08-09 02:00:36 +03:00
|
|
|
|
: base(updateActivity: false)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
|
|
|
|
_cryptoService = Resolver.Resolve<ICryptoService>();
|
|
|
|
|
_authService = Resolver.Resolve<IAuthService>();
|
2016-08-24 05:43:17 +03:00
|
|
|
|
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
|
2016-07-23 09:17:11 +03:00
|
|
|
|
_appIdService = Resolver.Resolve<IAppIdService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FormEntryCell CodeCell { get; set; }
|
|
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
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-11-26 00:54:33 +03:00
|
|
|
|
CodeCell = new FormEntryCell(AppResources.VerificationCode, useLabelAsPlaceholder: true,
|
2016-08-14 04:43:15 +03:00
|
|
|
|
imageSource: "lock", containerPadding: padding);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
CodeCell.Entry.Keyboard = Keyboard.Numeric;
|
|
|
|
|
CodeCell.Entry.ReturnType = Enums.ReturnType.Go;
|
|
|
|
|
CodeCell.Entry.Completed += Entry_Completed;
|
|
|
|
|
|
|
|
|
|
var table = new ExtendedTableView
|
|
|
|
|
{
|
|
|
|
|
Intent = TableIntent.Settings,
|
|
|
|
|
EnableScrolling = false,
|
|
|
|
|
HasUnevenRows = true,
|
|
|
|
|
EnableSelection = true,
|
|
|
|
|
NoFooter = true,
|
|
|
|
|
VerticalOptions = LayoutOptions.Start,
|
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
|
|
|
|
new TableSection()
|
|
|
|
|
{
|
|
|
|
|
CodeCell
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var codeLabel = new Label
|
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
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 layout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
Children = { table, codeLabel },
|
|
|
|
|
Spacing = 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var scrollView = new ScrollView { Content = layout };
|
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = 70;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
var continueToolbarItem = new ToolbarItem(AppResources.Continue, null, async () =>
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
|
|
|
|
await LogIn();
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
|
|
|
|
ToolbarItems.Add(continueToolbarItem);
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Title = AppResources.VerificationCode;
|
2016-07-23 09:17:11 +03:00
|
|
|
|
Content = scrollView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2016-08-19 07:27:37 +03:00
|
|
|
|
CodeCell.Entry.FocusWithDelay();
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void Entry_Completed(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
await LogIn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task LogIn()
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(CodeCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
2016-11-26 00:54:33 +03:00
|
|
|
|
AppResources.VerificationCode), AppResources.Ok);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var request = new TokenTwoFactorRequest
|
|
|
|
|
{
|
|
|
|
|
Code = CodeCell.Entry.Text,
|
|
|
|
|
Provider = "Authenticator",
|
2016-08-24 05:43:17 +03:00
|
|
|
|
Device = new DeviceRequest(_appIdService, _deviceInfoService)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.ValidatingCode, MaskType.Black);
|
2016-08-17 02:20:41 +03:00
|
|
|
|
var response = await _authService.TokenTwoFactorPostAsync(request);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
if(!response.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_authService.Token = response.Result.Token;
|
|
|
|
|
_authService.UserId = response.Result.Profile.Id;
|
|
|
|
|
_authService.Email = response.Result.Profile.Email;
|
|
|
|
|
|
|
|
|
|
var task = Task.Run(async () => await _syncService.FullSyncAsync());
|
|
|
|
|
Application.Current.MainPage = new MainPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|