1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-27 03:52:57 +02:00

support for two factor login flow

This commit is contained in:
Kyle Spearrin 2016-07-23 02:17:11 -04:00
parent 2911af2c16
commit cf27ace05e
7 changed files with 169 additions and 14 deletions

View File

@ -6,6 +6,7 @@ namespace Bit.App.Abstractions
public interface IAuthService
{
bool IsAuthenticated { get; }
bool IsAuthenticatedTwoFactor { get; }
string Token { get; set; }
string UserId { get; set; }
string Email { get; set; }
@ -13,5 +14,6 @@ namespace Bit.App.Abstractions
void LogOut();
Task<ApiResult<TokenResponse>> TokenPostAsync(TokenRequest request);
Task<ApiResult<TokenResponse>> TokenTwoFactorPostAsync(TokenTwoFactorRequest request);
}
}

View File

@ -110,6 +110,7 @@
<Compile Include="Models\Page\VaultViewSitePageModel.cs" />
<Compile Include="Pages\HomePage.cs" />
<Compile Include="Pages\Lock\LockPasswordPage.cs" />
<Compile Include="Pages\LoginTwoFactorPage.cs" />
<Compile Include="Pages\PasswordHintPage.cs" />
<Compile Include="Pages\RegisterPage.cs" />
<Compile Include="Pages\Settings\SettingsPinPage.cs" />

View File

@ -4,5 +4,6 @@
{
public string Code { get; set; }
public string Provider { get; set; }
public DeviceRequest Device { get; set; }
}
}

View File

@ -151,11 +151,18 @@ namespace Bit.App.Pages
_cryptoService.Key = key;
_authService.Token = response.Result.Token;
_authService.UserId = response.Result.Profile.Id;
_authService.Email = response.Result.Profile.Email;
_authService.UserId = response.Result?.Profile?.Id;
_authService.Email = response.Result?.Profile?.Email;
var task = Task.Run(async () => await _syncService.FullSyncAsync());
Application.Current.MainPage = new MainPage();
if(_authService.IsAuthenticatedTwoFactor)
{
await Navigation.PushAsync(new LoginTwoFactorPage());
}
else
{
var task = Task.Run(async () => await _syncService.FullSyncAsync());
Application.Current.MainPage = new MainPage();
}
}
}
}

View File

@ -0,0 +1,141 @@
using System;
using System.Linq;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Models.Api;
using Bit.App.Resources;
using Plugin.DeviceInfo.Abstractions;
using Xamarin.Forms;
using XLabs.Ioc;
using Acr.UserDialogs;
using System.Threading.Tasks;
namespace Bit.App.Pages
{
public class LoginTwoFactorPage : ExtendedContentPage
{
private ICryptoService _cryptoService;
private IAuthService _authService;
private IDeviceInfo _deviceInfo;
private IAppIdService _appIdService;
private IUserDialogs _userDialogs;
private ISyncService _syncService;
public LoginTwoFactorPage()
{
_cryptoService = Resolver.Resolve<ICryptoService>();
_authService = Resolver.Resolve<IAuthService>();
_deviceInfo = Resolver.Resolve<IDeviceInfo>();
_appIdService = Resolver.Resolve<IAppIdService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_syncService = Resolver.Resolve<ISyncService>();
Init();
}
public FormEntryCell CodeCell { get; set; }
private void Init()
{
CodeCell = new FormEntryCell("Verification Code", useLabelAsPlaceholder: true,
imageSource: "lock", containerPadding: new Thickness(15, 20));
CodeCell.Entry.Keyboard = Keyboard.Numeric;
CodeCell.Entry.ReturnType = Enums.ReturnType.Go;
CodeCell.Entry.Completed += Entry_Completed;
var table = new ExtendedTableView
{
Intent = TableIntent.Settings,
EnableScrolling = false,
HasUnevenRows = true,
EnableSelection = true,
NoFooter = true,
VerticalOptions = LayoutOptions.Start,
Root = new TableRoot
{
new TableSection()
{
CodeCell
}
}
};
var codeLabel = new Label
{
Text = "Enter your two-step verification code.",
LineBreakMode = LineBreakMode.WordWrap,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
Style = (Style)Application.Current.Resources["text-muted"],
Margin = new Thickness(15, (this.IsLandscape() ? 5 : 0), 15, 25)
};
var layout = new StackLayout
{
Children = { table, codeLabel },
Spacing = 0
};
var scrollView = new ScrollView { Content = layout };
if(Device.OS == TargetPlatform.iOS)
{
table.RowHeight = -1;
table.EstimatedRowHeight = 70;
}
var continueToolbarItem = new ToolbarItem("Continue", null, async () =>
{
await LogIn();
}, ToolbarItemOrder.Default, 0);
ToolbarItems.Add(continueToolbarItem);
Title = "Verification Code";
Content = scrollView;
}
protected override void OnAppearing()
{
base.OnAppearing();
CodeCell.Entry.Focus();
}
private async void Entry_Completed(object sender, EventArgs e)
{
await LogIn();
}
private async Task LogIn()
{
if(string.IsNullOrWhiteSpace(CodeCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, "Verification code"), AppResources.Ok);
return;
}
var request = new TokenTwoFactorRequest
{
Code = CodeCell.Entry.Text,
Provider = "Authenticator",
Device = new DeviceRequest(_appIdService, _deviceInfo)
};
var responseTask = _authService.TokenTwoFactorPostAsync(request);
_userDialogs.ShowLoading("Validating code...", MaskType.Black);
var response = await responseTask;
_userDialogs.HideLoading();
if(!response.Succeeded)
{
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
return;
}
_authService.Token = response.Result.Token;
_authService.UserId = response.Result.Profile.Id;
_authService.Email = response.Result.Profile.Email;
var task = Task.Run(async () => await _syncService.FullSyncAsync());
Application.Current.MainPage = new MainPage();
}
}
}

View File

@ -14,22 +14,12 @@ namespace Bit.App.Pages
{
public class PasswordHintPage : ExtendedContentPage
{
private ICryptoService _cryptoService;
private IAuthService _authService;
private IDeviceInfo _deviceInfo;
private IAppIdService _appIdService;
private IUserDialogs _userDialogs;
private ISyncService _syncService;
private IAccountsApiRepository _accountApiRepository;
public PasswordHintPage()
{
_cryptoService = Resolver.Resolve<ICryptoService>();
_authService = Resolver.Resolve<IAuthService>();
_deviceInfo = Resolver.Resolve<IDeviceInfo>();
_appIdService = Resolver.Resolve<IAppIdService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_syncService = Resolver.Resolve<ISyncService>();
_accountApiRepository = Resolver.Resolve<IAccountsApiRepository>();
Init();

View File

@ -131,6 +131,13 @@ namespace Bit.App.Services
return _cryptoService.Key != null && Token != null && UserId != null;
}
}
public bool IsAuthenticatedTwoFactor
{
get
{
return _cryptoService.Key != null && Token != null && UserId == null;
}
}
public string PIN
{
@ -179,5 +186,11 @@ namespace Bit.App.Services
// TODO: move more logic in here
return await _authApiRepository.PostTokenAsync(request);
}
public async Task<ApiResult<TokenResponse>> TokenTwoFactorPostAsync(TokenTwoFactorRequest request)
{
// TODO: move more logic in here
return await _authApiRepository.PostTokenTwoFactorAsync(request);
}
}
}