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;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
using Bit.App.Resources;
|
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>();
|
|
|
|
|
|
|
|
|
|
var emailEntry = new Entry
|
|
|
|
|
{
|
|
|
|
|
Keyboard = Keyboard.Email,
|
2016-05-07 20:42:09 +03:00
|
|
|
|
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,
|
2016-05-07 20:42:09 +03:00
|
|
|
|
Placeholder = AppResources.MasterPassword
|
2016-05-02 09:52:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
masterPasswordEntry.Behaviors.Add(new RequiredValidationBehavior());
|
|
|
|
|
|
|
|
|
|
var loginButton = new Button
|
|
|
|
|
{
|
2016-05-07 20:42:09 +03:00
|
|
|
|
Text = AppResources.LogIn,
|
2016-05-02 09:52:09 +03:00
|
|
|
|
Command = new Command(async () =>
|
|
|
|
|
{
|
|
|
|
|
if(string.IsNullOrWhiteSpace(emailEntry.Text))
|
|
|
|
|
{
|
2016-05-07 20:42:09 +03:00
|
|
|
|
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))
|
|
|
|
|
{
|
2016-05-07 20:42:09 +03:00
|
|
|
|
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,
|
|
|
|
|
MasterPasswordHash = cryptoService.HashPasswordBase64(key, masterPasswordEntry.Text)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var response = await authService.TokenPostAsync(request);
|
|
|
|
|
if(!response.Succeeded)
|
|
|
|
|
{
|
2016-05-07 20:42:09 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, response.Errors.First().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);
|
|
|
|
|
|
2016-05-07 20:42:09 +03:00
|
|
|
|
Title = AppResources.LogIn;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
Content = stackLayout;
|
|
|
|
|
NavigationPage.SetHasNavigationBar(this, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|