Skip to content

Commit

Permalink
#66 wip: add ability to create key versions
Browse files Browse the repository at this point in the history
  • Loading branch information
cricketthomas committed Jul 17, 2024
1 parent 8d132b5 commit 7a63dd8
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 0 deletions.
90 changes: 90 additions & 0 deletions KeyVaultExplorer/ViewModels/CreateNewKeyVersionViewModel .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using KeyVaultExplorer.Views;
using KeyVaultExplorer.Services;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using Azure.Security.KeyVault.Secrets;
using System;
using Azure.Security.KeyVault.Keys;

namespace KeyVaultExplorer.ViewModels;

public partial class CreateNewKeyVersionViewModel : ViewModelBase
{
[ObservableProperty]
private bool isBusy = false;

[ObservableProperty]
private bool isEdit = false;

public bool HasActivationDate => KeyVaultKeyModel is not null && KeyVaultKeyModel.NotBefore.HasValue;
public bool HasExpirationDate => KeyVaultKeyModel is not null && KeyVaultKeyModel.ExpiresOn.HasValue;

[ObservableProperty]
private string value;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Location))]
[NotifyPropertyChangedFor(nameof(HasActivationDate))]
[NotifyPropertyChangedFor(nameof(HasExpirationDate))]
private KeyProperties keyVaultKeyModel;

[ObservableProperty]
private TimeSpan? expiresOnTimespan;

[ObservableProperty]
private TimeSpan? notBeforeTimespan;

public string? Location => KeyVaultKeyModel?.VaultUri.ToString();
public string? Identifier => KeyVaultKeyModel?.Id.ToString();

private readonly AuthService _authService;
private readonly VaultService _vaultService;
private NotificationViewModel _notificationViewModel;

public CreateNewKeyVersionViewModel()
{
_authService = Defaults.Locator.GetRequiredService<AuthService>();
_vaultService = Defaults.Locator.GetRequiredService<VaultService>();
_notificationViewModel = Defaults.Locator.GetRequiredService<NotificationViewModel>();
}

[RelayCommand]
public async Task EditDetails()
{
if (KeyVaultKeyModel.NotBefore.HasValue)
KeyVaultKeyModel.NotBefore = KeyVaultKeyModel.NotBefore.Value.Date + (NotBeforeTimespan.HasValue ? NotBeforeTimespan.Value : TimeSpan.Zero);

if (KeyVaultKeyModel.ExpiresOn.HasValue)
KeyVaultKeyModel.ExpiresOn = KeyVaultKeyModel.ExpiresOn.Value.Date + (ExpiresOnTimespan.HasValue ? ExpiresOnTimespan.Value : TimeSpan.Zero);

//var updatedProps = await _vaultService.UpdateSecret(KeyVaultKeyModel, KeyVaultKeyModel.VaultUri);
//KeyVaultKeyModel = updatedProps;
}

[RelayCommand]
public async Task NewVersion()
{
var newSecret = new KeyVaultSecret(KeyVaultKeyModel.Name, Value);
if (KeyVaultKeyModel.NotBefore.HasValue)
newSecret.Properties.NotBefore = KeyVaultKeyModel.NotBefore.Value.Date + (NotBeforeTimespan.HasValue ? NotBeforeTimespan.Value : TimeSpan.Zero);

if (KeyVaultKeyModel.ExpiresOn.HasValue)
newSecret.Properties.ExpiresOn = KeyVaultKeyModel.ExpiresOn.Value.Date + (ExpiresOnTimespan.HasValue ? ExpiresOnTimespan.Value : TimeSpan.Zero);


var newVersion = await _vaultService.CreateSecret(newSecret, KeyVaultKeyModel.VaultUri);
var properties = (await _vaultService.GetSecretProperties(newVersion.Properties.VaultUri, newVersion.Name)).First();
//KeyVaultKeyModel = properties;
}

partial void OnKeyVaultKeyModelChanging(KeyProperties model)
{
ExpiresOnTimespan = model is not null && model.ExpiresOn.HasValue ? model?.ExpiresOn.Value.LocalDateTime.TimeOfDay : null;
NotBeforeTimespan = model is not null && model.NotBefore.HasValue ? model?.NotBefore.Value.LocalDateTime.TimeOfDay : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<UserControl
x:Class="KeyVaultExplorer.CreateNewKeyVersion"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:KeyVaultExplorer.ViewModels"
d:DesignHeight="450"
d:DesignWidth="800"
x:DataType="vm:CreateNewKeyVersionViewModel"
mc:Ignorable="d">



<ScrollViewer>
<StackPanel MinWidth="400" Margin="10">

<Grid ColumnDefinitions="*" RowDefinitions="*,*,*,*,*,*,*,*,*,*">

<StackPanel Grid.Row="0" Orientation="Vertical">
<TextBlock
Margin="0,0,10,0"
VerticalAlignment="Center"
Text="Name" />
<TextBox IsEnabled="{Binding Identifier, Converter={x:Static ObjectConverters.IsNull}}" Text="{Binding KeyVaultKeyModel.Name}" />
</StackPanel>


<StackPanel
Grid.Row="1"
Orientation="Vertical"
ToolTip.Tip="Type of key to create.">
<TextBlock
Margin="0,0,10,0"
VerticalAlignment="Center"
Text="Key type" />
<RadioButton Content="RSA" GroupName="keyType" />
<RadioButton Content="EC" GroupName="keyType" />
</StackPanel>

<StackPanel Grid.Row="2" Orientation="Vertical">
<TextBlock
Margin="0,0,10,0"
VerticalAlignment="Center"
Text="Key type" />
<RadioButton Content="2048" GroupName="keySize" />
<RadioButton Content="3072" GroupName="keySize" />
<RadioButton Content="4096" GroupName="keySize" />

</StackPanel>


<StackPanel Grid.Row="3">
<CheckBox
Name="SetActivationDateCheckbox"
IsChecked="{Binding HasActivationDate}"
ToolTip.Tip="Sets when this resource will become active. This sets the 'nbf' property on the resource. A new current but inactive version will still be created after this operation.">
Set Activation Date
</CheckBox>
<StackPanel
IsVisible="{Binding #SetActivationDateCheckbox.IsChecked}"
Orientation="Vertical"
Spacing="5">
<DatePicker
MinWidth="400"
HorizontalAlignment="Left"
SelectedDate="{Binding KeyVaultKeyModel.NotBefore}" />
<TimePicker MinWidth="400" SelectedTime="{Binding NotBeforeTimespan}" />
</StackPanel>
<CheckBox
Name="SetExpirationDateCheckbox"
IsChecked="{Binding HasExpirationDate}"
ToolTip.Tip="Sets when this resource will become inactive. This sets the 'exp' property on the resource.">
Set Expiration Date
</CheckBox>
<StackPanel
IsVisible="{Binding #SetExpirationDateCheckbox.IsChecked}"
Orientation="Vertical"
Spacing="5">
<DatePicker MinWidth="400" SelectedDate="{Binding KeyVaultKeyModel.ExpiresOn}" />
<TimePicker MinWidth="400" SelectedTime="{Binding ExpiresOnTimespan, Mode=TwoWay}" />
</StackPanel>
</StackPanel>


<StackPanel
Grid.Row="4"
Orientation="Horizontal"
Spacing="5">
<TextBlock
Margin="0,4,0,0"
VerticalAlignment="Center"
Text="Enabled" />
<ToggleSwitch
IsChecked="{Binding KeyVaultKeyModel.Enabled}"
OffContent="No"
OnContent="Yes" />
</StackPanel>




</Grid>

</StackPanel>
</ScrollViewer>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;


using KeyVaultExplorer.ViewModels;

namespace KeyVaultExplorer;

public partial class CreateNewKeyVersion : UserControl
{
public CreateNewKeyVersion()
{
InitializeComponent();
DataContext = new CreateNewKeyVersionViewModel();
}

private void InputField_OnAttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
{
// We will set the focus into our input field just after it got attached to the visual tree.
if (sender is InputElement inputElement)
{
Dispatcher.UIThread.InvokeAsync(() =>
{
inputElement.Focus(NavigationMethod.Unspecified, KeyModifiers.None);
});
}
}
}

0 comments on commit 7a63dd8

Please sign in to comment.