bitwarden-android/src/App/Pages/Accounts/TwoFactorPageViewModel.cs

243 lines
9.3 KiB
C#
Raw Normal View History

2019-05-24 04:19:45 +03:00
using Bit.App.Abstractions;
using Bit.App.Resources;
using Bit.Core.Abstractions;
2019-05-27 17:28:38 +03:00
using Bit.Core.Enums;
2019-05-24 04:19:45 +03:00
using Bit.Core.Exceptions;
2019-05-27 17:28:38 +03:00
using Bit.Core.Models.Request;
2019-05-24 04:19:45 +03:00
using Bit.Core.Utilities;
2019-05-27 17:28:38 +03:00
using System.Linq;
2019-05-27 18:57:10 +03:00
using System.Net;
2019-05-24 04:19:45 +03:00
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Bit.App.Pages
{
public class TwoFactorPageViewModel : BaseViewModel
{
private readonly IDeviceActionService _deviceActionService;
private readonly IAuthService _authService;
private readonly ISyncService _syncService;
private readonly IStorageService _storageService;
2019-05-27 17:28:38 +03:00
private readonly IApiService _apiService;
private readonly IPlatformUtilsService _platformUtilsService;
2019-05-27 18:57:10 +03:00
private readonly IEnvironmentService _environmentService;
2019-05-24 04:19:45 +03:00
2019-05-27 17:28:38 +03:00
private bool _u2fSupported = false;
private TwoFactorProviderType? _selectedProviderType;
private string _twoFactorEmail;
2019-05-27 18:57:10 +03:00
private string _webVaultUrl = "https://vault.bitwarden.com";
2019-05-24 04:19:45 +03:00
public TwoFactorPageViewModel()
{
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_authService = ServiceContainer.Resolve<IAuthService>("authService");
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
2019-05-27 17:28:38 +03:00
_apiService = ServiceContainer.Resolve<IApiService>("apiService");
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
2019-05-27 18:57:10 +03:00
_environmentService = ServiceContainer.Resolve<IEnvironmentService>("environmentService");
2019-05-28 16:04:20 +03:00
PageTitle = AppResources.TwoStepLogin;
2019-05-24 04:19:45 +03:00
}
2019-05-27 17:28:38 +03:00
public string TwoFactorEmail
2019-05-24 04:19:45 +03:00
{
2019-05-27 17:28:38 +03:00
get => _twoFactorEmail;
set => SetProperty(ref _twoFactorEmail, value);
2019-05-24 04:19:45 +03:00
}
2019-05-27 17:28:38 +03:00
public bool Remember { get; set; }
public string Token { get; set; }
2019-05-28 16:04:20 +03:00
public bool DuoMethod => SelectedProviderType == TwoFactorProviderType.Duo ||
SelectedProviderType == TwoFactorProviderType.OrganizationDuo;
2019-05-27 17:28:38 +03:00
public bool YubikeyMethod => SelectedProviderType == TwoFactorProviderType.YubiKey;
public bool AuthenticatorMethod => SelectedProviderType == TwoFactorProviderType.Authenticator;
public bool EmailMethod => SelectedProviderType == TwoFactorProviderType.Email;
2019-05-27 18:57:10 +03:00
public bool TotpMethod => AuthenticatorMethod || EmailMethod;
public string TotpInstruction => AuthenticatorMethod ? AppResources.EnterVerificationCodeApp :
AppResources.EnterVerificationCodeEmail;
public string YubikeyInstruction => Device.RuntimePlatform == Device.iOS ? AppResources.YubiKeyInstructionIos :
AppResources.YubiKeyInstruction;
2019-05-27 17:28:38 +03:00
public TwoFactorProviderType? SelectedProviderType
2019-05-24 04:19:45 +03:00
{
2019-05-27 17:28:38 +03:00
get => _selectedProviderType;
set => SetProperty(ref _selectedProviderType, value, additionalPropertyNames: new string[]
{
nameof(EmailMethod),
nameof(DuoMethod),
nameof(YubikeyMethod),
2019-05-27 18:57:10 +03:00
nameof(AuthenticatorMethod),
nameof(TotpMethod),
nameof(TotpInstruction)
2019-05-27 17:28:38 +03:00
});
}
public void Init()
{
if(string.IsNullOrWhiteSpace(_authService.Email) ||
string.IsNullOrWhiteSpace(_authService.MasterPasswordHash) ||
_authService.TwoFactorProvidersData == null)
{
// TODO: dismiss modal?
return;
}
2019-05-27 18:57:10 +03:00
if(!string.IsNullOrWhiteSpace(_environmentService.BaseUrl))
{
_webVaultUrl = _environmentService.BaseUrl;
}
else if(!string.IsNullOrWhiteSpace(_environmentService.WebVaultUrl))
{
_webVaultUrl = _environmentService.WebVaultUrl;
}
2019-05-27 17:28:38 +03:00
// TODO: init U2F
_u2fSupported = false;
2019-05-24 04:19:45 +03:00
2019-05-27 18:57:10 +03:00
SelectedProviderType = _authService.GetDefaultTwoFactorProvider(_u2fSupported);
2019-05-27 17:28:38 +03:00
Load();
}
public void Load()
{
if(SelectedProviderType == null)
{
PageTitle = AppResources.LoginUnavailable;
return;
}
2019-05-28 16:12:05 +03:00
var page = Page as TwoFactorPage;
2019-05-27 17:28:38 +03:00
PageTitle = _authService.TwoFactorProviders[SelectedProviderType.Value].Name;
var providerData = _authService.TwoFactorProvidersData[SelectedProviderType.Value];
switch(SelectedProviderType.Value)
{
case TwoFactorProviderType.U2f:
// TODO
break;
case TwoFactorProviderType.Duo:
case TwoFactorProviderType.OrganizationDuo:
2019-05-28 16:12:05 +03:00
page.RemoveContinueButton();
2019-05-27 18:57:10 +03:00
var host = WebUtility.UrlEncode(providerData["Host"] as string);
var req = WebUtility.UrlEncode(providerData["Signature"] as string);
page.DuoWebView.Uri = $"{_webVaultUrl}/duo-connector.html?host={host}&request={req}";
page.DuoWebView.RegisterAction(async sig =>
{
Token = sig;
await SubmitAsync();
});
2019-05-27 17:28:38 +03:00
break;
case TwoFactorProviderType.Email:
2019-05-28 16:12:05 +03:00
page.AddContinueButton();
2019-05-27 17:28:38 +03:00
TwoFactorEmail = providerData["Email"] as string;
if(_authService.TwoFactorProvidersData.Count > 1)
{
var emailTask = Task.Run(() => SendEmailAsync(false, false));
}
break;
default:
2019-05-28 16:12:05 +03:00
page.AddContinueButton();
2019-05-27 17:28:38 +03:00
break;
}
2019-05-24 04:19:45 +03:00
}
public async Task SubmitAsync()
{
2019-05-27 17:28:38 +03:00
if(SelectedProviderType == null)
{
return;
}
if(string.IsNullOrWhiteSpace(Token))
{
await _platformUtilsService.ShowDialogAsync(
string.Format(AppResources.ValidationFieldRequired, AppResources.VerificationCode),
AppResources.AnErrorHasOccurred);
}
if(SelectedProviderType == TwoFactorProviderType.Email ||
SelectedProviderType == TwoFactorProviderType.Authenticator)
{
Token = Token.Replace(" ", string.Empty).Trim();
}
try
{
await _deviceActionService.ShowLoadingAsync(AppResources.LoggingIn);
await _authService.LogInTwoFactorAsync(SelectedProviderType.Value, Token, Remember);
await _deviceActionService.HideLoadingAsync();
var task = Task.Run(() => _syncService.FullSyncAsync(true));
Application.Current.MainPage = new TabsPage();
}
catch(ApiException e)
{
await _deviceActionService.HideLoadingAsync();
await _platformUtilsService.ShowDialogAsync(e.Error.GetSingleMessage(),
AppResources.AnErrorHasOccurred);
}
}
2019-05-24 04:19:45 +03:00
2019-05-27 17:28:38 +03:00
public async Task AnotherMethodAsync()
{
var supportedProviders = _authService.GetSupportedTwoFactorProviders();
var options = supportedProviders.Select(p => p.Name).ToList();
options.Add(AppResources.RecoveryCodeTitle);
var method = await Page.DisplayActionSheet(AppResources.TwoStepLoginOptions, AppResources.Cancel,
null, options.ToArray());
if(method == AppResources.RecoveryCodeTitle)
{
_platformUtilsService.LaunchUri("https://help.bitwarden.com/article/lost-two-step-device/");
}
else
{
SelectedProviderType = supportedProviders.FirstOrDefault(p => p.Name == method)?.Type;
Load();
}
}
public async Task<bool> SendEmailAsync(bool showLoading, bool doToast)
{
if(SelectedProviderType != TwoFactorProviderType.Email)
{
return false;
}
try
{
if(showLoading)
{
await _deviceActionService.ShowLoadingAsync(AppResources.Submitting);
}
var request = new TwoFactorEmailRequest
{
Email = _authService.Email,
MasterPasswordHash = _authService.MasterPasswordHash
};
await _apiService.PostTwoFactorEmailAsync(request);
if(showLoading)
{
await _deviceActionService.HideLoadingAsync();
}
if(doToast)
{
_platformUtilsService.ShowToast("success", null, AppResources.VerificationEmailSent);
}
return true;
}
catch(ApiException)
{
if(showLoading)
{
await _deviceActionService.HideLoadingAsync();
}
await _platformUtilsService.ShowDialogAsync(AppResources.VerificationEmailNotSent);
return false;
}
2019-05-24 04:19:45 +03:00
}
}
}