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;
|
2017-02-06 17:39:07 +03:00
|
|
|
|
using PushNotification.Plugin.Abstractions;
|
2017-04-20 06:16:09 +03:00
|
|
|
|
using Bit.App.Models;
|
2017-05-30 21:13:53 +03:00
|
|
|
|
using Bit.App.Utilities;
|
2017-06-27 23:18:32 +03:00
|
|
|
|
using Bit.App.Enums;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
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;
|
2017-02-06 17:39:07 +03:00
|
|
|
|
private IGoogleAnalyticsService _googleAnalyticsService;
|
|
|
|
|
private IPushNotification _pushNotification;
|
|
|
|
|
private readonly string _email;
|
|
|
|
|
private readonly string _masterPasswordHash;
|
2017-04-22 21:36:31 +03:00
|
|
|
|
private readonly SymmetricCryptoKey _key;
|
2017-06-27 23:18:32 +03:00
|
|
|
|
private readonly Dictionary<TwoFactorProviderType, Dictionary<string, object>> _providers;
|
|
|
|
|
private readonly TwoFactorProviderType? _providerType;
|
2017-06-28 00:10:40 +03:00
|
|
|
|
private readonly FullLoginResult _result;
|
2017-02-06 17:39:07 +03:00
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
public LoginTwoFactorPage(string email, FullLoginResult result, TwoFactorProviderType? type = null)
|
2016-08-09 02:00:36 +03:00
|
|
|
|
: base(updateActivity: false)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-02-06 17:39:07 +03:00
|
|
|
|
_email = email;
|
2017-06-28 00:10:40 +03:00
|
|
|
|
_result = result;
|
2017-06-27 23:18:32 +03:00
|
|
|
|
_masterPasswordHash = result.MasterPasswordHash;
|
|
|
|
|
_key = result.Key;
|
|
|
|
|
_providers = result.TwoFactorProviders;
|
|
|
|
|
_providerType = type ?? GetDefaultProvider();
|
2017-02-06 17:39:07 +03:00
|
|
|
|
|
2016-07-23 09:17:11 +03:00
|
|
|
|
_authService = Resolver.Resolve<IAuthService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
2017-02-06 17:39:07 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
|
|
|
|
_pushNotification = Resolver.Resolve<IPushNotification>();
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
public FormEntryCell TokenCell { get; set; }
|
|
|
|
|
public ExtendedSwitchCell RememberCell { get; set; }
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var scrollView = new ScrollView();
|
2016-08-14 04:43:15 +03:00
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var continueToolbarItem = new ToolbarItem(AppResources.Continue, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
var token = TokenCell?.Entry.Text.Trim().Replace(" ", "");
|
|
|
|
|
await LogInAsync(token);
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
if(!_providerType.HasValue)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var noProviderLabel = new Label
|
|
|
|
|
{
|
|
|
|
|
Text = "No provider.",
|
|
|
|
|
LineBreakMode = LineBreakMode.WordWrap,
|
|
|
|
|
Margin = new Thickness(15),
|
|
|
|
|
HorizontalTextAlignment = TextAlignment.Center
|
|
|
|
|
};
|
|
|
|
|
scrollView.Content = noProviderLabel;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var padding = Helpers.OnPlatform(
|
|
|
|
|
iOS: new Thickness(15, 20),
|
|
|
|
|
Android: new Thickness(15, 8),
|
|
|
|
|
WinPhone: new Thickness(15, 20));
|
|
|
|
|
|
|
|
|
|
TokenCell = new FormEntryCell(AppResources.VerificationCode, useLabelAsPlaceholder: true,
|
|
|
|
|
imageSource: "lock", containerPadding: padding);
|
|
|
|
|
|
|
|
|
|
TokenCell.Entry.Keyboard = Keyboard.Numeric;
|
|
|
|
|
TokenCell.Entry.ReturnType = ReturnType.Go;
|
|
|
|
|
|
|
|
|
|
RememberCell = new ExtendedSwitchCell
|
|
|
|
|
{
|
|
|
|
|
Text = "Remember me",
|
|
|
|
|
On = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var table = new ExtendedTableView
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-06-27 23:18:32 +03:00
|
|
|
|
Intent = TableIntent.Settings,
|
|
|
|
|
EnableScrolling = false,
|
|
|
|
|
HasUnevenRows = true,
|
|
|
|
|
EnableSelection = true,
|
|
|
|
|
NoFooter = true,
|
|
|
|
|
NoHeader = true,
|
|
|
|
|
VerticalOptions = LayoutOptions.Start,
|
|
|
|
|
Root = new TableRoot
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-06-27 23:18:32 +03:00
|
|
|
|
new TableSection(" ")
|
|
|
|
|
{
|
|
|
|
|
TokenCell,
|
|
|
|
|
RememberCell
|
|
|
|
|
}
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
2017-06-27 23:18:32 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(Device.RuntimePlatform == Device.iOS)
|
|
|
|
|
{
|
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = 70;
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var instruction = new Label
|
|
|
|
|
{
|
|
|
|
|
Text = AppResources.EnterVerificationCode,
|
|
|
|
|
LineBreakMode = LineBreakMode.WordWrap,
|
|
|
|
|
Margin = new Thickness(15),
|
|
|
|
|
HorizontalTextAlignment = TextAlignment.Center
|
|
|
|
|
};
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var anotherMethodButton = new ExtendedButton
|
|
|
|
|
{
|
|
|
|
|
Text = "Use another two-step login method",
|
|
|
|
|
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
|
|
|
|
|
Margin = new Thickness(15, 0, 15, 25),
|
2017-06-28 00:10:40 +03:00
|
|
|
|
Command = new Command(() => AnotherMethodAsync()),
|
2017-06-27 23:18:32 +03:00
|
|
|
|
Uppercase = false,
|
|
|
|
|
BackgroundColor = Color.Transparent
|
|
|
|
|
};
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var layout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
Children = { instruction, table, anotherMethodButton },
|
|
|
|
|
Spacing = 0
|
|
|
|
|
};
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
scrollView.Content = layout;
|
|
|
|
|
|
|
|
|
|
switch(_providerType.Value)
|
|
|
|
|
{
|
|
|
|
|
case TwoFactorProviderType.Authenticator:
|
|
|
|
|
instruction.Text = "Enter the 6 digit verification code from your authenticator app.";
|
|
|
|
|
layout.Children.Add(instruction);
|
|
|
|
|
layout.Children.Add(table);
|
|
|
|
|
layout.Children.Add(anotherMethodButton);
|
|
|
|
|
|
|
|
|
|
ToolbarItems.Add(continueToolbarItem);
|
|
|
|
|
Title = AppResources.VerificationCode;
|
|
|
|
|
break;
|
|
|
|
|
case TwoFactorProviderType.Email:
|
|
|
|
|
var emailParams = _providers[TwoFactorProviderType.Email];
|
|
|
|
|
var redactedEmail = emailParams["Email"].ToString();
|
|
|
|
|
|
|
|
|
|
instruction.Text = "Enter the 6 digit verification code from your authenticator app.";
|
|
|
|
|
var resendEmailButton = new ExtendedButton
|
|
|
|
|
{
|
|
|
|
|
Text = $"Enter the 6 digit verification code that was emailed to {redactedEmail}.",
|
|
|
|
|
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
|
|
|
|
|
Margin = new Thickness(15, 0, 15, 25),
|
|
|
|
|
Command = new Command(() => SendEmail()),
|
|
|
|
|
Uppercase = false,
|
|
|
|
|
BackgroundColor = Color.Transparent
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
layout.Children.Add(instruction);
|
|
|
|
|
layout.Children.Add(table);
|
|
|
|
|
layout.Children.Add(resendEmailButton);
|
|
|
|
|
layout.Children.Add(anotherMethodButton);
|
|
|
|
|
|
|
|
|
|
ToolbarItems.Add(continueToolbarItem);
|
|
|
|
|
Title = AppResources.VerificationCode;
|
|
|
|
|
break;
|
|
|
|
|
case TwoFactorProviderType.Duo:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
Content = scrollView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2017-06-27 23:18:32 +03:00
|
|
|
|
|
|
|
|
|
if(TokenCell != null)
|
|
|
|
|
{
|
|
|
|
|
TokenCell.InitEvents();
|
|
|
|
|
TokenCell.Entry.FocusWithDelay();
|
|
|
|
|
TokenCell.Entry.Completed += Entry_Completed;
|
|
|
|
|
}
|
2017-02-15 08:28:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
2017-06-27 23:18:32 +03:00
|
|
|
|
|
|
|
|
|
if(TokenCell != null)
|
|
|
|
|
{
|
|
|
|
|
TokenCell.Dispose();
|
|
|
|
|
TokenCell.Entry.Completed -= Entry_Completed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 00:10:40 +03:00
|
|
|
|
private async void AnotherMethodAsync()
|
2017-06-27 23:18:32 +03:00
|
|
|
|
{
|
2017-06-28 00:10:40 +03:00
|
|
|
|
await Navigation.PushForDeviceAsync(new TwoFactorMethodsPage(_email, _result));
|
2017-06-27 23:18:32 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SendEmail()
|
|
|
|
|
{
|
|
|
|
|
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
private void Recover()
|
2016-11-26 09:03:02 +03:00
|
|
|
|
{
|
2017-05-26 06:33:50 +03:00
|
|
|
|
Device.OpenUri(new Uri("https://help.bitwarden.com/article/lost-two-step-device/"));
|
2016-11-26 09:03:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-23 09:17:11 +03:00
|
|
|
|
private async void Entry_Completed(object sender, EventArgs e)
|
|
|
|
|
{
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var token = TokenCell.Entry.Text.Trim().Replace(" ", "");
|
|
|
|
|
await LogInAsync(token);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 23:18:32 +03:00
|
|
|
|
private async Task LogInAsync(string token)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-06-27 23:18:32 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(token))
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.ValidatingCode, MaskType.Black);
|
2017-06-27 23:18:32 +03:00
|
|
|
|
var response = await _authService.TokenPostTwoFactorAsync(_providerType.Value, token, RememberCell.On,
|
|
|
|
|
_email, _masterPasswordHash, _key);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
2017-04-20 05:04:43 +03:00
|
|
|
|
if(!response.Success)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-04-20 05:04:43 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, response.ErrorMessage, AppResources.Ok);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 17:39:07 +03:00
|
|
|
|
_googleAnalyticsService.TrackAppEvent("LoggedIn From Two-step");
|
|
|
|
|
|
2017-05-30 21:13:53 +03:00
|
|
|
|
if(Device.RuntimePlatform == Device.Android)
|
2017-02-06 17:39:07 +03:00
|
|
|
|
{
|
|
|
|
|
_pushNotification.Register();
|
|
|
|
|
}
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
2017-02-07 07:40:24 +03:00
|
|
|
|
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
|
2016-07-23 09:17:11 +03:00
|
|
|
|
Application.Current.MainPage = new MainPage();
|
|
|
|
|
}
|
2017-06-27 23:18:32 +03:00
|
|
|
|
|
|
|
|
|
private TwoFactorProviderType? GetDefaultProvider()
|
|
|
|
|
{
|
|
|
|
|
TwoFactorProviderType? provider = null;
|
|
|
|
|
|
|
|
|
|
if(_providers != null)
|
|
|
|
|
{
|
|
|
|
|
if(_providers.Count == 1)
|
|
|
|
|
{
|
|
|
|
|
return _providers.First().Key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach(var p in _providers)
|
|
|
|
|
{
|
|
|
|
|
switch(p.Key)
|
|
|
|
|
{
|
|
|
|
|
case TwoFactorProviderType.Authenticator:
|
|
|
|
|
if(provider == TwoFactorProviderType.Duo)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case TwoFactorProviderType.Email:
|
|
|
|
|
if(provider.HasValue)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case TwoFactorProviderType.Duo:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
provider = p.Key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return provider;
|
|
|
|
|
}
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
|
|
|
|
}
|