2020-04-22 18:12:28 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using GalaSoft.MvvmLight;
|
|
|
|
|
using GalaSoft.MvvmLight.Command;
|
|
|
|
|
using MediatR;
|
|
|
|
|
using Messages;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
using ModernKeePass.Application.Security.Commands.GenerateKeyFile;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
using ModernKeePass.Domain.Common;
|
|
|
|
|
using ModernKeePass.Domain.Dtos;
|
2020-04-22 18:12:28 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
public class SetCredentialsVm : ViewModelBase
|
2020-04-22 18:12:28 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly IMediator _mediator;
|
2020-05-05 17:32:07 +02:00
|
|
|
|
private readonly IResourceProxy _resource;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
private readonly IFileProxy _file;
|
|
|
|
|
|
2020-04-22 18:12:28 +02:00
|
|
|
|
public bool HasPassword
|
|
|
|
|
{
|
|
|
|
|
get { return _hasPassword; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Set(() => HasPassword, ref _hasPassword, value);
|
2020-04-30 15:32:42 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsPasswordValid));
|
2020-04-22 18:12:28 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsValid));
|
|
|
|
|
GenerateCredentialsCommand.RaiseCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasKeyFile
|
|
|
|
|
{
|
|
|
|
|
get { return _hasKeyFile; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Set(() => HasKeyFile, ref _hasKeyFile, value);
|
2020-04-30 15:32:42 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsKeyFileValid));
|
2020-04-22 18:12:28 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsValid));
|
2020-05-04 12:48:27 +02:00
|
|
|
|
OpenKeyFileCommand.RaiseCanExecuteChanged();
|
|
|
|
|
CreateKeyFileCommand.RaiseCanExecuteChanged();
|
2020-04-22 18:12:28 +02:00
|
|
|
|
GenerateCredentialsCommand.RaiseCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Password
|
|
|
|
|
{
|
|
|
|
|
get { return _password; }
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-05-25 19:23:32 +02:00
|
|
|
|
Set(() => Password, ref _password, value);
|
2020-04-30 15:32:42 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsPasswordValid));
|
2020-04-22 18:12:28 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsValid));
|
|
|
|
|
GenerateCredentialsCommand.RaiseCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public string ConfirmPassword
|
|
|
|
|
{
|
|
|
|
|
get { return _confirmPassword; }
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-05-25 19:23:32 +02:00
|
|
|
|
Set(() => ConfirmPassword, ref _confirmPassword, value);
|
2020-04-30 15:32:42 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsPasswordValid));
|
2020-04-22 18:12:28 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsValid));
|
|
|
|
|
GenerateCredentialsCommand.RaiseCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string KeyFilePath
|
|
|
|
|
{
|
|
|
|
|
get { return _keyFilePath; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_keyFilePath = value;
|
2020-04-30 15:32:42 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsKeyFileValid));
|
2020-04-22 18:12:28 +02:00
|
|
|
|
RaisePropertyChanged(nameof(IsValid));
|
|
|
|
|
GenerateCredentialsCommand.RaiseCanExecuteChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string KeyFileText
|
|
|
|
|
{
|
|
|
|
|
get { return _keyFileText; }
|
|
|
|
|
set { Set(() => KeyFileText, ref _keyFileText, value); }
|
|
|
|
|
}
|
2020-04-30 15:32:42 +02:00
|
|
|
|
|
|
|
|
|
public bool IsPasswordValid => HasPassword && Password == ConfirmPassword || !HasPassword;
|
|
|
|
|
public bool IsKeyFileValid => HasKeyFile && !string.IsNullOrEmpty(KeyFilePath) || !HasKeyFile;
|
2020-04-30 19:40:48 +02:00
|
|
|
|
public bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && !string.IsNullOrEmpty(KeyFilePath) && (HasPassword || HasKeyFile);
|
2020-04-22 18:12:28 +02:00
|
|
|
|
|
2020-05-04 12:48:27 +02:00
|
|
|
|
public RelayCommand OpenKeyFileCommand { get; }
|
|
|
|
|
public RelayCommand CreateKeyFileCommand { get; }
|
2020-04-22 18:12:28 +02:00
|
|
|
|
public RelayCommand GenerateCredentialsCommand{ get; }
|
|
|
|
|
|
|
|
|
|
private bool _hasPassword;
|
|
|
|
|
private bool _hasKeyFile;
|
|
|
|
|
private string _password = string.Empty;
|
2020-04-30 15:32:42 +02:00
|
|
|
|
private string _confirmPassword = string.Empty;
|
2020-04-22 18:12:28 +02:00
|
|
|
|
private string _keyFilePath;
|
|
|
|
|
private string _keyFileText;
|
2020-04-23 19:00:38 +02:00
|
|
|
|
|
2020-05-25 19:23:32 +02:00
|
|
|
|
public SetCredentialsVm(IMediator mediator, IResourceProxy resource, IFileProxy file)
|
2020-04-22 18:12:28 +02:00
|
|
|
|
{
|
|
|
|
|
_mediator = mediator;
|
2020-05-05 17:32:07 +02:00
|
|
|
|
_resource = resource;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
_file = file;
|
|
|
|
|
|
|
|
|
|
OpenKeyFileCommand = new RelayCommand(async () => await OpenKeyFile(), () => HasKeyFile);
|
|
|
|
|
CreateKeyFileCommand = new RelayCommand(async () => await CreateKeyFile(), () => HasKeyFile);
|
2020-04-22 18:12:28 +02:00
|
|
|
|
GenerateCredentialsCommand = new RelayCommand(GenerateCredentials, () => IsValid);
|
|
|
|
|
|
|
|
|
|
_keyFileText = resource.GetResourceValue("CompositeKeyDefaultKeyFile");
|
2020-05-25 19:23:32 +02:00
|
|
|
|
|
|
|
|
|
MessengerInstance.Register<PasswordGeneratedMessage>(this, message =>
|
|
|
|
|
{
|
|
|
|
|
Password = message.Password;
|
|
|
|
|
ConfirmPassword = message.Password;
|
|
|
|
|
});
|
2020-04-22 18:12:28 +02:00
|
|
|
|
}
|
2020-05-04 12:48:27 +02:00
|
|
|
|
|
|
|
|
|
private async Task OpenKeyFile()
|
|
|
|
|
{
|
|
|
|
|
var file = await _file.OpenFile(string.Empty, Constants.Extensions.Any, false);
|
2020-05-05 17:32:07 +02:00
|
|
|
|
if (file == null) return;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
SetKeyFileInfo(file);
|
|
|
|
|
}
|
2020-04-22 18:12:28 +02:00
|
|
|
|
|
2020-05-04 12:48:27 +02:00
|
|
|
|
private async Task CreateKeyFile()
|
2020-04-22 18:12:28 +02:00
|
|
|
|
{
|
2020-05-05 17:32:07 +02:00
|
|
|
|
var file = await _file.CreateFile(_resource.GetResourceValue("CompositeKeyFileNameSuggestion"),
|
|
|
|
|
Constants.Extensions.Key, _resource.GetResourceValue("CompositeKeyFileTypeDesc"),
|
|
|
|
|
false);
|
|
|
|
|
if (file == null) return;
|
2020-05-04 12:48:27 +02:00
|
|
|
|
SetKeyFileInfo(file);
|
|
|
|
|
|
|
|
|
|
await _mediator.Send(new GenerateKeyFileCommand { KeyFilePath = KeyFilePath });
|
2020-04-22 18:12:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 12:48:27 +02:00
|
|
|
|
private void SetKeyFileInfo(FileInfo file)
|
|
|
|
|
{
|
|
|
|
|
KeyFilePath = file.Id;
|
|
|
|
|
KeyFileText = file.Name;
|
|
|
|
|
HasKeyFile = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 18:12:28 +02:00
|
|
|
|
private void GenerateCredentials()
|
|
|
|
|
{
|
2020-04-23 19:00:38 +02:00
|
|
|
|
MessengerInstance.Send(new CredentialsSetMessage
|
2020-04-22 18:12:28 +02:00
|
|
|
|
{
|
|
|
|
|
Password = HasPassword ? Password : null,
|
|
|
|
|
KeyFilePath = HasKeyFile ? KeyFilePath : null
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|