2016-07-23 09:17:11 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Controls;
|
|
|
|
|
using Bit.App.Models.Api;
|
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-02-06 17:39:07 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
|
|
|
|
using PushNotification.Plugin.Abstractions;
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public class LoginTwoFactorPage : ExtendedContentPage
|
|
|
|
|
{
|
|
|
|
|
private ICryptoService _cryptoService;
|
|
|
|
|
private IAuthService _authService;
|
2017-02-04 09:12:25 +03:00
|
|
|
|
private ITokenService _tokenService;
|
2016-08-24 05:43:17 +03:00
|
|
|
|
private IDeviceInfoService _deviceInfoService;
|
2016-07-23 09:17:11 +03:00
|
|
|
|
private IAppIdService _appIdService;
|
|
|
|
|
private IUserDialogs _userDialogs;
|
|
|
|
|
private ISyncService _syncService;
|
2017-02-06 17:39:07 +03:00
|
|
|
|
private ISettings _settings;
|
|
|
|
|
private IGoogleAnalyticsService _googleAnalyticsService;
|
|
|
|
|
private IPushNotification _pushNotification;
|
|
|
|
|
private readonly string _email;
|
|
|
|
|
private readonly string _masterPasswordHash;
|
|
|
|
|
private readonly byte[] _key;
|
|
|
|
|
|
|
|
|
|
public LoginTwoFactorPage(string email, string masterPasswordHash, byte[] key)
|
2016-08-09 02:00:36 +03:00
|
|
|
|
: base(updateActivity: false)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-02-06 17:39:07 +03:00
|
|
|
|
_email = email;
|
|
|
|
|
_masterPasswordHash = masterPasswordHash;
|
|
|
|
|
_key = key;
|
|
|
|
|
|
2016-07-23 09:17:11 +03:00
|
|
|
|
_cryptoService = Resolver.Resolve<ICryptoService>();
|
|
|
|
|
_authService = Resolver.Resolve<IAuthService>();
|
2017-02-04 09:12:25 +03:00
|
|
|
|
_tokenService = Resolver.Resolve<ITokenService>();
|
2016-08-24 05:43:17 +03:00
|
|
|
|
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
|
2016-07-23 09:17:11 +03:00
|
|
|
|
_appIdService = Resolver.Resolve<IAppIdService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
2017-02-06 17:39:07 +03:00
|
|
|
|
_settings = Resolver.Resolve<ISettings>();
|
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
|
|
|
|
_pushNotification = Resolver.Resolve<IPushNotification>();
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FormEntryCell CodeCell { get; set; }
|
|
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2016-08-14 04:43:15 +03:00
|
|
|
|
var padding = Device.OnPlatform(
|
|
|
|
|
iOS: new Thickness(15, 20),
|
|
|
|
|
Android: new Thickness(15, 8),
|
|
|
|
|
WinPhone: new Thickness(15, 20));
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
CodeCell = new FormEntryCell(AppResources.VerificationCode, useLabelAsPlaceholder: true,
|
2016-08-14 04:43:15 +03:00
|
|
|
|
imageSource: "lock", containerPadding: padding);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Text = AppResources.EnterVerificationCode,
|
2016-07-23 09:17:11 +03:00
|
|
|
|
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)
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-26 09:03:02 +03:00
|
|
|
|
var lostAppButton = new ExtendedButton
|
|
|
|
|
{
|
|
|
|
|
Text = AppResources.Lost2FAApp,
|
|
|
|
|
Style = (Style)Application.Current.Resources["btn-primaryAccent"],
|
|
|
|
|
Margin = new Thickness(15, 0, 15, 25),
|
|
|
|
|
Command = new Command(() => Lost2FAApp()),
|
|
|
|
|
Uppercase = false,
|
|
|
|
|
BackgroundColor = Color.Transparent
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-23 09:17:11 +03:00
|
|
|
|
var layout = new StackLayout
|
|
|
|
|
{
|
2016-11-26 09:03:02 +03:00
|
|
|
|
Children = { table, codeLabel, lostAppButton },
|
2016-07-23 09:17:11 +03:00
|
|
|
|
Spacing = 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var scrollView = new ScrollView { Content = layout };
|
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = 70;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
var continueToolbarItem = new ToolbarItem(AppResources.Continue, null, async () =>
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
|
|
|
|
await LogIn();
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
|
|
|
|
ToolbarItems.Add(continueToolbarItem);
|
2016-11-26 00:54:33 +03:00
|
|
|
|
Title = AppResources.VerificationCode;
|
2016-07-23 09:17:11 +03:00
|
|
|
|
Content = scrollView;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2016-08-19 07:27:37 +03:00
|
|
|
|
CodeCell.Entry.FocusWithDelay();
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-26 09:03:02 +03:00
|
|
|
|
private void Lost2FAApp()
|
|
|
|
|
{
|
|
|
|
|
Device.OpenUri(new Uri("https://vault.bitwarden.com/#/recover"));
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-23 09:17:11 +03:00
|
|
|
|
private async void Entry_Completed(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
await LogIn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task LogIn()
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(CodeCell.Entry.Text))
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
2016-11-26 00:54:33 +03:00
|
|
|
|
AppResources.VerificationCode), AppResources.Ok);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 09:12:25 +03:00
|
|
|
|
var request = new TokenRequest
|
2016-07-23 09:17:11 +03:00
|
|
|
|
{
|
2017-02-06 17:39:07 +03:00
|
|
|
|
Email = _email,
|
|
|
|
|
MasterPasswordHash = _masterPasswordHash,
|
2017-02-04 09:12:25 +03:00
|
|
|
|
Token = CodeCell.Entry.Text.Replace(" ", ""),
|
2017-02-06 17:39:07 +03:00
|
|
|
|
Provider = 0, // Authenticator app (only 1 provider for now, so hard coded)
|
2016-08-24 05:43:17 +03:00
|
|
|
|
Device = new DeviceRequest(_appIdService, _deviceInfoService)
|
2016-07-23 09:17:11 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-26 00:54:33 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.ValidatingCode, MaskType.Black);
|
2017-02-04 09:12:25 +03:00
|
|
|
|
var response = await _authService.TokenPostAsync(request);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
if(!response.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 17:39:07 +03:00
|
|
|
|
_cryptoService.Key = _key;
|
2017-02-04 09:12:25 +03:00
|
|
|
|
_tokenService.Token = response.Result.AccessToken;
|
|
|
|
|
_tokenService.RefreshToken = response.Result.RefreshToken;
|
|
|
|
|
_authService.UserId = _tokenService.TokenUserId;
|
|
|
|
|
_authService.Email = _tokenService.TokenEmail;
|
2017-02-06 17:39:07 +03:00
|
|
|
|
_settings.AddOrUpdateValue(Constants.LastLoginEmail, _authService.Email);
|
|
|
|
|
_googleAnalyticsService.RefreshUserId();
|
|
|
|
|
_googleAnalyticsService.TrackAppEvent("LoggedIn From Two-step");
|
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.Android)
|
|
|
|
|
{
|
|
|
|
|
_pushNotification.Register();
|
|
|
|
|
}
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
2017-02-07 07:40:24 +03:00
|
|
|
|
var task = Task.Run(async () => await _syncService.FullSyncAsync(true));
|
2016-07-23 09:17:11 +03:00
|
|
|
|
Application.Current.MainPage = new MainPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|