1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-07-01 11:24:59 +02:00
bitwarden-mobile/src/App/Pages/Accounts/TwoFactorPage.xaml.cs

128 lines
3.6 KiB
C#
Raw Normal View History

2019-05-27 17:57:10 +02:00
using Bit.App.Controls;
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;
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;
2019-05-24 03:19:45 +02:00
private TwoFactorPageViewModel _vm;
public TwoFactorPage()
{
InitializeComponent();
2019-05-28 15:54:08 +02:00
SetActivityIndicator();
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
2019-05-24 03:19:45 +02:00
_vm = BindingContext as TwoFactorPageViewModel;
_vm.Page = this;
2019-05-27 17:57:10 +02:00
DuoWebView = _duoWebView;
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()
{
if(ToolbarItems.Count == 0)
{
ToolbarItems.Add(_continueItem);
}
}
public void RemoveContinueButton()
{
if(ToolbarItems.Count > 0)
{
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-28 15:54:08 +02:00
_broadcasterService.Subscribe(nameof(TwoFactorPage), async (message) =>
{
if(message.Command == "gotYubiKeyOTP")
{
if(_vm.YubikeyMethod)
{
_vm.Token = (string)message.Data;
await _vm.SubmitAsync();
}
}
else if(message.Command == "resumeYubiKey")
{
if(_vm.YubikeyMethod)
{
_messagingService.Send("listenYubiKeyOTP", true);
}
}
});
2019-05-28 15:04:20 +02:00
await LoadOnAppearedAsync(_scrollView, true, () =>
{
_vm.Init();
2019-05-28 16:12:51 +02:00
if(_vm.TotpMethod)
{
RequestFocus(_totpEntry);
}
2019-05-28 15:04:20 +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();
if(!_vm.YubikeyMethod)
{
_messagingService.Send("listenYubiKeyOTP", false);
_broadcasterService.Unsubscribe(nameof(TwoFactorPage));
}
}
protected override bool OnBackButtonPressed()
{
// ref: https://github.com/bitwarden/mobile/issues/350
if(_vm.YubikeyMethod)
{
if(Device.RuntimePlatform == Device.Android)
{
return true;
}
_messagingService.Send("listenYubiKeyOTP", false);
_broadcasterService.Unsubscribe(nameof(TwoFactorPage));
}
return base.OnBackButtonPressed();
}
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
{
if(DoOnce())
{
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
{
if(DoOnce())
{
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)
{
if(DoOnce())
{
await _vm.SendEmailAsync(true, true);
2019-05-24 03:19:45 +02:00
}
}
}
}