Skip to content

Commit

Permalink
update avalonia
Browse files Browse the repository at this point in the history
wip: add ability to create new secret
  • Loading branch information
cricketthomas committed Jul 23, 2024
1 parent 2ec1f8d commit 9c896e8
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 123 deletions.
2 changes: 1 addition & 1 deletion Desktop/Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@


<ItemGroup>
<PackageReference Include="Avalonia.Desktop" Version="11.1.0-rc2" />
<PackageReference Include="Avalonia.Desktop" Version="11.1.0" />
</ItemGroup>


Expand Down
19 changes: 1 addition & 18 deletions KeyVaultExplorer/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,7 @@ public App()
DataContext = new AppViewModel();
}

public static void ConfigureDesktopServices()
{
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddMemoryCache();
serviceCollection.AddSingleton<AuthService>();
serviceCollection.AddSingleton<VaultService>();
serviceCollection.AddSingleton<TabViewPageViewModel>();
serviceCollection.AddSingleton<ToolBarViewModel>();
serviceCollection.AddSingleton<KeyVaultTreeListViewModel>();
serviceCollection.AddSingleton<SettingsPageViewModel>();
serviceCollection.AddSingleton<MainViewModel>();
serviceCollection.AddSingleton<NotificationViewModel>();
serviceCollection.AddSingleton<KvExplorerDb>();
serviceCollection.AddTransient<AppSettingReader>();
serviceCollection.AddSingleton<IClipboard, ClipboardService>();
serviceCollection.AddSingleton<IStorageProvider, StorageProviderService>();
}


public static void CreateDesktopResources()
{
Directory.CreateDirectory(Constants.LocalAppDataFolder);
Expand Down
Binary file added KeyVaultExplorer/Assets/Certificate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions KeyVaultExplorer/Assets/Certificate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added KeyVaultExplorer/Assets/Key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions KeyVaultExplorer/Assets/Key.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added KeyVaultExplorer/Assets/Lock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions KeyVaultExplorer/Assets/Lock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions KeyVaultExplorer/KeyVaultExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@

<ItemGroup>
<None Remove="Assets\AppIcon.ico" />
<None Remove="Assets\Certificate.png" />
<None Remove="Assets\Certificate.svg" />
<None Remove="Assets\Close.svg" />
<None Remove="Assets\Cloud.png" />
<None Remove="Assets\Cloud.svg" />
<None Remove="Assets\CollapseAll.svg" />
<None Remove="Assets\folder.png" />
<None Remove="Assets\FolderClosed.svg" />
<None Remove="Assets\ForeignKey.svg" />
<None Remove="Assets\Key.png" />
<None Remove="Assets\Key.svg" />
<None Remove="Assets\KeyVault.svg" />
<None Remove="Assets\kv-gray.png" />
<None Remove="Assets\Lock.png" />
<None Remove="Assets\Lock.svg" />
<None Remove="Assets\Pin.png" />
<None Remove="Assets\Pin.svg" />
<None Remove="Assets\Refresh.svg" />
Expand All @@ -42,17 +48,17 @@


<ItemGroup>
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.0-rc2" />
<PackageReference Include="Avalonia.Svg.Skia" Version="11.1.0-rc2" />
<PackageReference Include="Avalonia.Controls.ItemsRepeater" Version="11.1.0" />
<PackageReference Include="Avalonia.Svg.Skia" Version="11.1.0" />
<PackageReference Include="DeviceId" Version="6.7.0" />
<PackageReference Include="FluentAvaloniaUI" Version="2.1.0-preview6" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0-rc2" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-preview.6.24327.7" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.0-preview.6.24327.4" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0-preview.6.24327.7" />
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.61.3" />
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.62.0" />
<PackageReference Include="Azure.ResourceManager.KeyVault" Version="1.3.0" />
<PackageReference Include="Azure.Security.KeyVault.Certificates" Version="4.6.0" />
<PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.6.0" />
Expand Down
1 change: 1 addition & 0 deletions KeyVaultExplorer/Services/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static void AddCommonServices(this IServiceCollection collection) {
collection.AddSingleton<KeyVaultTreeListViewModel>();
collection.AddSingleton<SettingsPageViewModel>();
collection.AddSingleton<MainViewModel>();
collection.AddSingleton<SubscriptionsPageViewModel>();
collection.AddSingleton<NotificationViewModel>();
collection.AddSingleton<KvExplorerDb>();
collection.AddTransient<AppSettingReader>();
Expand Down
25 changes: 24 additions & 1 deletion KeyVaultExplorer/ViewModels/CreateNewSecretVersionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Linq;
using Azure.Security.KeyVault.Secrets;
using System;
using System.Collections.Generic;
using KeyVaultExplorer.Models;

namespace KeyVaultExplorer.ViewModels;

Expand All @@ -20,12 +22,18 @@ public partial class CreateNewSecretVersionViewModel : ViewModelBase
[ObservableProperty]
private bool isEdit = false;

[ObservableProperty]
private bool isNew = false;

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

[ObservableProperty]
private string secretValue;

[ObservableProperty]
public List<KvResourceGroupModel> resourceGroupItems;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(Location))]
[NotifyPropertyChangedFor(nameof(HasActivationDate))]
Expand All @@ -39,17 +47,19 @@ public partial class CreateNewSecretVersionViewModel : ViewModelBase
private TimeSpan? notBeforeTimespan;

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

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

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

[RelayCommand]
Expand Down Expand Up @@ -87,4 +97,17 @@ partial void OnKeyVaultSecretModelChanging(SecretProperties 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;
}

public async Task GetSubscriptions()
{
//var subscriptions = _vaultService.GetStoredSelectedSubscriptions();
//var subscriptions = await _subscriptionsPageViewModel.Subscriptions;

//ResourceGroupItems = subscriptions.Select(sub => new KvResourceGroupModel
//{
// ResourceGroupDisplayName = sub.SubscriptionDisplayName,
// ResourceGroupResource = sub.Subscription,
// KeyVaultResources = new ObservableCollection<KeyVaultResourcePlaceholder>()
//}).ToList();
}
}
4 changes: 4 additions & 0 deletions KeyVaultExplorer/ViewModels/PropertiesPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ private async Task NewVersion()
}
}




private async void OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
var def = args.GetDeferral();
Expand All @@ -240,6 +243,7 @@ private async Task EditVersion()
IsPrimaryButtonEnabled = true,
PrimaryButtonText = "Apply Changes",
CloseButtonText = "Cancel",
MinWidth = 600
};


Expand Down
22 changes: 21 additions & 1 deletion KeyVaultExplorer/Views/CustomControls/ToolBar.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,27 @@
</StackPanel>
</Button.Content>
</Button>

<Button
Name="CeeateSecret"
Height="25"
Click="CreateSecret_Clicked"
FontSize="13"
Theme="{StaticResource TransparentButton}"
ToolTip.Tip="Create New Secret"
ToolTip.VerticalOffset="10">
<Button.Content>
<StackPanel Orientation="Horizontal">
<ui:BitmapIcon
Width="16"
Height="16"
Margin="0,0,2,0"
RenderOptions.BitmapInterpolationMode="HighQuality"
ShowAsMonochrome="True"
UriSource="avares://KeyVaultExplorer/Assets/Lock.png" />
<TextBlock VerticalAlignment="{OnPlatform Bottom, Windows=Center}" Text="New Secret" />
</StackPanel>
</Button.Content>
</Button>

<Button
Name="SettingsPage"
Expand Down
56 changes: 56 additions & 0 deletions KeyVaultExplorer/Views/CustomControls/ToolBar.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Azure.Security.KeyVault.Secrets;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Windowing;
using KeyVaultExplorer.Exceptions;
using KeyVaultExplorer.ViewModels;
using KeyVaultExplorer.Views.Pages;
using System.Linq;
using System.Threading.Tasks;

namespace KeyVaultExplorer.Views.CustomControls;

public partial class ToolBar : UserControl
{
private readonly NotificationViewModel _notificationViewModel;

public ToolBar()
{
InitializeComponent();
DataContext = Defaults.Locator.GetRequiredService<ToolBarViewModel>();
_notificationViewModel = Defaults.Locator.GetRequiredService<NotificationViewModel>();
}

private void SettingsButton_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
Expand All @@ -30,4 +41,49 @@ private void IsPaneToggledButton_Click(object? sender, Avalonia.Interactivity.Ro
Control control = (Control)sender!;
control.RaiseEvent(new RoutedEventArgs(TabViewPage.PaneToggledRoutedEvent));
}

private void CreateSecret_Clicked(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
Dispatcher.UIThread.Post(() => CreateNewSecret());
}

private async Task CreateNewSecret()
{
try
{
var lifetime = App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
var applyChangesBtn = new TaskDialogButton("Apply Changes", "ApplyChangesButtonResult");
var dialog = new TaskDialog()
{
Title = "New Secret",
XamlRoot = lifetime.Windows.Last() as AppWindow,
Buttons = { applyChangesBtn, TaskDialogButton.CancelButton, },
MinWidth = 600,

Content = new CreateNewSecretVersion()
{
DataContext = new CreateNewSecretVersionViewModel()
{
KeyVaultSecretModel = new SecretProperties("new_secret_1"),
IsEdit = false,
IsNew = true,
}
}
};

applyChangesBtn.Click += async (sender, args) =>
{
await (DataContext as CreateNewSecretVersionViewModel)!.NewVersionCommand.ExecuteAsync(null);
};

var result = await dialog.ShowAsync();
}
catch (KeyVaultItemNotFoundException ex)
{
}
catch (KeyVaultInSufficientPrivileges ex)
{
_notificationViewModel.ShowErrorPopup(new Avalonia.Controls.Notifications.Notification { Message = ex.Message, Title = "Insufficient Rights" });
}
}
}
Loading

0 comments on commit 9c896e8

Please sign in to comment.