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

547 lines
19 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 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;
2017-06-28 17:09:52 +03:00
using System.Net;
2017-06-29 05:24:04 +03:00
using FFImageLoading.Forms;
2016-07-23 09:17:11 +03:00
namespace Bit.App.Pages
{
public class LoginTwoFactorPage : ExtendedContentPage
{
2017-06-29 05:24:04 +03:00
private DateTime? _lastAction;
2016-07-23 09:17:11 +03:00
private IAuthService _authService;
private IUserDialogs _userDialogs;
private ISyncService _syncService;
2017-06-29 05:24:04 +03:00
private IDeviceInfoService _deviceInfoService;
private IGoogleAnalyticsService _googleAnalyticsService;
private ITwoFactorApiRepository _twoFactorApiRepository;
private IPushNotification _pushNotification;
private IAppSettingsService _appSettingsService;
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 TwoFactorProviderType? _providerType;
2017-06-28 00:10:40 +03:00
private readonly FullLoginResult _result;
2017-06-27 23:18:32 +03:00
public LoginTwoFactorPage(string email, FullLoginResult result, TwoFactorProviderType? type = null)
: base(updateActivity: false)
2016-07-23 09:17:11 +03:00
{
2017-06-29 05:24:04 +03:00
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
_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();
2016-07-23 09:17:11 +03:00
_authService = Resolver.Resolve<IAuthService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_syncService = Resolver.Resolve<ISyncService>();
_appSettingsService = Resolver.Resolve<IAppSettingsService>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
_twoFactorApiRepository = Resolver.Resolve<ITwoFactorApiRepository>();
_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()
{
SubscribeYubiKey(true);
if(_providers.Count > 1)
{
var sendEmailTask = SendEmailAsync(false);
}
ToolbarItems.Clear();
2017-06-27 23:18:32 +03:00
var scrollView = new ScrollView();
2017-06-29 05:24:04 +03:00
var anotherMethodButton = new ExtendedButton
{
Text = AppResources.UseAnotherTwoStepMethod,
2017-06-29 05:24:04 +03:00
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
Margin = new Thickness(15, 0, 15, 25),
Command = new Command(() => AnotherMethodAsync()),
Uppercase = false,
BackgroundColor = Color.Transparent,
VerticalOptions = LayoutOptions.Start
2017-06-29 05:24:04 +03:00
};
var instruction = new Label
{
LineBreakMode = LineBreakMode.WordWrap,
Margin = new Thickness(15),
HorizontalTextAlignment = TextAlignment.Center
};
RememberCell = new ExtendedSwitchCell
{
Text = AppResources.RememberMe,
2017-06-29 05:24:04 +03:00
On = false
};
2017-06-27 23:18:32 +03:00
if(!_providerType.HasValue)
2016-07-23 09:17:11 +03:00
{
instruction.Text = AppResources.NoTwoStepAvailable;
2017-06-29 05:24:04 +03:00
var layout = new StackLayout
2017-06-27 23:18:32 +03:00
{
2017-06-29 05:24:04 +03:00
Children = { instruction, anotherMethodButton },
Spacing = 0
2017-06-27 23:18:32 +03:00
};
2017-06-29 05:24:04 +03:00
scrollView.Content = layout;
Title = AppResources.LoginUnavailable;
2017-06-29 05:24:04 +03:00
Content = scrollView;
2017-06-27 23:18:32 +03:00
}
2017-06-28 17:09:52 +03:00
else if(_providerType.Value == TwoFactorProviderType.Authenticator ||
_providerType.Value == TwoFactorProviderType.Email)
2017-06-27 23:18:32 +03:00
{
2017-06-28 17:09:52 +03:00
var continueToolbarItem = new ToolbarItem(AppResources.Continue, null, async () =>
{
var token = TokenCell?.Entry.Text.Trim().Replace(" ", "");
await LogInAsync(token);
2017-06-28 17:09:52 +03:00
}, ToolbarItemOrder.Default, 0);
2017-06-27 23:18:32 +03:00
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;
2017-06-29 05:24:04 +03:00
var table = new TwoFactorTable(
new TableSection(" ")
2016-07-23 09:17:11 +03:00
{
2017-06-29 05:24:04 +03:00
TokenCell,
RememberCell
});
2016-07-23 09:17:11 +03:00
2017-06-27 23:18:32 +03:00
var layout = new StackLayout
{
Children = { instruction, table },
2017-06-27 23:18:32 +03:00
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 = AppResources.EnterVerificationCodeApp;
2017-06-27 23:18:32 +03:00
layout.Children.Add(anotherMethodButton);
break;
case TwoFactorProviderType.Email:
var emailParams = _providers[TwoFactorProviderType.Email];
var redactedEmail = emailParams["Email"].ToString();
instruction.Text = string.Format(AppResources.EnterVerificationCodeEmail, redactedEmail);
2017-06-27 23:18:32 +03:00
var resendEmailButton = new ExtendedButton
{
Text = AppResources.SendVerificationCodeAgain,
2017-06-27 23:18:32 +03:00
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
Margin = new Thickness(15, 0, 15, 0),
Command = new Command(async () => await SendEmailAsync(true)),
2017-06-27 23:18:32 +03:00
Uppercase = false,
BackgroundColor = Color.Transparent,
VerticalOptions = LayoutOptions.Start
2017-06-27 23:18:32 +03:00
};
layout.Children.Add(resendEmailButton);
layout.Children.Add(anotherMethodButton);
break;
default:
break;
}
2017-06-28 17:09:52 +03:00
ToolbarItems.Add(continueToolbarItem);
Title = AppResources.VerificationCode;
Content = scrollView;
TokenCell.Entry.FocusWithDelay();
2017-06-27 23:18:32 +03:00
}
2017-06-28 17:09:52 +03:00
else if(_providerType == TwoFactorProviderType.Duo)
{
var duoParams = _providers[TwoFactorProviderType.Duo];
var host = WebUtility.UrlEncode(duoParams["Host"].ToString());
var req = WebUtility.UrlEncode(duoParams["Signature"].ToString());
2016-07-23 09:17:11 +03:00
2017-08-29 01:08:26 +03:00
var webVaultUrl = "https://vault.bitwarden.com";
if(!string.IsNullOrWhiteSpace(_appSettingsService.BaseUrl))
{
2017-08-29 01:08:26 +03:00
webVaultUrl = _appSettingsService.BaseUrl;
}
2017-08-29 01:08:26 +03:00
else if(!string.IsNullOrWhiteSpace(_appSettingsService.WebVaultUrl))
{
2017-08-29 01:08:26 +03:00
webVaultUrl = _appSettingsService.WebVaultUrl;
}
2017-06-29 05:24:04 +03:00
var webView = new HybridWebView
2017-06-28 17:09:52 +03:00
{
2017-08-29 01:08:26 +03:00
Uri = $"{webVaultUrl}/duo-connector.html?host={host}&request={req}",
2017-06-28 17:09:52 +03:00
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
MinimumHeightRequest = 400
2017-06-28 17:09:52 +03:00
};
2017-06-29 05:24:04 +03:00
webView.RegisterAction(async (sig) =>
2017-06-28 17:09:52 +03:00
{
await LogInAsync(sig);
2017-06-28 17:09:52 +03:00
});
var table = new TwoFactorTable(
new TableSection(" ")
{
RememberCell
});
var layout = new StackLayout
{
Children = { webView, table, anotherMethodButton },
Spacing = 0
};
scrollView.Content = layout;
2017-06-28 17:09:52 +03:00
Title = "Duo";
Content = scrollView;
2017-06-29 05:24:04 +03:00
}
else if(_providerType == TwoFactorProviderType.YubiKey)
{
instruction.Text = AppResources.YubiKeyInstruction;
2017-06-29 05:24:04 +03:00
var image = new CachedImage
{
Source = "yubikey",
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Center,
WidthRequest = 266,
HeightRequest = 160,
Margin = new Thickness(0, 0, 0, 25)
};
var table = new TwoFactorTable(
new TableSection(" ")
{
RememberCell
});
var layout = new StackLayout
{
Children = { instruction, image, table, anotherMethodButton },
Spacing = 0
};
scrollView.Content = layout;
Title = AppResources.YubiKeyTitle;
2017-06-29 05:24:04 +03:00
Content = scrollView;
2017-06-28 17:09:52 +03:00
}
2016-07-23 09:17:11 +03:00
}
protected override void OnAppearing()
{
base.OnAppearing();
2017-06-29 05:24:04 +03:00
ListenYubiKey(true);
2017-06-27 23:18:32 +03:00
InitEvents();
if(TokenCell == null && Device.RuntimePlatform == Device.Android)
{
MessagingCenter.Send(Application.Current, "DismissKeyboard");
}
}
private void InitEvents()
{
2017-06-27 23:18:32 +03:00
if(TokenCell != null)
{
TokenCell.InitEvents();
TokenCell.Entry.Completed += Entry_Completed;
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
2017-06-29 05:24:04 +03:00
ListenYubiKey(false);
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
{
var beforeProviderType = _providerType;
2017-06-27 23:18:32 +03:00
var options = new List<string>();
if(_providers.ContainsKey(TwoFactorProviderType.Authenticator))
{
options.Add(AppResources.AuthenticatorAppTitle);
}
if(_providers.ContainsKey(TwoFactorProviderType.Duo))
{
options.Add("Duo");
}
if(_providers.ContainsKey(TwoFactorProviderType.YubiKey))
{
var nfcKey = _providers[TwoFactorProviderType.YubiKey].ContainsKey("Nfc") &&
(bool)_providers[TwoFactorProviderType.YubiKey]["Nfc"];
2017-06-29 19:42:59 +03:00
if(_deviceInfoService.NfcEnabled && nfcKey)
{
options.Add(AppResources.YubiKeyTitle);
}
}
2017-06-27 23:18:32 +03:00
if(_providers.ContainsKey(TwoFactorProviderType.Email))
{
options.Add(AppResources.Email);
}
options.Add(AppResources.RecoveryCodeTitle);
var selection = await DisplayActionSheet(AppResources.TwoStepLoginOptions, AppResources.Cancel, null,
options.ToArray());
if(selection == AppResources.AuthenticatorAppTitle)
{
_providerType = TwoFactorProviderType.Authenticator;
}
else if(selection == "Duo")
{
_providerType = TwoFactorProviderType.Duo;
}
else if(selection == AppResources.YubiKeyTitle)
{
_providerType = TwoFactorProviderType.YubiKey;
}
else if(selection == AppResources.Email)
{
_providerType = TwoFactorProviderType.Email;
}
else if(selection == AppResources.RecoveryCodeTitle)
{
Device.OpenUri(new Uri("https://help.bitwarden.com/article/lost-two-step-device/"));
return;
}
if(beforeProviderType != _providerType)
{
Init();
ListenYubiKey(false, beforeProviderType == TwoFactorProviderType.YubiKey);
ListenYubiKey(true);
InitEvents();
}
2016-07-23 09:17:11 +03:00
}
private async Task SendEmailAsync(bool doToast)
{
if(_providerType != TwoFactorProviderType.Email)
{
return;
}
var response = await _twoFactorApiRepository.PostSendEmailLoginAsync(new Models.Api.TwoFactorEmailRequest
{
Email = _email,
MasterPasswordHash = _masterPasswordHash
});
if(response.Succeeded && doToast)
{
_userDialogs.Toast(AppResources.VerificationEmailSent);
}
else if(!response.Succeeded)
{
_userDialogs.Alert(AppResources.VerificationEmailNotSent);
}
}
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
}
private async Task LogInAsync(string token)
2016-07-23 09:17:11 +03:00
{
if(!_providerType.HasValue || _lastAction.LastActionWasRecent())
2017-06-29 05:24:04 +03:00
{
return;
}
_lastAction = DateTime.UtcNow;
2017-06-27 23:18:32 +03:00
if(string.IsNullOrWhiteSpace(token))
2016-07-23 09:17:11 +03:00
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.VerificationCode), AppResources.Ok);
2016-07-23 09:17:11 +03:00
return;
}
_userDialogs.ShowLoading(string.Concat(AppResources.Validating, "..."), MaskType.Black);
var response = await _authService.TokenPostTwoFactorAsync(_providerType.Value, token, RememberCell.On,
2017-06-27 23:18:32 +03:00
_email, _masterPasswordHash, _key);
2016-07-23 09:17:11 +03:00
_userDialogs.HideLoading();
if(!response.Success)
2016-07-23 09:17:11 +03:00
{
2017-06-29 05:24:04 +03:00
ListenYubiKey(true);
await DisplayAlert(AppResources.AnErrorHasOccurred, response.ErrorMessage, AppResources.Ok);
2016-07-23 09:17:11 +03:00
return;
}
_googleAnalyticsService.TrackAppEvent("LoggedIn From Two-step", _providerType.Value.ToString());
2017-05-30 21:13:53 +03:00
if(Device.RuntimePlatform == Device.Android)
{
_pushNotification.Register();
}
2016-07-23 09:17:11 +03:00
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
2017-06-28 17:09:52 +03:00
Device.BeginInvokeOnMainThread(() =>
{
Application.Current.MainPage = new MainPage();
});
2016-07-23 09:17:11 +03:00
}
2017-06-27 23:18:32 +03:00
private TwoFactorProviderType? GetDefaultProvider()
{
TwoFactorProviderType? provider = null;
if(_providers != null)
{
foreach(var p in _providers)
{
switch(p.Key)
{
case TwoFactorProviderType.Authenticator:
2017-06-29 05:24:04 +03:00
if(provider == TwoFactorProviderType.Duo || provider == TwoFactorProviderType.YubiKey)
2017-06-27 23:18:32 +03:00
{
continue;
}
break;
case TwoFactorProviderType.Email:
if(provider.HasValue)
{
continue;
}
break;
case TwoFactorProviderType.Duo:
2017-06-29 05:24:04 +03:00
if(provider == TwoFactorProviderType.YubiKey)
{
continue;
}
break;
case TwoFactorProviderType.YubiKey:
var nfcKey = p.Value.ContainsKey("Nfc") && (bool)p.Value["Nfc"];
if(!_deviceInfoService.NfcEnabled || !nfcKey)
2017-06-29 05:24:04 +03:00
{
continue;
}
2017-06-27 23:18:32 +03:00
break;
default:
continue;
}
provider = p.Key;
}
}
return provider;
}
2017-06-29 05:24:04 +03:00
private void ListenYubiKey(bool listen, bool overrideCheck = false)
2017-06-29 05:24:04 +03:00
{
if(_providerType == TwoFactorProviderType.YubiKey || overrideCheck)
2017-06-29 05:24:04 +03:00
{
MessagingCenter.Send(Application.Current, "ListenYubiKeyOTP", listen);
}
}
private void SubscribeYubiKey(bool subscribe)
{
if(_providerType != TwoFactorProviderType.YubiKey)
{
return;
}
MessagingCenter.Unsubscribe<Application, string>(Application.Current, "GotYubiKeyOTP");
MessagingCenter.Unsubscribe<Application>(Application.Current, "ResumeYubiKey");
if(!subscribe)
{
return;
}
MessagingCenter.Subscribe<Application, string>(Application.Current, "GotYubiKeyOTP", async (sender, otp) =>
{
MessagingCenter.Unsubscribe<Application, string>(Application.Current, "GotYubiKeyOTP");
if(_providerType == TwoFactorProviderType.YubiKey)
{
await LogInAsync(otp);
2017-06-29 05:24:04 +03:00
}
});
SubscribeYubiKeyResume();
}
private void SubscribeYubiKeyResume()
{
MessagingCenter.Subscribe<Application>(Application.Current, "ResumeYubiKey", (sender) =>
{
MessagingCenter.Unsubscribe<Application>(Application.Current, "ResumeYubiKey");
if(_providerType == TwoFactorProviderType.YubiKey)
{
MessagingCenter.Send(Application.Current, "ListenYubiKeyOTP", true);
SubscribeYubiKeyResume();
}
});
}
public class TwoFactorTable : ExtendedTableView
{
public TwoFactorTable(TableSection section)
{
Intent = TableIntent.Settings;
EnableScrolling = false;
HasUnevenRows = true;
EnableSelection = true;
NoFooter = true;
NoHeader = true;
VerticalOptions = LayoutOptions.Start;
Root = Root = new TableRoot
{
section
};
if(Device.RuntimePlatform == Device.iOS)
{
RowHeight = -1;
EstimatedRowHeight = 70;
}
}
}
2016-07-23 09:17:11 +03:00
}
}