Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add password generation settings GUI scaffolding (#167)

Leendert de Borst51b382a7391c
diff --git a/src/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor b/src/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
new file mode 100644
index 000000000..28067aa4a
--- /dev/null
+++ b/src/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
@@ -0,0 +1,198 @@
+@inject DbService DbService
+
+<label for="@Id" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">@Label</label>
+<div class="relative">
+ <input type="@(ShowPassword ? "text" : "password")" id="@Id" autocomplete="off" class="outline-0 shadow-sm bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg block w-full p-2.5 pr-20 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" value="@Value" @oninput="OnInputChanged" placeholder="@Placeholder">
+ <div class="absolute inset-y-0 right-0 flex items-center">
+ <button type="button" class="h-full px-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200" @onclick="TogglePasswordVisibility">
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
+ @if (ShowPassword)
+ {
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"></path>
+ }
+ else
+ {
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>
+ }
+ </svg>
+ </button>
+ <button type="button" class="h-full px-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200" @onclick="TogglePasswordSettings">
+ <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path>
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
+ </svg>
+ </button>
+ <button type="button" class="h-full px-2 text-white bg-gray-700 hover:bg-gray-800 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium rounded-r-lg text-sm dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800" @onclick="GeneratePassword">
+ New
+ </button>
+ </div>
+</div>
+
+@if (IsPasswordSettingsVisible)
+{
+ <div class="fixed inset-0 bg-gray-600 bg-opacity-50 z-30 overflow-y-auto h-full w-full" @onclick="ClosePasswordSettings">
+ <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white dark:bg-gray-800" @onclick:stopPropagation>
+ <div class="mt-3">
+ <h3 class="text-lg leading-6 font-medium text-gray-900 dark:text-white">Password Generation Settings</h3>
+ <div class="mt-4 space-y-4">
+ <div>
+ <label for="password-length" class="block text-sm font-medium text-gray-700 dark:text-gray-300">Password Length</label>
+ <input type="number" id="password-length" min="8" max="64" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white" @bind="PasswordSettings.Length">
+ </div>
+
+ <div class="flex items-center">
+ <input id="use-lowercase" type="checkbox" class="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded dark:bg-gray-700 dark:border-gray-600" @bind="PasswordSettings.UseLowercase">
+ <label for="use-lowercase" class="ml-2 block text-sm text-gray-700 dark:text-gray-300">Include lowercase letters (a-z)</label>
+ </div>
+
+ <div class="flex items-center">
+ <input id="use-uppercase" type="checkbox" class="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded dark:bg-gray-700 dark:border-gray-600" @bind="PasswordSettings.UseUppercase">
+ <label for="use-uppercase" class="ml-2 block text-sm text-gray-700 dark:text-gray-300">Include uppercase letters (A-Z)</label>
+ </div>
+
+ <div class="flex items-center">
+ <input id="use-numbers" type="checkbox" class="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded dark:bg-gray-700 dark:border-gray-600" @bind="PasswordSettings.UseNumbers">
+ <label for="use-numbers" class="ml-2 block text-sm text-gray-700 dark:text-gray-300">Include numbers (0-9)</label>
+ </div>
+
+ <div class="flex items-center">
+ <input id="use-special-chars" type="checkbox" class="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded dark:bg-gray-700 dark:border-gray-600" @bind="PasswordSettings.UseSpecialChars">
+ <label for="use-special-chars" class="ml-2 block text-sm text-gray-700 dark:text-gray-300">Include special characters (!@@#$%^&amp;*)</label>
+ </div>
+
+ <div class="flex items-center">
+ <input id="use-non-ambiguous" type="checkbox" class="h-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded dark:bg-gray-700 dark:border-gray-600" @bind="PasswordSettings.UseNonAmbiguousChars">
+ <label for="use-non-ambiguous" class="ml-2 block text-sm text-gray-700 dark:text-gray-300">Avoid ambiguous characters (1, l, I, 0, O, etc.)</label>
+ </div>
+
+ <div class="flex justify-between pt-4">
+ <button type="button" class="px-4 py-2 bg-gray-200 text-gray-800 rounded-md hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-gray-400 dark:bg-gray-700 dark:text-white dark:hover:bg-gray-600" @onclick="ClosePasswordSettings">
+ Cancel
+ </button>
+ <button type="button" class="px-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-primary-500 dark:bg-primary-700 dark:hover:bg-primary-600" @onclick="SavePasswordSettings">
+ Save Settings
+ </button>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+}
+
+@code {
+ /// <summary>
+ /// Id for the input field.
+ /// </summary>
+ [Parameter]
+ public string Id { get; set; } = string.Empty;
+
+ /// <summary>
+ /// Label for the input field.
+ /// </summary>
+ [Parameter]
+ public string Label { get; set; } = "Password";
+
+ /// <summary>
+ /// Value of the input field.
+ /// </summary>
+ [Parameter]
+ public string Value { get; set; } = string.Empty;
+
+ /// <summary>
+ /// Callback that is triggered when the value changes.
+ /// </summary>
+ [Parameter]
+ public EventCallback<string?> ValueChanged { get; set; }
+
+ /// <summary>
+ /// Placeholder text for the input field.
+ /// </summary>
+ [Parameter]
+ public string Placeholder { get; set; } = string.Empty;
+
+ private bool ShowPassword { get; set; } = false;
+ private bool IsPasswordSettingsVisible { get; set; } = false;
+
+ private PasswordSettings PasswordSettings { get; set; } = new();
+
+ /// <inheritdoc />
+ protected override async Task OnInitializedAsync()
+ {
+ await base.OnInitializedAsync();
+ await LoadPasswordSettings();
+ }
+
+ /// <summary>
+ /// Loads the password settings from the database. If it fails, we use the model's default values.
+ /// </summary>
+ private async Task LoadPasswordSettings()
+ {
+ try
+ {
+ var settingsJson = await DbService.Settings.GetSettingAsync<string>("PasswordGenerationSettings");
+ if (!string.IsNullOrEmpty(settingsJson))
+ {
+ // If settings are saved, load them.
+ PasswordSettings = System.Text.Json.JsonSerializer.Deserialize<PasswordSettings>(settingsJson) ?? new PasswordSettings();
+ }
+ }
+ catch
+ {
+ // If there's an error loading settings, use defaults that model provides.
+ }
+ }
+
+ private async Task SavePasswordSettings()
+ {
+ var settingsJson = System.Text.Json.JsonSerializer.Serialize(PasswordSettings);
+ await DbService.Settings.SetSettingAsync("PasswordGenerationSettings", settingsJson);
+ ClosePasswordSettings();
+
+ // Generate a new password with the new settings
+ await GeneratePassword();
+ }
+
+ private void TogglePasswordVisibility()
+ {
+ ShowPassword = !ShowPassword;
+ }
+
+ private void TogglePasswordSettings()
+ {
+ IsPasswordSettingsVisible = !IsPasswordSettingsVisible;
+ }
+
+ private void ClosePasswordSettings()
+ {
+ IsPasswordSettingsVisible = false;
+ }
+
+ private async Task OnInputChanged(ChangeEventArgs e)
+ {
+ Value = e.Value?.ToString() ?? string.Empty;
+ await ValueChanged.InvokeAsync(Value);
+ }
+
+ private async Task GeneratePassword()
+ {
+ // Create a password builder using the SpamOK library
+ var passwordBuilder = new SpamOK.PasswordGenerator.BasicPasswordBuilder();
+
+ // Apply the current settings
+ passwordBuilder
+ .SetLength(PasswordSettings.Length)
+ .UseLowercaseLetters(PasswordSettings.UseLowercase)
+ .UseUppercaseLetters(PasswordSettings.UseUppercase)
+ .UseNumbers(PasswordSettings.UseNumbers)
+ .UseSpecialChars(PasswordSettings.UseSpecialChars)
+ .UseNonAmbiguousChars(PasswordSettings.UseNonAmbiguousChars);
+
+ // Generate the password
+ string password = passwordBuilder.GeneratePassword().ToString();
+
+ // Update the value
+ Value = password;
+ await ValueChanged.InvokeAsync(Value);
+ }
+}
diff --git a/src/AliasVault.Client/Main/Models/PasswordSettings.cs b/src/AliasVault.Client/Main/Models/PasswordSettings.cs
new file mode 100644
index 000000000..4a3dc31df
--- /dev/null
+++ b/src/AliasVault.Client/Main/Models/PasswordSettings.cs
@@ -0,0 +1,44 @@
+//-----------------------------------------------------------------------
+// <copyright file="PasswordSettings.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Client.Main.Models;
+
+/// <summary>
+/// Settings for password generation.
+/// </summary>
+public class PasswordSettings
+{
+ /// <summary>
+ /// Gets or sets the length of the password.
+ /// </summary>
+ public int Length { get; set; } = 18;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to use lowercase letters.
+ /// </summary>
+ public bool UseLowercase { get; set; } = true;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to use uppercase letters.
+ /// </summary>
+ public bool UseUppercase { get; set; } = true;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to use numbers.
+ /// </summary>
+ public bool UseNumbers { get; set; } = true;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to use special characters.
+ /// </summary>
+ public bool UseSpecialChars { get; set; } = true;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to use non-ambiguous characters.
+ /// </summary>
+ public bool UseNonAmbiguousChars { get; set; } = false;
+}
diff --git a/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor b/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
index 380e66dff..fc44c8cc6 100644
--- a/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
+++ b/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
@@ -98,10 +98,7 @@ else
</div>
</div>
<div class="col-span-6 sm:col-span-3">
- <div class="relative">
- <EditFormRow Id="password" Label="Password" @bind-Value="Obj.Password.Value"></EditFormRow>
- <button type="button" class="text-white absolute end-1 bottom-1 bg-gray-700 hover:bg-gray-800 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium rounded-lg text-sm px-4 py-2 dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800" @onclick="GenerateRandomPassword">New Password</button>
- </div>
+ <EditPasswordFormRow Id="password" Label="Password" @bind-Value="Obj.Password.Value"></EditPasswordFormRow>
</div>
</div>
</div>
@@ -317,14 +314,6 @@ else
Obj.Username = generator.GenerateUsername(identity);
}
- /// <summary>
- /// Generate a new random password.
- /// </summary>
- private void GenerateRandomPassword()
- {
- Obj.Password.Value = CredentialService.GenerateRandomPassword();
- }
-
/// <summary>
/// Cancel the edit operation and navigate back to the credentials view.
/// </summary>
diff --git a/src/AliasVault.Client/Services/CredentialService.cs b/src/AliasVault.Client/Services/CredentialService.cs
index a62ab1df1..e0f038bf7 100644
--- a/src/AliasVault.Client/Services/CredentialService.cs
+++ b/src/AliasVault.Client/Services/CredentialService.cs
@@ -42,6 +42,34 @@ public sealed class CredentialService(HttpClient httpClient, DbService dbService
return passwordGenerator.GenerateRandomPassword();
}
+ /// <summary>
+ /// Generates a random password for a credential using the specified settings.
+ /// </summary>
+ /// <param name="length">The length of the password.</param>
+ /// <param name="useLowercase">Whether to include lowercase letters.</param>
+ /// <param name="useUppercase">Whether to include uppercase letters.</param>
+ /// <param name="useNumbers">Whether to include numbers.</param>
+ /// <param name="useSpecialChars">Whether to include special characters.</param>
+ /// <param name="useNonAmbiguousChars">Whether to avoid ambiguous characters.</param>
+ /// <returns>Random password.</returns>
+ public static string GenerateRandomPassword(int length, bool useLowercase, bool useUppercase, bool useNumbers, bool useSpecialChars, bool useNonAmbiguousChars)
+ {
+ // Generate a random password using a IPasswordGenerator implementation.
+ var passwordGenerator = new SpamOkPasswordGenerator();
+ var passwordBuilder = new SpamOK.PasswordGenerator.BasicPasswordBuilder();
+ string password = passwordBuilder
+ .SetLength(length)
+ .UseLowercaseLetters(useLowercase)
+ .UseUppercaseLetters(useUppercase)
+ .UseNumbers(useNumbers)
+ .UseSpecialChars(useSpecialChars)
+ .UseNonAmbiguousChars(useNonAmbiguousChars)
+ .GeneratePassword()
+ .ToString();
+
+ return password;
+ }
+
/// <summary>
/// Generates a random identity for a credential.
/// </summary>
diff --git a/src/AliasVault.Client/Services/SettingsService.cs b/src/AliasVault.Client/Services/SettingsService.cs
index cc444937f..5cbfe7fcc 100644
--- a/src/AliasVault.Client/Services/SettingsService.cs
+++ b/src/AliasVault.Client/Services/SettingsService.cs
@@ -104,6 +104,30 @@ public sealed class SettingsService
/// <returns>Task.</returns>
public Task SetCredentialsSortOrder(string value) => SetSettingAsync("CredentialsSortOrder", value);
+ /// <summary>
+ /// Gets a setting value by key.
+ /// </summary>
+ /// <typeparam name="T">The type to cast the setting to.</typeparam>
+ /// <param name="key">The key of the setting.</param>
+ /// <returns>The setting value cast to type T, or default if not found.</returns>
+ public Task<T?> GetSettingAsync<T>(string key)
+ {
+ return Task.FromResult(GetSetting<T>(key, default));
+ }
+
+ /// <summary>
+ /// Sets a setting asynchronously, converting the value to a string so its compatible with the database field.
+ /// </summary>
+ /// <typeparam name="T">The type of the value being set.</typeparam>
+ /// <param name="key">The key of the setting.</param>
+ /// <param name="value">The value to set.</param>
+ /// <returns>Task.</returns>
+ public Task SetSettingAsync<T>(string key, T value)
+ {
+ string stringValue = ConvertToString(value);
+ return SetSettingAsync(key, stringValue);
+ }
+
/// <summary>
/// Initializes the settings service asynchronously.
/// </summary>
@@ -245,19 +269,6 @@ public sealed class SettingsService
}
}
- /// <summary>
- /// Sets a setting asynchronously, converting the value to a string so its compatible with the database field.
- /// </summary>
- /// <typeparam name="T">The type of the value being set.</typeparam>
- /// <param name="key">The key of the setting.</param>
- /// <param name="value">The value to set.</param>
- /// <returns>Task.</returns>
- private Task SetSettingAsync<T>(string key, T value)
- {
- string stringValue = ConvertToString(value);
- return SetSettingAsync(key, stringValue);
- }
-
/// <summary>
/// Set setting value in database.
/// </summary>
diff --git a/src/AliasVault.Client/wwwroot/css/tailwind.css b/src/AliasVault.Client/wwwroot/css/tailwind.css
index 2f8f15242..fad807915 100644
--- a/src/AliasVault.Client/wwwroot/css/tailwind.css
+++ b/src/AliasVault.Client/wwwroot/css/tailwind.css
@@ -1172,6 +1172,10 @@ video {
justify-content: space-between;
}
+.gap-2 {
+ gap: 0.5rem;
+}
+
.gap-3 {
gap: 0.75rem;
}
@@ -1184,10 +1188,6 @@ video {
gap: 1.5rem;
}
-.gap-2 {
- gap: 0.5rem;
-}
-
.space-x-1 > :not([hidden]) ~ :not([hidden]) {
--tw-space-x-reverse: 0;
margin-right: calc(0.25rem * var(--tw-space-x-reverse));
@@ -1719,6 +1719,10 @@ video {
padding-bottom: 2rem;
}
+.pe-3 {
+ padding-inline-end: 0.75rem;
+}
+
.pl-2 {
padding-left: 0.5rem;
}
@@ -1735,6 +1739,10 @@ video {
padding-right: 0.75rem;
}
+.ps-3 {
+ padding-inline-start: 0.75rem;
+}
+
.pt-16 {
padding-top: 4rem;
}
@@ -1755,14 +1763,6 @@ video {
padding-top: 2rem;
}
-.pe-3 {
- padding-inline-end: 0.75rem;
-}
-
-.ps-3 {
- padding-inline-start: 0.75rem;
-}
-
.text-left {
text-align: left;
}
@@ -1860,6 +1860,10 @@ video {
text-transform: uppercase;
}
+.lowercase {
+ text-transform: lowercase;
+}
+
.italic {
font-style: italic;
}
@@ -2227,6 +2231,11 @@ video {
color: rgb(107 114 128 / var(--tw-text-opacity));
}
+.hover\:text-gray-700:hover {
+ --tw-text-opacity: 1;
+ color: rgb(55 65 81 / var(--tw-text-opacity));
+}
+
.hover\:text-gray-800:hover {
--tw-text-opacity: 1;
color: rgb(31 41 55 / var(--tw-text-opacity));
@@ -2506,6 +2515,11 @@ video {
background-color: rgb(214 131 56 / var(--tw-bg-opacity));
}
+.dark\:bg-primary-700:is(.dark *) {
+ --tw-bg-opacity: 1;
+ background-color: rgb(184 112 47 / var(--tw-bg-opacity));
+}
+
.dark\:bg-primary-900:is(.dark *) {
--tw-bg-opacity: 1;
background-color: rgb(123 74 30 / var(--tw-bg-opacity));