Skip to content

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

Refactor (#145)

Leendert de Borst885630b5db1f
diff --git a/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor b/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
index 52758ccbd..ed956ade4 100644
--- a/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
+++ b/src/AliasVault.Client/Main/Pages/Credentials/AddEdit.razor
@@ -416,7 +416,7 @@ else
// Get the first valid domain from private or public domains
string GetFirstValidDomain() =>
- Config.PrivateEmailDomains.FirstOrDefault(IsValidDomain) ??
+ Config.PrivateEmailDomains.Find(IsValidDomain) ??
Config.PublicEmailDomains.FirstOrDefault() ??
"example.com";
diff --git a/src/AliasVault.Client/Services/Database/DbService.cs b/src/AliasVault.Client/Services/Database/DbService.cs
index 8414f25cb..cce7a8db2 100644
--- a/src/AliasVault.Client/Services/Database/DbService.cs
+++ b/src/AliasVault.Client/Services/Database/DbService.cs
@@ -27,7 +27,7 @@ public class DbService : IDisposable
private readonly HttpClient _httpClient;
private readonly DbServiceState _state = new();
private readonly Config _config;
- private SettingsService _settingsService = new();
+ private readonly SettingsService _settingsService = new();
private SqliteConnection _sqlConnection;
private AliasClientDbContext _dbContext;
private bool _isSuccessfullyInitialized;
diff --git a/src/AliasVault.Client/Services/SettingsService.cs b/src/AliasVault.Client/Services/SettingsService.cs
index 504aad6b4..d7f97ad15 100644
--- a/src/AliasVault.Client/Services/SettingsService.cs
+++ b/src/AliasVault.Client/Services/SettingsService.cs
@@ -77,6 +77,78 @@ public class SettingsService
_initialized = true;
}
+ /// <summary>
+ /// Casts a setting value from the database string type to the specified requested type.
+ /// </summary>
+ /// <param name="value">Value (string) to cast.</param>
+ /// <typeparam name="T">Type to cast it to.</typeparam>
+ /// <returns>The value casted to the requested type.</returns>
+ private static T? CastSetting<T>(string value)
+ {
+ if (string.IsNullOrEmpty(value))
+ {
+ if (default(T) is null)
+ {
+ return default;
+ }
+
+ throw new InvalidOperationException($"Setting value is null or empty for non-nullable type {typeof(T)}");
+ }
+
+ if (typeof(T) == typeof(bool))
+ {
+ return (T)(object)(bool.TryParse(value, out bool result) && result);
+ }
+
+ if (typeof(T) == typeof(int))
+ {
+ return (T)(object)int.Parse(value);
+ }
+
+ if (typeof(T) == typeof(double))
+ {
+ return (T)(object)double.Parse(value);
+ }
+
+ if (typeof(T) == typeof(string))
+ {
+ return (T)(object)value;
+ }
+
+ // For complex types, attempt JSON deserialization
+ try
+ {
+ var result = JsonSerializer.Deserialize<T>(value);
+ if (result is null && default(T) is not null)
+ {
+ throw new InvalidOperationException($"Deserialization resulted in null for non-nullable type {typeof(T)}");
+ }
+
+ return result;
+ }
+ catch (JsonException ex)
+ {
+ throw new InvalidOperationException($"Failed to deserialize value to type {typeof(T)}", ex);
+ }
+ }
+
+ /// <summary>
+ /// Converts a value of any type to a string.
+ /// </summary>
+ /// <param name="value">The value to convert.</param>
+ /// <typeparam name="T">The type of the existing value.</typeparam>
+ /// <returns>Value converted to string.</returns>
+ private static string ConvertToString<T>(T value)
+ {
+ if (value is bool || value is int || value is double || value is string)
+ {
+ return value.ToString() ?? string.Empty;
+ }
+
+ // For complex types, use JSON serialization
+ return JsonSerializer.Serialize(value);
+ }
+
/// <summary>
/// Get setting value from database.
/// </summary>
@@ -163,76 +235,4 @@ public class SettingsService
await _dbService.SaveDatabaseAsync();
}
-
- /// <summary>
- /// Casts a setting value from the database string type to the specified requested type.
- /// </summary>
- /// <param name="value">Value (string) to cast.</param>
- /// <typeparam name="T">Type to cast it to.</typeparam>
- /// <returns>The value casted to the requested type.</returns>
- private T? CastSetting<T>(string value)
- {
- if (string.IsNullOrEmpty(value))
- {
- if (default(T) is null)
- {
- return default;
- }
-
- throw new InvalidOperationException($"Setting value is null or empty for non-nullable type {typeof(T)}");
- }
-
- if (typeof(T) == typeof(bool))
- {
- return (T)(object)(bool.TryParse(value, out bool result) && result);
- }
-
- if (typeof(T) == typeof(int))
- {
- return (T)(object)int.Parse(value);
- }
-
- if (typeof(T) == typeof(double))
- {
- return (T)(object)double.Parse(value);
- }
-
- if (typeof(T) == typeof(string))
- {
- return (T)(object)value;
- }
-
- // For complex types, attempt JSON deserialization
- try
- {
- var result = JsonSerializer.Deserialize<T>(value);
- if (result is null && default(T) is not null)
- {
- throw new InvalidOperationException($"Deserialization resulted in null for non-nullable type {typeof(T)}");
- }
-
- return result;
- }
- catch (JsonException ex)
- {
- throw new InvalidOperationException($"Failed to deserialize value to type {typeof(T)}", ex);
- }
- }
-
- /// <summary>
- /// Converts a value of any type to a string.
- /// </summary>
- /// <param name="value">The value to convert.</param>
- /// <typeparam name="T">The type of the existing value.</typeparam>
- /// <returns>Value converted to string.</returns>
- private string ConvertToString<T>(T value)
- {
- if (value is bool || value is int || value is double || value is string)
- {
- return value.ToString() ?? string.Empty;
- }
-
- // For complex types, use JSON serialization
- return JsonSerializer.Serialize(value);
- }
}
diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/GeneralSettingsTest.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/GeneralSettingsTest.cs
index fd6481396..47b7ab958 100644
--- a/src/Tests/AliasVault.E2ETests/Tests/Client/GeneralSettingsTest.cs
+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/GeneralSettingsTest.cs
@@ -31,7 +31,7 @@ public class GeneralSettingsTest : ClientPlaywrightTest
// Set the default email domain to "example2.tld".
var defaultEmailDomainField = Page.Locator("select[id='defaultEmailDomain']");
- await defaultEmailDomainField.SelectOptionAsync(new[] { "example2.tld" });
+ await defaultEmailDomainField.SelectOptionAsync("example2.tld");
// Go to new credential create page and assert that the default email domain is visible on the page.
await NavigateUsingBlazorRouter("add-credentials");