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;
|
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-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,
|
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,
|
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);
|
|
|
|
|
|
2016-06-22 06:54:51 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, "Cancel"));
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-07 20:42:09 +03:00
|
|
|
|
Title = AppResources.LogIn;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
Content = stackLayout;
|
2016-06-22 06:54:51 +03:00
|
|
|
|
BackgroundImage = "bg.png";
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|