Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update AuthHelper.cs (#2159)
Leendert de Borste892196cfa60
diff --git a/apps/server/AliasVault.Api/Helpers/AuthHelper.cs b/apps/server/AliasVault.Api/Helpers/AuthHelper.csindex 2a1528e7e..4c2df6537 100644--- a/apps/server/AliasVault.Api/Helpers/AuthHelper.cs+++ b/apps/server/AliasVault.Api/Helpers/AuthHelper.cs@@ -10,6 +10,7 @@ namespace AliasVault.Api.Helpers;using AliasServerDb;using AliasVault.Api.Headers;using AliasVault.Cryptography.Client;+using Microsoft.EntityFrameworkCore;using Microsoft.Extensions.Caching.Memory;using SecureRemotePassword;@@ -28,36 +29,16 @@ public static class AuthHelper/// </summary>public static readonly string CachePrefixFakeData = "FakeData_";- /// <summary>- /// The KeyType value for password-based vault keys (KEK derived from the master password via Argon2id).- /// </summary>- public static readonly string VaultKeyTypePassword = "password";-- /// <summary>- /// The WrapScheme value for a VEK wrapped symmetrically with a method-derived KEK (self-unlock keys).- /// </summary>- public static readonly string WrapSchemeAesGcmKek = "aesgcm-kek";-- /// <summary>- /// The KeyType value for a folder-access grant: a wrapped VEK held by a member of a shared (non-root) manifest.- /// </summary>- public static readonly string VaultKeyTypeShared = "shared";-- /// <summary>- /// The asymmetric WrapScheme values used when wrapping a shared folder's VEK for a recipient's public key.- /// Can support other schemes in the future, but only rsa-oaep is currently used.- /// </summary>- public static readonly IReadOnlySet<string> AsymmetricWrapSchemes = new HashSet<string>(StringComparer.Ordinal) { "rsa-oaep" };-/// <summary>/// Helper method that validates the SRP session based on provided SRP identity, ephemeral and proof./// </summary>/// <param name="cache">IMemoryCache instance.</param>+ /// <param name="context">Database context, used to resolve the user's current SRP credentials.</param>/// <param name="user">The user object.</param>/// <param name="clientEphemeral">The client ephemeral value.</param>/// <param name="clientSessionProof">The client session proof.</param>/// <returns>SrpSession if validation succeeds, null otherwise.</returns>- public static SrpSession? ValidateSrpSession(IMemoryCache cache, AliasVaultUser user, string clientEphemeral, string clientSessionProof)+ public static async Task<SrpSession?> ValidateSrpSessionAsync(IMemoryCache cache, AliasServerDbContext context, AliasVaultUser user, string clientEphemeral, string clientSessionProof){// Get or create SRP identity. For existing users without SrpIdentity, fall back to username (lowercase).var srpIdentity = user.SrpIdentity ?? user.UserName!.ToLowerInvariant();@@ -68,7 +49,7 @@ public static class AuthHelper}// Retrieve latest vault of user which contains the current salt and verifier.- var latestVaultEncryptionSettings = GetUserLatestVaultEncryptionSettings(user);+ var latestVaultEncryptionSettings = await GetUserLatestVaultEncryptionSettingsAsync(context, user);// Use SrpIdentity for the SRP session derivation. This is the fixed identity that was used// when the verifier was originally created, ensuring username changes don't break authentication.@@ -90,21 +71,27 @@ public static class AuthHelper/// <summary>/// Get the user's current SRP salt/verifier and key derivation settings. For users migrated to the KEK/VEK- /// model these live on the password VaultKey row; for legacy users they live on the root vault manifest.+ /// model these live on the password VaultKey row; for legacy users they live on the root vault manifest,+ /// which is resolved through the user's personal group — the group is the only ownership path to a manifest./// </summary>+ /// <param name="context">Database context.</param>/// <param name="user">User object.</param>/// <returns>Tuple with salt, verifier, encryption type and encryption settings.</returns>- public static (string Salt, string Verifier, string EncryptionType, string EncryptionSettings) GetUserLatestVaultEncryptionSettings(AliasVaultUser user)+ public static async Task<(string Salt, string Verifier, string EncryptionType, string EncryptionSettings)> GetUserLatestVaultEncryptionSettingsAsync(AliasServerDbContext context, AliasVaultUser user){// KEK/VEK model: the password VaultKey row is the authority for SRP credentials once it exists.- var passwordKey = user.VaultKeys.FirstOrDefault(x => x.KeyType == VaultKeyTypePassword);+ var passwordKey = await context.VaultKeys.FirstOrDefaultAsync(x => x.UserId == user.Id && x.Type == VaultKeyType.Password);if (passwordKey is not null){- return (passwordKey.Salt!, passwordKey.Verifier!, passwordKey.EncryptionType!, passwordKey.EncryptionSettings!);+ return VaultKeyMetadata.Parse(passwordKey.Metadata).RequireSrpCredentials();}- // Legacy model: the root manifest carries the current encryption settings.- var latestVault = user.VaultManifests.Where(x => x.IsRoot).Select(x => new { x.Salt, x.Verifier, x.EncryptionType, x.EncryptionSettings }).First();+ // Legacy model: the root manifest carries the current encryption settings. TODO: delete once all users have migrated to the KEK/VEK model.+ var latestVault = await context.VaultManifests+ .Where(m => m.IsRoot && m.OwnerGroupId == user.PersonalGroupId)+ .Select(x => new { x.Salt, x.Verifier, x.EncryptionType, x.EncryptionSettings })+ .FirstAsync();+return (latestVault.Salt, latestVault.Verifier, latestVault.EncryptionType, latestVault.EncryptionSettings);}