Ensure containers show up in the menu when they are created
This commit is contained in:
@@ -5,26 +5,27 @@ using Toolkit.Foundation;
|
||||
|
||||
namespace Bitvault;
|
||||
|
||||
public class CreateContainerHandler(IContainerComponentFactory componentFactory) :
|
||||
IHandler<Create<Vault>, bool>
|
||||
public class CreateContainerHandler(IContainerComponentFactory componentFactory,
|
||||
IPublisher publisher) :
|
||||
IHandler<Create<Container>, bool>
|
||||
{
|
||||
public async Task<bool> Handle(Create<Vault> args,
|
||||
public async Task<bool> Handle(Create<Container> args,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (args.Value is Vault vault && vault.Name is { Length: > 0 } name &&
|
||||
vault.Password is { Length: > 0 } password)
|
||||
if (args.Value is Container container && container.Name is { Length: > 0 } name &&
|
||||
container.Password is { Length: > 0 } password)
|
||||
{
|
||||
if (componentFactory.Create(name) is IComponentHost host)
|
||||
{
|
||||
ISecurityKeyFactory keyVaultFactory = host.Services.GetRequiredService<ISecurityKeyFactory>();
|
||||
IContainer<SecurityKey> vaultKeyContainer = host.Services.GetRequiredService<IContainer<SecurityKey>>();
|
||||
IContainer vaultStorage = host.Services.GetRequiredService<IContainer>();
|
||||
IValueStore<SecurityKey> secureKeyStore = host.Services.GetRequiredService<IValueStore<SecurityKey>>();
|
||||
IContainerFactory containerFactory = host.Services.GetRequiredService<IContainerFactory>();
|
||||
|
||||
if (keyVaultFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key)
|
||||
{
|
||||
vaultKeyContainer.Set(key);
|
||||
secureKeyStore.Set(key);
|
||||
|
||||
if (await vaultStorage.Create(name, key))
|
||||
if (await containerFactory.Create(name, key))
|
||||
{
|
||||
IWritableConfiguration<ContainerConfiguration> configuration =
|
||||
host.Services.GetRequiredService<IWritableConfiguration<ContainerConfiguration>>();
|
||||
@@ -32,6 +33,7 @@ public class CreateContainerHandler(IContainerComponentFactory componentFactory)
|
||||
configuration.Write(args => args.Key = $"{Convert.ToBase64String(key.Salt)}:{Convert.ToBase64String(key.EncryptedKey)}:{Convert.ToBase64String(key.DecryptedKey)}");
|
||||
host.Start();
|
||||
|
||||
await publisher.Publish(Activated.As(host), cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user