Skip to content

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

4.3 KB
Raw Blame
//-----------------------------------------------------------------------
// <copyright file="VaultManifestAccessKey.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 AliasServerDb;

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using AliasVault.Shared.Models.Enums;

/// <summary>
/// A per-(user, manifest) VEK access path.
/// </summary>
public class VaultManifestAccessKey
{
    /// <summary>
    /// Gets or sets the primary key.
    /// </summary>
    [Key]
    public Guid Id { get; set; }

    /// <summary>
    /// Gets or sets the foreign key to the user this key belongs to.
    /// </summary>
    [StringLength(255)]
    public required string UserId { get; set; }

    /// <summary>
    /// Gets or sets the user object.
    /// </summary>
    [ForeignKey("UserId")]
    public virtual AliasVaultUser User { get; set; } = null!;

    /// <summary>
    /// Gets or sets the manifest this key unlocks.
    /// </summary>
    public required Guid VaultManifestId { get; set; }

    /// <summary>
    /// Gets or sets what encrypts this row's VEK.
    /// </summary>
    [StringLength(30)]
    public required ManifestKeyType Type { get; set; }

    /// <summary>
    /// Gets or sets the algorithm <see cref="EncryptedVek"/> is encrypted with.
    /// </summary>
    [StringLength(30)]
    public required VaultKeyAlgorithm Algorithm { get; set; }

    /// <summary>
    /// Gets or sets the manifest's VEK, encrypted per <see cref="Type"/>.
    /// </summary>
    public required string EncryptedVek { get; set; }

    /// <summary>
    /// Gets or sets the version of the manifest's VEK that <see cref="EncryptedVek"/> yields.
    /// </summary>
    public int KeyVersion { get; set; }

    /// <summary>
    /// Gets or sets the version of the user's Account Key that encrypted <see cref="EncryptedVek"/> (see
    /// <see cref="UserUnlockMethod.AccountKeyVersion"/>), or null when this row is a grant. Grants are encrypted to a
    /// public key rather than to the AK, and name their wrapping key through <see cref="UserGrantKeyId"/> instead.
    /// </summary>
    public int? AccountKeyVersion { get; set; }

    /// <summary>
    /// Gets or sets the id of the account keypair (<see cref="UserGrantKey"/>) the VEK was encrypted to.
    /// </summary>
    public Guid? UserGrantKeyId { get; set; }

    /// <summary>
    /// Gets or sets the navigation property to the account keypair the VEK was encrypted to (grant rows only).
    /// </summary>
    public virtual UserGrantKey? UserGrantKey { get; set; }

    /// <summary>
    /// Gets or sets the signature over this grant by the user who handed it out. Grant rows only: an account-key row is
    /// encrypted with the user's own Account Key, which nobody else can do.
    /// </summary>
    [StringLength(255)]
    public string? GrantSignature { get; set; }

    /// <summary>
    /// Gets or sets the id of the user who signed this grant (grant rows only). Not a foreign key: the grant stays
    /// verifiable after its signer deletes their account.
    /// </summary>
    [StringLength(255)]
    public string? GrantSignerUserId { get; set; }

    /// <summary>
    /// Gets or sets the signing public key <see cref="GrantSignature"/> verifies under (grant rows only), kept on the row
    /// for the same reason as <see cref="GrantSignerUserId"/>.
    /// </summary>
    [StringLength(100)]
    public string? GrantSignerPublicKey { get; set; }

    /// <summary>
    /// Gets or sets optional per-key-type fields as JSON. Unused so far; reserved for future key types.
    /// </summary>
    public string? Metadata { get; set; }

    /// <summary>
    /// Gets or sets the timestamp at which this key was created.
    /// </summary>
    public DateTime CreatedAt { get; set; }

    /// <summary>
    /// Gets or sets the timestamp at which this key was last updated.
    /// </summary>
    public DateTime UpdatedAt { get; set; }

    /// <summary>
    /// Gets or sets the timestamp at which this key was last used to unlock, or null if never recorded.
    /// </summary>
    public DateTime? LastUsedAt { get; set; }
}