mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Added lots of commands
Simplified KeePass client
This commit is contained in:
31
ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs
Normal file
31
ModernKeePass.Infrastructure/KeePass/GroupMappingProfile.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using AutoMapper;
|
||||
using ModernKeePass.Domain.Entities;
|
||||
using ModernKeePassLib;
|
||||
|
||||
namespace ModernKeePass.Infrastructure.KeePass
|
||||
{
|
||||
public class GroupMappingProfile : Profile
|
||||
{
|
||||
public GroupMappingProfile()
|
||||
{
|
||||
FromModelToDto();
|
||||
FromDtoToModel();
|
||||
}
|
||||
|
||||
private void FromDtoToModel()
|
||||
{
|
||||
CreateMap<PwGroup, GroupEntity>()
|
||||
.ForMember(d => d.Id, opts => opts.MapFrom(s => s.Uuid.ToHexString()))
|
||||
.ForMember(d => d.Name, opts => opts.MapFrom(s => s.Name))
|
||||
.ForMember(d => d.Icon, opts => opts.MapFrom(s => IconMapper.MapPwIconToIcon(s.IconId)))
|
||||
.ForMember(d => d.LastModificationDate, opts => opts.MapFrom(s => s.LastModificationTime))
|
||||
.ForMember(d => d.Entries, opts => opts.MapFrom(s => s.Entries))
|
||||
.ForMember(d => d.SubGroups, opts => opts.MapFrom(s => s.Groups));
|
||||
}
|
||||
|
||||
private void FromModelToDto()
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@@ -7,7 +7,6 @@ using ModernKeePass.Domain.Dtos;
|
||||
using ModernKeePass.Domain.Entities;
|
||||
using ModernKeePass.Domain.Exceptions;
|
||||
using ModernKeePassLib;
|
||||
using ModernKeePassLib.Cryptography.Cipher;
|
||||
using ModernKeePassLib.Cryptography.KeyDerivation;
|
||||
using ModernKeePassLib.Interfaces;
|
||||
using ModernKeePassLib.Keys;
|
||||
@@ -23,68 +22,41 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
private readonly IMapper _mapper;
|
||||
private readonly PwDatabase _pwDatabase = new PwDatabase();
|
||||
private string _fileAccessToken;
|
||||
private CompositeKey _compositeKey;
|
||||
private Credentials _credentials;
|
||||
|
||||
// Main information
|
||||
public bool IsOpen => (_pwDatabase?.IsOpen).GetValueOrDefault();
|
||||
public string Name => _pwDatabase?.Name;
|
||||
public GroupEntity RootGroup { get; set; }
|
||||
public GroupEntity RootGroup { get; private set; }
|
||||
|
||||
// Settings
|
||||
public GroupEntity RecycleBin
|
||||
public string RecycleBinId
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pwDatabase.RecycleBinEnabled)
|
||||
{
|
||||
var pwGroup = _pwDatabase.RootGroup.FindGroup(_pwDatabase.RecycleBinUuid, true);
|
||||
var group = new GroupEntity
|
||||
{
|
||||
Id = pwGroup.Uuid.ToHexString(),
|
||||
Name = pwGroup.Name,
|
||||
Icon = IconMapper.MapPwIconToIcon(pwGroup.IconId),
|
||||
Entries = pwGroup.Entries.Select(e => _mapper.Map<EntryEntity>(e)).ToList(),
|
||||
SubGroups = pwGroup.Groups.Select(BuildHierarchy).ToList()
|
||||
};
|
||||
return group;
|
||||
return pwGroup.Uuid.ToHexString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
set
|
||||
{
|
||||
|
||||
}
|
||||
set { _pwDatabase.RecycleBinUuid = BuildIdFromString(value); }
|
||||
}
|
||||
public BaseEntity Cipher
|
||||
public string CipherId
|
||||
{
|
||||
get
|
||||
{
|
||||
var cipher = CipherPool.GlobalPool.GetCipher(_pwDatabase.DataCipherUuid);
|
||||
return new BaseEntity
|
||||
{
|
||||
Id = cipher.CipherUuid.ToHexString(),
|
||||
Name = cipher.DisplayName
|
||||
};
|
||||
}
|
||||
set { _pwDatabase.DataCipherUuid = BuildIdFromString(value.Id); }
|
||||
get { return _pwDatabase.DataCipherUuid.ToHexString(); }
|
||||
set { _pwDatabase.DataCipherUuid = BuildIdFromString(value); }
|
||||
}
|
||||
|
||||
public BaseEntity KeyDerivation
|
||||
public string KeyDerivationId
|
||||
{
|
||||
get
|
||||
{
|
||||
var keyDerivation = KdfPool.Engines.First(e => e.Uuid.Equals(_pwDatabase.KdfParameters.KdfUuid));
|
||||
return new BaseEntity
|
||||
{
|
||||
Id = keyDerivation.Uuid.ToHexString(),
|
||||
Name = keyDerivation.Name
|
||||
};
|
||||
}
|
||||
get { return _pwDatabase.KdfParameters.KdfUuid.ToHexString(); }
|
||||
set
|
||||
{
|
||||
_pwDatabase.KdfParameters = KdfPool.Engines
|
||||
.FirstOrDefault(e => e.Uuid.Equals(BuildIdFromString(value.Name)))?.GetDefaultParameters();
|
||||
.FirstOrDefault(e => e.Uuid.Equals(BuildIdFromString(value)))?.GetDefaultParameters();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +66,8 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
set { _pwDatabase.Compression = (PwCompressionAlgorithm) Enum.Parse(typeof(PwCompressionAlgorithm), value); }
|
||||
}
|
||||
|
||||
public bool IsRecycleBinEnabled => _pwDatabase.RecycleBinEnabled;
|
||||
|
||||
public KeePassDatabaseClient(ISettingsProxy settings, IFileProxy fileService, IMapper mapper)
|
||||
{
|
||||
_settings = settings;
|
||||
@@ -101,22 +75,19 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<DatabaseEntity> Open(FileInfo fileInfo, Credentials credentials)
|
||||
public async Task<GroupEntity> Open(FileInfo fileInfo, Credentials credentials)
|
||||
{
|
||||
try
|
||||
{
|
||||
_compositeKey = await CreateCompositeKey(credentials);
|
||||
var compositeKey = await CreateCompositeKey(credentials);
|
||||
var ioConnection = await BuildConnectionInfo(fileInfo);
|
||||
|
||||
_pwDatabase.Open(ioConnection, _compositeKey, new NullStatusLogger());
|
||||
_pwDatabase.Open(ioConnection, compositeKey, new NullStatusLogger());
|
||||
|
||||
_credentials = credentials;
|
||||
_fileAccessToken = fileInfo.Path;
|
||||
|
||||
return new DatabaseEntity
|
||||
{
|
||||
Name = _pwDatabase.Name,
|
||||
RootGroupEntity = BuildHierarchy(_pwDatabase.RootGroup)
|
||||
};
|
||||
return _mapper.Map<GroupEntity>(_pwDatabase.RootGroup);
|
||||
}
|
||||
catch (InvalidCompositeKeyException ex)
|
||||
{
|
||||
@@ -124,12 +95,17 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DatabaseEntity> Create(FileInfo fileInfo, Credentials credentials)
|
||||
public async Task<GroupEntity> ReOpen()
|
||||
{
|
||||
_compositeKey = await CreateCompositeKey(credentials);
|
||||
return await Open(new FileInfo {Path = _fileAccessToken}, _credentials);
|
||||
}
|
||||
|
||||
public async Task<GroupEntity> Create(FileInfo fileInfo, Credentials credentials)
|
||||
{
|
||||
var compositeKey = await CreateCompositeKey(credentials);
|
||||
var ioConnection = await BuildConnectionInfo(fileInfo);
|
||||
|
||||
_pwDatabase.New(ioConnection, _compositeKey);
|
||||
_pwDatabase.New(ioConnection, compositeKey);
|
||||
|
||||
var fileFormat = _settings.GetSetting<string>("DefaultFileFormat");
|
||||
switch (fileFormat)
|
||||
@@ -142,10 +118,7 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
_fileAccessToken = fileInfo.Path;
|
||||
|
||||
// TODO: create sample data depending on settings
|
||||
return new DatabaseEntity
|
||||
{
|
||||
RootGroupEntity = BuildHierarchy(_pwDatabase.RootGroup)
|
||||
};
|
||||
return _mapper.Map<GroupEntity>(_pwDatabase.RootGroup);
|
||||
}
|
||||
|
||||
public async Task SaveDatabase()
|
||||
@@ -185,49 +158,78 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
_fileService.ReleaseFile(_fileAccessToken);
|
||||
}
|
||||
|
||||
public async Task AddEntry(GroupEntity parentGroupEntity, EntryEntity entry)
|
||||
public async Task AddEntry(string parentGroupId, string entryId)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupEntity.Id), true);
|
||||
|
||||
var pwEntry = new PwEntry(true, true);
|
||||
_mapper.Map(entry, pwEntry);
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
||||
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(entryId), true);
|
||||
parentPwGroup.AddEntry(pwEntry, true);
|
||||
entry.Id = pwEntry.Uuid.ToHexString();
|
||||
});
|
||||
}
|
||||
public async Task AddGroup(GroupEntity parentGroupEntity, GroupEntity group)
|
||||
public async Task AddGroup(string parentGroupId, string groupId)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupEntity.Id), true);
|
||||
|
||||
var pwGroup = new PwGroup(true, true)
|
||||
{
|
||||
Name = group.Name
|
||||
};
|
||||
parentPwGroup.AddGroup(pwGroup, true);
|
||||
group.Id = pwGroup.Uuid.ToHexString();
|
||||
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
||||
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
|
||||
parentPwGroup.Groups.Add(pwGroup);
|
||||
});
|
||||
}
|
||||
public async Task RemoveEntry(string parentGroupId, string entryId)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
||||
var pwEntry = parentPwGroup.FindEntry(BuildIdFromString(entryId), false);
|
||||
parentPwGroup.Entries.Remove(pwEntry);
|
||||
});
|
||||
}
|
||||
|
||||
public Task UpdateEntry(EntryEntity entry)
|
||||
public async Task RemoveGroup(string parentGroupId, string groupId)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
||||
var pwGroup = parentPwGroup.FindGroup(BuildIdFromString(groupId), false);
|
||||
parentPwGroup.Groups.Remove(pwGroup);
|
||||
});
|
||||
}
|
||||
|
||||
public Task UpdateEntry(string entry)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task UpdateGroup(GroupEntity group)
|
||||
public Task UpdateGroup(string group)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public EntryEntity CreateEntry(string parentGroupId)
|
||||
{
|
||||
var pwEntry = new PwEntry(true, true);
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
||||
parentPwGroup.Entries.Add(pwEntry);
|
||||
|
||||
public async Task DeleteEntry(EntryEntity entry)
|
||||
return _mapper.Map<EntryEntity>(pwEntry);
|
||||
}
|
||||
|
||||
public GroupEntity CreateGroup(string parentGroupId, string name, bool isRecycleBin = false)
|
||||
{
|
||||
var pwGroup = new PwGroup(true, true, name, isRecycleBin? PwIcon.TrashBin : PwIcon.Folder);
|
||||
var parentPwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(parentGroupId), true);
|
||||
parentPwGroup.Groups.Add(pwGroup);
|
||||
if (isRecycleBin) _pwDatabase.RecycleBinUuid = pwGroup.Uuid;
|
||||
|
||||
return _mapper.Map<GroupEntity>(pwGroup);
|
||||
}
|
||||
|
||||
public async Task DeleteEntry(string entryId)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(entry.Id), true);
|
||||
var pwEntry = _pwDatabase.RootGroup.FindEntry(BuildIdFromString(entryId), true);
|
||||
var id = pwEntry.Uuid;
|
||||
pwEntry.ParentGroup.Entries.Remove(pwEntry);
|
||||
|
||||
@@ -237,11 +239,12 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
}
|
||||
});
|
||||
}
|
||||
public async Task DeleteGroup(GroupEntity group)
|
||||
|
||||
public async Task DeleteGroup(string groupId)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(group.Id), true);
|
||||
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
|
||||
var id = pwGroup.Uuid;
|
||||
pwGroup.ParentGroup.Groups.Remove(pwGroup);
|
||||
|
||||
@@ -252,6 +255,19 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
});
|
||||
}
|
||||
|
||||
public void SortEntries(string groupId)
|
||||
{
|
||||
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
|
||||
var comparer = new PwEntryComparer(PwDefs.TitleField, true, false);
|
||||
pwGroup.Entries.Sort(comparer);
|
||||
}
|
||||
|
||||
public void SortSubGroups(string groupId)
|
||||
{
|
||||
var pwGroup = _pwDatabase.RootGroup.FindGroup(BuildIdFromString(groupId), true);
|
||||
pwGroup.SortSubGroups(false);
|
||||
}
|
||||
|
||||
public async Task UpdateCredentials(Credentials credentials)
|
||||
{
|
||||
_pwDatabase.MasterKey = await CreateCompositeKey(credentials);
|
||||
@@ -274,21 +290,7 @@ namespace ModernKeePass.Infrastructure.KeePass
|
||||
var fileContents = await _fileService.OpenBinaryFile(fileInfo.Path);
|
||||
return IOConnectionInfo.FromByteArray(fileContents);
|
||||
}
|
||||
|
||||
private GroupEntity BuildHierarchy(PwGroup pwGroup)
|
||||
{
|
||||
// TODO: build entity hierarchy in an iterative way or implement lazy loading
|
||||
var group = new GroupEntity
|
||||
{
|
||||
Id = pwGroup.Uuid.ToHexString(),
|
||||
Name = pwGroup.Name,
|
||||
Icon = IconMapper.MapPwIconToIcon(pwGroup.IconId),
|
||||
Entries = pwGroup.Entries.Select(e => _mapper.Map<EntryEntity>(e)).ToList(),
|
||||
SubGroups = pwGroup.Groups.Select(BuildHierarchy).ToList()
|
||||
};
|
||||
return group;
|
||||
}
|
||||
|
||||
|
||||
private PwUuid BuildIdFromString(string id)
|
||||
{
|
||||
return new PwUuid(MemUtil.HexStringToByteArray(id));
|
||||
|
Reference in New Issue
Block a user