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

97 lines
3.5 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using Bit.App.Abstractions;
using Bit.App.Behaviors;
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-05-03 00:50:16 +03:00
namespace Bit.App.Pages
2016-05-02 09:52:09 +03:00
{
public class LoginPage : ContentPage
{
public LoginPage()
{
var cryptoService = Resolver.Resolve<ICryptoService>();
var authService = Resolver.Resolve<IAuthService>();
2016-06-22 05:29:29 +03:00
var deviceInfo = Resolver.Resolve<IDeviceInfo>();
var appIdService = Resolver.Resolve<IAppIdService>();
2016-05-02 09:52:09 +03:00
var emailEntry = new Entry
{
Keyboard = Keyboard.Email,
Placeholder = AppResources.EmailAddress
2016-05-02 09:52:09 +03:00
};
emailEntry.Behaviors.Add(new RequiredValidationBehavior());
emailEntry.Behaviors.Add(new EmailValidationBehavior());
var masterPasswordEntry = new Entry
{
IsPassword = true,
Placeholder = AppResources.MasterPassword
2016-05-02 09:52:09 +03:00
};
masterPasswordEntry.Behaviors.Add(new RequiredValidationBehavior());
var loginButton = new Button
{
Text = AppResources.LogIn,
2016-05-02 09:52:09 +03:00
Command = new Command(async () =>
{
if(string.IsNullOrWhiteSpace(emailEntry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.EmailAddress), AppResources.Ok);
2016-05-02 09:52:09 +03:00
return;
}
if(string.IsNullOrWhiteSpace(masterPasswordEntry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.MasterPassword), AppResources.Ok);
2016-05-02 09:52:09 +03:00
return;
}
var key = cryptoService.MakeKeyFromPassword(masterPasswordEntry.Text, emailEntry.Text);
var request = new TokenRequest
{
Email = emailEntry.Text,
2016-06-22 05:29:29 +03:00
MasterPasswordHash = cryptoService.HashPasswordBase64(key, masterPasswordEntry.Text),
Device = new DeviceRequest(appIdService, deviceInfo)
2016-05-02 09:52:09 +03:00
};
var response = await authService.TokenPostAsync(request);
if(!response.Succeeded)
{
2016-06-22 05:29:29 +03:00
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.FirstOrDefault()?.Message, AppResources.Ok);
2016-05-03 00:50:16 +03:00
return;
2016-05-02 09:52:09 +03:00
}
cryptoService.Key = key;
authService.Token = response.Result.Token;
2016-05-03 00:50:16 +03:00
authService.UserId = response.Result.Profile.Id;
Application.Current.MainPage = new MainPage();
2016-05-02 09:52:09 +03:00
})
};
var stackLayout = new StackLayout
{
Padding = new Thickness(10, 50, 10, 0)
};
stackLayout.Children.Add(emailEntry);
stackLayout.Children.Add(masterPasswordEntry);
stackLayout.Children.Add(loginButton);
Title = AppResources.LogIn;
2016-05-02 09:52:09 +03:00
Content = stackLayout;
NavigationPage.SetHasNavigationBar(this, false);
}
}
}