Skip to content

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

Refactor password generator and make all use general settings (#167)

Leendert de Borstef328718cd66
diff --git a/.gitignore b/.gitignore
index 221883cc0..f9aa936e6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -423,3 +423,6 @@ database/postgres-dev
# Temp files
temp
+
+# VScode
+.vscode
diff --git a/aliasvault.sln b/aliasvault.sln
index d20754add..2de5fc1ab 100644
--- a/aliasvault.sln
+++ b/aliasvault.sln
@@ -59,8 +59,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.Cryptography.Cli
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Generators", "Generators", "{03D55CA4-20B3-4FEA-9ADD-3C7B5B10E0FE}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.Generators.Password", "src\Generators\AliasVault.Generators.Password\AliasVault.Generators.Password.csproj", "{47F47A1B-49E0-406A-81C8-31FF2E4C339B}"
-EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AliasVault.Generators.Identity", "src\Generators\AliasVault.Generators.Identity\AliasVault.Generators.Identity.csproj", "{80E74FBC-4EC8-45FB-B210-473337C484B5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{DD359F0A-0180-4F8F-9E48-46213386BA4D}"
@@ -161,10 +159,6 @@ Global
{542C7B7D-C2B4-4AE3-9B2C-C62FCF4DFF8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{542C7B7D-C2B4-4AE3-9B2C-C62FCF4DFF8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{542C7B7D-C2B4-4AE3-9B2C-C62FCF4DFF8E}.Release|Any CPU.Build.0 = Release|Any CPU
- {47F47A1B-49E0-406A-81C8-31FF2E4C339B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {47F47A1B-49E0-406A-81C8-31FF2E4C339B}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {47F47A1B-49E0-406A-81C8-31FF2E4C339B}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {47F47A1B-49E0-406A-81C8-31FF2E4C339B}.Release|Any CPU.Build.0 = Release|Any CPU
{80E74FBC-4EC8-45FB-B210-473337C484B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80E74FBC-4EC8-45FB-B210-473337C484B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80E74FBC-4EC8-45FB-B210-473337C484B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -188,6 +182,7 @@ Global
GlobalSection(NestedProjects) = preSolution
{ED328644-A152-403D-86EB-81201AA07744} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}
{8E6A418A-B305-465D-857D-49953605C18E} = {29DE523D-EEF2-41E9-AC12-F20D8D02BEBB}
+ {15EFE0D0-F41B-47D7-86B7-8F840335CB82} = {DD359F0A-0180-4F8F-9E48-46213386BA4D}
{AF013D08-1BF6-4E23-87D2-37F614BE7952} = {29DE523D-EEF2-41E9-AC12-F20D8D02BEBB}
{1277105D-50CD-4CE0-9C2C-549F46867E54} = {5F7417F6-4388-49CC-9511-ED63C4A6488A}
{FE10F294-817F-477E-A24F-8597A15AF0B5} = {5F7417F6-4388-49CC-9511-ED63C4A6488A}
@@ -198,16 +193,14 @@ Global
{1C7C8DE9-5F2A-43DB-A25E-33319E80A509} = {29DE523D-EEF2-41E9-AC12-F20D8D02BEBB}
{857BCD0E-753F-437A-AF75-B995B4D9A5FE} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}
{FF0B0E64-1AE2-415C-A404-0EB78010821A} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}
+ {59642CEF-D90A-4A6B-AD3F-9C6300D1E3FC} = {DD359F0A-0180-4F8F-9E48-46213386BA4D}
{951C3DF8-DF22-4B2B-839F-FBA26DDD8ABD} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}
{E8D9C551-67D2-4651-8EDF-4262DF7375CE} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}
{DA175274-0FF7-4436-9266-742F96C2D1ED} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}
{BB7E701E-B1C6-453E-800A-E12CE256318D} = {01AB9389-2F89-4F8E-A688-BF4BF1FC42C8}
{341EC443-0B6B-4E8C-AF46-D6156573CEA5} = {BB7E701E-B1C6-453E-800A-E12CE256318D}
{542C7B7D-C2B4-4AE3-9B2C-C62FCF4DFF8E} = {BB7E701E-B1C6-453E-800A-E12CE256318D}
- {47F47A1B-49E0-406A-81C8-31FF2E4C339B} = {03D55CA4-20B3-4FEA-9ADD-3C7B5B10E0FE}
{80E74FBC-4EC8-45FB-B210-473337C484B5} = {03D55CA4-20B3-4FEA-9ADD-3C7B5B10E0FE}
- {59642CEF-D90A-4A6B-AD3F-9C6300D1E3FC} = {DD359F0A-0180-4F8F-9E48-46213386BA4D}
- {15EFE0D0-F41B-47D7-86B7-8F840335CB82} = {DD359F0A-0180-4F8F-9E48-46213386BA4D}
{40CA41BF-9E67-4D0A-A3F8-38B94992E4CA} = {DD359F0A-0180-4F8F-9E48-46213386BA4D}
{D631A936-DD1C-40CC-B735-BD0A5D4F46A1} = {8A477241-B96C-4174-968D-D40CB77F1ECD}
{34FADEB6-4B56-463B-B359-F844B43D76D9} = {DD359F0A-0180-4F8F-9E48-46213386BA4D}
diff --git a/src/AliasVault.Client/AliasVault.Client.csproj b/src/AliasVault.Client/AliasVault.Client.csproj
index d425b8257..a885b6809 100644
--- a/src/AliasVault.Client/AliasVault.Client.csproj
+++ b/src/AliasVault.Client/AliasVault.Client.csproj
@@ -54,6 +54,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.2" />
+ <PackageReference Include="SpamOK.PasswordGenerator" Version="1.1.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -77,7 +78,6 @@
<ItemGroup>
<ProjectReference Include="..\Databases\AliasClientDb\AliasClientDb.csproj" />
<ProjectReference Include="..\Generators\AliasVault.Generators.Identity\AliasVault.Generators.Identity.csproj" />
- <ProjectReference Include="..\Generators\AliasVault.Generators.Password\AliasVault.Generators.Password.csproj" />
<ProjectReference Include="..\Shared\AliasVault.RazorComponents\AliasVault.RazorComponents.csproj" />
<ProjectReference Include="..\Shared\AliasVault.Shared.Core\AliasVault.Shared.Core.csproj" />
<ProjectReference Include="..\Shared\AliasVault.Shared\AliasVault.Shared.csproj" />
diff --git a/src/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor b/src/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
index cc873ec95..45e573976 100644
--- a/src/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
+++ b/src/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
@@ -3,12 +3,12 @@
<label for="@Id" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">@Label</label>
<div class="flex">
<div class="relative flex-grow">
- <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-l-lg block w-full p-2.5 pr-16 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" value="@Value" @oninput="OnInputChanged" placeholder="@Placeholder">
+ <input type="@(_internalShowPassword ? "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-l-lg block w-full p-2.5 pr-16 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white" value="@Value" @oninput="OnInputChanged" placeholder="@Placeholder">
</div>
<div class="flex">
<button type="button" class="px-3 text-gray-500 dark:text-white bg-gray-200 hover:bg-gray-300 focus:ring-4 focus:outline-none focus:ring-gray-300 font-medium text-sm dark:bg-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-800" @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)
+ @if (_internalShowPassword)
{
<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>
@@ -115,35 +115,34 @@
[Parameter]
public string Placeholder { get; set; } = string.Empty;
- private bool ShowPassword { get; set; } = false;
- private bool IsPasswordSettingsVisible { get; set; } = false;
+ /// <summary>
+ /// Controls whether the password is visible in plain text or not.
+ /// </summary>
+ [Parameter]
+ public bool ShowPassword { get; set; } = false;
private PasswordSettings PasswordSettings { get; set; } = new();
+ private bool IsPasswordSettingsVisible { get; set; }
+ private bool _internalShowPassword;
+ private bool _hasInitialized;
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
- await LoadPasswordSettings();
+ PasswordSettings = DbService.Settings.PasswordSettings;
+ _internalShowPassword = ShowPassword;
}
- /// <summary>
- /// Loads the password settings from the database. If it fails, we use the model's default values.
- /// </summary>
- private async Task LoadPasswordSettings()
+ /// <inheritdoc />
+ protected override void OnParametersSet()
{
- 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
+ base.OnParametersSet();
+
+ if (!_hasInitialized)
{
- // If there's an error loading settings, use defaults that model provides.
+ _internalShowPassword = ShowPassword;
+ _hasInitialized = true;
}
}
@@ -159,7 +158,7 @@
private void TogglePasswordVisibility()
{
- ShowPassword = !ShowPassword;
+ _internalShowPassword = !_internalShowPassword;
}
private void TogglePasswordSettings()
@@ -180,26 +179,14 @@
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();
+ string password = CredentialService.GenerateRandomPassword(PasswordSettings);
// Update the value
Value = password;
await ValueChanged.InvokeAsync(Value);
- // Make password visible when it's (re)generated so the user gets feedback that the password has changed.
- ShowPassword = true;
+ // Make password visible when it's (re)generated
+ _internalShowPassword = true;
}
}
diff --git a/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor b/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
index 79be19896..19a702b3c 100644
--- a/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
+++ b/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
@@ -95,7 +95,7 @@ else
<EditUsernameFormRow Id="username" Label="Username" @bind-Value="Obj.Username" OnGenerateNewUsername="GenerateRandomUsername"></EditUsernameFormRow>
</div>
<div class="col-span-6 sm:col-span-3">
- <EditPasswordFormRow Id="password" Label="Password" @bind-Value="Obj.Password.Value"></EditPasswordFormRow>
+ <EditPasswordFormRow Id="password" Label="Password" @bind-Value="Obj.Password.Value" ShowPassword="IsPasswordVisible"></EditPasswordFormRow>
</div>
</div>
</div>
@@ -139,6 +139,7 @@ else
private bool EditMode { get; set; }
private EditForm EditFormRef { get; set; } = null!;
private bool Loading { get; set; } = true;
+ private bool IsPasswordVisible { get; set; } = false;
private CredentialEdit Obj { get; set; } = new();
private IJSObjectReference? Module;
@@ -277,6 +278,7 @@ else
StateHasChanged();
Obj = CredentialEdit.FromEntity(await CredentialService.GenerateRandomIdentity(Obj.ToEntity()));
+ IsPasswordVisible = true;
GlobalLoadingSpinner.Hide();
StateHasChanged();
diff --git a/src/AliasVault.Client/Services/CredentialService.cs b/src/AliasVault.Client/Services/CredentialService.cs
index e0f038bf7..3935c28e9 100644
--- a/src/AliasVault.Client/Services/CredentialService.cs
+++ b/src/AliasVault.Client/Services/CredentialService.cs
@@ -16,7 +16,6 @@ using System.Threading.Tasks;
using AliasClientDb;
using AliasVault.Generators.Identity.Implementations.Factories;
using AliasVault.Generators.Identity.Models;
-using AliasVault.Generators.Password.Implementations;
using AliasVault.Shared.Models.WebApi.Favicon;
using Microsoft.EntityFrameworkCore;
@@ -31,39 +30,24 @@ public sealed class CredentialService(HttpClient httpClient, DbService dbService
/// </summary>
public const string DefaultServiceUrl = "https://";
- /// <summary>
- /// Generates a random password for a credential.
- /// </summary>
- /// <returns>Random password.</returns>
- public static string GenerateRandomPassword()
- {
- // Generate a random password using a IPasswordGenerator implementation.
- var passwordGenerator = new SpamOkPasswordGenerator();
- 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>
+ /// <param name="settings">PasswordSettings model.</param>
/// <returns>Random password.</returns>
- public static string GenerateRandomPassword(int length, bool useLowercase, bool useUppercase, bool useNumbers, bool useSpecialChars, bool useNonAmbiguousChars)
+ public static string GenerateRandomPassword(PasswordSettings settings)
{
// 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)
+
+ // Apply the settings.
+ var password = passwordBuilder
+ .SetLength(settings.Length)
+ .UseLowercaseLetters(settings.UseLowercase)
+ .UseUppercaseLetters(settings.UseUppercase)
+ .UseNumbers(settings.UseNumbers)
+ .UseSpecialChars(settings.UseSpecialChars)
+ .UseNonAmbiguousChars(settings.UseNonAmbiguousChars)
.GeneratePassword()
.ToString();
@@ -116,7 +100,8 @@ public sealed class CredentialService(HttpClient httpClient, DbService dbService
while (isEmailTaken && attempts < MaxAttempts);
// Generate password
- credential.Passwords.First().Value = GenerateRandomPassword();
+ var passwordSettings = dbService.Settings.PasswordSettings;
+ credential.Passwords.First().Value = GenerateRandomPassword(passwordSettings);
return credential;
}
diff --git a/src/AliasVault.Client/Services/SettingsService.cs b/src/AliasVault.Client/Services/SettingsService.cs
index 5cbfe7fcc..e6baf4201 100644
--- a/src/AliasVault.Client/Services/SettingsService.cs
+++ b/src/AliasVault.Client/Services/SettingsService.cs
@@ -62,6 +62,32 @@ public sealed class SettingsService
/// <returns>Credentials sort order as string.</returns>
public string CredentialsSortOrder => GetSetting("CredentialsSortOrder", "asc")!;
+ /// <summary>
+ /// Gets the password settings from the database. If it fails, we use the model's default values.
+ /// </summary>
+ public PasswordSettings PasswordSettings
+ {
+ get
+ {
+ try
+ {
+ var settingsJson = GetSetting<string>("PasswordGenerationSettings");
+ if (!string.IsNullOrEmpty(settingsJson))
+ {
+ // If settings are saved, load them.
+ return System.Text.Json.JsonSerializer.Deserialize<PasswordSettings>(settingsJson) ?? new PasswordSettings();
+ }
+ }
+ catch
+ {
+ // Ignore.
+ }
+
+ // If no settings are saved, return default settings.
+ return new PasswordSettings();
+ }
+ }
+
/// <summary>
/// Sets the DefaultEmailDomain setting.
/// </summary>
diff --git a/src/Generators/AliasVault.Generators.Password/AliasVault.Generators.Password.csproj b/src/Generators/AliasVault.Generators.Password/AliasVault.Generators.Password.csproj
deleted file mode 100644
index 0c5788664..000000000
--- a/src/Generators/AliasVault.Generators.Password/AliasVault.Generators.Password.csproj
+++ /dev/null
@@ -1,34 +0,0 @@
-<Project Sdk="Microsoft.NET.Sdk">
-
- <PropertyGroup>
- <TargetFramework>net9.0</TargetFramework>
- <ImplicitUsings>enable</ImplicitUsings>
- <Nullable>enable</Nullable>
- <LangVersion>13</LangVersion>
- </PropertyGroup>
-
- <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
- <DocumentationFile>bin\Debug\net9.0\AliasVault.Generators.Password.xml</DocumentationFile>
- <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
- </PropertyGroup>
-
- <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
- <DocumentationFile>bin\Release\net9.0\AliasVault.Generators.Password.xml</DocumentationFile>
- <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
- </PropertyGroup>
-
- <ItemGroup>
- <AdditionalFiles Include="..\..\stylecop.json">
- <Link>stylecop.json</Link>
- </AdditionalFiles>
- </ItemGroup>
-
- <ItemGroup>
- <PackageReference Include="SpamOK.PasswordGenerator" Version="1.1.0" />
- <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
- <PrivateAssets>all</PrivateAssets>
- <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
- </PackageReference>
- </ItemGroup>
-
-</Project>
diff --git a/src/Generators/AliasVault.Generators.Password/IPasswordGenerator.cs b/src/Generators/AliasVault.Generators.Password/IPasswordGenerator.cs
deleted file mode 100644
index 4d1da670d..000000000
--- a/src/Generators/AliasVault.Generators.Password/IPasswordGenerator.cs
+++ /dev/null
@@ -1,20 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="IPasswordGenerator.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.Generators.Password;
-
-/// <summary>
-/// Interface for password generators.
-/// </summary>
-public interface IPasswordGenerator
-{
- /// <summary>
- /// Generates a random password.
- /// </summary>
- /// <returns>Random generated password as string.</returns>
- string GenerateRandomPassword();
-}
diff --git a/src/Generators/AliasVault.Generators.Password/Implementations/SpamOkPasswordGenerator.cs b/src/Generators/AliasVault.Generators.Password/Implementations/SpamOkPasswordGenerator.cs
deleted file mode 100644
index 44336206b..000000000
--- a/src/Generators/AliasVault.Generators.Password/Implementations/SpamOkPasswordGenerator.cs
+++ /dev/null
@@ -1,31 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="SpamOkPasswordGenerator.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.Generators.Password.Implementations;
-
-/// <summary>
-/// Implementation of IPasswordGenerator which generates passwords using the SpamOK library.
-/// </summary>
-public class SpamOkPasswordGenerator : IPasswordGenerator
-{
- /// <inheritdoc/>
- public string GenerateRandomPassword()
- {
- var passwordBuilder = new SpamOK.PasswordGenerator.BasicPasswordBuilder();
- string password = passwordBuilder
- .SetLength(18)
- .UseLowercaseLetters(true)
- .UseUppercaseLetters(true)
- .UseNumbers(true)
- .UseSpecialChars(true)
- .UseNonAmbiguousChars(false)
- .GeneratePassword()
- .ToString();
-
- return password;
- }
-}