2019-05-27 17:57:10 +02:00
|
|
|
|
using Bit.App.Controls;
|
2020-04-13 18:32:23 +02:00
|
|
|
|
using Bit.App.Models;
|
2019-05-28 15:54:08 +02:00
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Utilities;
|
2019-05-27 17:57:10 +02:00
|
|
|
|
using System;
|
2019-05-28 15:04:20 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2020-09-03 18:30:40 +02:00
|
|
|
|
using Bit.App.Utilities;
|
2019-05-28 15:54:08 +02:00
|
|
|
|
using Xamarin.Forms;
|
2019-05-24 03:19:45 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public partial class TwoFactorPage : BaseContentPage
|
|
|
|
|
{
|
2019-05-28 15:54:08 +02:00
|
|
|
|
private readonly IBroadcasterService _broadcasterService;
|
|
|
|
|
private readonly IMessagingService _messagingService;
|
2020-04-13 18:32:23 +02:00
|
|
|
|
private readonly IStorageService _storageService;
|
2020-09-03 18:30:40 +02:00
|
|
|
|
private readonly IVaultTimeoutService _vaultTimeoutService;
|
2020-04-13 18:32:23 +02:00
|
|
|
|
private readonly AppOptions _appOptions;
|
2019-05-28 15:54:08 +02:00
|
|
|
|
|
2019-05-24 03:19:45 +02:00
|
|
|
|
private TwoFactorPageViewModel _vm;
|
2019-06-10 16:45:22 +02:00
|
|
|
|
private bool _inited;
|
2020-09-03 18:30:40 +02:00
|
|
|
|
private bool _authingWithSso;
|
2020-10-13 22:01:14 +02:00
|
|
|
|
private string _orgIdentifier;
|
2019-05-24 03:19:45 +02:00
|
|
|
|
|
2020-10-13 22:01:14 +02:00
|
|
|
|
public TwoFactorPage(bool? authingWithSso = false, AppOptions appOptions = null, string orgIdentifier = null)
|
2019-05-24 03:19:45 +02:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2019-05-28 15:54:08 +02:00
|
|
|
|
SetActivityIndicator();
|
2020-09-03 18:30:40 +02:00
|
|
|
|
_authingWithSso = authingWithSso ?? false;
|
2020-04-13 18:32:23 +02:00
|
|
|
|
_appOptions = appOptions;
|
2020-10-13 22:01:14 +02:00
|
|
|
|
_orgIdentifier = orgIdentifier;
|
2020-04-13 18:32:23 +02:00
|
|
|
|
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
|
2019-05-28 15:54:08 +02:00
|
|
|
|
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
|
|
|
|
|
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
|
2020-09-03 18:30:40 +02:00
|
|
|
|
_vaultTimeoutService = ServiceContainer.Resolve<IVaultTimeoutService>("vaultTimeoutService");
|
2019-05-24 03:19:45 +02:00
|
|
|
|
_vm = BindingContext as TwoFactorPageViewModel;
|
|
|
|
|
_vm.Page = this;
|
2020-09-03 18:30:40 +02:00
|
|
|
|
_vm.StartSetPasswordAction = () =>
|
|
|
|
|
Device.BeginInvokeOnMainThread(async () => await StartSetPasswordAsync());
|
|
|
|
|
_vm.TwoFactorAuthSuccessAction = () =>
|
|
|
|
|
Device.BeginInvokeOnMainThread(async () => await TwoFactorAuthSuccessAsync());
|
2020-05-29 18:26:36 +02:00
|
|
|
|
_vm.CloseAction = async () => await Navigation.PopModalAsync();
|
2019-05-27 17:57:10 +02:00
|
|
|
|
DuoWebView = _duoWebView;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (Device.RuntimePlatform == Device.Android)
|
2019-06-12 03:31:51 +02:00
|
|
|
|
{
|
2019-06-15 00:08:08 +02:00
|
|
|
|
ToolbarItems.Remove(_cancelItem);
|
2019-06-12 03:31:51 +02:00
|
|
|
|
}
|
2019-05-24 03:19:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 17:57:10 +02:00
|
|
|
|
public HybridWebView DuoWebView { get; set; }
|
|
|
|
|
|
2019-05-28 15:12:05 +02:00
|
|
|
|
public void AddContinueButton()
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!ToolbarItems.Contains(_continueItem))
|
2019-05-28 15:12:05 +02:00
|
|
|
|
{
|
|
|
|
|
ToolbarItems.Add(_continueItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveContinueButton()
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (ToolbarItems.Contains(_continueItem))
|
2019-05-28 15:12:05 +02:00
|
|
|
|
{
|
|
|
|
|
ToolbarItems.Remove(_continueItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-28 15:54:08 +02:00
|
|
|
|
|
2019-05-28 15:04:20 +02:00
|
|
|
|
protected async override void OnAppearing()
|
2019-05-24 03:19:45 +02:00
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2019-05-30 17:40:33 +02:00
|
|
|
|
_broadcasterService.Subscribe(nameof(TwoFactorPage), (message) =>
|
2019-05-28 15:54:08 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (message.Command == "gotYubiKeyOTP")
|
2019-05-28 15:54:08 +02:00
|
|
|
|
{
|
2019-07-07 03:59:13 +02:00
|
|
|
|
var token = (string)message.Data;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_vm.YubikeyMethod && !string.IsNullOrWhiteSpace(token) &&
|
2019-07-22 19:59:12 +02:00
|
|
|
|
token.Length == 44 && !token.Contains(" "))
|
2019-05-28 15:54:08 +02:00
|
|
|
|
{
|
2019-05-30 17:40:33 +02:00
|
|
|
|
Device.BeginInvokeOnMainThread(async () =>
|
|
|
|
|
{
|
2019-07-07 03:59:13 +02:00
|
|
|
|
_vm.Token = token;
|
2019-05-30 17:40:33 +02:00
|
|
|
|
await _vm.SubmitAsync();
|
|
|
|
|
});
|
2019-05-28 15:54:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (message.Command == "resumeYubiKey")
|
2019-05-28 15:54:08 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_vm.YubikeyMethod)
|
2019-05-28 15:54:08 +02:00
|
|
|
|
{
|
|
|
|
|
_messagingService.Send("listenYubiKeyOTP", true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-06-10 16:45:22 +02:00
|
|
|
|
|
2020-05-20 15:09:28 +02:00
|
|
|
|
await LoadOnAppearedAsync(_scrollView, true, () =>
|
2019-05-28 15:04:20 +02:00
|
|
|
|
{
|
2020-05-20 15:09:28 +02:00
|
|
|
|
if (!_inited)
|
2019-05-28 16:12:51 +02:00
|
|
|
|
{
|
2020-05-20 15:09:28 +02:00
|
|
|
|
_inited = true;
|
2019-06-10 16:45:22 +02:00
|
|
|
|
_vm.Init();
|
2020-05-20 15:09:28 +02:00
|
|
|
|
}
|
|
|
|
|
if (_vm.TotpMethod)
|
|
|
|
|
{
|
|
|
|
|
RequestFocus(_totpEntry);
|
2021-02-02 20:56:44 +01:00
|
|
|
|
} else if (_vm.YubikeyMethod)
|
|
|
|
|
{
|
|
|
|
|
RequestFocus(_yubikeyTokenEntry);
|
2020-05-20 15:09:28 +02:00
|
|
|
|
}
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
});
|
2019-05-24 03:19:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:54:08 +02:00
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!_vm.YubikeyMethod)
|
2019-05-28 15:54:08 +02:00
|
|
|
|
{
|
|
|
|
|
_messagingService.Send("listenYubiKeyOTP", false);
|
2019-06-10 16:53:11 +02:00
|
|
|
|
_broadcasterService.Unsubscribe(nameof(TwoFactorPage));
|
2019-05-28 15:54:08 +02:00
|
|
|
|
}
|
2019-06-10 16:53:11 +02:00
|
|
|
|
}
|
|
|
|
|
protected override bool OnBackButtonPressed()
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_vm.YubikeyMethod)
|
2019-06-10 16:53:11 +02:00
|
|
|
|
{
|
|
|
|
|
_messagingService.Send("listenYubiKeyOTP", false);
|
|
|
|
|
_broadcasterService.Unsubscribe(nameof(TwoFactorPage));
|
|
|
|
|
}
|
|
|
|
|
return base.OnBackButtonPressed();
|
2019-05-28 15:54:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:28:38 +02:00
|
|
|
|
private async void Continue_Clicked(object sender, EventArgs e)
|
2019-05-24 03:19:45 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DoOnce())
|
2019-05-24 03:19:45 +02:00
|
|
|
|
{
|
2019-05-27 16:28:38 +02:00
|
|
|
|
await _vm.SubmitAsync();
|
2019-05-24 03:19:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:28:38 +02:00
|
|
|
|
private async void Methods_Clicked(object sender, EventArgs e)
|
2019-05-24 03:19:45 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DoOnce())
|
2019-05-24 03:19:45 +02:00
|
|
|
|
{
|
2019-05-27 16:28:38 +02:00
|
|
|
|
await _vm.AnotherMethodAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-24 03:19:45 +02:00
|
|
|
|
|
2019-05-27 16:28:38 +02:00
|
|
|
|
private async void ResendEmail_Clicked(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DoOnce())
|
2019-05-27 16:28:38 +02:00
|
|
|
|
{
|
|
|
|
|
await _vm.SendEmailAsync(true, true);
|
2019-05-24 03:19:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-12 03:31:51 +02:00
|
|
|
|
|
2020-09-03 18:30:40 +02:00
|
|
|
|
private void Close_Clicked(object sender, System.EventArgs e)
|
2019-06-12 03:31:51 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DoOnce())
|
2019-06-12 03:31:51 +02:00
|
|
|
|
{
|
2020-05-29 18:26:36 +02:00
|
|
|
|
_vm.CloseAction();
|
2019-06-12 03:31:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-07 03:59:13 +02:00
|
|
|
|
|
|
|
|
|
private void TryAgain_Clicked(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DoOnce())
|
2019-07-07 03:59:13 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_vm.YubikeyMethod)
|
2019-07-07 03:59:13 +02:00
|
|
|
|
{
|
|
|
|
|
_messagingService.Send("listenYubiKeyOTP", true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-03 18:30:40 +02:00
|
|
|
|
|
|
|
|
|
private async Task StartSetPasswordAsync()
|
|
|
|
|
{
|
|
|
|
|
_vm.CloseAction();
|
2020-10-13 22:01:14 +02:00
|
|
|
|
var page = new SetPasswordPage(_appOptions, _orgIdentifier);
|
2020-09-03 18:30:40 +02:00
|
|
|
|
await Navigation.PushModalAsync(new NavigationPage(page));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task TwoFactorAuthSuccessAsync()
|
2020-04-13 18:32:23 +02:00
|
|
|
|
{
|
2020-09-03 18:30:40 +02:00
|
|
|
|
if (_authingWithSso)
|
2020-04-13 18:32:23 +02:00
|
|
|
|
{
|
2020-09-03 18:30:40 +02:00
|
|
|
|
Application.Current.MainPage = new NavigationPage(new LockPage(_appOptions));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (AppHelpers.SetAlternateMainPage(_appOptions))
|
2020-04-13 18:32:23 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-03 18:30:40 +02:00
|
|
|
|
var previousPage = await AppHelpers.ClearPreviousPage();
|
|
|
|
|
Application.Current.MainPage = new TabsPage(_appOptions, previousPage);
|
2020-04-13 18:32:23 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-24 03:19:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|