From 2a4194ee22af477ba50364ac358f357bd2558b93 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Mon, 29 Apr 2024 21:42:04 +0100 Subject: [PATCH] encyption wip --- Bitvault.Avalonia/App.axaml.cs | 20 ++++-- Bitvault.Avalonia/CreateVaultView.axaml | 12 +++- Bitvault.Avalonia/LockView.axaml | 13 ++-- Bitvault/AesDecryptor.cs | 29 ++++++++ Bitvault/AesEncryptor.cs | 29 ++++++++ Bitvault/CreateVaultHandler.cs | 32 +++++++++ Bitvault/CreateVaultStorageHandler.cs | 73 +++++++++++++++++++++ Bitvault/CreateVaultViewModel.cs | 6 +- Bitvault/IDecryptor.cs | 7 ++ Bitvault/IEncryptor.cs | 8 +++ Bitvault/IKeyDeriver.cs | 6 ++ Bitvault/IPasswordHasher.cs | 6 ++ Bitvault/IVaultConnectionPersistence.cs | 9 +++ Bitvault/IVaultFactory.cs | 11 ---- Bitvault/KeyDeriver.cs | 13 ++++ Bitvault/LockViewModel.cs | 21 +++++- Bitvault/OpenVaultHandler.cs | 24 +++++++ Bitvault/OpenVaultStorageHandler.cs | 41 ++++++++++++ Bitvault/PasswordHasher.cs | 19 ++++++ Bitvault/Vault.cs | 30 ++++++++- Bitvault/VaultCollectionInitializer.cs | 21 ++++++ Bitvault/VaultConnectionPersistence.cs | 24 +++++++ Bitvault/VaultHandler.cs | 29 -------- Bitvault/VaultInitializer.cs | 26 -------- Bitvault/VaultNavigationViewModelHandler.cs | 20 ++++-- Bitvault/VaultStorageHandler.cs | 27 -------- Bitvault/VaultsInitializer.cs | 18 ----- 27 files changed, 437 insertions(+), 137 deletions(-) create mode 100644 Bitvault/AesDecryptor.cs create mode 100644 Bitvault/AesEncryptor.cs create mode 100644 Bitvault/CreateVaultHandler.cs create mode 100644 Bitvault/CreateVaultStorageHandler.cs create mode 100644 Bitvault/IDecryptor.cs create mode 100644 Bitvault/IEncryptor.cs create mode 100644 Bitvault/IKeyDeriver.cs create mode 100644 Bitvault/IPasswordHasher.cs create mode 100644 Bitvault/IVaultConnectionPersistence.cs delete mode 100644 Bitvault/IVaultFactory.cs create mode 100644 Bitvault/KeyDeriver.cs create mode 100644 Bitvault/OpenVaultHandler.cs create mode 100644 Bitvault/OpenVaultStorageHandler.cs create mode 100644 Bitvault/PasswordHasher.cs create mode 100644 Bitvault/VaultCollectionInitializer.cs create mode 100644 Bitvault/VaultConnectionPersistence.cs delete mode 100644 Bitvault/VaultHandler.cs delete mode 100644 Bitvault/VaultInitializer.cs delete mode 100644 Bitvault/VaultStorageHandler.cs delete mode 100644 Bitvault/VaultsInitializer.cs diff --git a/Bitvault.Avalonia/App.axaml.cs b/Bitvault.Avalonia/App.axaml.cs index 1e9f4d4..e2b35be 100644 --- a/Bitvault.Avalonia/App.axaml.cs +++ b/Bitvault.Avalonia/App.axaml.cs @@ -1,7 +1,6 @@ using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; -using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -55,14 +54,25 @@ public partial class App : Application { args.AddServices(services => { + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); + services.AddDbContextFactory(args => { args.UseSqlite(); }); services.AddDbContextFactory(); - services.AddHandler(); + services.AddHandler(); + + services.AddHandler(); + services.AddHandler(); + + services.AddTemplate(); services.AddTemplate(); services.AddTemplate(); services.AddTemplate(); @@ -74,15 +84,13 @@ public partial class App : Application })!); services.AddSingleton(); - services.AddHandler(); + services.AddHandler(); - //services.AddInitializer(); + services.AddInitializer(); services.AddTemplate("Main"); - services.AddTemplate(); services.AddHandler(); - services.AddTransient(); services.AddTemplate(); diff --git a/Bitvault.Avalonia/CreateVaultView.axaml b/Bitvault.Avalonia/CreateVaultView.axaml index f09c84d..a0be7d8 100644 --- a/Bitvault.Avalonia/CreateVaultView.axaml +++ b/Bitvault.Avalonia/CreateVaultView.axaml @@ -12,7 +12,15 @@ Margin="0,0,0,18" Text="{Binding Name}" Watermark="Enter vault name" /> - - + + diff --git a/Bitvault.Avalonia/LockView.axaml b/Bitvault.Avalonia/LockView.axaml index d3abba9..959c287 100644 --- a/Bitvault.Avalonia/LockView.axaml +++ b/Bitvault.Avalonia/LockView.axaml @@ -1,16 +1,19 @@ + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:vm="using:Bitvault" + x:DataType="vm:LockViewModel"> + PasswordChar="●" + Text="{Binding Password}"> - - - + + + diff --git a/Bitvault/AesDecryptor.cs b/Bitvault/AesDecryptor.cs new file mode 100644 index 0000000..76db061 --- /dev/null +++ b/Bitvault/AesDecryptor.cs @@ -0,0 +1,29 @@ +using System.Security.Cryptography; + +namespace Bitvault; + +public class AesDecryptor : + IDecryptor +{ + private const int IvSize = 16; + + public string Decrypt(string cipherText, byte[] key) + { + byte[] cipherData = Convert.FromBase64String(cipherText); + + byte[] iv = new byte[IvSize]; + Array.Copy(cipherData, 0, iv, 0, IvSize); // Extract the IV from the start of the cipher data + + using var aes = Aes.Create(); + aes.Key = key; + aes.IV = iv; + + using var memoryStream = new MemoryStream(cipherData, IvSize, cipherData.Length - IvSize); + using (var decryptor = aes.CreateDecryptor(aes.Key, aes.IV)) + using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) + using (var streamReader = new StreamReader(cryptoStream)) + { + return streamReader.ReadToEnd(); // Return the decrypted text + } + } +} diff --git a/Bitvault/AesEncryptor.cs b/Bitvault/AesEncryptor.cs new file mode 100644 index 0000000..6f1c168 --- /dev/null +++ b/Bitvault/AesEncryptor.cs @@ -0,0 +1,29 @@ +using System.Security.Cryptography; + +namespace Bitvault; + +public class AesEncryptor : IEncryptor +{ + public string Encrypt(string plainText, byte[] key) + { + const int IvSize = 16; + + using var aes = Aes.Create(); + aes.Key = key; + aes.GenerateIV(); + + byte[] iv = aes.IV; + + using var memoryStream = new MemoryStream(); + memoryStream.Write(iv, 0, IvSize); // Store IV at the start of the stream + + using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV)) + using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) + using (var streamWriter = new StreamWriter(cryptoStream)) + { + streamWriter.Write(plainText); + } + + return Convert.ToBase64String(memoryStream.ToArray()); // Return the encrypted data in base64 + } +} diff --git a/Bitvault/CreateVaultHandler.cs b/Bitvault/CreateVaultHandler.cs new file mode 100644 index 0000000..b5ba8e3 --- /dev/null +++ b/Bitvault/CreateVaultHandler.cs @@ -0,0 +1,32 @@ +using Microsoft.Extensions.DependencyInjection; +using Toolkit.Foundation; + +namespace Bitvault; + +public class CreateVaultHandler(IComponentFactory componentFactory) : + IHandler, bool> +{ + public async Task Handle(Create args, + CancellationToken cancellationToken) + { + if (args.Value is Vault vault) + { + if (vault.Name is { Length: > 0 } name && vault.Password is { Length: > 0 } password) + { + if (componentFactory.Create($"Vault:{name}", new VaultConfiguration { Name = name }) is IComponentHost host) + { + if (host.Services.GetRequiredService() is IMediator mediator) + { + if (await mediator.Handle, bool>(Create.As(new VaultStorage(name, password)), cancellationToken)) + { + await host.StartAsync(cancellationToken); + return true; + } + } + } + } + } + + return false; + } +} \ No newline at end of file diff --git a/Bitvault/CreateVaultStorageHandler.cs b/Bitvault/CreateVaultStorageHandler.cs new file mode 100644 index 0000000..2ac39ea --- /dev/null +++ b/Bitvault/CreateVaultStorageHandler.cs @@ -0,0 +1,73 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; +using System.Security.Cryptography; +using Toolkit.Foundation; + +namespace Bitvault; + +public class CreateVaultStorageHandler(IHostEnvironment environment, + IKeyDeriver deriver, + IEncryptor encryptor, + IDecryptor decryptor, + IDbContextFactory dbContextFactory) : IHandler, bool> +{ + public async Task Handle(Create args, CancellationToken cancellationToken) + { + if (args.Value is VaultStorage vault) + { + if (vault.Name is { Length: > 0 } name && vault.Password is { Length: > 0 } password) + { + byte[] salt = new byte[16]; + RandomNumberGenerator.Fill(salt); + + byte[] key = new byte[32]; + RandomNumberGenerator.Fill(key); + + byte[] derivedKey = deriver.DeriveKey(password, salt); + string? encryptedKey = encryptor.Encrypt(Convert.ToBase64String(key), derivedKey); + + + byte[] derivedKey2 = deriver.DeriveKey(password, salt); + var dod = decryptor.Decrypt(encryptedKey, derivedKey2); + + // Derive the key for encryption + + //byte[] encryptionKey = deriver.DeriveKey(password, salt); + + //// Derive the key for decryption + //byte[] decryptionKey = deriver.DeriveKey(password, salt); + + //// Compare keys to ensure they're the same + //bool areKeysEqual = encryptionKey.SequenceEqual(decryptionKey); + + ////byte[] derivedKey = deriver.DeriveKey(password, salt); + //string? encrypted = encryptor.Encrypt(password, derivedKey); + + //var storedSalt = Convert.ToBase64String(salt); + + + //byte[] derivedKey2 = deriver.DeriveKey(password, salt); + + //var d = decryptor.Decrypt(encrypted, derivedKey2); + + // Generate a hash + //string hash = hasher.HashPassword(password); + + //string[] parts = hash.Split(':'); + + //// Store the salt only + //string storedSalt = parts[0]; + + //// Use the hash as the password + //string storedHash = parts[1]; + + //context.Database.SetConnectionString($"Data Source={Path.Combine(environment.ContentRootPath, name)}.vault;Mode=ReadWriteCreate;Password={storedHash}"); + //await context.Database.EnsureCreatedAsync(); + + return true; + } + } + + return false; + } +} \ No newline at end of file diff --git a/Bitvault/CreateVaultViewModel.cs b/Bitvault/CreateVaultViewModel.cs index d772ea0..90c5eb7 100644 --- a/Bitvault/CreateVaultViewModel.cs +++ b/Bitvault/CreateVaultViewModel.cs @@ -17,8 +17,12 @@ public partial class CreateVaultViewModel(IServiceProvider provider, [ObservableProperty] private string name; + [MaybeNull] + [ObservableProperty] + private string password; + public async Task Confirm() { - return await Mediator.Handle, bool>(Create.As(new Vault(Name, ""))); + return await Mediator.Handle, bool>(Create.As(new Vault(Name, Password))); } } \ No newline at end of file diff --git a/Bitvault/IDecryptor.cs b/Bitvault/IDecryptor.cs new file mode 100644 index 0000000..8a59e8e --- /dev/null +++ b/Bitvault/IDecryptor.cs @@ -0,0 +1,7 @@ +namespace Bitvault +{ + public interface IDecryptor + { + string? Decrypt(string cipherText, byte[] key); + } +} \ No newline at end of file diff --git a/Bitvault/IEncryptor.cs b/Bitvault/IEncryptor.cs new file mode 100644 index 0000000..5deea87 --- /dev/null +++ b/Bitvault/IEncryptor.cs @@ -0,0 +1,8 @@ +namespace Bitvault +{ + public interface IEncryptor + { + + string? Encrypt(string plainText, byte[] key); + } +} \ No newline at end of file diff --git a/Bitvault/IKeyDeriver.cs b/Bitvault/IKeyDeriver.cs new file mode 100644 index 0000000..a181c48 --- /dev/null +++ b/Bitvault/IKeyDeriver.cs @@ -0,0 +1,6 @@ +namespace Bitvault; + +public interface IKeyDeriver +{ + byte[] DeriveKey(string password, byte[] salt, int keySize = 32, int iterations = 10000); +} diff --git a/Bitvault/IPasswordHasher.cs b/Bitvault/IPasswordHasher.cs new file mode 100644 index 0000000..27e66d0 --- /dev/null +++ b/Bitvault/IPasswordHasher.cs @@ -0,0 +1,6 @@ +namespace Bitvault; + +public interface IPasswordHasher +{ + string HashPassword(string password, int iterations = 10000); +} \ No newline at end of file diff --git a/Bitvault/IVaultConnectionPersistence.cs b/Bitvault/IVaultConnectionPersistence.cs new file mode 100644 index 0000000..c1b56b3 --- /dev/null +++ b/Bitvault/IVaultConnectionPersistence.cs @@ -0,0 +1,9 @@ +namespace Bitvault +{ + public interface IVaultConnectionPersistence + { + void Dispose(); + string? Get(string key); + void Set(string key, string connection); + } +} \ No newline at end of file diff --git a/Bitvault/IVaultFactory.cs b/Bitvault/IVaultFactory.cs deleted file mode 100644 index 75dcc2b..0000000 --- a/Bitvault/IVaultFactory.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Toolkit.Foundation; - -namespace Bitvault -{ - public interface IVaultFactory - { - IComponentHost? Create(string name, - string password, - VaultConfiguration configuration); - } -} \ No newline at end of file diff --git a/Bitvault/KeyDeriver.cs b/Bitvault/KeyDeriver.cs new file mode 100644 index 0000000..f82e14d --- /dev/null +++ b/Bitvault/KeyDeriver.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; + +namespace Bitvault; + +public class KeyDeriver : + IKeyDeriver +{ + public byte[] DeriveKey(string password, byte[] salt, int keySize = 32, int iterations = 100000) + { + using Rfc2898DeriveBytes pbkdf2 = new(password, salt, iterations, HashAlgorithmName.SHA256); + return pbkdf2.GetBytes(keySize); + } +} diff --git a/Bitvault/LockViewModel.cs b/Bitvault/LockViewModel.cs index 806ef5d..a87e320 100644 --- a/Bitvault/LockViewModel.cs +++ b/Bitvault/LockViewModel.cs @@ -1,11 +1,26 @@ -using Toolkit.Foundation; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using Toolkit.Foundation; namespace Bitvault; -public class LockViewModel(IServiceProvider provider, +public partial class LockViewModel(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, ISubscriber subscriber, IDisposer disposer) : - ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer); \ No newline at end of file + ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer) +{ + [ObservableProperty] + private string? password; + + [RelayCommand] + private void Unlock() + { + if (Password is { Length: > 0 }) + { + Mediator.Handle, bool>(Open.As(new Vault(Password))); + } + } +} \ No newline at end of file diff --git a/Bitvault/OpenVaultHandler.cs b/Bitvault/OpenVaultHandler.cs new file mode 100644 index 0000000..5ad2cc3 --- /dev/null +++ b/Bitvault/OpenVaultHandler.cs @@ -0,0 +1,24 @@ +using Toolkit.Foundation; + +namespace Bitvault; + +public class OpenVaultHandler(IMediator mediator) : + IHandler, bool> +{ + public async Task Handle(Open args, + CancellationToken cancellationToken) + { + if (args.Value is Vault vault) + { + if (vault.Password is { Length: > 0 } password) + { + if (await mediator.Handle, bool>(Open.As(new VaultStorage("Personal", password)), cancellationToken)) + { + return true; + } + } + } + + return false; + } +} \ No newline at end of file diff --git a/Bitvault/OpenVaultStorageHandler.cs b/Bitvault/OpenVaultStorageHandler.cs new file mode 100644 index 0000000..0d74fea --- /dev/null +++ b/Bitvault/OpenVaultStorageHandler.cs @@ -0,0 +1,41 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; +using Toolkit.Foundation; + +namespace Bitvault; + +public class OpenVaultStorageHandler(IHostEnvironment environment, + IDbContextFactory dbContextFactory) : IHandler, bool> +{ + public async Task Handle(Open args, CancellationToken cancellationToken) + { + if (args.Value is VaultStorage vault) + { + if (vault.Name is { Length: > 0 } name && vault.Password is { Length: > 0 } password) + { + using VaultDbContext context = dbContextFactory.CreateDbContext(); + var d = context.Database.GetDbConnection().ConnectionString; + context.Database.SetConnectionString($"Data Source={Path.Combine(environment.ContentRootPath, name)}.vault;Mode=ReadWriteCreate;Password={password}"); + + bool isOpen = false; + await Task.Run(async () => + { + try + { + await context.Database.OpenConnectionAsync(); + isOpen = true; + } + catch + { + // We are ignoring this exception as it is either a go, or not. + } + + }, cancellationToken); + + return isOpen; + } + } + + return false; + } +} diff --git a/Bitvault/PasswordHasher.cs b/Bitvault/PasswordHasher.cs new file mode 100644 index 0000000..b077e8f --- /dev/null +++ b/Bitvault/PasswordHasher.cs @@ -0,0 +1,19 @@ +using System.Security.Cryptography; + +namespace Bitvault; + +public class PasswordHasher : + IPasswordHasher +{ + private const int SaltSize = 16; + + public string HashPassword(string password, int iterations = 10000) + { + using Rfc2898DeriveBytes pbkdf2 = new(password, SaltSize, iterations, HashAlgorithmName.SHA256); + + byte[] salt = pbkdf2.Salt; + byte[] hash = pbkdf2.GetBytes(32); + + return $"{Convert.ToBase64String(salt)}:{Convert.ToBase64String(hash)}"; + } +} diff --git a/Bitvault/Vault.cs b/Bitvault/Vault.cs index 058004b..5f7e3ce 100644 --- a/Bitvault/Vault.cs +++ b/Bitvault/Vault.cs @@ -1,3 +1,29 @@ -namespace Bitvault; +using System.Diagnostics.CodeAnalysis; -public record Vault(string Name, string Password); \ No newline at end of file +namespace Bitvault; + +public record Vault +{ + public Vault(string name, string password) + { + Name = name; + Password = password; + } + + public Vault(string password) + { + Password = password; + } + + + public Vault() + { + + } + + [MaybeNull] + public string Name { get; } + + [MaybeNull] + public string? Password { get; } +} diff --git a/Bitvault/VaultCollectionInitializer.cs b/Bitvault/VaultCollectionInitializer.cs new file mode 100644 index 0000000..a4134b3 --- /dev/null +++ b/Bitvault/VaultCollectionInitializer.cs @@ -0,0 +1,21 @@ +using Toolkit.Foundation; + +namespace Bitvault; + +public class VaultCollectionInitializer(IEnumerable> configurations, + IComponentFactory componentFactory, + IVaultHostCollection vaults) : IInitializer +{ + public async Task Initialize() + { + foreach (IConfigurationDescriptor configuration in configurations) + { + if (componentFactory.Create(configuration.Section, configuration.Value) is IComponentHost host) + { + vaults.Add(host); + await host.StartAsync(); + } + + } + } +} diff --git a/Bitvault/VaultConnectionPersistence.cs b/Bitvault/VaultConnectionPersistence.cs new file mode 100644 index 0000000..5345973 --- /dev/null +++ b/Bitvault/VaultConnectionPersistence.cs @@ -0,0 +1,24 @@ +namespace Bitvault; + +public class VaultConnectionPersistence : + IVaultConnectionPersistence, + IDisposable +{ + private string? connection; + + public void Dispose() + { + connection = null; + } + + public string? Get(string key) + { + return connection; + } + + public void Set(string key, + string connection) + { + this.connection = connection; + } +} diff --git a/Bitvault/VaultHandler.cs b/Bitvault/VaultHandler.cs deleted file mode 100644 index dbefb36..0000000 --- a/Bitvault/VaultHandler.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Toolkit.Foundation; - -namespace Bitvault; - - -public class VaultHandler(IComponentFactory factory) : - IHandler, bool> -{ - public async Task Handle(Create args, - CancellationToken cancellationToken) - { - if (args.Value is Vault vault) - { - if (factory.Create($"Vault:{vault.Name}", new VaultConfiguration { Name = vault.Name }) is IComponentHost host) - { - if (host.Services.GetRequiredService() is IMediator mediator) - { - if (await mediator.Handle, bool>(Create.As(new VaultStorage(vault.Name, vault.Password)), cancellationToken)) - { - await host.StartAsync(cancellationToken); - } - } - } - } - - return false; - } -} \ No newline at end of file diff --git a/Bitvault/VaultInitializer.cs b/Bitvault/VaultInitializer.cs deleted file mode 100644 index 50a3f80..0000000 --- a/Bitvault/VaultInitializer.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Toolkit.Foundation; - -namespace Bitvault; - -public class VaultInitializer(IServiceProvider provider, - IProxyService publisher) : IInitializer -{ - public async Task Initialize() - { - if (provider.GetService() is IComponentHost vault) - { - if (vault.Services.GetRequiredService() is VaultConfiguration configuration) - { - if (vault.Services.GetRequiredService() is IServiceFactory factory) - { - if (factory.Create(configuration.Name) is VaultNavigationViewModel viewModel) - { - await publisher.Proxy.Publish(new Create(viewModel), - nameof(MainViewModel)); - } - } - } - } - } -} \ No newline at end of file diff --git a/Bitvault/VaultNavigationViewModelHandler.cs b/Bitvault/VaultNavigationViewModelHandler.cs index c9a74a9..31c61f7 100644 --- a/Bitvault/VaultNavigationViewModelHandler.cs +++ b/Bitvault/VaultNavigationViewModelHandler.cs @@ -1,21 +1,27 @@ -using Toolkit.Foundation; +using Microsoft.Extensions.DependencyInjection; +using Toolkit.Foundation; namespace Bitvault; public class VaultNavigationViewModelHandler(IPublisher publisher, - IServiceFactory factory, - IEnumerable> descriptors) : + IVaultHostCollection vaults) : INotificationHandler> { public async Task Handle(Enumerate args, CancellationToken cancellationToken = default) { - foreach (IConfigurationDescriptor descriptor in descriptors) + foreach (IComponentHost vault in vaults) { - if (factory.Create(descriptor.Value.Name) is VaultNavigationViewModel viewModel) + if (vault.Services.GetRequiredService() is VaultConfiguration configuration) { - await publisher.Publish(new Create(viewModel), - nameof(MainViewModel), cancellationToken); + if (vault.Services.GetRequiredService() is IServiceFactory factory) + { + if (factory.Create(configuration.Name) is VaultNavigationViewModel viewModel) + { + await publisher.Publish(new Create(viewModel), + nameof(MainViewModel), cancellationToken); + } + } } } } diff --git a/Bitvault/VaultStorageHandler.cs b/Bitvault/VaultStorageHandler.cs deleted file mode 100644 index e1e1255..0000000 --- a/Bitvault/VaultStorageHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Hosting; -using Toolkit.Foundation; - -namespace Bitvault; - -public class VaultStorageHandler(IHostEnvironment environment, - IDbContextFactory dbContextFactory) : - IHandler, bool> -{ - public async Task Handle(Create args, CancellationToken cancellationToken) - { - if (args.Value is VaultStorage storage) - { - using VaultDbContext context = dbContextFactory.CreateDbContext(); - await Task.Run(async () => - { - context.Database.SetConnectionString($"Data Source={Path.Combine(environment.ContentRootPath, storage.Name)}.vault;Mode=ReadWriteCreate;Password={storage.Password}"); - await context.Database.EnsureCreatedAsync(cancellationToken); - }, cancellationToken); - - return true; - } - - return false; - } -} diff --git a/Bitvault/VaultsInitializer.cs b/Bitvault/VaultsInitializer.cs deleted file mode 100644 index c454762..0000000 --- a/Bitvault/VaultsInitializer.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Toolkit.Foundation; - -namespace Bitvault; - -public class VaultsInitializer(IEnumerable> configurations, - IVaultFactory factory) : IInitializer -{ - public async Task Initialize() - { - foreach (IConfigurationDescriptor configuration in configurations) - { - //if (factory.Create(configuration.Section, configuration.Value) is IComponentHost host) - //{ - // await host.StartAsync(); - //} - } - } -} \ No newline at end of file