Skip to content

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

Replace SpamOK.PasswordGenerator with shared TS implementation (#896)

Leendert de Borst538675f39105
diff --git a/apps/server/AliasVault.Client/AliasVault.Client.csproj b/apps/server/AliasVault.Client/AliasVault.Client.csproj
index 158c92640..226627189 100644
--- a/apps/server/AliasVault.Client/AliasVault.Client.csproj
+++ b/apps/server/AliasVault.Client/AliasVault.Client.csproj
@@ -54,7 +54,6 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.4" />
- <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>
diff --git a/apps/server/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor b/apps/server/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
index d815c9a52..20338c76f 100644
--- a/apps/server/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
+++ b/apps/server/AliasVault.Client/Main/Components/Forms/EditPasswordFormRow.razor
@@ -1,4 +1,5 @@
@inject DbService DbService
+@inject CredentialService CredentialService
<label for="@Id" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">@Label</label>
<div class="flex">
@@ -168,7 +169,7 @@
/// </summary>
private async Task GeneratePassword()
{
- string newPassword = CredentialService.GenerateRandomPassword(_internalPasswordSettings);
+ string newPassword = await CredentialService.GenerateRandomPasswordAsync(_internalPasswordSettings);
// Update the local value.
Value = newPassword;
diff --git a/apps/server/AliasVault.Client/Main/Components/Settings/PasswordSettingsPopup.razor b/apps/server/AliasVault.Client/Main/Components/Settings/PasswordSettingsPopup.razor
index 82fd3054d..639340662 100644
--- a/apps/server/AliasVault.Client/Main/Components/Settings/PasswordSettingsPopup.razor
+++ b/apps/server/AliasVault.Client/Main/Components/Settings/PasswordSettingsPopup.razor
@@ -2,6 +2,7 @@
@inject DbService DbService
@inject GlobalLoadingService GlobalLoadingService
@inject GlobalNotificationService GlobalNotificationService
+@inject CredentialService CredentialService
<ClickOutsideHandler OnClose="OnClose" ContentId="passwordSettingsModal">
<ModalWrapper OnEnter="HandleEnterKey">
@@ -17,31 +18,31 @@
</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"
+ <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="_workingSettings.UseLowercase" @bind:after="OnPasswordSettingsChanged">
<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"
+ <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="_workingSettings.UseUppercase" @bind:after="OnPasswordSettingsChanged">
<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"
+ <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="_workingSettings.UseNumbers" @bind:after="OnPasswordSettingsChanged">
<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"
+ <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="_workingSettings.UseSpecialChars" @bind:after="OnPasswordSettingsChanged">
<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"
+ <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="_workingSettings.UseNonAmbiguousChars" @bind:after="OnPasswordSettingsChanged">
<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>
@@ -115,7 +116,7 @@
private string _previewPassword = string.Empty;
/// <inheritdoc />
- protected override void OnInitialized()
+ protected override async Task OnInitializedAsync()
{
// Clone the settings to avoid reference issues
_workingSettings = new PasswordSettings
@@ -128,16 +129,17 @@
UseNonAmbiguousChars = PasswordSettings.UseNonAmbiguousChars
};
- RefreshPreview();
+ await RefreshPreview();
}
/// <summary>
/// Refresh the preview password.
/// </summary>
- private void RefreshPreview()
+ private async Task RefreshPreview()
{
- try {
- _previewPassword = CredentialService.GenerateRandomPassword(_workingSettings);
+ try
+ {
+ _previewPassword = await CredentialService.GenerateRandomPasswordAsync(_workingSettings);
}
catch
{
@@ -148,22 +150,22 @@
/// <summary>
/// Handle input from the password length input.
/// </summary>
- private void HandleLengthInput(ChangeEventArgs e)
+ private async Task HandleLengthInput(ChangeEventArgs e)
{
int newLength;
if (int.TryParse(e.Value?.ToString(), out newLength))
{
_workingSettings.Length = newLength;
- RefreshPreview();
+ await RefreshPreview();
}
}
/// <summary>
/// Handle changes to the password settings.
/// </summary>
- private void OnPasswordSettingsChanged()
+ private async Task OnPasswordSettingsChanged()
{
- RefreshPreview();
+ await RefreshPreview();
}
/// <summary>
diff --git a/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs b/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs
index bafa259e7..89eb767f2 100644
--- a/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs
+++ b/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs
@@ -7,6 +7,8 @@
namespace AliasVault.Client.Main.Models;
+using System.Text.Json.Serialization;
+
/// <summary>
/// Settings for password generation.
/// </summary>
@@ -15,30 +17,36 @@ public class PasswordSettings
/// <summary>
/// Gets or sets the length of the password.
/// </summary>
+ [JsonPropertyName("Length")]
public int Length { get; set; } = 18;
/// <summary>
/// Gets or sets a value indicating whether to use lowercase letters.
/// </summary>
+ [JsonPropertyName("UseLowercase")]
public bool UseLowercase { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to use uppercase letters.
/// </summary>
+ [JsonPropertyName("UseUppercase")]
public bool UseUppercase { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to use numbers.
/// </summary>
+ [JsonPropertyName("UseNumbers")]
public bool UseNumbers { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to use special characters.
/// </summary>
+ [JsonPropertyName("UseSpecialChars")]
public bool UseSpecialChars { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to use non-ambiguous characters.
/// </summary>
+ [JsonPropertyName("UseNonAmbiguousChars")]
public bool UseNonAmbiguousChars { get; set; } = false;
}
diff --git a/apps/server/AliasVault.Client/Services/CredentialService.cs b/apps/server/AliasVault.Client/Services/CredentialService.cs
index a39923538..4ff9325d1 100644
--- a/apps/server/AliasVault.Client/Services/CredentialService.cs
+++ b/apps/server/AliasVault.Client/Services/CredentialService.cs
@@ -33,29 +33,15 @@ public sealed class CredentialService(HttpClient httpClient, DbService dbService
/// </summary>
/// <param name="settings">PasswordSettings model.</param>
/// <returns>Random password.</returns>
- public static string GenerateRandomPassword(PasswordSettings settings)
+ public async Task<string> GenerateRandomPasswordAsync(PasswordSettings settings)
{
- // Generate a random password using a IPasswordGenerator implementation.
- var passwordBuilder = new SpamOK.PasswordGenerator.BasicPasswordBuilder();
-
// Sanity check: if all settings are false, then default to use lowercase letters only.
if (!settings.UseLowercase && !settings.UseUppercase && !settings.UseNumbers && !settings.UseSpecialChars && !settings.UseNonAmbiguousChars)
{
settings.UseLowercase = true;
}
- // 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();
-
- return password;
+ return await jsInteropService.GenerateRandomPasswordAsync(settings);
}
/// <summary>
diff --git a/apps/server/AliasVault.Client/Services/JsInteropService.cs b/apps/server/AliasVault.Client/Services/JsInteropService.cs
index 2bbecfeed..bc84d9082 100644
--- a/apps/server/AliasVault.Client/Services/JsInteropService.cs
+++ b/apps/server/AliasVault.Client/Services/JsInteropService.cs
@@ -291,8 +291,8 @@ public sealed class JsInteropService(IJSRuntime jsRuntime)
/// Generates a random identity using the specified language.
/// </summary>
/// <param name="language">The language to use for generating the identity (e.g. "en", "nl").</param>
- /// <returns>A tuple containing the generated identity information.</returns>
- public async Task<(string FirstName, string LastName, string NickName, string EmailPrefix, string Gender, DateTime BirthDate)> GenerateRandomIdentityAsync(string language)
+ /// <returns>A <see cref="IdentityGeneratorResult"/> containing the generated identity information.</returns>
+ public async Task<IdentityGeneratorResult> GenerateRandomIdentityAsync(string language)
{
try
{
@@ -306,15 +306,9 @@ public sealed class JsInteropService(IJSRuntime jsRuntime)
}
var generatorInstance = await _identityGeneratorModule.InvokeAsync<IJSObjectReference>("createGenerator", language);
- var result = await generatorInstance.InvokeAsync<JsonElement>("generateRandomIdentity");
-
- return (
- result.GetProperty("firstName").GetString()!,
- result.GetProperty("lastName").GetString()!,
- result.GetProperty("nickName").GetString()!,
- result.GetProperty("emailPrefix").GetString()!,
- result.GetProperty("gender").GetString()!,
- result.GetProperty("birthDate").GetDateTime());
+ var result = await generatorInstance.InvokeAsync<IdentityGeneratorResult>("generateRandomIdentity");
+
+ return result;
}
catch (JSException ex)
{
@@ -354,6 +348,42 @@ public sealed class JsInteropService(IJSRuntime jsRuntime)
}
}
+ /// <summary>
+ /// Represents the result of a JavaScript identity generator operation.
+ /// </summary>
+ public sealed class IdentityGeneratorResult
+ {
+ /// <summary>
+ /// Gets the first name.
+ /// </summary>
+ public string? FirstName { get; init; }
+
+ /// <summary>
+ /// Gets the last name.
+ /// </summary>
+ public string? LastName { get; init; }
+
+ /// <summary>
+ /// Gets the birth date.
+ /// </summary>
+ public DateTime BirthDate { get; init; }
+
+ /// <summary>
+ /// Gets the email prefix.
+ /// </summary>
+ public string? EmailPrefix { get; init; }
+
+ /// <summary>
+ /// Gets the nickname.
+ /// </summary>
+ public string? NickName { get; init; }
+
+ /// <summary>
+ /// Gets the gender.
+ /// </summary>
+ public string? Gender { get; init; }
+ }
+
/// <summary>
/// Represents the result of a WebAuthn get credential operation.
/// </summary>
diff --git a/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.d.ts b/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.d.ts
index 373c840bf..4af3336ce 100644
--- a/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.d.ts
+++ b/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.d.ts
@@ -112,8 +112,9 @@ declare class PasswordGenerator {
/**
* Creates a new password generator.
+ * @param settings - The settings for the password generator.
* @returns A new password generator instance.
*/
-declare const createPasswordGenerator: () => PasswordGenerator;
+declare const createPasswordGenerator: (settings: PasswordSettings) => PasswordGenerator;
export { PasswordGenerator, type PasswordSettings, createPasswordGenerator };
diff --git a/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.js b/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.js
index f155250ad..a62f6fc8c 100644
--- a/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.js
+++ b/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.js
@@ -233,8 +233,8 @@ var PasswordGenerator = class {
};
// src/factories/PasswordGeneratorFactory.ts
-var createPasswordGenerator = () => {
- return new PasswordGenerator();
+var createPasswordGenerator = (settings) => {
+ return new PasswordGenerator(settings);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
diff --git a/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.mjs b/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.mjs
index 8d845caa4..01610dfba 100644
--- a/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.mjs
+++ b/apps/server/AliasVault.Client/wwwroot/js/shared/password-generator/index.mjs
@@ -206,8 +206,8 @@ var PasswordGenerator = class {
};
// src/factories/PasswordGeneratorFactory.ts
-var createPasswordGenerator = () => {
- return new PasswordGenerator();
+var createPasswordGenerator = (settings) => {
+ return new PasswordGenerator(settings);
};
export {
PasswordGenerator,