mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Most services are implemented as command/queries
Code cleanup
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MediatR;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
|
||||
namespace ModernKeePass.Application.Security.Commands.GenerateKeyFile
|
||||
{
|
||||
public class GenerateKeyFileCommand : IRequest
|
||||
{
|
||||
public string KeyFilePath { get; set; }
|
||||
public bool AddAdditionalEntropy { get; set; }
|
||||
|
||||
public class GenerateKeyFileCommandHandler : IAsyncRequestHandler<GenerateKeyFileCommand>
|
||||
{
|
||||
private readonly IPasswordProxy _security;
|
||||
private readonly IFileProxy _file;
|
||||
|
||||
public GenerateKeyFileCommandHandler(IPasswordProxy security, IFileProxy file)
|
||||
{
|
||||
_security = security;
|
||||
_file = file;
|
||||
}
|
||||
|
||||
public async Task Handle(GenerateKeyFileCommand message)
|
||||
{
|
||||
byte[] entropy = null;
|
||||
if (message.AddAdditionalEntropy)
|
||||
{
|
||||
entropy = new byte[10];
|
||||
var random = new Random();
|
||||
random.NextBytes(entropy);
|
||||
}
|
||||
var keyFile = _security.GenerateKeyFile(entropy);
|
||||
await _file.WriteBinaryContentsToFile(message.KeyFilePath, keyFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
using MediatR;
|
||||
using ModernKeePass.Application.Common.Interfaces;
|
||||
using ModernKeePass.Domain.Dtos;
|
||||
|
||||
namespace ModernKeePass.Application.Security.Commands.GeneratePassword
|
||||
{
|
||||
public class GeneratePasswordCommand: IRequest<string>
|
||||
{
|
||||
public int PasswordLength { get; set; }
|
||||
public bool UpperCasePatternSelected { get; set; }
|
||||
public bool LowerCasePatternSelected { get; set; }
|
||||
public bool DigitsPatternSelected { get; set; }
|
||||
public bool SpecialPatternSelected { get; set; }
|
||||
public bool MinusPatternSelected { get; set; }
|
||||
public bool UnderscorePatternSelected { get; set; }
|
||||
public bool SpacePatternSelected { get; set; }
|
||||
public bool BracketsPatternSelected { get; set; }
|
||||
public string CustomChars { get; set; }
|
||||
|
||||
public class GeneratePasswordCommandHandler: IRequestHandler<GeneratePasswordCommand, string>
|
||||
{
|
||||
private readonly IPasswordProxy _security;
|
||||
|
||||
public GeneratePasswordCommandHandler(IPasswordProxy security)
|
||||
{
|
||||
_security = security;
|
||||
}
|
||||
|
||||
public string Handle(GeneratePasswordCommand message)
|
||||
{
|
||||
var options = new PasswordGenerationOptions
|
||||
{
|
||||
PasswordLength = message.PasswordLength,
|
||||
BracketsPatternSelected = message.BracketsPatternSelected,
|
||||
CustomChars = message.CustomChars,
|
||||
DigitsPatternSelected = message.DigitsPatternSelected,
|
||||
LowerCasePatternSelected = message.LowerCasePatternSelected,
|
||||
MinusPatternSelected = message.MinusPatternSelected,
|
||||
SpacePatternSelected = message.SpacePatternSelected,
|
||||
SpecialPatternSelected = message.SpecialPatternSelected,
|
||||
UnderscorePatternSelected = message.UnderscorePatternSelected,
|
||||
UpperCasePatternSelected = message.UpperCasePatternSelected
|
||||
};
|
||||
return _security.GeneratePassword(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user