Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update API for updated group enrollment workflow (#2536)
Leendert de Borstea97b884e1c0
diff --git a/apps/server/AliasVault.Api/Controllers/Tests/CreateSharedGroupRequest.cs b/apps/server/AliasVault.Api/Controllers/Tests/CreateSharedGroupRequest.csdeleted file mode 100644index be603b679..000000000--- a/apps/server/AliasVault.Api/Controllers/Tests/CreateSharedGroupRequest.cs+++ /dev/null@@ -1,32 +0,0 @@-//------------------------------------------------------------------------// <copyright file="CreateSharedGroupRequest.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.Controllers.Tests;--#if DEBUG--/// <summary>-/// Request to create a shared group from the E2E TestController.-/// </summary>-public class CreateSharedGroupRequest-{- /// <summary>- /// Gets the group name.- /// </summary>- public required string Name { get; init; }-- /// <summary>- /// Gets the username of the group's owner.- /// </summary>- public required string OwnerUsername { get; init; }-- /// <summary>- /// Gets the usernames added to the group as plain members.- /// </summary>- public List<string> MemberUsernames { get; init; } = [];-}-#endifdiff --git a/apps/server/AliasVault.Api/Controllers/V2/GroupsController.cs b/apps/server/AliasVault.Api/Controllers/V2/GroupsController.csindex 57c788c7f..4df8045c3 100644--- a/apps/server/AliasVault.Api/Controllers/V2/GroupsController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/GroupsController.cs@@ -29,7 +29,8 @@ using Microsoft.EntityFrameworkCore.Storage;using Microsoft.Extensions.Caching.Memory;/// <summary>-/// Groups controller which manages the shared manifests of a group and who inside the group can open them.+/// Groups controller which manages shared manifests and who can open them.+/// its members are the people it is shared with./// </summary>/// <param name="dbContextFactory">The database context factory.</param>/// <param name="userManager">The user manager.</param>@@ -40,16 +41,6 @@ using Microsoft.Extensions.Caching.Memory;[ApiVersion("2")]public class GroupsController(IAliasServerDbContextFactory dbContextFactory, UserManager<AliasVaultUser> userManager, TimeProvider timeProvider, IMemoryCache cache, AuthLoggingService authLoggingService, IdentityCodeLookupRateLimitService identityCodeLookupRateLimit) : AuthenticatedRequestController(userManager){- /// <summary>- /// How many shared manifests one group may hold. TODO: hardcoded for now; make this dynamic when needed.- /// </summary>- private const int MaxSharedVaults = 3;-- /// <summary>- /// How long an offer of access stays open before it expires unanswered.- /// </summary>- private static readonly TimeSpan InvitationLifetime = TimeSpan.FromDays(7);-/// <summary>/// Get the overview of the caller's shared groups and the access offers awaiting their answer./// </summary>@@ -73,7 +64,7 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use{UserId = me.Id,ReceivedInvitations = await GetReceivedInvitationsAsync(context, me.Id, InvitationCutoff()),- ReceivedGroupInvitations = await GetReceivedGroupInvitationsAsync(context, me.Id, InvitationCutoff()),+ VerifiedPeople = await GetVerifiedPeopleAsync(context, me.Id),};if (memberships.Count == 0)@@ -110,7 +101,6 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use// Open offers are only shown to the admins who may withdraw them.var openInvitations = administeredGroupIds.Count > 0 ? await GetOpenInvitationsByManifestAsync(context, [.. administeredGroupIds], InvitationCutoff()) : [];- var openJoinInvitations = administeredGroupIds.Count > 0 ? await GetOpenJoinInvitationsByGroupAsync(context, [.. administeredGroupIds], InvitationCutoff()) : [];foreach (var membership in memberships){@@ -143,215 +133,12 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, UseSigningPublicKey = canAdminister ? publicKeys.GetValueOrDefault(m.UserId)?.SigningPublicKey : null,KeyCertification = canAdminister ? certifications.GetValueOrDefault(m.UserId) : null,})],- PendingJoinInvitations = canAdminister ? openJoinInvitations.GetValueOrDefault(membership.GroupId) ?? [] : [],});}return Ok(response);}- /// <summary>- /// Create a shared group (a family) owned by the caller.- /// </summary>- /// <returns>The created group id.</returns>- [HttpPost]- [RequireCapability(CapabilityKeys.VaultSharing)]- public async Task<IActionResult> CreateGroup()- {- await using var context = await dbContextFactory.CreateDbContextAsync();- var me = await GetCurrentUserAsync();- if (me == null)- {- return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);- }-- var now = timeProvider.GetUtcNow().UtcDateTime;- var group = new Group { Id = Guid.NewGuid(), Name = "Family", Type = GroupType.Shared, CreatedAt = now, UpdatedAt = now };-- // Check and insert under the user's lock, so two concurrent requests cannot both create a group.- var strategy = context.Database.CreateExecutionStrategy();- return await strategy.ExecuteAsync<IActionResult>(async () =>- {- await using var transaction = await context.Database.BeginTransactionAsync();- await context.Database.ExecuteSqlInterpolatedAsync($"SELECT pg_advisory_xact_lock(hashtextextended({"user-owned-groups:" + me.Id}, 0))");-- if (await context.GroupMembers.AnyAsync(m => m.UserId == me.Id && m.Role == GroupRole.Owner && m.Group.Type == GroupType.Shared))- {- return ApiError.Result(ApiErrorCode.GROUP_LIMIT_REACHED, 400);- }-- context.Groups.Add(group);- context.GroupMembers.Add(GroupHelper.CreateOwnerMembership(group, me.Id, now));- await context.SaveChangesAsync();- await transaction.CommitAsync();- return Ok(new CreateGroupResponse { GroupId = group.Id });- });- }-- /// <summary>- /// Start leaving a shared group: serves the SRP handshake the confirm endpoint checks the master password with.- /// </summary>- /// <param name="groupId">The group to leave.</param>- /// <returns>The SRP handshake values for the confirm endpoint.</returns>- [HttpPost("{groupId:guid}/leave/initiate")]- public async Task<IActionResult> InitiateLeaveGroup(Guid groupId)- {- await using var context = await dbContextFactory.CreateDbContextAsync();- var me = await GetCurrentUserAsync();- if (me == null)- {- return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);- }-- var membership = await context.GroupMembers.FirstOrDefaultAsync(m => m.GroupId == groupId && m.UserId == me.Id && m.Group.Type == GroupType.Shared);- if (membership is null)- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-- if (membership.Role == GroupRole.Owner)- {- return ApiError.Result(ApiErrorCode.GROUP_OWNER_CANNOT_LEAVE, 400);- }-- return Ok(await CreateStepUpChallengeAsync(context, me, SrpPurpose.GroupLeave));- }-- /// <summary>- /// Leave a shared group (confirmed with the master password), giving up access to every one of its shared manifests. The- /// owner cannot leave; they delete the group instead.- /// </summary>- /// <param name="groupId">The group to leave.</param>- /// <param name="model">The SRP proof of the caller's master password.</param>- /// <returns>Ok on success.</returns>- [HttpPost("{groupId:guid}/leave/confirm")]- public async Task<IActionResult> ConfirmLeaveGroup(Guid groupId, [FromBody] PasswordConfirmationRequest model)- {- await using var context = await dbContextFactory.CreateDbContextAsync();- var me = await GetCurrentUserAsync();- if (me == null)- {- return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);- }-- var stepUpFailure = await ValidateStepUpAsync(context, me, SrpPurpose.GroupLeave, AuthEventType.FamilyLeave, model);- if (stepUpFailure is not null)- {- return stepUpFailure;- }-- var strategy = context.Database.CreateExecutionStrategy();- return await strategy.ExecuteAsync<IActionResult>(async () =>- {- await using var transaction = await context.Database.BeginTransactionAsync();- await LockGroupMembersAsync(context, groupId);- await LockGroupManifestsAsync(context, groupId);-- var membership = await context.GroupMembers.FirstOrDefaultAsync(m => m.GroupId == groupId && m.UserId == me.Id && m.Group.Type == GroupType.Shared);- if (membership is null)- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-- if (membership.Role == GroupRole.Owner)- {- return ApiError.Result(ApiErrorCode.GROUP_OWNER_CANNOT_LEAVE, 400);- }-- return await RemoveMembershipAndCommitAsync(context, transaction, membership);- });- }-- /// <summary>- /// Start removing a member from a shared group: serves the SRP handshake the confirm endpoint checks the master password with.- /// </summary>- /// <param name="groupId">The group.</param>- /// <param name="userId">The member to remove.</param>- /// <returns>The SRP handshake values for the confirm endpoint.</returns>- [HttpPost("{groupId:guid}/members/{userId}/remove/initiate")]- public async Task<IActionResult> InitiateRemoveMember(Guid groupId, string userId)- {- await using var context = await dbContextFactory.CreateDbContextAsync();- var me = await GetCurrentUserAsync();- if (me == null)- {- return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);- }-- if (string.Equals(userId, me.Id, StringComparison.Ordinal))- {- return ApiError.Result(ApiErrorCode.INVALID_REQUEST, 400);- }-- if (!await GroupHelper.IsSharedGroupAdminAsync(context, groupId, me.Id))- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-- return Ok(await CreateStepUpChallengeAsync(context, me, SrpPurpose.GroupMemberRemoval));- }-- /// <summary>- /// Take a member out of a shared group (confirmed with the master password), and with it their access to every one of- /// its shared manifests. The owner cannot be removed.- /// </summary>- /// <param name="groupId">The group.</param>- /// <param name="userId">The member to remove.</param>- /// <param name="model">The SRP proof of the caller's master password.</param>- /// <returns>Ok on success.</returns>- [HttpPost("{groupId:guid}/members/{userId}/remove/confirm")]- public async Task<IActionResult> ConfirmRemoveMember(Guid groupId, string userId, [FromBody] PasswordConfirmationRequest model)- {- await using var context = await dbContextFactory.CreateDbContextAsync();- var me = await GetCurrentUserAsync();- if (me == null)- {- return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);- }-- if (string.Equals(userId, me.Id, StringComparison.Ordinal))- {- return ApiError.Result(ApiErrorCode.INVALID_REQUEST, 400);- }-- if (!await GroupHelper.IsSharedGroupAdminAsync(context, groupId, me.Id))- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-- var stepUpFailure = await ValidateStepUpAsync(context, me, SrpPurpose.GroupMemberRemoval, AuthEventType.FamilyMemberRemoval, model);- if (stepUpFailure is not null)- {- return stepUpFailure;- }-- var strategy = context.Database.CreateExecutionStrategy();- return await strategy.ExecuteAsync<IActionResult>(async () =>- {- await using var transaction = await context.Database.BeginTransactionAsync();- await LockGroupMembersAsync(context, groupId);- await LockGroupManifestsAsync(context, groupId);-- if (!await GroupHelper.IsSharedGroupAdminAsync(context, groupId, me.Id))- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-- var membership = await context.GroupMembers.FirstOrDefaultAsync(m => m.GroupId == groupId && m.UserId == userId);- if (membership is null)- {- return ApiError.Result(ApiErrorCode.NOT_GROUP_MEMBER, 400);- }-- if (membership.Role == GroupRole.Owner)- {- return ApiError.Result(ApiErrorCode.INVALID_REQUEST, 400);- }-- return await RemoveMembershipAndCommitAsync(context, transaction, membership);- });- }-/// <summary>/// The key certifications the caller made, for a key rotation to check under the old key and sign again with the new one./// </summary>@@ -373,58 +160,16 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use.ToListAsync());}- /// <summary>- /// Delete a shared group the caller owns. Its shared manifests have to be deleted and its other members gone first.- /// </summary>- /// <param name="groupId">The group to delete.</param>- /// <returns>Ok on success.</returns>- [HttpDelete("{groupId:guid}")]- public async Task<IActionResult> DeleteGroup(Guid groupId)- {- await using var context = await dbContextFactory.CreateDbContextAsync();- var me = await GetCurrentUserAsync();- if (me == null)- {- return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);- }-- // Checked and deleted under the locks a join or a manifest creation takes, so neither can slip in between.- var strategy = context.Database.CreateExecutionStrategy();- return await strategy.ExecuteAsync<IActionResult>(async () =>- {- await using var transaction = await context.Database.BeginTransactionAsync();- await LockGroupMembersAsync(context, groupId);- await LockGroupManifestsAsync(context, groupId);-- var group = await context.Groups.FirstOrDefaultAsync(g => g.Id == groupId && g.Type == GroupType.Shared && g.Members.Any(m => m.UserId == me.Id && m.Role == GroupRole.Owner));- if (group is null)- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-- if (await context.GroupMembers.AnyAsync(m => m.GroupId == groupId && m.UserId != me.Id) || await context.VaultManifests.AnyAsync(m => m.OwnerGroupId == groupId))- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_EMPTY, 400);- }-- // Memberships, invitations, queued client actions and quota rows cascade from the group row.- context.Groups.Remove(group);- await context.SaveChangesAsync();- await transaction.CommitAsync();- return Ok();- });- }-/// <summary>/// Find the account an identity code (the fingerprint of its signing key) belongs to. The caller's client checks the returned- /// key against the code before it certifies the key or invites the account.+ /// key against the code before it certifies the key or invites the account. Not tied to a group, so a folder can be shared+ /// only once the person it is shared with has been found by their code./// </summary>- /// <param name="groupId">The group the caller wants to invite to.</param>/// <param name="model">The identity code.</param>/// <returns>The account and its signing public key.</returns>- [HttpPost("{groupId:guid}/members/lookup")]+ [HttpPost("lookup")][RequireCapability(CapabilityKeys.VaultSharing)]- public async Task<IActionResult> LookupIdentityCode(Guid groupId, [FromBody] IdentityCodeLookupRequest model)+ public async Task<IActionResult> LookupIdentityCode([FromBody] IdentityCodeLookupRequest model){await using var context = await dbContextFactory.CreateDbContextAsync();var me = await GetCurrentUserAsync();@@ -433,11 +178,6 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);}- if (!await GroupHelper.IsSharedGroupAdminAsync(context, groupId, me.Id))- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-if (!identityCodeLookupRateLimit.TryConsume(me.Id)){return ApiError.Result(ApiErrorCode.RATE_LIMIT_EXCEEDED, 429);@@ -456,7 +196,17 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.IDENTITY_CODE_NOT_FOUND, 404);}- return Ok(new IdentityCodeLookupResponse { UserId = matches[0].UserId, Username = matches[0].Username, SigningPublicKey = matches[0].PublicKey });+ // The account public key lets the admin's client encrypt a shared manifest's key for an account that is not a member yet.+ var accountKey = (await GrantHelper.GetPrimaryKeysAsync(context, [matches[0].UserId])).GetValueOrDefault(matches[0].UserId);+ return Ok(new IdentityCodeLookupResponse+ {+ UserId = matches[0].UserId,+ Username = matches[0].Username,+ SigningPublicKey = matches[0].PublicKey,+ AccountPublicKeyId = accountKey?.PublicKeyId,+ AccountPublicKey = accountKey?.PublicKey,+ AccountPublicKeySignature = accountKey?.PublicKeySignature,+ });}/// <summary>@@ -517,78 +267,13 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use}/// <summary>- /// Invite an account, found by its identity code, to join the group.- /// </summary>- /// <param name="groupId">The group.</param>- /// <param name="model">The account to invite.</param>- /// <returns>Ok on success.</returns>- [HttpPost("{groupId:guid}/invitations")]- [RequireCapability(CapabilityKeys.VaultSharing)]- public async Task<IActionResult> InviteToGroup(Guid groupId, [FromBody] InviteToGroupRequest model)- {- await using var context = await dbContextFactory.CreateDbContextAsync();- var me = await GetCurrentUserAsync();- if (me == null)- {- return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);- }-- if (!await GroupHelper.IsSharedGroupAdminAsync(context, groupId, me.Id))- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-- if (await GroupHelper.IsSharedGroupMemberAsync(context, groupId, model.UserId))- {- return ApiError.Result(ApiErrorCode.ALREADY_GROUP_MEMBER, 400);- }-- var subjectKey = await GrantHelper.GetPrimarySigningKeyAsync(context, model.UserId);- if (subjectKey is null || !await context.UserKeyCertifications.AnyAsync(c => c.CertifierUserId == me.Id && c.SubjectUserId == model.UserId && c.SubjectSigningPublicKey == subjectKey))- {- return ApiError.Result(ApiErrorCode.MEMBER_NOT_VERIFIED, 400);- }-- var now = timeProvider.GetUtcNow().UtcDateTime;- var cutoff = InvitationCutoff();- foreach (var expired in await context.GroupInvitations.Where(i => i.GroupId == groupId && i.InviteeUserId == model.UserId && i.VaultManifestId == null && i.State == GroupInvitationState.Pending && i.CreatedAt < cutoff).ToListAsync())- {- CloseInvitation(expired, GroupInvitationState.Expired);- }-- context.GroupInvitations.Add(new GroupInvitation- {- Id = Guid.NewGuid(),- GroupId = groupId,- InviterUserId = me.Id,- InviteeUserId = model.UserId,- Role = GroupRole.Member,- State = GroupInvitationState.Pending,- CreatedAt = now,- UpdatedAt = now,- });-- try- {- await context.SaveChangesAsync();- }- catch (DbUpdateException)- {- return ApiError.Result(ApiErrorCode.INVITATION_ALREADY_EXISTS, 400);- }-- return Ok();- }-- /// <summary>- /// Create another shared manifest for a group, together with the caller's own grant on it.+ /// Create a shared manifest with a group of its own, owned by the caller, together with the caller's own grant on it./// </summary>- /// <param name="groupId">The shared group ID.</param>/// <param name="model">The create manifest request.</param>- /// <returns>The created manifest id and its revision.</returns>- [HttpPost("{groupId:guid}/manifests")]+ /// <returns>The created manifest id, its group and its revision.</returns>+ [HttpPost("manifests")][RequireCapability(CapabilityKeys.VaultSharing)]- public async Task<IActionResult> CreateManifest(Guid groupId, [FromBody] CreateSharedManifestRequest model)+ public async Task<IActionResult> CreateManifest([FromBody] CreateSharedManifestRequest model){await using var context = await dbContextFactory.CreateDbContextAsync();var me = await GetCurrentUserAsync();@@ -608,11 +293,6 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.MANIFEST_ID_INVALID, 400);}- if (!await GroupHelper.IsSharedGroupAdminAsync(context, groupId, me.Id))- {- return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);- }-// The public key the user's own grant is encrypted for must be one of theirs.var selfPublicKeyId = await context.UserGrantKeys.Where(x => x.UserId == me.Id && x.PublicKey == model.SelfAccountPublicKey)@@ -631,10 +311,12 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.SIGNATURE_INVALID, 400);}+ var now = timeProvider.GetUtcNow().UtcDateTime;+ var group = new Group { Id = Guid.NewGuid(), Name = "Shared", Type = GroupType.Shared, CreatedAt = now, UpdatedAt = now };var manifest = new VaultManifest{ManifestId = model.ManifestId,- OwnerGroupId = groupId,+ OwnerGroupId = group.Id,StorageFormat = VaultManifestBase.ManifestStorageFormat,RevisionNumber = 0,KeyVersion = 0,@@ -647,19 +329,14 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usevar shareDetails = new VaultManifestShareDetails { ManifestId = manifest.ManifestId, EncryptedName = model.EncryptedName, CreatedAt = timeProvider.GetUtcNow().UtcDateTime, UpdatedAt = timeProvider.GetUtcNow().UtcDateTime };var selfGrant = GrantHelper.BuildGrant(manifest.ManifestId, me.Id, selfPublicKeyId.Value, model.SelfEncryptedVek, algorithm, manifest.KeyVersion, model.SelfGrantSignature, me.Id, signerPublicKey!, timeProvider.GetUtcNow().UtcDateTime);- // Count and insert under the group lock, so concurrent creations cannot both pass the cap.var strategy = context.Database.CreateExecutionStrategy();var result = await strategy.ExecuteAsync<IActionResult?>(async () =>{await using var transaction = await context.Database.BeginTransactionAsync();- await LockGroupManifestsAsync(context, groupId);- if (await context.VaultManifests.CountAsync(x => x.OwnerGroupId == groupId) >= MaxSharedVaults)- {- return ApiError.Result(ApiErrorCode.GROUP_MANIFEST_LIMIT_REACHED, 400);- }-- // Create the empty manifest.+ // Create the group and the empty manifest.+ context.Groups.Add(group);+ context.GroupMembers.Add(GroupHelper.CreateOwnerMembership(group, me.Id, now));context.VaultManifests.Add(manifest);context.VaultManifestShareDetails.Add(shareDetails);context.VaultManifestAccessKeys.Add(selfGrant);@@ -685,7 +362,7 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Useawait authLoggingService.LogAuthEventSuccessAsync(me.UserName!, AuthEventType.SharedVaultCreation);- return Ok(new CreateSharedManifestResponse { ManifestId = manifest.ManifestId, RevisionNumber = manifest.RevisionNumber });+ return Ok(new CreateSharedManifestResponse { GroupId = group.Id, ManifestId = manifest.ManifestId, RevisionNumber = manifest.RevisionNumber });}/// <summary>@@ -737,8 +414,11 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.SHARED_MANIFEST_NOT_FOUND, 404);}- // Access only ever goes to somebody already on the group's roster, which is administered outside the client.- if (!await GroupHelper.IsSharedGroupMemberAsync(context, groupId, model.UserId))+ /*+ * Access goes to a member of the group, or to an account the caller certified with its identity code: accepting+ * such an offer also makes them a member. Without the certification nobody can be invited by a bare user id.+ */+ if (!await GroupHelper.IsSharedGroupMemberAsync(context, groupId, model.UserId) && !await HasCertifiedAsync(context, me.Id, model.UserId, await GrantHelper.GetPrimarySigningKeyAsync(context, model.UserId))){return ApiError.Result(ApiErrorCode.NOT_GROUP_MEMBER, 400);}@@ -768,46 +448,55 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.ACCESS_ALREADY_GRANTED, 400);}- await CloseStaleInvitationsAsync(context, manifestId, manifestKeyVersion.Value);- await CloseExpiredInvitationsAsync(context, manifestId);-- if (await context.GroupInvitations.AnyAsync(i => i.VaultManifestId == manifestId && i.InviteeUserId == model.UserId && i.State == GroupInvitationState.Pending))+ // The offer is inserted under the group's member lock, which accepting and unsharing take as well.+ var strategy = context.Database.CreateExecutionStrategy();+ return await strategy.ExecuteAsync<IActionResult>(async () =>{- return ApiError.Result(ApiErrorCode.INVITATION_ALREADY_EXISTS, 400);- }+ await using var transaction = await context.Database.BeginTransactionAsync();+ await LockGroupMembersAsync(context, groupId);- var invitation = new GroupInvitation- {- Id = Guid.NewGuid(),- GroupId = groupId,- InviterUserId = me.Id,- InviteeUserId = model.UserId,- Role = GroupRole.Member,- State = GroupInvitationState.Pending,- VaultManifestId = manifestId,- EncryptedVek = model.Grant.EncryptedVek,- EncryptedName = model.Grant.EncryptedName,- EncryptedNameSignature = model.Grant.EncryptedName is null ? null : model.Grant.EncryptedNameSignature,- UserGrantKeyId = model.Grant.RecipientAccountPublicKeyId,- GrantSignature = model.Grant.Signature,- GrantSignerPublicKey = signerPublicKey,- VaultKeyVersion = manifestKeyVersion.Value,- Algorithm = algorithm,- CreatedAt = timeProvider.GetUtcNow().UtcDateTime,- UpdatedAt = timeProvider.GetUtcNow().UtcDateTime,- };- context.GroupInvitations.Add(invitation);+ await CloseStaleInvitationsAsync(context, manifestId, manifestKeyVersion.Value);+ await CloseExpiredInvitationsAsync(context, manifestId);- try- {- await context.SaveChangesAsync();- }- catch (DbUpdateException)- {- return ApiError.Result(ApiErrorCode.INVITATION_ALREADY_EXISTS, 400);- }+ if (await context.GroupInvitations.AnyAsync(i => i.VaultManifestId == manifestId && i.InviteeUserId == model.UserId && i.State == GroupInvitationState.Pending))+ {+ return ApiError.Result(ApiErrorCode.INVITATION_ALREADY_EXISTS, 400);+ }++ var invitation = new GroupInvitation+ {+ Id = Guid.NewGuid(),+ GroupId = groupId,+ InviterUserId = me.Id,+ InviteeUserId = model.UserId,+ Role = GroupRole.Member,+ State = GroupInvitationState.Pending,+ VaultManifestId = manifestId,+ EncryptedVek = model.Grant.EncryptedVek,+ EncryptedName = model.Grant.EncryptedName,+ EncryptedNameSignature = model.Grant.EncryptedName is null ? null : model.Grant.EncryptedNameSignature,+ UserGrantKeyId = model.Grant.RecipientAccountPublicKeyId,+ GrantSignature = model.Grant.Signature,+ GrantSignerPublicKey = signerPublicKey,+ VaultKeyVersion = manifestKeyVersion.Value,+ Algorithm = algorithm,+ CreatedAt = timeProvider.GetUtcNow().UtcDateTime,+ UpdatedAt = timeProvider.GetUtcNow().UtcDateTime,+ };+ context.GroupInvitations.Add(invitation);- return Ok(new GrantManifestAccessResponse { InvitationId = invitation.Id });+ try+ {+ await context.SaveChangesAsync();+ }+ catch (DbUpdateException)+ {+ return ApiError.Result(ApiErrorCode.INVITATION_ALREADY_EXISTS, 400);+ }++ await transaction.CommitAsync();+ return Ok(new GrantManifestAccessResponse { InvitationId = invitation.Id });+ });}/// <summary>@@ -1045,36 +734,61 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Useawait strategy.ExecuteAsync(async () =>{await using var transaction = await context.Database.BeginTransactionAsync();+ await LockGroupMembersAsync(context, groupId);- // Close any open invite.- foreach (var invitation in await context.GroupInvitations.Where(i => i.VaultManifestId == manifestId && i.State == GroupInvitationState.Pending).ToListAsync())- {- CloseInvitation(invitation, GroupInvitationState.Revoked);- }+ await DeleteManifestAndGroupAsync(context, groupId, manifest);+ await transaction.CommitAsync();+ });- // Delete any active grant.- var holders = await context.VaultManifestAccessKeys- .Where(k => k.VaultManifestId == manifestId && k.Type == ManifestKeyType.GrantKey)- .Select(k => k.UserId)- .Distinct()- .ToListAsync();- foreach (var holder in holders)- {- await GrantHelper.RevokeAccessAsync(context, manifestId, holder);- }+ return Ok();+ }- await context.SaveChangesAsync();+ /// <summary>+ /// Turn a shared manifest nobody else is in (no other member, no open invitation) back into a personal one: its owner's+ /// client has moved the contents out, and the server deletes the empty shared manifest and its group. Nothing sensitive+ /// is left in it, so this takes no master password; taking people out does.+ /// </summary>+ /// <param name="groupId">The group ID.</param>+ /// <param name="manifestId">The shared manifest.</param>+ /// <returns>Ok on success.</returns>+ [HttpPost("{groupId:guid}/manifests/{manifestId:guid}/unshare")]+ public async Task<IActionResult> UnshareManifest(Guid groupId, Guid manifestId)+ {+ await using var context = await dbContextFactory.CreateDbContextAsync();+ var me = await GetCurrentUserAsync();+ if (me == null)+ {+ return ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);+ }- // Key rows and queued client actions reference the manifest without a cascading foreign key, so leftovers are deleted explicitly.- await context.VaultManifestAccessKeys.Where(k => k.VaultManifestId == manifestId).ExecuteDeleteAsync();- await context.ClientActions.Where(a => a.ManifestId == manifestId).ExecuteDeleteAsync();- context.VaultManifests.Remove(manifest);+ if (await GroupHelper.GetOwnerUserIdAsync(context, groupId) != me.Id)+ {+ return ApiError.Result(ApiErrorCode.GROUP_NOT_FOUND, 404);+ }- await context.SaveChangesAsync();+ var manifest = await context.VaultManifests.FirstOrDefaultAsync(m => m.ManifestId == manifestId && m.OwnerGroupId == groupId);+ if (manifest == null)+ {+ return ApiError.Result(ApiErrorCode.SHARED_MANIFEST_NOT_FOUND, 404);+ }++ // Checked under the member lock that inviting and accepting take too, so nobody can join while it is deleted.+ var strategy = context.Database.CreateExecutionStrategy();+ return await strategy.ExecuteAsync<IActionResult>(async () =>+ {+ await using var transaction = await context.Database.BeginTransactionAsync();+ await LockGroupMembersAsync(context, groupId);++ var cutoff = InvitationCutoff();+ if (await context.GroupMembers.AnyAsync(m => m.GroupId == groupId && m.UserId != me.Id) || await context.GroupInvitations.AnyAsync(i => i.GroupId == groupId && i.State == GroupInvitationState.Pending && i.CreatedAt >= cutoff))+ {+ return ApiError.Result(ApiErrorCode.SHARED_MANIFEST_IN_USE, 400);+ }++ await DeleteManifestAndGroupAsync(context, groupId, manifest);await transaction.CommitAsync();+ return Ok();});-- return Ok();}/// <summary>@@ -1095,10 +809,10 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.NOT_AUTHENTICATED, 401);}- // Read the offer's manifest only to know which lock to take; the offer itself is read again under the lock.+ // Read the offer's group and manifest only to know which locks to take; the offer itself is read again under the locks.var offer = await context.GroupInvitations.Where(i => i.Id == invitationId && i.InviteeUserId == me.Id && i.State == GroupInvitationState.Pending)- .Select(i => new { i.VaultManifestId })+ .Select(i => new { i.GroupId, i.VaultManifestId }).FirstOrDefaultAsync();if (offer is null)@@ -1108,14 +822,19 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Useif (offer.VaultManifestId is not Guid offeredManifestId){- return await AcceptJoinInvitationAsync(context, invitationId, me.Id);+ return ApiError.Result(ApiErrorCode.INVITATION_NOT_FOUND, 404);}- // Under the manifest's grant lock, so a concurrent revoke either closes the offer first or sees the new grant.+ /*+ * Under the group's member lock (accepting can add the invitee to the group, and a removal or deletion either lands+ * first or sees the new member) and the manifest's grant lock (a concurrent revoke either closes the offer first or+ * sees the new grant). The member lock is taken first, as everywhere else.+ */var strategy = context.Database.CreateExecutionStrategy();return await strategy.ExecuteAsync<IActionResult>(async () =>{await using var transaction = await context.Database.BeginTransactionAsync();+ await LockGroupMembersAsync(context, offer.GroupId);await LockManifestGrantsAsync(context, offeredManifestId);var invitation = await context.GroupInvitations.FirstOrDefaultAsync(i => i.Id == invitationId && i.InviteeUserId == me.Id && i.State == GroupInvitationState.Pending);@@ -1124,12 +843,8 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.INVITATION_NOT_FOUND, 404);}- // Accepting no longer joins anything: the roster decided that, and a member who was taken off it in the- // meantime has nothing left to accept.- if (!await GroupHelper.IsSharedGroupMemberAsync(context, invitation.GroupId, me.Id))- {- return ApiError.Result(ApiErrorCode.INVITATION_NOT_FOUND, 404);- }+ // Accepting makes the invitee a member of the shared manifest's group.+ var isMember = await GroupHelper.IsSharedGroupMemberAsync(context, invitation.GroupId, me.Id);var manifestKeyVersion = invitation.VaultManifestId is null ? null : await context.VaultManifests.Where(m => m.ManifestId == invitation.VaultManifestId.Value && m.OwnerGroupId == invitation.GroupId)@@ -1183,6 +898,12 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn ApiError.Result(ApiErrorCode.INVITATION_NOT_FOUND, 404);}+ if (!isMember)+ {+ var joinedAt = timeProvider.GetUtcNow().UtcDateTime;+ context.GroupMembers.Add(new GroupMember { Id = Guid.NewGuid(), GroupId = invitation.GroupId, UserId = me.Id, Role = GroupRole.Member, CreatedAt = joinedAt, UpdatedAt = joinedAt });+ }+invitation.State = GroupInvitationState.Accepted;invitation.RespondedAt = timeProvider.GetUtcNow().UtcDateTime;invitation.UpdatedAt = timeProvider.GetUtcNow().UtcDateTime;@@ -1276,51 +997,6 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use.ToDictionary(g => g.Key, g => g.Select(i => i.Invitation).ToList());}- /// <summary>- /// The open invitations to join each group, for the admins who may withdraw them.- /// </summary>- /// <param name="context">Database context.</param>- /// <param name="groupIds">The groups to list invitations of.</param>- /// <param name="createdAfter">Invitations created before this moment have expired and are left out.</param>- /// <returns>Group id to its open join invitations.</returns>- private static async Task<Dictionary<Guid, List<SentManifestInvitation>>> GetOpenJoinInvitationsByGroupAsync(AliasServerDbContext context, List<Guid> groupIds, DateTime createdAfter)- {- return (await context.GroupInvitations- .Where(i => groupIds.Contains(i.GroupId) && i.State == GroupInvitationState.Pending && i.VaultManifestId == null && i.CreatedAt >= createdAfter)- .Select(i => new { i.GroupId, Invitation = new SentManifestInvitation { Id = i.Id, InviteeUserId = i.InviteeUserId, InviteeUsername = i.Invitee.UserName ?? string.Empty, CreatedAt = i.CreatedAt } })- .ToListAsync())- .GroupBy(i => i.GroupId)- .ToDictionary(g => g.Key, g => g.Select(i => i.Invitation).ToList());- }-- /// <summary>- /// The open invitations to join a group addressed to one user, from inviters who still administer it.- /// </summary>- /// <param name="context">Database context.</param>- /// <param name="userId">The invitee.</param>- /// <param name="createdAfter">Invitations created before this moment have expired and are left out.</param>- /// <returns>Their open join invitations.</returns>- private static async Task<List<ReceivedGroupInvitation>> GetReceivedGroupInvitationsAsync(AliasServerDbContext context, string userId, DateTime createdAfter)- {- var invitations = await context.GroupInvitations- .Where(i => i.InviteeUserId == userId && i.State == GroupInvitationState.Pending && i.VaultManifestId == null && i.Group.Type == GroupType.Shared && i.CreatedAt >= createdAfter)- .Where(i => context.GroupMembers.Any(m => m.GroupId == i.GroupId && m.UserId == i.InviterUserId && (m.Role == GroupRole.Owner || m.Role == GroupRole.Admin)))- .OrderBy(i => i.CreatedAt)- .Select(i => new ReceivedGroupInvitation { Id = i.Id, GroupId = i.GroupId, InviterUserId = i.InviterUserId, InviterUsername = i.Inviter.UserName ?? string.Empty, CreatedAt = i.CreatedAt })- .ToListAsync();-- var inviterIds = invitations.Select(i => i.InviterUserId).Distinct(StringComparer.Ordinal).ToList();- var inviterKeys = await context.UserSigningKeys.Where(k => inviterIds.Contains(k.UserId) && k.IsPrimary).ToDictionaryAsync(k => k.UserId, k => k.PublicKey);- var certifications = await GetCertificationsByAsync(context, userId, inviterIds);- foreach (var invitation in invitations)- {- invitation.InviterSigningPublicKey = inviterKeys.GetValueOrDefault(invitation.InviterUserId);- invitation.InviterCertification = certifications.GetValueOrDefault((invitation.InviterUserId, invitation.InviterSigningPublicKey ?? string.Empty));- }-- return invitations;- }-/// <summary>/// The certifications one user made of the given users' keys, keyed by subject and certified key./// </summary>@@ -1335,6 +1011,35 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use.ToDictionaryAsync(c => (c.SubjectUserId, c.SubjectSigningPublicKey), c => c.Signature);}+ /// <summary>+ /// The people whose current signing key a user certified, with the keys their client encrypts a shared manifest's key with.+ /// </summary>+ /// <param name="context">Database context.</param>+ /// <param name="userId">The certifying user.</param>+ /// <returns>The people, sorted by username.</returns>+ private static async Task<List<GroupMemberInfo>> GetVerifiedPeopleAsync(AliasServerDbContext context, string userId)+ {+ var certifications = await context.UserKeyCertifications+ .Where(c => c.CertifierUserId == userId && context.UserSigningKeys.Any(k => k.UserId == c.SubjectUserId && k.IsPrimary && k.PublicKey == c.SubjectSigningPublicKey))+ .Select(c => new { c.SubjectUserId, c.Signature, Username = context.AliasVaultUsers.Where(u => u.Id == c.SubjectUserId).Select(u => u.UserName).FirstOrDefault() ?? string.Empty })+ .ToListAsync();+ var publicKeys = await GrantHelper.GetPrimaryKeysAsync(context, certifications.Select(c => c.SubjectUserId));++ return [.. certifications+ .OrderBy(c => c.Username, StringComparer.OrdinalIgnoreCase)+ .Select(c => new GroupMemberInfo+ {+ UserId = c.SubjectUserId,+ Username = c.Username,+ Role = KebabCaseEnumConverter.ToToken(GroupRole.Member),+ AccountPublicKeyId = publicKeys.GetValueOrDefault(c.SubjectUserId)?.PublicKeyId,+ AccountPublicKey = publicKeys.GetValueOrDefault(c.SubjectUserId)?.PublicKey,+ AccountPublicKeySignature = publicKeys.GetValueOrDefault(c.SubjectUserId)?.PublicKeySignature,+ SigningPublicKey = publicKeys.GetValueOrDefault(c.SubjectUserId)?.SigningPublicKey,+ KeyCertification = c.Signature,+ })];+ }+/// <summary>/// The open offers of access addressed to one user./// </summary>@@ -1540,20 +1245,46 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use}/// <summary>- /// Delete the key certifications between a user leaving a group and the group's other members, in both directions.+ /// Delete a shared manifest with its grants, keys, open invitations and queued client actions, and its group once it+ /// owns no other manifest. Runs inside the caller's transaction, under the group's member lock./// </summary>- /// <param name="context">The database context.</param>+ /// <param name="context">The database context, inside the open transaction.</param>/// <param name="groupId">The group.</param>- /// <param name="userId">The user leaving it.</param>- /// <returns>A task.</returns>- private static async Task DeleteCertificationsBetweenAsync(AliasServerDbContext context, Guid groupId, string userId)+ /// <param name="manifest">The shared manifest.</param>+ private async Task DeleteManifestAndGroupAsync(AliasServerDbContext context, Guid groupId, VaultManifest manifest){- // A member the user still shares another group with keeps their certifications.- var stillSharedWith = context.GroupMembers.Where(m => m.GroupId != groupId && m.UserId != userId && context.GroupMembers.Any(mine => mine.GroupId == m.GroupId && mine.UserId == userId)).Select(m => m.UserId);- var otherMemberIds = await context.GroupMembers.Where(m => m.GroupId == groupId && m.UserId != userId && !stillSharedWith.Contains(m.UserId)).Select(m => m.UserId).ToListAsync();- await context.UserKeyCertifications- .Where(c => (c.CertifierUserId == userId && otherMemberIds.Contains(c.SubjectUserId)) || (c.SubjectUserId == userId && otherMemberIds.Contains(c.CertifierUserId)))- .ExecuteDeleteAsync();+ var manifestId = manifest.ManifestId;++ // Close any open invite.+ foreach (var invitation in await context.GroupInvitations.Where(i => i.VaultManifestId == manifestId && i.State == GroupInvitationState.Pending).ToListAsync())+ {+ CloseInvitation(invitation, GroupInvitationState.Revoked);+ }++ // Delete any active grant.+ var holders = await context.VaultManifestAccessKeys+ .Where(k => k.VaultManifestId == manifestId && k.Type == ManifestKeyType.GrantKey)+ .Select(k => k.UserId)+ .Distinct()+ .ToListAsync();+ foreach (var holder in holders)+ {+ await GrantHelper.RevokeAccessAsync(context, manifestId, holder);+ }++ await context.SaveChangesAsync();++ // Key rows and queued client actions reference the manifest without a cascading foreign key, so leftovers are deleted explicitly.+ await context.VaultManifestAccessKeys.Where(k => k.VaultManifestId == manifestId).ExecuteDeleteAsync();+ await context.ClientActions.Where(a => a.ManifestId == manifestId).ExecuteDeleteAsync();+ context.VaultManifests.Remove(manifest);+ await context.SaveChangesAsync();++ // The group exists for its shared manifest; memberships, invitations, client actions and quota rows cascade from it.+ if (!await context.VaultManifests.AnyAsync(m => m.OwnerGroupId == groupId))+ {+ await context.Groups.Where(g => g.Id == groupId && g.Type == GroupType.Shared).ExecuteDeleteAsync();+ }}/// <summary>@@ -1602,67 +1333,6 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn true;}- /// <summary>- /// Accept an invitation to join a group: the invitee becomes a member with the offered role.- /// </summary>- /// <param name="context">The database context.</param>- /// <param name="invitationId">The invitation.</param>- /// <param name="userId">The accepting user.</param>- /// <returns>Ok on success.</returns>- private async Task<IActionResult> AcceptJoinInvitationAsync(AliasServerDbContext context, Guid invitationId, string userId)- {- var strategy = context.Database.CreateExecutionStrategy();- return await strategy.ExecuteAsync<IActionResult>(async () =>- {- await using var transaction = await context.Database.BeginTransactionAsync();- var groupId = await context.GroupInvitations.Where(i => i.Id == invitationId && i.InviteeUserId == userId && i.State == GroupInvitationState.Pending && i.VaultManifestId == null).Select(i => (Guid?)i.GroupId).FirstOrDefaultAsync();- if (groupId is null)- {- return ApiError.Result(ApiErrorCode.INVITATION_NOT_FOUND, 404);- }-- // A concurrent revoke of the inviter's admin role, or a deletion of the group, either lands first or sees the new member.- await LockGroupMembersAsync(context, groupId.Value);-- // Read again under the lock: a deletion of the group that landed first took the invitation with it.- var invitation = await context.GroupInvitations.FirstOrDefaultAsync(i => i.Id == invitationId && i.InviteeUserId == userId && i.State == GroupInvitationState.Pending && i.VaultManifestId == null);- if (invitation is null)- {- return ApiError.Result(ApiErrorCode.INVITATION_NOT_FOUND, 404);- }-- if (invitation.CreatedAt < InvitationCutoff())- {- return await CloseAndCommitAsync(context, transaction, invitation, GroupInvitationState.Expired, ApiErrorCode.INVITATION_NOT_FOUND, 404);- }-- if (!await GroupHelper.IsSharedGroupAdminAsync(context, invitation.GroupId, invitation.InviterUserId))- {- return await CloseAndCommitAsync(context, transaction, invitation, GroupInvitationState.Revoked, ApiErrorCode.INVITATION_NOT_FOUND, 404);- }-- // The invitee has certified the inviter's current signing key (their client did, with the inviter's identity code).- if (!await HasCertifiedAsync(context, userId, invitation.InviterUserId, await GrantHelper.GetPrimarySigningKeyAsync(context, invitation.InviterUserId)))- {- return ApiError.Result(ApiErrorCode.INVITER_NOT_VERIFIED, 400);- }-- var now = timeProvider.GetUtcNow().UtcDateTime;- if (!await GroupHelper.IsSharedGroupMemberAsync(context, invitation.GroupId, userId))- {- context.GroupMembers.Add(new GroupMember { Id = Guid.NewGuid(), GroupId = invitation.GroupId, UserId = userId, Role = invitation.Role, CreatedAt = now, UpdatedAt = now });- }-- invitation.State = GroupInvitationState.Accepted;- invitation.RespondedAt = now;- invitation.UpdatedAt = now;-- await context.SaveChangesAsync();- await transaction.CommitAsync();- return Ok();- });- }-/// <summary>/// Start the SRP handshake that proves the caller's master password for one password confirmed action./// </summary>@@ -1714,6 +1384,7 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn await strategy.ExecuteAsync<IActionResult>(async () =>{await using var transaction = await context.Database.BeginTransactionAsync();+ await LockGroupMembersAsync(context, groupId);await LockManifestGrantsAsync(context, manifestId);if (await GrantHelper.IsLastGrantHolderAsync(context, manifestId, userId))@@ -1731,59 +1402,18 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Useawait ClientActionHelper.EnqueueForGroupAsync(context, ClientActionType.RotateManifestDeliveryKey, groupId, manifestId, timeProvider.GetUtcNow().UtcDateTime);}+ // The group is the people the shared manifest is shared with, so losing access to it is leaving the group.await context.SaveChangesAsync();- await transaction.CommitAsync();- return Ok();- });- }-- /// <summary>- /// Take a membership out of its group, with the member's grants, open invitations and key certifications, and commit.- /// Runs under the group's member and manifest locks; refuses a member who holds the only grant on a manifest.- /// </summary>- /// <param name="context">The database context, inside the open transaction.</param>- /// <param name="transaction">The open transaction to commit.</param>- /// <param name="membership">The membership to remove.</param>- /// <returns>Ok, or the error the removal is refused with.</returns>- private async Task<IActionResult> RemoveMembershipAndCommitAsync(AliasServerDbContext context, IDbContextTransaction transaction, GroupMember membership)- {- var groupId = membership.GroupId;- var userId = membership.UserId;-- // Every grant lock is taken first, in one order, so a concurrent accept or revoke on any of them either lands before or after the whole removal.- var manifestIds = await context.VaultManifests.Where(m => m.OwnerGroupId == groupId).Select(m => m.ManifestId).OrderBy(id => id).ToListAsync();- foreach (var manifestId in manifestIds)- {- await LockManifestGrantsAsync(context, manifestId);- }-- foreach (var manifestId in manifestIds)- {- if (await GrantHelper.IsLastGrantHolderAsync(context, manifestId, userId))- {- return ApiError.Result(ApiErrorCode.LAST_MANIFEST_GRANT_HOLDER, 400);- }- }-- // Offers to the member, and offers the member made as an admin, cannot be acted on anymore.- foreach (var invitation in await context.GroupInvitations.Where(i => i.GroupId == groupId && (i.InviteeUserId == userId || i.InviterUserId == userId) && i.State == GroupInvitationState.Pending).ToListAsync())- {- CloseInvitation(invitation, GroupInvitationState.Revoked);- }-- foreach (var manifestId in manifestIds)- {- if (await GrantHelper.RevokeAccessAsync(context, manifestId, userId))+ var membership = await context.GroupMembers.FirstOrDefaultAsync(m => m.GroupId == groupId && m.UserId == userId && m.Role != GroupRole.Owner);+ if (membership is not null && !await context.VaultManifests.AnyAsync(m => m.OwnerGroupId == groupId && context.VaultManifestAccessKeys.Any(k => k.VaultManifestId == m.ManifestId && k.UserId == userId && k.Type == ManifestKeyType.GrantKey))){- await ClientActionHelper.EnqueueForGroupAsync(context, ClientActionType.RotateManifestDeliveryKey, groupId, manifestId, timeProvider.GetUtcNow().UtcDateTime);+ context.GroupMembers.Remove(membership);}- }- await DeleteCertificationsBetweenAsync(context, groupId, userId);- context.GroupMembers.Remove(membership);- await context.SaveChangesAsync();- await transaction.CommitAsync();- return Ok();+ await context.SaveChangesAsync();+ await transaction.CommitAsync();+ return Ok();+ });}/// <summary>@@ -1810,7 +1440,7 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use/// <returns>The cutoff in UTC.</returns>private DateTime InvitationCutoff(){- return timeProvider.GetUtcNow().UtcDateTime - InvitationLifetime;+ return timeProvider.GetUtcNow().UtcDateTime - GroupHelper.InvitationLifetime;}/// <summary>diff --git a/apps/server/AliasVault.Api/Controllers/V2/StatusController.cs b/apps/server/AliasVault.Api/Controllers/V2/StatusController.csindex 4d3f46667..d39efb515 100644--- a/apps/server/AliasVault.Api/Controllers/V2/StatusController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/StatusController.cs@@ -30,8 +30,9 @@ using StatusModels = AliasVault.Shared.Models.WebApi.V2.Status;/// <param name="userManager">UserManager.</param>/// <param name="capabilityService">Resolves which capabilities the caller may use.</param>/// <param name="config">Config instance.</param>+/// <param name="timeProvider">Time provider, for the expiry of invitations.</param>[ApiVersion("2")]-public class StatusController(IAliasServerDbContextFactory dbContextFactory, UserManager<AliasVaultUser> userManager, CapabilityService capabilityService, Config config) : AuthenticatedRequestController(userManager)+public class StatusController(IAliasServerDbContextFactory dbContextFactory, UserManager<AliasVaultUser> userManager, CapabilityService capabilityService, Config config, TimeProvider timeProvider) : AuthenticatedRequestController(userManager){/// <summary>/// Status endpoint called by the client to get the full sync status in one call.@@ -84,6 +85,7 @@ public class StatusController(IAliasServerDbContextFactory dbContextFactory, UseCapabilities = await capabilityService.GetCapabilitiesAsync(user.Id, ClientHeader),PendingActions = await ClientActionHelper.GetPendingActionsAsync(context, user.Id),EmailDomains = EmailDomainHelper.GetEmailDomains(config),+ PendingSharingInvitations = await GroupHelper.CountOpenInvitationsAsync(context, user.Id, timeProvider.GetUtcNow().UtcDateTime - GroupHelper.InvitationLifetime),});}}diff --git a/apps/server/AliasVault.Api/Controllers/V2/Tests/TestController.cs b/apps/server/AliasVault.Api/Controllers/V2/Tests/TestController.csindex 057c7d903..b32cb2e9d 100644--- a/apps/server/AliasVault.Api/Controllers/V2/Tests/TestController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/Tests/TestController.cs@@ -494,45 +494,6 @@ public class TestController(return Ok(new { code });}- /// <summary>- /// Create a shared group with an owner and members.- /// Only available in DEBUG builds.- /// </summary>- /// <param name="request">The group name and the usernames of its owner and members.</param>- /// <returns>OK with the group id.</returns>- [AllowAnonymous]- [HttpPost("shared-groups")]- public async Task<IActionResult> CreateSharedGroup([FromBody] CreateSharedGroupRequest request)- {- if (!environment.IsDevelopment())- {- return NotFound();- }-- await using var context = await dbContextFactory.CreateDbContextAsync();-- var usernames = request.MemberUsernames.Prepend(request.OwnerUsername).Select(u => u.ToUpperInvariant()).ToList();- var users = await context.AliasVaultUsers.Where(u => usernames.Contains(u.NormalizedUserName!)).ToDictionaryAsync(u => u.NormalizedUserName!, u => u.Id);- var missing = usernames.Where(u => !users.ContainsKey(u)).ToList();- if (missing.Count > 0)- {- return NotFound($"Users not found: {string.Join(", ", missing)}");- }-- var now = DateTime.UtcNow;- var group = new Group { Id = Guid.NewGuid(), Name = request.Name, Type = GroupType.Shared, CreatedAt = now, UpdatedAt = now };- context.Groups.Add(group);- context.GroupMembers.Add(GroupHelper.CreateOwnerMembership(group, users[usernames[0]], now));- foreach (var username in usernames.Skip(1))- {- context.GroupMembers.Add(new GroupMember { Id = Guid.NewGuid(), GroupId = group.Id, UserId = users[username], Role = GroupRole.Member, CreatedAt = now, UpdatedAt = now });- }-- await context.SaveChangesAsync();-- return Ok(new { groupId = group.Id });- }-/// <summary>/// Set a capability for one account with a per-account rule, and drop the rule cache so it applies right away./// Only available in DEBUG builds.diff --git a/apps/server/AliasVault.Api/Helpers/GroupHelper.cs b/apps/server/AliasVault.Api/Helpers/GroupHelper.csindex aef560398..8a9af40c5 100644--- a/apps/server/AliasVault.Api/Helpers/GroupHelper.cs+++ b/apps/server/AliasVault.Api/Helpers/GroupHelper.cs@@ -8,6 +8,7 @@namespace AliasVault.Api.Helpers;using AliasServerDb;+using AliasVault.Shared.Models.Enums;using Microsoft.EntityFrameworkCore;/// <summary>@@ -15,6 +16,28 @@ using Microsoft.EntityFrameworkCore;/// </summary>public static class GroupHelper{+ /// <summary>+ /// How long an offer of access stays open before it expires unanswered.+ /// </summary>+ public static readonly TimeSpan InvitationLifetime = TimeSpan.FromDays(7);++ /// <summary>+ /// The number of open offers of access to a shared manifest addressed to a user that they can still accept: not+ /// expired, and made by somebody who is still an admin of the group and holds the manifest's key.+ /// </summary>+ /// <param name="context">Database context.</param>+ /// <param name="userId">The invitee.</param>+ /// <param name="createdAfter">Offers created before this have expired.</param>+ /// <returns>The number of open offers.</returns>+ public static async Task<int> CountOpenInvitationsAsync(AliasServerDbContext context, string userId, DateTime createdAfter)+ {+ return await context.GroupInvitations+ .Where(i => i.InviteeUserId == userId && i.State == GroupInvitationState.Pending && i.Group.Type == GroupType.Shared && i.VaultManifestId != null && i.CreatedAt >= createdAfter)+ .Where(i => context.GroupMembers.Any(m => m.GroupId == i.GroupId && m.UserId == i.InviterUserId && (m.Role == GroupRole.Owner || m.Role == GroupRole.Admin)))+ .Where(i => context.VaultManifestAccessKeys.Any(k => k.UserId == i.InviterUserId && k.Type == ManifestKeyType.GrantKey && k.VaultManifestId == i.VaultManifestId))+ .CountAsync();+ }+/// <summary>/// Get the ids of every group the user may administer./// </summary>@@ -105,6 +128,17 @@ public static class GroupHelperreturn await context.GroupMembers.AnyAsync(m => m.GroupId == groupId && m.UserId == userId && (m.Role == GroupRole.Admin || m.Role == GroupRole.Owner));}+ /// <summary>+ /// The owner of a group.+ /// </summary>+ /// <param name="context">Database context.</param>+ /// <param name="groupId">The group.</param>+ /// <returns>The owner's user id, or null when the group has none.</returns>+ public static async Task<string?> GetOwnerUserIdAsync(AliasServerDbContext context, Guid groupId)+ {+ return await context.GroupMembers.Where(m => m.GroupId == groupId && m.Role == GroupRole.Owner).Select(m => m.UserId).FirstOrDefaultAsync();+ }+/// <summary>/// The manifests owned by a <see cref="GroupType.Shared"/> group, i.e. every manifest that is somebody's shared/// vault rather than somebody's personal one.diff --git a/apps/server/Shared/AliasVault.Shared.Server/Capabilities/CapabilityRegistry.cs b/apps/server/Shared/AliasVault.Shared.Server/Capabilities/CapabilityRegistry.csindex 7f4abc9f9..ba0f1ea4a 100644--- a/apps/server/Shared/AliasVault.Shared.Server/Capabilities/CapabilityRegistry.cs+++ b/apps/server/Shared/AliasVault.Shared.Server/Capabilities/CapabilityRegistry.cs@@ -22,7 +22,7 @@ public static class CapabilityRegistryCapabilityKeys.VaultSharing,CapabilityRuleKind.Entitlement,CapabilityValue.Off,- "Shared vaults: creating a shared vault, giving family members access to it and accepting such access. Currently in beta."),+ "Shared vaults: creating a shared vault, giving other people access to it and accepting such access. Currently in beta."),}.AsReadOnly();private static readonly Dictionary<string, CapabilityDefinition> ByKey = Definitions.ToDictionary(d => d.Key, StringComparer.OrdinalIgnoreCase);diff --git a/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.cs b/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.csindex ba59eff74..7264c08db 100644--- a/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/Enums/ApiErrorCode.cs@@ -197,11 +197,6 @@ public enum ApiErrorCode/// </summary>GROUP_NOT_FOUND,- /// <summary>- /// The group already holds as many shared manifests as it is entitled to, so no further one can be created.- /// </summary>- GROUP_MANIFEST_LIMIT_REACHED,-/// <summary>/// The invitation does not exist, is not addressed to (or sent by) the caller, or was already answered./// </summary>@@ -362,30 +357,15 @@ public enum ApiErrorCodeIDENTITY_CODE_NOT_FOUND,/// <summary>- /// The account is already a member of the group.- /// </summary>- ALREADY_GROUP_MEMBER,-- /// <summary>- /// The caller already owns the maximum number of shared groups.+ /// The shared manifest still has other members or open invitations, so it cannot become personal again./// </summary>- GROUP_LIMIT_REACHED,+ SHARED_MANIFEST_IN_USE,/// <summary>/// The caller has not certified the member's current signing key with their identity code./// </summary>MEMBER_NOT_VERIFIED,- /// <summary>- /// The owner of a shared group cannot leave it; they delete it instead.- /// </summary>- GROUP_OWNER_CANNOT_LEAVE,-- /// <summary>- /// The shared group still has other members or shared manifests, so it cannot be deleted yet.- /// </summary>- GROUP_NOT_EMPTY,-/// <summary>/// The caller has not certified the inviter's current signing key with their identity code./// </summary>diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/CreateGroupResponse.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/CreateGroupResponse.csdeleted file mode 100644index 49248bd88..000000000--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/CreateGroupResponse.cs+++ /dev/null@@ -1,17 +0,0 @@-//------------------------------------------------------------------------// <copyright file="CreateGroupResponse.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.Shared.Models.WebApi.V2.Groups;--/// <summary>-/// The created group, as served by POST /v2/Groups.-/// </summary>-public class CreateGroupResponse-{- /// <summary>Gets or sets the id of the new group.</summary>- public required Guid GroupId { get; set; }-}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/CreateSharedManifestResponse.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/CreateSharedManifestResponse.csindex 4b40d9cec..8f7c37edc 100644--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/CreateSharedManifestResponse.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/CreateSharedManifestResponse.cs@@ -8,10 +8,13 @@namespace AliasVault.Shared.Models.WebApi.V2.Groups;/// <summary>-/// Response for POST /v2/Groups/{groupId}/manifests. Identifies the newly created shared manifest.+/// Response for POST /v2/Groups/manifests. Identifies the newly created shared manifest and the group it is shared in./// </summary>public class CreateSharedManifestResponse{+ /// <summary>Gets or sets the id of the group created for the shared manifest.</summary>+ public required Guid GroupId { get; set; }+/// <summary>Gets or sets the id of the created manifest (used to grant/revoke access and to sync it).</summary>public required Guid ManifestId { get; set; }diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupInfo.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupInfo.csindex 6c6ea60ce..2058c769f 100644--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupInfo.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupInfo.cs@@ -31,9 +31,4 @@ public class GroupInfo/// Gets or sets the group's members./// </summary>public List<GroupMemberInfo> Members { get; set; } = [];-- /// <summary>- /// Gets or sets the open invitations to join this group, for admins.- /// </summary>- public List<SentManifestInvitation> PendingJoinInvitations { get; set; } = [];}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupOverviewResponse.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupOverviewResponse.csindex 5b40d12da..7185256c6 100644--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupOverviewResponse.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/GroupOverviewResponse.cs@@ -18,17 +18,18 @@ public class GroupOverviewResponsepublic required string UserId { get; set; }/// <summary>- /// Gets or sets the shared groups the caller belongs to, with each groups metadata.+ /// Gets or sets the shared groups the caller belongs to, one per shared manifest./// </summary>public List<GroupInfo> Groups { get; set; } = [];/// <summary>- /// Gets or sets the offers of access to a shared manifest addressed to the caller, awaiting their accept or decline.+ /// Gets or sets the people whose current signing key the caller certified, with the keys to invite them to a shared+ /// manifest without their identity code. The caller's client checks each certification itself./// </summary>- public List<ReceivedManifestInvitation> ReceivedInvitations { get; set; } = [];+ public List<GroupMemberInfo> VerifiedPeople { get; set; } = [];/// <summary>- /// Gets or sets the invitations to join a group addressed to the caller.+ /// Gets or sets the offers of access to a shared manifest addressed to the caller, awaiting their accept or decline./// </summary>- public List<ReceivedGroupInvitation> ReceivedGroupInvitations { get; set; } = [];+ public List<ReceivedManifestInvitation> ReceivedInvitations { get; set; } = [];}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupRequest.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupRequest.csindex 163533d68..fe5490f36 100644--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupRequest.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupRequest.cs@@ -8,10 +8,10 @@namespace AliasVault.Shared.Models.WebApi.V2.Groups;/// <summary>-/// Look up the account an identity code belongs to (POST /v2/Groups/{groupId}/members/lookup).+/// Look up the account an identity code belongs to (POST /v2/Groups/lookup)./// </summary>public class IdentityCodeLookupRequest{- /// <summary>Gets or sets the identity code the invitee gave the admin; separators are ignored.</summary>+ /// <summary>Gets or sets the identity code the other person gave; separators are ignored.</summary>public required string Code { get; set; }}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupResponse.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupResponse.csindex 411747d79..55cb8a528 100644--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupResponse.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/IdentityCodeLookupResponse.cs@@ -20,4 +20,13 @@ public class IdentityCodeLookupResponse/// <summary>Gets or sets the account's signing public key (base64), whose fingerprint is the code.</summary>public required string SigningPublicKey { get; set; }++ /// <summary>Gets or sets the id of the account public key a vault key is encrypted for them with, if they have one.</summary>+ public Guid? AccountPublicKeyId { get; set; }++ /// <summary>Gets or sets that account public key (JWK), if they have one.</summary>+ public string? AccountPublicKey { get; set; }++ /// <summary>Gets or sets the signature of the account public key by the signing key.</summary>+ public string? AccountPublicKeySignature { get; set; }}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/InviteToGroupRequest.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/InviteToGroupRequest.csdeleted file mode 100644index 17566ba09..000000000--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/InviteToGroupRequest.cs+++ /dev/null@@ -1,17 +0,0 @@-//------------------------------------------------------------------------// <copyright file="InviteToGroupRequest.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.Shared.Models.WebApi.V2.Groups;--/// <summary>-/// Invite an account to join a group (POST /v2/Groups/{groupId}/invitations).-/// </summary>-public class InviteToGroupRequest-{- /// <summary>Gets or sets the account to invite, as found by its identity code.</summary>- public required string UserId { get; set; }-}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/ReceivedGroupInvitation.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/ReceivedGroupInvitation.csdeleted file mode 100644index 9b386fcec..000000000--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Groups/ReceivedGroupInvitation.cs+++ /dev/null@@ -1,35 +0,0 @@-//------------------------------------------------------------------------// <copyright file="ReceivedGroupInvitation.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.Shared.Models.WebApi.V2.Groups;--/// <summary>-/// An open invitation to join a group, addressed to the caller.-/// </summary>-public class ReceivedGroupInvitation-{- /// <summary>Gets or sets the invitation id.</summary>- public required Guid Id { get; set; }-- /// <summary>Gets or sets the group it invites to.</summary>- public required Guid GroupId { get; set; }-- /// <summary>Gets or sets the admin who sent it.</summary>- public required string InviterUserId { get; set; }-- /// <summary>Gets or sets the username of the admin who sent it.</summary>- public required string InviterUsername { get; set; }-- /// <summary>Gets or sets when it was sent.</summary>- public required DateTime CreatedAt { get; set; }-- /// <summary>Gets or sets the inviter's current signing public key, which the invitee certifies with the inviter's identity code.</summary>- public string? InviterSigningPublicKey { get; set; }-- /// <summary>Gets or sets the invitee's own certification of <see cref="InviterSigningPublicKey"/>, when they certified the inviter before.</summary>- public string? InviterCertification { get; set; }-}diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Status/StatusResponse.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Status/StatusResponse.csindex 2d57175e4..82a094d79 100644--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Status/StatusResponse.cs+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Status/StatusResponse.cs@@ -64,4 +64,10 @@ public class StatusResponse/// Gets or sets the email domains this server serves, so clients pick up a domain change without a vault pull./// </summary>public EmailDomains EmailDomains { get; set; } = new();++ /// <summary>+ /// Gets or sets the number of open invitations to shared manifests addressed to this caller, so a client knows when+ /// to fetch them.+ /// </summary>+ public int PendingSharingInvitations { get; set; }}