Skip to content

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

7.1 KB
Raw Blame
//-----------------------------------------------------------------------
// <copyright file="GrantHelper.cs" company="aliasvault">
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------

namespace AliasVault.Api.Helpers;

using AliasServerDb;
using AliasVault.Api.Models;
using AliasVault.Shared.Models.Enums;
using Microsoft.EntityFrameworkCore;

/// <summary>
/// Grant helper for shared manifests.
/// </summary>
public static class GrantHelper
{
    /// <summary>
    /// Build one recipient's grant on a manifest.
    /// </summary>
    /// <param name="manifestId">The manifest ID.</param>
    /// <param name="userId">The recipient user ID.</param>
    /// <param name="publicKeyId">The recipient public key ID.</param>
    /// <param name="encryptedVek">The encrypted VEK.</param>
    /// <param name="algorithm">The algorithm it was encrypted with.</param>
    /// <param name="keyVersion">The version of the manifest's VEK that <paramref name="encryptedVek"/> yields.</param>
    /// <param name="signature">The granter's signature over the grant.</param>
    /// <param name="signerUserId">The granter.</param>
    /// <param name="signerPublicKey">The granter's signing public key.</param>
    /// <param name="now">Current time.</param>
    /// <returns>The unpersisted grant.</returns>
    public static VaultManifestAccessKey BuildGrant(Guid manifestId, string userId, Guid publicKeyId, string encryptedVek, VaultKeyAlgorithm algorithm, int keyVersion, string signature, string signerUserId, string signerPublicKey, DateTime now)
    {
        return new VaultManifestAccessKey
        {
            Id = Guid.NewGuid(),
            UserId = userId,
            VaultManifestId = manifestId,
            Type = ManifestKeyType.GrantKey,
            Algorithm = algorithm,
            EncryptedVek = encryptedVek,
            KeyVersion = keyVersion,
            UserGrantKeyId = publicKeyId,
            GrantSignature = signature,
            GrantSignerUserId = signerUserId,
            GrantSignerPublicKey = signerPublicKey,
            CreatedAt = now,
            UpdatedAt = now,
        };
    }

    /// <summary>
    /// Get the users holding a grant on each of the given manifests.
    /// </summary>
    /// <param name="context">The database context.</param>
    /// <param name="manifestIds">The manifests to look at.</param>
    /// <returns>Manifest id to the user ids holding a grant on it.</returns>
    public static async Task<Dictionary<Guid, HashSet<string>>> GetGrantHoldersByManifestAsync(AliasServerDbContext context, List<Guid> manifestIds)
    {
        return (await context.VaultManifestAccessKeys
                .Where(k => manifestIds.Contains(k.VaultManifestId) && k.Type == ManifestKeyType.GrantKey)
                .Select(k => new { k.VaultManifestId, k.UserId })
                .ToListAsync())
            .GroupBy(k => k.VaultManifestId)
            .ToDictionary(g => g.Key, g => g.Select(k => k.UserId).ToHashSet(StringComparer.Ordinal));
    }

    /// <summary>
    /// Get the primary public key of each given user.
    /// </summary>
    /// <param name="context">The database context.</param>
    /// <param name="userIds">The user IDs to look up.</param>
    /// <returns>User ID to the public key a vault key is encrypted for them with. Users without a keypair are absent.</returns>
    public static async Task<Dictionary<string, MemberPublicKey>> GetPrimaryKeysAsync(AliasServerDbContext context, IEnumerable<string> userIds)
    {
        var ids = userIds.Distinct(StringComparer.Ordinal).ToList();
        if (ids.Count == 0)
        {
            return [];
        }

        var signingKeys = await context.UserSigningKeys
            .Where(k => ids.Contains(k.UserId) && k.IsPrimary)
            .ToDictionaryAsync(k => k.UserId, k => k.PublicKey);

        return await context.UserGrantKeys
            .Where(k => ids.Contains(k.UserId) && k.IsPrimary)
            .ToDictionaryAsync(k => k.UserId, k => new MemberPublicKey(k.Id, k.PublicKey, k.PublicKeySignature, signingKeys.GetValueOrDefault(k.UserId)));
    }

    /// <summary>
    /// Get a user's primary signing public key.
    /// </summary>
    /// <param name="context">The database context.</param>
    /// <param name="userId">The user.</param>
    /// <returns>The public key, or null for an account not yet on the account key model (no key hierarchy at all).</returns>
    public static async Task<string?> GetPrimarySigningKeyAsync(AliasServerDbContext context, string userId)
    {
        return await context.UserSigningKeys.Where(k => k.UserId == userId && k.IsPrimary).Select(k => k.PublicKey).FirstOrDefaultAsync();
    }

    /// <summary>
    /// Take a transaction-scoped lock on the key certifications a user made, so a certification and a key rotation of that
    /// user never interleave (a certification signed with the old key after the rotation re-signed the others).
    /// </summary>
    /// <param name="context">Database context, inside an open transaction.</param>
    /// <param name="userId">The certifying user.</param>
    /// <returns>Task.</returns>
    public static async Task LockUserCertificationsAsync(AliasServerDbContext context, string userId)
    {
        await context.Database.ExecuteSqlInterpolatedAsync($"SELECT pg_advisory_xact_lock(hashtextextended({"user-certifications:" + userId}, 0))");
    }

    /// <summary>
    /// Take a user's access to a shared manifest away.
    /// </summary>
    /// <param name="context">Database context.</param>
    /// <param name="manifestId">The shared manifest.</param>
    /// <param name="userId">The user losing access.</param>
    /// <returns>True when the user actually held a grant, i.e. when something was revoked.</returns>
    public static async Task<bool> RevokeAccessAsync(AliasServerDbContext context, Guid manifestId, string userId)
    {
        var grants = await context.VaultManifestAccessKeys
            .Where(k => k.VaultManifestId == manifestId && k.UserId == userId && k.Type == ManifestKeyType.GrantKey)
            .ToListAsync();

        if (grants.Count == 0)
        {
            return false;
        }

        context.VaultManifestAccessKeys.RemoveRange(grants);
        return true;
    }

    /// <summary>
    /// Whether taking this user's access away would leave nobody able to open the manifest.
    /// </summary>
    /// <param name="context">Database context.</param>
    /// <param name="manifestId">The shared manifest.</param>
    /// <param name="userId">The user about to lose access.</param>
    /// <returns>True when the user holds the only grant on it.</returns>
    public static async Task<bool> IsLastGrantHolderAsync(AliasServerDbContext context, Guid manifestId, string userId)
    {
        var holders = await context.VaultManifestAccessKeys
            .Where(k => k.VaultManifestId == manifestId && k.Type == ManifestKeyType.GrantKey)
            .Select(k => k.UserId)
            .Distinct()
            .ToListAsync();

        return holders.Count == 1 && holders.Contains(userId, StringComparer.Ordinal);
    }
}