2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Bit.App.Abstractions;
|
2016-06-22 06:54:51 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Bit.App.Models.Api;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
using Bit.App.Resources;
|
2016-06-22 05:29:29 +03:00
|
|
|
|
using Plugin.DeviceInfo.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
namespace Bit.App.Pages
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-28 02:53:31 +03:00
|
|
|
|
public class LoginPage : ExtendedContentPage
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private ICryptoService _cryptoService;
|
|
|
|
|
private IAuthService _authService;
|
|
|
|
|
private IDeviceInfo _deviceInfo;
|
|
|
|
|
private IAppIdService _appIdService;
|
|
|
|
|
private IUserDialogs _userDialogs;
|
2016-07-01 03:08:34 +03:00
|
|
|
|
private ISyncService _syncService;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
public LoginPage()
|
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_cryptoService = Resolver.Resolve<ICryptoService>();
|
|
|
|
|
_authService = Resolver.Resolve<IAuthService>();
|
|
|
|
|
_deviceInfo = Resolver.Resolve<IDeviceInfo>();
|
|
|
|
|
_appIdService = Resolver.Resolve<IAppIdService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
2016-07-01 03:08:34 +03:00
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Init();
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
public FormEntryCell PasswordCell { get; set; }
|
|
|
|
|
public FormEntryCell EmailCell { get; set; }
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2016-07-03 07:27:10 +03:00
|
|
|
|
var padding = new Thickness(15, 20);
|
|
|
|
|
|
2016-06-28 05:17:49 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.MasterPassword, IsPassword: true,
|
2016-07-03 07:27:10 +03:00
|
|
|
|
useLabelAsPlaceholder: true, imageSource: "lock", containerPadding: padding);
|
2016-06-28 05:17:49 +03:00
|
|
|
|
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry,
|
2016-07-03 07:27:10 +03:00
|
|
|
|
entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true, imageSource: "envelope",
|
|
|
|
|
containerPadding: padding);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
PasswordCell.Entry.ReturnType = Enums.ReturnType.Go;
|
|
|
|
|
PasswordCell.Entry.Completed += Entry_Completed;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
var table = new ExtendedTableView
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Intent = TableIntent.Settings,
|
2016-06-26 03:54:17 +03:00
|
|
|
|
EnableScrolling = true,
|
2016-06-24 06:03:00 +03:00
|
|
|
|
HasUnevenRows = true,
|
|
|
|
|
EnableSelection = false,
|
|
|
|
|
Root = new TableRoot
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
new TableSection()
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
EmailCell,
|
|
|
|
|
PasswordCell
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
|
|
|
|
};
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
var loginToolbarItem = new ToolbarItem(AppResources.LogIn, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
await LogIn();
|
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = 70;
|
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, "Cancel"));
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
ToolbarItems.Add(loginToolbarItem);
|
|
|
|
|
Title = AppResources.Bitwarden;
|
|
|
|
|
Content = table;
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
|
|
|
|
EmailCell.Entry.Focus();
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:06:33 +03:00
|
|
|
|
private async void Entry_Completed(object sender, EventArgs e)
|
2016-06-24 06:03:00 +03:00
|
|
|
|
{
|
2016-06-24 06:06:33 +03:00
|
|
|
|
await LogIn();
|
2016-06-24 06:03:00 +03:00
|
|
|
|
}
|
2016-05-03 00:50:16 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
private async Task LogIn()
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(EmailCell.Entry.Text))
|
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress), AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
|
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.MasterPassword), AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
var key = _cryptoService.MakeKeyFromPassword(PasswordCell.Entry.Text, EmailCell.Entry.Text);
|
|
|
|
|
|
|
|
|
|
var request = new TokenRequest
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Email = EmailCell.Entry.Text,
|
|
|
|
|
MasterPasswordHash = _cryptoService.HashPasswordBase64(key, PasswordCell.Entry.Text),
|
|
|
|
|
Device = new DeviceRequest(_appIdService, _deviceInfo)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
var responseTask = _authService.TokenPostAsync(request);
|
|
|
|
|
_userDialogs.ShowLoading("Logging in...", MaskType.Black);
|
|
|
|
|
var response = await responseTask;
|
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
if(!response.Succeeded)
|
2016-06-22 06:54:51 +03:00
|
|
|
|
{
|
2016-06-24 06:03:00 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
|
|
|
|
|
return;
|
2016-06-22 06:54:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 06:03:00 +03:00
|
|
|
|
_cryptoService.Key = key;
|
|
|
|
|
_authService.Token = response.Result.Token;
|
|
|
|
|
_authService.UserId = response.Result.Profile.Id;
|
2016-07-19 02:16:27 +03:00
|
|
|
|
_authService.Email = response.Result.Profile.Email;
|
2016-06-24 06:03:00 +03:00
|
|
|
|
|
2016-07-20 01:59:46 +03:00
|
|
|
|
var task = Task.Run(async () => await _syncService.FullSyncAsync());
|
2016-06-24 06:03:00 +03:00
|
|
|
|
Application.Current.MainPage = new MainPage();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|