bitwarden-android/src/App/Pages/LoginPage.cs

130 lines
4.4 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Linq;
using Bit.App.Abstractions;
using Bit.App.Controls;
2016-05-02 09:52:09 +03:00
using Bit.App.Models.Api;
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
{
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-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-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-06-28 03:56:59 +03:00
PasswordCell = new FormEntryCell(AppResources.MasterPassword, IsPassword: true, useLabelAsPlaceholder: true);
EmailCell = new FormEntryCell(AppResources.EmailAddress, nextElement: PasswordCell.Entry, entryKeyboard: Keyboard.Email, useLabelAsPlaceholder: true);
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,
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-24 06:03:00 +03:00
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
return;
}
2016-06-24 06:03:00 +03:00
_cryptoService.Key = key;
_authService.Token = response.Result.Token;
_authService.UserId = response.Result.Profile.Id;
Application.Current.MainPage = new MainPage();
2016-05-02 09:52:09 +03:00
}
}
}