Skip to content

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

Add Rust password generator scaffolding to web app (#776)

Leendert de Borstf98f132a3ff9
diff --git a/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs b/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs
index 2d6b86e57..c06091369 100644
--- a/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs
+++ b/apps/server/AliasVault.Client/Main/Models/PasswordSettings.cs
@@ -49,4 +49,40 @@ public class PasswordSettings
/// </summary>
[JsonPropertyName("UseNonAmbiguousChars")]
public bool UseNonAmbiguousChars { get; set; } = false;
+
+ /// <summary>
+ /// Gets or sets which generator to use ("basic" or "diceware"). Defaults to "basic".
+ /// </summary>
+ [JsonPropertyName("Type")]
+ public string Type { get; set; } = "basic";
+
+ /// <summary>
+ /// Gets or sets the number of words in a Diceware passphrase.
+ /// </summary>
+ [JsonPropertyName("WordCount")]
+ public int WordCount { get; set; } = 4;
+
+ /// <summary>
+ /// Gets or sets the Diceware wordlist language. Unknown languages fall back to English in the Rust core.
+ /// </summary>
+ [JsonPropertyName("Language")]
+ public string Language { get; set; } = "English";
+
+ /// <summary>
+ /// Gets or sets the Diceware capitalization ("None", "TitleCase", "Uppercase", "Lowercase" or "Random").
+ /// </summary>
+ [JsonPropertyName("Capitalization")]
+ public string Capitalization { get; set; } = "Lowercase";
+
+ /// <summary>
+ /// Gets or sets the separator between Diceware words ("None", "Dash", "Space", "Underscore" or "Dot").
+ /// </summary>
+ [JsonPropertyName("Separator")]
+ public string Separator { get; set; } = "Dash";
+
+ /// <summary>
+ /// Gets or sets the optional random salt character for a Diceware passphrase ("None", "Prefix", "Sprinkle" or "Suffix").
+ /// </summary>
+ [JsonPropertyName("Salt")]
+ public string Salt { get; set; } = "None";
}
diff --git a/apps/server/AliasVault.Client/Services/ItemService.cs b/apps/server/AliasVault.Client/Services/ItemService.cs
index 131e76354..f6239614f 100644
--- a/apps/server/AliasVault.Client/Services/ItemService.cs
+++ b/apps/server/AliasVault.Client/Services/ItemService.cs
@@ -18,12 +18,13 @@ using System.Threading.Tasks;
using AliasClientDb;
using AliasClientDb.Models;
using AliasVault.Client.Main.Models;
+using AliasVault.Client.Services.JsInterop.RustCore;
using Microsoft.EntityFrameworkCore;
/// <summary>
/// Service class for Item operations.
/// </summary>
-public sealed class ItemService(HttpClient httpClient, DbService dbService, Config config, JsInteropService jsInteropService, FaviconService faviconService)
+public sealed class ItemService(HttpClient httpClient, DbService dbService, Config config, JsInteropService jsInteropService, RustCoreService rustCoreService, FaviconService faviconService)
{
/// <summary>
/// The default service URL used as placeholder in forms. When this value is set, the URL field is considered empty
@@ -50,13 +51,14 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
/// <returns>Random password.</returns>
public async Task<string> GenerateRandomPasswordAsync(PasswordSettings settings)
{
- // Sanity check: if all settings are false, then default to use lowercase letters only.
+ // Sanity check: if all character classes are disabled for the basic generator, default to lowercase
+ // letters only. This does not affect the diceware generator.
if (!settings.UseLowercase && !settings.UseUppercase && !settings.UseNumbers && !settings.UseSpecialChars && !settings.UseNonAmbiguousChars)
{
settings.UseLowercase = true;
}
- return await jsInteropService.GenerateRandomPasswordAsync(settings);
+ return await rustCoreService.GenerateRandomPasswordAsync(settings);
}
/// <summary>
@@ -124,9 +126,9 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
}
while (isEmailTaken && attempts < MaxAttempts);
- // Generate password using the TypeScript library
+ // Generate password using the Rust core.
var passwordSettings = dbService.Settings.PasswordSettings;
- var password = await jsInteropService.GenerateRandomPasswordAsync(passwordSettings);
+ var password = await rustCoreService.GenerateRandomPasswordAsync(passwordSettings);
SetFieldValue(item, FieldKey.LoginPassword, password);
return item;
diff --git a/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs b/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs
index c4ac37471..91a99cc6e 100644
--- a/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs
+++ b/apps/server/AliasVault.Client/Services/JsInterop/RustCore/RustCoreService.cs
@@ -7,7 +7,10 @@
namespace AliasVault.Client.Services.JsInterop.RustCore;
+using System.Security.Cryptography;
using System.Text.Json;
+using System.Text.Json.Nodes;
+using AliasVault.Client.Main.Models;
using Microsoft.JSInterop;
/// <summary>
@@ -331,6 +334,69 @@ public class RustCoreService : IAsyncDisposable
}
}
+ /// <summary>
+ /// Generate a cryptographically random 32-byte RNG seed as a 64-character lowercase hex string.
+ /// Supplying the same seed to <see cref="GenerateRandomPasswordAsync"/> yields the same output,
+ /// so the UI can re-apply formatting options to the same underlying password/words for easy comparison.
+ /// </summary>
+ /// <returns>A 64-character lowercase hex string.</returns>
+ public string GenerateSeed()
+ {
+ return Convert.ToHexString(RandomNumberGenerator.GetBytes(32)).ToLowerInvariant();
+ }
+
+ /// <summary>
+ /// Generate a password or passphrase using the Rust core.
+ /// The <see cref="PasswordSettings.Type"/> field selects the generator ("basic" or "diceware").
+ /// </summary>
+ /// <param name="settings">The password settings to use.</param>
+ /// <param name="seed">Optional 64-character hex RNG seed for deterministic generation; pass null for a fresh random password.</param>
+ /// <returns>The generated password/passphrase.</returns>
+ /// <exception cref="InvalidOperationException">Thrown if generation fails or WASM module is unavailable.</exception>
+ public async Task<string> GenerateRandomPasswordAsync(PasswordSettings settings, string? seed = null)
+ {
+ if (!await WaitForAvailabilityAsync())
+ {
+ throw new InvalidOperationException("Rust WASM module is not available.");
+ }
+
+ // Serialize to a JSON object. The PasswordSettings properties carry explicit [JsonPropertyName]
+ // attributes (PascalCase), which the Rust core expects, so the naming policy is irrelevant here.
+ if (JsonSerializer.SerializeToNode(settings) is not JsonObject node)
+ {
+ throw new InvalidOperationException("Failed to serialize password settings.");
+ }
+
+ if (!string.IsNullOrEmpty(seed))
+ {
+ node["Seed"] = seed;
+ }
+
+ return await jsRuntime.InvokeAsync<string>("rustCoreGeneratePassword", node.ToJsonString());
+ }
+
+ /// <summary>
+ /// Get the list of bundled Diceware wordlist language codes (first is the default, English).
+ /// </summary>
+ /// <returns>Array of language codes.</returns>
+ public async Task<string[]> GetDicewareLanguagesAsync()
+ {
+ if (!await IsAvailableAsync())
+ {
+ return ["English"];
+ }
+
+ try
+ {
+ var languages = await jsRuntime.InvokeAsync<string[]>("rustCoreGetDicewareLanguages");
+ return languages is { Length: > 0 } ? languages : ["English"];
+ }
+ catch
+ {
+ return ["English"];
+ }
+ }
+
/// <inheritdoc/>
public ValueTask DisposeAsync()
{
diff --git a/apps/server/AliasVault.Client/wwwroot/css/tailwind.css b/apps/server/AliasVault.Client/wwwroot/css/tailwind.css
index e2e9278fa..3d8e62587 100644
--- a/apps/server/AliasVault.Client/wwwroot/css/tailwind.css
+++ b/apps/server/AliasVault.Client/wwwroot/css/tailwind.css
@@ -961,6 +961,10 @@ video {
margin-top: 2rem;
}
+.-mb-px {
+ margin-bottom: -1px;
+}
+
.block {
display: block;
}
@@ -1302,6 +1306,10 @@ video {
max-width: 36rem;
}
+.max-w-\[90vw\] {
+ max-width: 90vw;
+}
+
.flex-1 {
flex: 1 1 0%;
}
@@ -1422,6 +1430,12 @@ video {
list-style-type: none;
}
+.appearance-none {
+ -webkit-appearance: none;
+ -moz-appearance: none;
+ appearance: none;
+}
+
.grid-cols-1 {
grid-template-columns: repeat(1, minmax(0, 1fr));
}
@@ -1442,6 +1456,10 @@ video {
grid-template-columns: repeat(7, minmax(0, 1fr));
}
+.grid-cols-3 {
+ grid-template-columns: repeat(3, minmax(0, 1fr));
+}
+
.flex-row {
flex-direction: row;
}
@@ -2339,6 +2357,10 @@ video {
padding-top: 2rem;
}
+.pt-0 {
+ padding-top: 0px;
+}
+
.text-left {
text-align: left;
}
diff --git a/apps/server/AliasVault.Client/wwwroot/js/rustCoreInterop.js b/apps/server/AliasVault.Client/wwwroot/js/rustCoreInterop.js
index 7cf0337bf..4af9cfeb8 100644
--- a/apps/server/AliasVault.Client/wwwroot/js/rustCoreInterop.js
+++ b/apps/server/AliasVault.Client/wwwroot/js/rustCoreInterop.js
@@ -205,6 +205,43 @@ window.rustCoreExtractRootDomain = async function(domain) {
}
};
+/**
+ * Generate a password or passphrase from JSON-serialized PasswordSettings.
+ * The "Type" field selects the generator ("basic" or "diceware").
+ * @param {string} settingsJson - JSON string containing PasswordSettings.
+ * @returns {Promise<string>} The generated password/passphrase.
+ */
+window.rustCoreGeneratePassword = async function(settingsJson) {
+ if (!await initRustCore()) {
+ throw new Error('Rust WASM module not available');
+ }
+
+ try {
+ return wasmModule.generatePassword(settingsJson);
+ } catch (error) {
+ console.error('[RustCore] Generate password failed:', error);
+ throw error;
+ }
+};
+
+/**
+ * Get the list of bundled Diceware language codes (first is the default, English).
+ * @returns {Promise<string[]>} Array of language codes.
+ */
+window.rustCoreGetDicewareLanguages = async function() {
+ if (!await initRustCore()) {
+ return ['English'];
+ }
+
+ try {
+ const languages = wasmModule.getDicewareLanguages();
+ return (languages && languages.length > 0) ? languages : ['English'];
+ } catch (error) {
+ console.error('[RustCore] Get diceware languages failed:', error);
+ return ['English'];
+ }
+};
+
/**
* Prune expired items from trash.
* Items that have been in trash (DeletedAt set) for longer than the retention period