From ecc169bd372076fb7f2a798a4db0a4475f1b65ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6ssler?= Date: Sun, 18 Aug 2024 18:53:12 +0200 Subject: [PATCH] Implement the deletion of security keys --- Guard.Core/Security/Auth.cs | 10 ++++ .../Security/WebAuthn/WebAuthnInterop.cs | 2 +- Guard.WPF/Resources/Strings.de.xaml | 5 ++ Guard.WPF/Resources/Strings.en.xaml | 5 ++ Guard.WPF/Resources/Strings.fr.xaml | 5 ++ Guard.WPF/Resources/Strings.zh_cn.xaml | 5 ++ Guard.WPF/Resources/Strings.zh_tw.xaml | 5 ++ .../Pages/Preferences/WebAuthnPage.xaml.cs | 50 +++++++++++++++---- 8 files changed, 75 insertions(+), 12 deletions(-) diff --git a/Guard.Core/Security/Auth.cs b/Guard.Core/Security/Auth.cs index 8e650d6..30840b0 100644 --- a/Guard.Core/Security/Auth.cs +++ b/Guard.Core/Security/Auth.cs @@ -415,6 +415,16 @@ internal static async Task AddWebAuthnDevice(WebauthnDevice device, AssertionRes await SaveFile(); } + public static async Task RemoveWebAuthnDevice(WebauthnDevice device) + { + ArgumentNullException.ThrowIfNull(authData); + + authData.WebAuthn ??= []; + + authData.WebAuthn.Remove(device); + await SaveFile(); + } + public static async Task LoginWithWebAuthn(IntPtr windowHandle) { if (authData == null || authData.LoginSalt == null) diff --git a/Guard.Core/Security/WebAuthn/WebAuthnInterop.cs b/Guard.Core/Security/WebAuthn/WebAuthnInterop.cs index 3c4d78d..d8f3ee7 100644 --- a/Guard.Core/Security/WebAuthn/WebAuthnInterop.cs +++ b/Guard.Core/Security/WebAuthn/WebAuthnInterop.cs @@ -25,7 +25,7 @@ public static int GetApiVersion() ); } _apiVersion = WebAuthNGetApiVersionNumber(); - Log.Logger.Debug("WebAuthn API version: {ApiVersion}", _apiVersion); + Log.Logger.Information("WebAuthn API version: {ApiVersion}", _apiVersion); } return _apiVersion ?? throw new PlatformNotSupportedException("Can not get WebAuthn API version."); diff --git a/Guard.WPF/Resources/Strings.de.xaml b/Guard.WPF/Resources/Strings.de.xaml index bca8b74..b161598 100644 --- a/Guard.WPF/Resources/Strings.de.xaml +++ b/Guard.WPF/Resources/Strings.de.xaml @@ -77,6 +77,8 @@ Melde dich mit einem Sicherheitsschlüssel an Konfigurieren Dein Betriebssystem scheint WebAuthn nicht zu unterstützen. + + Sicherheitsschlüssel (FIDO2) Sicherheitsschlüssel hinzufügen Registriere einen neuen Schlüssel @@ -87,6 +89,9 @@ Name zur Identifikation des Schlüssels Es muss ein Name für den Sicherheitsschlüssel eingegeben werden Es existiert bereits ein Sicherheitsschlüssel mit diesem Namen + Sicherheitsschlüssel entfernen + Bist du sicher, dass du den Sicherheitsschlüssel @Name entfernen möchten? Du kannst diesen Schlüssel nicht mehr für die Authentifizierung verwenden. + Entfernen Vielen Dank fürs Herunterladen! Bitte wähle, wie du deine Token sichern möchtest. diff --git a/Guard.WPF/Resources/Strings.en.xaml b/Guard.WPF/Resources/Strings.en.xaml index 50720bf..3ea1a00 100644 --- a/Guard.WPF/Resources/Strings.en.xaml +++ b/Guard.WPF/Resources/Strings.en.xaml @@ -76,6 +76,8 @@ Login with a hardware security key Configure Your operating system does not seem to support WebAuthn. + + Security Key (FIDO2) Add Security Key Register a new key @@ -86,6 +88,9 @@ Please note that after the system pop-up says that you can now log in with the security key, you have to complete the directly following authentication step to finish the registration. You have to enter a name for the security key A security key with this name already exists + Remove Security Key + Are you sure you want to remove the security key @Name? You can not use this key for authentication anymore. + Remove Thanks for downloading! Please choose how you would like to secure your tokens. diff --git a/Guard.WPF/Resources/Strings.fr.xaml b/Guard.WPF/Resources/Strings.fr.xaml index 7e6d3de..61cf33d 100644 --- a/Guard.WPF/Resources/Strings.fr.xaml +++ b/Guard.WPF/Resources/Strings.fr.xaml @@ -76,6 +76,8 @@ Se connecter avec une clé de sécurité matérielle Configurer Votre système d'exploitation ne semble pas prendre en charge WebAuthn. + + Clé de sécurité (FIDO2) Ajouter une clé de sécurité Enregistrer une nouvelle clé @@ -86,6 +88,9 @@ Veuillez noter qu'après l'apparition de la fenêtre contextuelle du système indiquant que vous pouvez désormais vous connecter avec la clé de sécurité, vous devez terminer l'étape d'authentification qui suit immédiatement pour finaliser l'inscription. Il existe déjà une clé de sécurité avec ce nom Vous devez entrer un nom pour la clé de sécurité + Supprimer la clé de sécurité + Êtes-vous sûr de vouloir supprimer la clé de sécurité @Name ? Vous ne pourrez plus utiliser cette clé pour l'authentification. + Supprimer Merci pour le téléchargement ! Veuillez choisir comment vous souhaitez sécuriser vos jetons. diff --git a/Guard.WPF/Resources/Strings.zh_cn.xaml b/Guard.WPF/Resources/Strings.zh_cn.xaml index 08d73c2..8c8238e 100644 --- a/Guard.WPF/Resources/Strings.zh_cn.xaml +++ b/Guard.WPF/Resources/Strings.zh_cn.xaml @@ -76,6 +76,8 @@ 使用硬件安全密鑰登錄 配置 您的操作系統似乎不支持WebAuthn。 + + 安全密鑰 (FIDO2) 添加安全密鑰 註冊新密鑰 @@ -86,6 +88,9 @@ 請注意,系統彈出提示您可以使用安全密鑰登錄後,您必須完成隨後的驗證步驟以完成註冊。 已經存在具有此名稱的安全密鑰 您必須輸入安全金鑰的名稱 + 移除安全密鑰 + 您確定要移除安全密鑰 @Name 嗎?您將無法再使用此密鑰進行身份驗證。 + 移除 感谢下载!请选择您想要如何保护您的令牌。 diff --git a/Guard.WPF/Resources/Strings.zh_tw.xaml b/Guard.WPF/Resources/Strings.zh_tw.xaml index 7c1e9c1..e34166d 100644 --- a/Guard.WPF/Resources/Strings.zh_tw.xaml +++ b/Guard.WPF/Resources/Strings.zh_tw.xaml @@ -76,6 +76,8 @@ 使用硬件安全密钥登录 配置 您的操作系统似乎不支持WebAuthn。 + + 安全密钥 (FIDO2) 添加安全密钥 注册新密钥 @@ -86,6 +88,9 @@ 请注意,系统弹出提示您可以使用安全密钥登录后,您必须完成随后验证步骤以完成注册。 已经存在具有此名称的安全密钥 您必须输入安全密钥的名称 + 移除安全密钥 + 您确定要移除安全密钥 @Name 吗?您将无法再使用此密钥进行身份验证。 + 移除 感謝下載!請選擇您想要如何保護您的令牌。 diff --git a/Guard.WPF/Views/Pages/Preferences/WebAuthnPage.xaml.cs b/Guard.WPF/Views/Pages/Preferences/WebAuthnPage.xaml.cs index df4d366..44764d6 100644 --- a/Guard.WPF/Views/Pages/Preferences/WebAuthnPage.xaml.cs +++ b/Guard.WPF/Views/Pages/Preferences/WebAuthnPage.xaml.cs @@ -1,5 +1,6 @@ using System.Windows; using System.Windows.Controls; +using Guard.Core; using Guard.Core.Models; using Guard.Core.Security; using Guard.Core.Security.WebAuthn; @@ -99,6 +100,7 @@ private async void Add_Click(object sender, RoutedEventArgs e) } catch (Exception ex) { + Log.Logger.Error("Unhandled WebAuthn exception on key creation: {0}", ex.Message); if (ex.Message == "Cancelled") { return; @@ -119,16 +121,41 @@ private void LoadKeys() KeysContainer.Children.Clear(); foreach (var key in keys) { + var delBtn = new Wpf.Ui.Controls.Button() + { + Margin = new Thickness(0, 0, 8, 0), + Icon = new SymbolIcon() { Symbol = SymbolRegular.Delete24 }, + }; + delBtn.Click += async (sender, e) => + { + var deleteMessageBox = new Wpf.Ui.Controls.MessageBox + { + Title = I18n.GetString("webauthn.delete.title"), + Content = I18n.GetString("webauthn.delete.content") + .Replace( + "@Name", + !string.IsNullOrEmpty(key.EncryptedName) + ? encryptionHelper.DecryptString(key.EncryptedName) + : "???" + ), + IsPrimaryButtonEnabled = true, + PrimaryButtonText = I18n.GetString("webauthn.delete.yes"), + CloseButtonText = I18n.GetString("dialog.close"), + MaxWidth = 400 + }; + + var result = await deleteMessageBox.ShowDialogAsync(); + if (result == Wpf.Ui.Controls.MessageBoxResult.Primary) + { + DeleteKey(key); + } + }; KeysContainer.Children.Add( new CardControl() { Width = 320, Margin = new Thickness(0, 15, 15, 0), - Icon = new SymbolIcon() - { - //FontSize = 32, - Symbol = SymbolRegular.UsbStick24 - }, + Icon = new SymbolIcon() { Symbol = SymbolRegular.UsbStick24 }, Header = new Grid() { Margin = new Thickness(0, 0, 35, 0), @@ -145,15 +172,16 @@ private void LoadKeys() }, } }, - Content = new Wpf.Ui.Controls.Button() - { - Margin = new Thickness(0, 0, 8, 0), - - Icon = new SymbolIcon() { Symbol = SymbolRegular.Delete24 }, - }, + Content = delBtn } ); } } + + private async void DeleteKey(WebauthnDevice key) + { + await Auth.RemoveWebAuthnDevice(key); + LoadKeys(); + } } }