Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor (#269)
Leendert de Borst4ccbba5b4b10
diff --git a/docs/misc/enable-webauthn-pfr-chrome.md b/docs/misc/enable-webauthn-pfr-chrome.mdnew file mode 100644index 000000000..257550f91--- /dev/null+++ b/docs/misc/enable-webauthn-pfr-chrome.md@@ -0,0 +1,13 @@+The webauthn implementation in order to quick unlock the vault requires the use of a FIDO2 authenticator.++This can be either the built-in browser authenticator or an external authenticator like a Yubikey.++At the time of writing (2024-10-04), only some browsers support the required PRF extension. In order to make it work in Chrome, you need to enable the PRF extension in the browser settings.++## Chrome++1. Open the Chrome browser and navigate to `chrome://flags/#enable-experimental-web-platform-features`.+2. Enable the `Experimental Web Platform features` flag.+3. Restart the browser.+4. Now it should be possible to use the built-in chrome password manager to unlock the vault.+```diff --git a/src/AliasVault.Client/Main/Pages/Settings/Security/Components/QuickVaultUnlockSection.razor b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/QuickVaultUnlockSection.razorindex 97d551a64..cfbfb469c 100644--- a/src/AliasVault.Client/Main/Pages/Settings/Security/Components/QuickVaultUnlockSection.razor+++ b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/QuickVaultUnlockSection.razor@@ -63,7 +63,7 @@{var webauthCredentialDerivedKey = await JsInteropService.CreateWebAuthnCredentialDerivedKey(await GetUsernameAsync());await AuthService.SetWebAuthnEnabledAsync(true, webauthCredentialDerivedKey.CredentialId, webauthCredentialDerivedKey.Salt, webauthCredentialDerivedKey.DerivedKey);- GlobalNotificationService.AddSuccessMessage("WebAuthn is successfully enabled. The next time your vault is locked you can unlock it with your created passkey.", true);+ GlobalNotificationService.AddSuccessMessage("Quick Vault Unlock is successfully enabled. The next time your vault is locked you can unlock it with your created passkey.", true);}catch (NotSupportedException){@@ -72,7 +72,7 @@}catch (Exception ex){- GlobalNotificationService.AddErrorMessage("An error occurred while trying to enable WebAuthn. Please try again (later).", true);+ GlobalNotificationService.AddErrorMessage("An error occurred while trying to enable Quick Vault Unlock. Please try again (later).", true);Logger.LogInformation(ex, "An error occurred while trying to enable WebAuthn.");return;}@@ -86,6 +86,7 @@public async Task DisableWebAuthn(){await AuthService.SetWebAuthnEnabledAsync(false, string.Empty, string.Empty, string.Empty);+ GlobalNotificationService.AddSuccessMessage("Quick Vault Unlock is successfully disabled.", true);await LoadData();}}diff --git a/src/AliasVault.Client/Services/Auth/AuthService.cs b/src/AliasVault.Client/Services/Auth/AuthService.csindex fe668a631..1c6ebc6ed 100644--- a/src/AliasVault.Client/Services/Auth/AuthService.cs+++ b/src/AliasVault.Client/Services/Auth/AuthService.cs@@ -174,7 +174,7 @@ public sealed class AuthService(HttpClient httpClient, ILocalStorageService locavar webauthnSalt = await localStorage.GetItemAsStringAsync("webAuthnSalt");if (string.IsNullOrEmpty(encryptedEncryptionKey) || string.IsNullOrEmpty(webauthnCredentialId) || string.IsNullOrEmpty(webauthnSalt)){- throw new NullReferenceException("WebAuthn encrypted encryption key is not set or WebAuthn credential ID is not set.");+ throw new InvalidOperationException("WebAuthn encrypted encryption key is not set or WebAuthn credential ID is not set.");}var webauthnCredentialDerivedKey = await jsInteropService.GetWebAuthnCredentialDerivedKey(webauthnCredentialId, webauthnSalt);diff --git a/src/AliasVault.Client/Services/JsInteropService.cs b/src/AliasVault.Client/Services/JsInteropService.csindex 9313eafde..5d1c19e01 100644--- a/src/AliasVault.Client/Services/JsInteropService.cs+++ b/src/AliasVault.Client/Services/JsInteropService.cs@@ -172,22 +172,18 @@ public sealed class JsInteropService(IJSRuntime jsRuntime){var result = await jsRuntime.InvokeAsync<WebAuthnGetCredentialResult>("getWebAuthnCredentialAndDeriveKey", credentialId, salt);- if (result.Error != null)+ if (result.Error is null){- switch (result.Error)- {- case "PRF_NOT_SUPPORTED":- throw new NotSupportedException("Authenticator does not support the PRF extension.");- case "PRF_DERIVATION_FAILED":- throw new InvalidOperationException("Failed to derive key using PRF extension.");- case "WEBAUTHN_GET_ERROR":- throw new CryptographicException($"Failed to get WebAuthn credential: {result.Message}");- default:- throw new CryptographicException($"Unknown error occurred: {result.Error}");- }+ return result.DerivedKey ?? throw new CryptographicException("Derived key is null");}- return result.DerivedKey ?? throw new CryptographicException("Derived key is null");+ throw result.Error switch+ {+ "PRF_NOT_SUPPORTED" => new NotSupportedException("Authenticator does not support the PRF extension."),+ "PRF_DERIVATION_FAILED" => new InvalidOperationException("Failed to derive key using PRF extension."),+ "WEBAUTHN_CREATE_ERROR" => new CryptographicException($"Failed to create WebAuthn credential: {result.Message}"),+ _ => new CryptographicException($"Unknown error occurred: {result.Error ?? "No error specified"}"),+ };}catch (JSException ex){@@ -209,22 +205,21 @@ public sealed class JsInteropService(IJSRuntime jsRuntime)try{var result = await jsRuntime.InvokeAsync<WebAuthnCreateCredentialResult>("createWebAuthnCredentialAndDeriveKey", "AliasVault | " + username);- if (result.CredentialId is null || result.Salt is null || result.DerivedKey is null || result.Error is not null || result.Message is not null)++ if (result.CredentialId is not null &&+ result.Salt is not null &&+ result.DerivedKey is not null){- switch (result.Error)- {- case "PRF_NOT_SUPPORTED":- throw new NotSupportedException("Authenticator does not support the PRF extension.");- case "PRF_DERIVATION_FAILED":- throw new InvalidOperationException("Failed to derive key using PRF extension.");- case "WEBAUTHN_CREATE_ERROR":- throw new CryptographicException($"Failed to create WebAuthn credential: {result.Message}");- default:- throw new CryptographicException($"Unknown error occurred: {result.Error}");- }+ return (result.CredentialId, result.Salt, result.DerivedKey);}- return (result.CredentialId, result.Salt, result.DerivedKey);+ throw result.Error switch+ {+ "PRF_NOT_SUPPORTED" => new NotSupportedException("Authenticator does not support the PRF extension."),+ "PRF_DERIVATION_FAILED" => new InvalidOperationException("Failed to derive key using PRF extension."),+ "WEBAUTHN_CREATE_ERROR" => new CryptographicException($"Failed to create WebAuthn credential: {result.Message}"),+ _ => new CryptographicException($"Unknown error occurred: {result.Error ?? "No error specified"}"),+ };}catch (JSException ex){@@ -234,54 +229,54 @@ public sealed class JsInteropService(IJSRuntime jsRuntime)}/// <summary>- /// Represents the result of a WebAuthn credential operation.+ /// Represents the result of a WebAuthn get credential operation./// </summary>- private class WebAuthnCreateCredentialResult+ private sealed class WebAuthnGetCredentialResult{/// <summary>- /// Gets or sets the credential ID as a base64 string.+ /// Gets the derived key./// </summary>- public string? CredentialId { get; set; }+ public string? DerivedKey { get; }/// <summary>- /// Gets or sets the salt as a base64 string.+ /// Gets the optional error message./// </summary>- public string? Salt { get; set; }+ public string? Error { get; }/// <summary>- /// Gets or sets the derived key as a base64 string.+ /// Gets the optional additional error details./// </summary>- public string? DerivedKey { get; set; }+ public string? Message { get; }+ }+ /// <summary>+ /// Represents the result of a WebAuthn credential operation.+ /// </summary>+ private sealed class WebAuthnCreateCredentialResult+ {/// <summary>- /// Gets or sets the error message.+ /// Gets the credential ID as a base64 string./// </summary>- public string? Error { get; set; }+ public string? CredentialId { get; }/// <summary>- /// Gets or sets additional error details.+ /// Gets the salt as a base64 string./// </summary>- public string? Message { get; set; }- }+ public string? Salt { get; }- /// <summary>- /// Represents the result of a WebAuthn get credential operation.- /// </summary>- private class WebAuthnGetCredentialResult- {/// <summary>- /// Gets or sets the derived key.+ /// Gets the derived key as a base64 string./// </summary>- public string? DerivedKey { get; set; }+ public string? DerivedKey { get; }/// <summary>- /// Gets or sets the error message.+ /// Gets the optional error message./// </summary>- public string? Error { get; set; }+ public string? Error { get; }/// <summary>- /// Gets or sets additional error details.+ /// Gets the optional additional error details./// </summary>- public string? Message { get; set; }+ public string? Message { get; }}}diff --git a/src/AliasVault.Client/wwwroot/js/utilities.js b/src/AliasVault.Client/wwwroot/js/utilities.jsindex 87ecb5bea..fd380898c 100644--- a/src/AliasVault.Client/wwwroot/js/utilities.js+++ b/src/AliasVault.Client/wwwroot/js/utilities.js@@ -186,21 +186,21 @@ async function getWebAuthnCredentialAndDeriveKey(credentialIdToUse, salt) {});const extensionsResult = existingCredential.getClientExtensionResults();- if (!extensionsResult.prf) {- return { error: "PRF_NOT_SUPPORTED" };+ if (!extensionsResult?.prf) {+ return { Error: "PRF_NOT_SUPPORTED" };}- if (!extensionsResult.prf.results || !extensionsResult.prf.results.first) {- return { error: "PRF_DERIVATION_FAILED" };+ if (!extensionsResult.prf?.results?.first) {+ return { Error: "PRF_DERIVATION_FAILED" };}const derivedKey = extensionsResult.prf.results.first;return {- derivedKey: btoa(String.fromCharCode.apply(null, new Uint8Array(derivedKey)))+ DerivedKey: btoa(String.fromCharCode.apply(null, new Uint8Array(derivedKey)))};} catch (error) {console.error("Error getting WebAuthn credential:", error);- return { error: "WEBAUTHN_GET_ERROR", message: error.message };+ return { Error: "WEBAUTHN_GET_ERROR", Message: error.message };}}@@ -257,10 +257,10 @@ async function createWebAuthnCredentialAndDeriveKey(username) {let extensionsResult = newCredential.getClientExtensionResults();if (!extensionsResult.prf) {- return { error: "PRF_NOT_SUPPORTED" };+ return { Error: "PRF_NOT_SUPPORTED" };}- if (!extensionsResult.prf.results || !extensionsResult.prf.results.first) {+ if (!extensionsResult.prf?.results?.first) {alert("Your authenticator has been successfully registered. Please use your authenticator again to complete the process.")// Note: Some authenticators do not return the derived key in the create response. In this case,@@ -288,8 +288,8 @@ async function createWebAuthnCredentialAndDeriveKey(username) {extensionsResult = existingCredential.getClientExtensionResults();}- if (!extensionsResult.prf.results || !extensionsResult.prf.results.first) {- return { error: "PRF_DERIVATION_FAILED" };+ if (!extensionsResult.prf?.results?.first) {+ return { Error: "PRF_DERIVATION_FAILED" };}const derivedKey = extensionsResult.prf.results.first;@@ -302,7 +302,7 @@ async function createWebAuthnCredentialAndDeriveKey(username) {};} catch (createError) {console.error("Error creating new WebAuthn credential:", createError);- return { error: "WEBAUTHN_CREATE_ERROR", message: createError.message };+ return { Error: "WEBAUTHN_CREATE_ERROR", Message: createError.message };}}