Skip to content

Commit

Permalink
Add for linked Key Store secret during publish if required
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Mar 25, 2024
1 parent 59ef681 commit c703de9
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 22 deletions.
27 changes: 27 additions & 0 deletions KeyManager.Library.UI/OpenFavoriteControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<UserControl x:Class="Leosac.KeyManager.Library.UI.OpenFavoriteControl"
x:Name="favControl"
xmlns="http:https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http:https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http:https://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http:https://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Leosac.KeyManager.Library.UI"
xmlns:materialDesign="http:https://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:properties="clr-namespace:Leosac.KeyManager.Library.UI.Properties"
mc:Ignorable="d"
d:DesignHeight="150" d:DesignWidth="290">
<StackPanel Margin="16" Width="250">
<Label Content="{Binding Path=Title, ElementName=favControl}" Margin="5" />
<PasswordBox materialDesign:HintAssist.HelperText="{x:Static properties:Resources.SecretHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.Secret}"
materialDesign:TextFieldAssist.HasClearButton="True" Margin="5"
materialDesign:TextFieldAssist.CharacterCounterVisibility="Visible"
Style="{StaticResource MaterialDesignFloatingHintRevealPasswordBox}"
materialDesign:PasswordBoxAssist.Password="{Binding Properties.Secret, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ElementName=favControl}"
MaxLength="{Binding Properties.SecretMaxLength, Mode=OneWay, ElementName=favControl}"
Visibility="{Binding Properties.StoreSecret, Converter={StaticResource InverseBooleanToVisibilityConverter}, ElementName=favControl}"/>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<Button Margin="0,8,8,0" CommandParameter="{Binding}" Command="{Binding Command, ElementName=favControl}" Content="{x:Static properties:Resources.OK}" IsDefault="True" Style="{StaticResource MaterialDesignFlatButton}" />
<Button Margin="0,8,8,0" Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Content="{x:Static properties:Resources.Cancel}" IsCancel="True" Style="{StaticResource MaterialDesignFlatButton}" />
</StackPanel>
</StackPanel>
</UserControl>
54 changes: 54 additions & 0 deletions KeyManager.Library.UI/OpenFavoriteControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Leosac.KeyManager.Library.KeyStore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(OpenFavoriteControl),
new FrameworkPropertyMetadata(""));

public ICommand? Command
{
get { return (ICommand?)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}

public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(OpenFavoriteControl));

public KeyStoreProperties? Properties
{
get { return (KeyStoreProperties?)GetValue(PropertiesProperty); }
set { SetValue(PropertiesProperty, value); }
}

public static readonly DependencyProperty PropertiesProperty = DependencyProperty.Register(nameof(Properties), typeof(KeyStoreProperties), typeof(OpenFavoriteControl));
}
}
28 changes: 22 additions & 6 deletions KeyManager.Library/KeyStore/KeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,21 @@ public virtual Task Store(IChangeKeyEntry change)
/// <param name="changes">The key entries details.</param>
public abstract Task Store(IList<IChangeKeyEntry> changes);

public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Action<KeyStore, KeyEntryClass, int>? initCallback)
public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, Action<KeyStore, KeyEntryClass, int>? initCallback)
{
var classes = SupportedClasses;
foreach (var keClass in classes)
{
await Publish(store, getFavoriteKeyStore, keClass, initCallback);
await Publish(store, getFavoriteKeyStore, askForKeyStoreSecretIfRequired, keClass, initCallback);
}
}

public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, KeyEntryClass keClass, Action<KeyStore, KeyEntryClass, int>? initCallback)
public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, KeyEntryClass keClass, Action<KeyStore, KeyEntryClass, int>? initCallback)
{
await Publish(store, getFavoriteKeyStore, keClass, null, initCallback);
await Publish(store, getFavoriteKeyStore, askForKeyStoreSecretIfRequired, keClass, null, initCallback);
}

public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback)
public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback)
{
var changes = new List<IChangeKeyEntry>();
if (ids == null)
Expand Down Expand Up @@ -291,6 +291,14 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
var ks = getFavoriteKeyStore(entry.Link.KeyStoreFavorite);
if (ks != null)
{
if (askForKeyStoreSecretIfRequired != null)
{
var c = await askForKeyStoreSecretIfRequired(ks);
if (!c)
{
throw new KeyStoreException("Missing secret for the linked key store.");
}
}
await ks.Open();
try
{
Expand Down Expand Up @@ -330,6 +338,14 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
var ks = getFavoriteKeyStore(kv.Key.Link.KeyStoreFavorite);
if (ks != null)
{
if (askForKeyStoreSecretIfRequired != null)
{
var c = await askForKeyStoreSecretIfRequired(ks);
if (!c)
{
throw new KeyStoreException("Missing secret for the linked key store.");
}
}
await ks.Open();
try
{
Expand Down Expand Up @@ -382,7 +398,7 @@ public virtual async Task Publish(KeyStore store, Func<string, KeyStore?> getFav
}
}

public virtual Task Diff(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback)
public virtual Task Diff(KeyStore store, Func<string, KeyStore?> getFavoriteKeyStore, Func<KeyStore, Task<bool>>? askForKeyStoreSecretIfRequired, KeyEntryClass keClass, IEnumerable<KeyEntryId>? ids, Action<KeyStore, KeyEntryClass, int>? initCallback)
{
throw new NotImplementedException();
}
Expand Down
25 changes: 24 additions & 1 deletion KeyManager/Domain/EditKeyStoreControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using CommunityToolkit.Mvvm.Input;
using System.Windows.Input;
using Org.BouncyCastle.Crypto;
using log4net;

namespace Leosac.KeyManager.Domain
{
Expand Down Expand Up @@ -229,8 +230,29 @@ private static void SaveToFavorite(Favorites favorites, Favorite fav)
favorites.KeyStores.Add(fav);
favorites.SaveToFile();
}

public static async Task<bool> AskForKeyStoreSecretIfRequired(KeyStore ks)
{
if (ks.Properties != null && (ks.Properties.StoreSecret || !string.IsNullOrEmpty(ks.Properties.Secret)))
{
return true;
}

var dialog = new OpenFavoriteControl
{
DataContext = ks,
Properties = ks.Properties,
Title = string.Format("{0} - {1}", Properties.Resources.OpenFavorite, ks.Name),
Command = new RelayCommand(() =>
{
DialogHost.CloseDialogCommand.Execute(ks, null);
})
};
var ret = await DialogHost.Show(dialog, "RootDialog");
return (ret != null);
}

public async Task RunOnKeyStore(UserControl dialog, Func<KeyStore, Func<string, KeyStore?>, KeyEntryClass, IEnumerable<KeyEntryId>?, Action<KeyStore, KeyEntryClass, int>?, Task> action)
public async Task RunOnKeyStore(UserControl dialog, Func<KeyStore, Func<string, KeyStore?>, Func<KeyStore, Task<bool>>?, KeyEntryClass, IEnumerable<KeyEntryId>?, Action<KeyStore, KeyEntryClass, int>?, Task> action)
{
var model = new PublishKeyStoreDialogViewModel();
dialog.DataContext = model;
Expand Down Expand Up @@ -290,6 +312,7 @@ public async Task RunOnKeyStore(UserControl dialog, Func<KeyStore, Func<string,
}
await action(deststore,
getFavoriteKeyStore,
AskForKeyStoreSecretIfRequired,
keModel.KeyEntryClass,
entries,
initCallback
Expand Down
16 changes: 1 addition & 15 deletions KeyManager/FavoritesControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,7 @@
<WrapPanel DockPanel.Dock="Right" Orientation="Horizontal" VerticalAlignment="Center">
<materialDesign:DialogHost DialogTheme="Inherit">
<materialDesign:DialogHost.DialogContent>
<StackPanel Margin="16" Width="250">
<Label Content="{x:Static properties:Resources.OpenFavorite}" Margin="5" />
<PasswordBox materialDesign:HintAssist.HelperText="{x:Static properties:Resources.SecretHelper}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.Secret}"
materialDesign:TextFieldAssist.HasClearButton="True" Margin="5"
materialDesign:TextFieldAssist.CharacterCounterVisibility="Visible"
Style="{StaticResource MaterialDesignFloatingHintRevealPasswordBox}"
materialDesign:PasswordBoxAssist.Password="{Binding Properties.Secret, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MaxLength="{Binding Properties.SecretMaxLength, Mode=OneWay}"
Visibility="{Binding Properties.StoreSecret, Converter={StaticResource InverseBooleanToVisibilityConverter}}"/>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<Button Margin="0,8,8,0" CommandParameter="{Binding}" Command="{Binding DataContext.OpenFavoriteCommand, ElementName=userControl}" Content="{x:Static properties:Resources.OK}" IsDefault="True" Style="{StaticResource MaterialDesignFlatButton}" />
<Button Margin="0,8,8,0" Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Content="{x:Static properties:Resources.Cancel}" IsCancel="True" Style="{StaticResource MaterialDesignFlatButton}" />
</StackPanel>
</StackPanel>
<kslib:OpenFavoriteControl Title="{x:Static properties:Resources.OpenFavorite}" Command="{Binding DataContext.OpenFavoriteCommand, ElementName=userControl}" Properties="{Binding Properties}" />
</materialDesign:DialogHost.DialogContent>

<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" ToolTip="{x:Static properties:Resources.OpenFavorite}" Margin="3">
Expand Down

0 comments on commit c703de9

Please sign in to comment.