diff --git a/API.IntegrationTests/Tests/PublicSharePasswordTests.cs b/API.IntegrationTests/Tests/PublicSharePasswordTests.cs index 601bc915..feca254f 100644 --- a/API.IntegrationTests/Tests/PublicSharePasswordTests.cs +++ b/API.IntegrationTests/Tests/PublicSharePasswordTests.cs @@ -6,6 +6,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using OpenShock.API.IntegrationTests.Helpers; +using OpenShock.API.Services.Account; using OpenShock.Common.Constants; using OpenShock.Common.Models.WebSocket.User; using OpenShock.Common.OpenShockDb; @@ -455,6 +456,25 @@ await scope.ServiceProvider.GetRequiredService() await AssertConnectionClosesAsync(connection); } + [Test] + public async Task Hub_DeactivatingTheOwner_ClosesGuestsAndLoggedInVisitors() + { + var (owner, shareId) = await CreateShareWithShocker("pwhubownerdeact", password: null); + var visitor = await TestHelper.CreateAndLoginUser(WebApplicationFactory, + TestHelper.UniqueUsername("pwhubownerdeactv"), TestHelper.UniqueEmail("pwhubownerdeactv"), "SecurePassword123#"); + await using var guestConnection = await ConnectAsync(shareId, unlockToken: null); + await using var visitorConnection = await ConnectAsync(shareId, unlockToken: null, sessionToken: visitor.SessionToken); + + await using (var scope = WebApplicationFactory.Services.CreateAsyncScope()) + { + await scope.ServiceProvider.GetRequiredService() + .DeactivateAccountAsync(owner.Id, owner.Id, deleteLater: false); + } + + await AssertConnectionClosesAsync(guestConnection); + await AssertConnectionClosesAsync(visitorConnection); + } + // --- Helpers --- private async Task<(AuthenticatedUser Owner, Guid ShareId)> CreateShareWithShocker(string prefix, diff --git a/API/Controller/Shares/Links/DeletePublicShare.cs b/API/Controller/Shares/Links/DeletePublicShare.cs index 5927c9c9..558466ec 100644 --- a/API/Controller/Shares/Links/DeletePublicShare.cs +++ b/API/Controller/Shares/Links/DeletePublicShare.cs @@ -20,6 +20,7 @@ public sealed partial class ShareLinksController /// /// /// + /// /// /// Deleted public share /// Public share not found or does not belong to you diff --git a/API/Realtime/RedisSubscriberService.cs b/API/Realtime/RedisSubscriberService.cs index a1c1914b..e6b845cc 100644 --- a/API/Realtime/RedisSubscriberService.cs +++ b/API/Realtime/RedisSubscriberService.cs @@ -76,7 +76,7 @@ private void HandleUserAuthRevoked(RedisChannel _, RedisValue message) if (!Guid.TryParse((string?)message, out var userId)) return; var closed = _hubConnections.AbortUser(userId); - if (closed > 0) _logger.LogDebug("Closed {Count} hub connection(s) of user [{UserId}]", closed, userId); + if (closed > 0) _logger.LogDebug("Closed {Count} hub connection(s) of user [{UserId}] or to their public shares", closed, userId); } private void HandlePublicShareAccessRevoked(RedisChannel _, RedisValue message) diff --git a/Common/Hubs/HubConnectionTracker.cs b/Common/Hubs/HubConnectionTracker.cs index 81a888d9..3df64107 100644 --- a/Common/Hubs/HubConnectionTracker.cs +++ b/Common/Hubs/HubConnectionTracker.cs @@ -54,11 +54,14 @@ public sealed class HubConnectionTracker /// Starts tracking a public share connection, or marks an already tracked (logged in) one as such, so it is /// closed when the share's access changes. /// + /// The connection. + /// The share the connection controls. + /// The share's owner, whose deactivation closes the connection too. /// The share's password stamp the visitor got in with, null when it had no password. - public TrackedHubConnection TrackPublicShare(HubCallerContext context, Guid publicShareId, Guid? passwordStamp) + public TrackedHubConnection TrackPublicShare(HubCallerContext context, Guid publicShareId, Guid publicShareOwnerId, Guid? passwordStamp) { var tracked = _connections.GetOrAdd(context.ConnectionId, _ => new TrackedHubConnection(context, userId: null, sessionId: null, apiTokenId: null)); - tracked.AttachPublicShare(publicShareId, passwordStamp); + tracked.AttachPublicShare(publicShareId, publicShareOwnerId, passwordStamp); // A logged in connection was already checked, but not against the share: the next call has to. tracked.ResetValidation(); // Kept on the connection too, so a call that arrives after a revocation untracked it can be told apart @@ -84,9 +87,9 @@ public TrackedHubConnection TrackPublicShare(HubCallerContext context, Guid publ /// How many connections were closed. public int AbortSession(string sessionId) => Abort(c => c.SessionId == sessionId); - /// Closes every connection of this user, whatever it authenticated with. + /// Closes every connection of this user, whatever it authenticated with, and the connections to their public shares. /// How many connections were closed. - public int AbortUser(Guid userId) => Abort(c => c.UserId == userId); + public int AbortUser(Guid userId) => Abort(c => c.UserId == userId || c.PublicShareOwnerId == userId); /// Closes the connections that authenticated with this API token. /// How many connections were closed. @@ -134,12 +137,16 @@ public TrackedHubConnection(HubCallerContext context, Guid? userId, string? sess /// The public share this connection controls, null for other hubs. public Guid? PublicShareId { get; private set; } + /// The owner of . + public Guid? PublicShareOwnerId { get; private set; } + /// The share's password stamp when this connection was let in, null when it had no password. public Guid? PasswordStamp { get; private set; } - public void AttachPublicShare(Guid publicShareId, Guid? passwordStamp) + public void AttachPublicShare(Guid publicShareId, Guid publicShareOwnerId, Guid? passwordStamp) { PublicShareId = publicShareId; + PublicShareOwnerId = publicShareOwnerId; PasswordStamp = passwordStamp; } diff --git a/Common/Hubs/PublicShareHub.cs b/Common/Hubs/PublicShareHub.cs index a2f41eb4..f0b324df 100644 --- a/Common/Hubs/PublicShareHub.cs +++ b/Common/Hubs/PublicShareHub.cs @@ -137,9 +137,9 @@ public override async Task OnConnectedAsync() AdditionalItems = additionalItems } }; - // Closed when the share's password is set or changed, or the share is deleted. Left unvalidated so the + // Closed when the share's password is set or changed, the share is deleted or its owner deactivated. Left unvalidated so the // first call checks the share again, covering a change made since it was read above. - _connectionTracker.TrackPublicShare(Context, id, share.PasswordStamp); + _connectionTracker.TrackPublicShare(Context, id, share.OwnerId, share.PasswordStamp); await Groups.AddToGroupAsync(Context.ConnectionId, GroupName(id)); await Clients.Caller.Welcome(user is null ? AuthType.Guest : AuthType.Authenticated);