Skip to content

Commit

Permalink
Add basic File Key Store Import/Export feature (as zip)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Oct 17, 2023
1 parent 8c18351 commit 215e623
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using CommunityToolkit.Mvvm.Input;
using Leosac.KeyManager.Library.UI.Domain;
using Leosac.WpfApp;
using log4net;
using Microsoft.Win32;
using System.IO;
using System.Security.Cryptography;

namespace Leosac.KeyManager.Library.KeyStore.File.UI.Domain
{
public class FileKeyStoreImportExportControlViewModel : KeyStoreAdditionalControlViewModel
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType);

public FileKeyStoreImportExportControlViewModel()
{
ImportCommand = new RelayCommand(Import);

ExportCommand = new RelayCommand(Export);
}

public RelayCommand ImportCommand { get; }

public RelayCommand ExportCommand { get; }

private void Import()
{
var ofd = new OpenFileDialog();
ofd.Filter = "File Key Store Archive (*.zip)|*.zip";
ofd.CheckFileExists = true;
if (KeyStore is FileKeyStore ks && ofd.ShowDialog() == true)
{
FileKeyStoreArchive.Import(ofd.FileName, ks);
}
}

private void Export()
{
var sfd = new SaveFileDialog();
sfd.Filter = "File Key Store Archive (*.zip)|*.zip";
if (KeyStore is FileKeyStore ks && sfd.ShowDialog() == true)
{
try
{
FileKeyStoreArchive.Export(sfd.FileName, ks);
}
catch (Exception ex)
{
log.Error("Export of the File Key Store as an archive failed.", ex);
SnackbarHelper.EnqueueError(SnackbarMessageQueue, ex);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<UserControl x:Class="Leosac.KeyManager.Library.KeyStore.File.UI.FileKeyStoreImportExportControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:domain="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI.Domain"
xmlns:libui="clr-namespace:Leosac.KeyManager.Library.UI;assembly=KeyManager.Library.UI"
xmlns:local="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:properties="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI.Properties"
xmlns:wpfappctrls="clr-namespace:Leosac.WpfApp.Controls;assembly=WpfApp"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance domain:FileKeyStoreImportExportControlViewModel}"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="5" Height="128" Command="{Binding ImportCommand}">
<Button.Content>
<WrapPanel>
<materialDesign:PackIcon Kind="Import" Width="64" Height="64" />
<TextBlock Text="{x:Static properties:Resources.Import}" VerticalAlignment="Center" Margin="3" />
</WrapPanel>
</Button.Content>
</Button>
<Button Grid.Column="1" Margin="5" Height="128" Command="{Binding ExportCommand}">
<Button.Content>
<WrapPanel>
<materialDesign:PackIcon Kind="Export" Width="64" Height="64" />
<TextBlock Text="{x:Static properties:Resources.Export}" VerticalAlignment="Center" Margin="3" />
</WrapPanel>
</Button.Content>
</Button>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Leosac.KeyManager.Library.KeyStore.File.UI.Domain;
using System.Windows.Controls;

namespace Leosac.KeyManager.Library.KeyStore.File.UI
{
/// <summary>
/// Interaction logic for FileKeyStoreImportExportControl.xaml
/// </summary>
public partial class FileKeyStoreImportExportControl : UserControl
{
public FileKeyStoreImportExportControl()
{
InitializeComponent();

DataContext = new FileKeyStoreImportExportControlViewModel();
}
}
}
5 changes: 4 additions & 1 deletion KeyManager.Library.KeyStore.File.UI/FileKeyStoreUIFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public override UserControl CreateKeyStorePropertiesControl()

public override IDictionary<string, UserControl> CreateKeyStoreAdditionalControls()
{
return new Dictionary<string, UserControl>();
return new Dictionary<string, UserControl>
{
{ Properties.Resources.ImportExport, new FileKeyStoreImportExportControl() }
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
Expand Down Expand Up @@ -41,6 +41,9 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Update="FileKeyStoreImportExportControl.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@
<data name="EncryptionKeyHelper" xml:space="preserve">
<value>Clé de chiffrement maitre pour le stockage des clés sur le disque</value>
</data>
<data name="Export" xml:space="preserve">
<value>Export</value>
</data>
<data name="Import" xml:space="preserve">
<value>Import</value>
</data>
<data name="ImportExport" xml:space="preserve">
<value>Import / Export</value>
</data>
<data name="StoreSecret" xml:space="preserve">
<value>Enregistrer</value>
</data>
Expand Down
9 changes: 9 additions & 0 deletions KeyManager.Library.KeyStore.File.UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@
<data name="EncryptionKeyHelper" xml:space="preserve">
<value>Master Encryption Key for key storage on disk</value>
</data>
<data name="Export" xml:space="preserve">
<value>Export</value>
</data>
<data name="Import" xml:space="preserve">
<value>Import</value>
</data>
<data name="ImportExport" xml:space="preserve">
<value>Import / Export</value>
</data>
<data name="StoreSecret" xml:space="preserve">
<value>Save</value>
</data>
Expand Down
25 changes: 25 additions & 0 deletions KeyManager.Library.KeyStore.File/FileKeyStoreArchive.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.IO.Compression;

namespace Leosac.KeyManager.Library.KeyStore.File
{
public class FileKeyStoreArchive
{
public static void Import(string fileName, FileKeyStore keyStore)
{
if (System.IO.File.Exists(fileName))
{
ZipFile.ExtractToDirectory(fileName, keyStore.GetFileProperties().Fullpath, true);
}
}

public static void Export(string fileName, FileKeyStore keyStore)
{
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}

ZipFile.CreateFromDirectory(keyStore.GetFileProperties().Fullpath, fileName);
}
}
}

0 comments on commit 215e623

Please sign in to comment.