From 18713054f6757242c2172c7691ff53a51de33010 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 28 Aug 2018 08:22:49 -0400 Subject: [PATCH] add logout notification --- src/Core/Enums/PushType.cs | 4 +++- src/Core/Models/PushNotification.cs | 2 +- src/Core/Services/IPushNotificationService.cs | 1 + .../AzureQueuePushNotificationService.cs | 17 +++++++++++------ .../MultiServicePushNotificationService.cs | 6 ++++++ .../NotificationHubPushNotificationService.cs | 17 +++++++++++------ .../NotificationsApiPushNotificationService.cs | 17 +++++++++++------ .../RelayPushNotificationService.cs | 17 +++++++++++------ .../NoopPushNotificationService.cs | 5 +++++ src/Notifications/HubHelpers.cs | 3 ++- 10 files changed, 62 insertions(+), 27 deletions(-) diff --git a/src/Core/Enums/PushType.cs b/src/Core/Enums/PushType.cs index 5d36113833..67ada37fa7 100644 --- a/src/Core/Enums/PushType.cs +++ b/src/Core/Enums/PushType.cs @@ -13,6 +13,8 @@ SyncFolderCreate = 7, SyncFolderUpdate = 8, SyncCipherDelete = 9, - SyncSettings = 10 + SyncSettings = 10, + + LogOut = 11, } } diff --git a/src/Core/Models/PushNotification.cs b/src/Core/Models/PushNotification.cs index e7d1deefce..0945da0db0 100644 --- a/src/Core/Models/PushNotification.cs +++ b/src/Core/Models/PushNotification.cs @@ -34,7 +34,7 @@ namespace Bit.Core.Models public DateTime RevisionDate { get; set; } } - public class SyncUserPushNotification + public class UserPushNotification { public Guid UserId { get; set; } public DateTime Date { get; set; } diff --git a/src/Core/Services/IPushNotificationService.cs b/src/Core/Services/IPushNotificationService.cs index 805bf846df..ffd9164437 100644 --- a/src/Core/Services/IPushNotificationService.cs +++ b/src/Core/Services/IPushNotificationService.cs @@ -18,6 +18,7 @@ namespace Bit.Core.Services Task PushSyncVaultAsync(Guid userId); Task PushSyncOrgKeysAsync(Guid userId); Task PushSyncSettingsAsync(Guid userId); + Task PushLogOutAsync(Guid userId); Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier); Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier); } diff --git a/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs b/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs index 4a8ba5c2aa..9d1c792298 100644 --- a/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs +++ b/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs @@ -104,27 +104,32 @@ namespace Bit.Core.Services public async Task PushSyncCiphersAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncCiphers); + await PushUserAsync(userId, PushType.SyncCiphers); } public async Task PushSyncVaultAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncVault); + await PushUserAsync(userId, PushType.SyncVault); } public async Task PushSyncOrgKeysAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncOrgKeys); + await PushUserAsync(userId, PushType.SyncOrgKeys); } public async Task PushSyncSettingsAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncSettings); + await PushUserAsync(userId, PushType.SyncSettings); } - private async Task PushSyncUserAsync(Guid userId, PushType type) + public async Task PushLogOutAsync(Guid userId) { - var message = new SyncUserPushNotification + await PushUserAsync(userId, PushType.LogOut); + } + + private async Task PushUserAsync(Guid userId, PushType type) + { + var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow diff --git a/src/Core/Services/Implementations/MultiServicePushNotificationService.cs b/src/Core/Services/Implementations/MultiServicePushNotificationService.cs index c64d5f8ed2..88ace67f54 100644 --- a/src/Core/Services/Implementations/MultiServicePushNotificationService.cs +++ b/src/Core/Services/Implementations/MultiServicePushNotificationService.cs @@ -107,6 +107,12 @@ namespace Bit.Core.Services return Task.FromResult(0); } + public Task PushLogOutAsync(Guid userId) + { + PushToServices((s) => s.PushLogOutAsync(userId)); + return Task.FromResult(0); + } + public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier) { PushToServices((s) => s.SendPayloadToUserAsync(userId, type, payload, identifier)); diff --git a/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs b/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs index 39cdb85202..0b3355bde4 100644 --- a/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs +++ b/src/Core/Services/Implementations/NotificationHubPushNotificationService.cs @@ -95,27 +95,32 @@ namespace Bit.Core.Services public async Task PushSyncCiphersAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncCiphers); + await PushUserAsync(userId, PushType.SyncCiphers); } public async Task PushSyncVaultAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncVault); + await PushUserAsync(userId, PushType.SyncVault); } public async Task PushSyncOrgKeysAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncOrgKeys); + await PushUserAsync(userId, PushType.SyncOrgKeys); } public async Task PushSyncSettingsAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncSettings); + await PushUserAsync(userId, PushType.SyncSettings); } - private async Task PushSyncUserAsync(Guid userId, PushType type) + public async Task PushLogOutAsync(Guid userId) { - var message = new SyncUserPushNotification + await PushUserAsync(userId, PushType.LogOut); + } + + private async Task PushUserAsync(Guid userId, PushType type) + { + var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow diff --git a/src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs b/src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs index 2aeb10326d..8444c0a168 100644 --- a/src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs +++ b/src/Core/Services/Implementations/NotificationsApiPushNotificationService.cs @@ -108,27 +108,32 @@ namespace Bit.Core.Services public async Task PushSyncCiphersAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncCiphers); + await PushUserAsync(userId, PushType.SyncCiphers); } public async Task PushSyncVaultAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncVault); + await PushUserAsync(userId, PushType.SyncVault); } public async Task PushSyncOrgKeysAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncOrgKeys); + await PushUserAsync(userId, PushType.SyncOrgKeys); } public async Task PushSyncSettingsAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncSettings); + await PushUserAsync(userId, PushType.SyncSettings); } - private async Task PushSyncUserAsync(Guid userId, PushType type) + public async Task PushLogOutAsync(Guid userId) { - var message = new SyncUserPushNotification + await PushUserAsync(userId, PushType.LogOut); + } + + private async Task PushUserAsync(Guid userId, PushType type) + { + var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow diff --git a/src/Core/Services/Implementations/RelayPushNotificationService.cs b/src/Core/Services/Implementations/RelayPushNotificationService.cs index f84ac9bfa4..831aae1a04 100644 --- a/src/Core/Services/Implementations/RelayPushNotificationService.cs +++ b/src/Core/Services/Implementations/RelayPushNotificationService.cs @@ -101,27 +101,32 @@ namespace Bit.Core.Services public async Task PushSyncCiphersAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncCiphers); + await PushUserAsync(userId, PushType.SyncCiphers); } public async Task PushSyncVaultAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncVault); + await PushUserAsync(userId, PushType.SyncVault); } public async Task PushSyncOrgKeysAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncOrgKeys); + await PushUserAsync(userId, PushType.SyncOrgKeys); } public async Task PushSyncSettingsAsync(Guid userId) { - await PushSyncUserAsync(userId, PushType.SyncSettings); + await PushUserAsync(userId, PushType.SyncSettings); } - private async Task PushSyncUserAsync(Guid userId, PushType type) + public async Task PushLogOutAsync(Guid userId) { - var message = new SyncUserPushNotification + await PushUserAsync(userId, PushType.LogOut); + } + + private async Task PushUserAsync(Guid userId, PushType type) + { + var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow diff --git a/src/Core/Services/NoopImplementations/NoopPushNotificationService.cs b/src/Core/Services/NoopImplementations/NoopPushNotificationService.cs index 51cc3d868e..a2ef04dc04 100644 --- a/src/Core/Services/NoopImplementations/NoopPushNotificationService.cs +++ b/src/Core/Services/NoopImplementations/NoopPushNotificationService.cs @@ -58,6 +58,11 @@ namespace Bit.Core.Services return Task.FromResult(0); } + public Task PushLogOutAsync(Guid userId) + { + return Task.FromResult(0); + } + public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier) { return Task.FromResult(0); diff --git a/src/Notifications/HubHelpers.cs b/src/Notifications/HubHelpers.cs index 2a9cc403d3..f8900f28e1 100644 --- a/src/Notifications/HubHelpers.cs +++ b/src/Notifications/HubHelpers.cs @@ -47,8 +47,9 @@ namespace Bit.Notifications case PushType.SyncVault: case PushType.SyncOrgKeys: case PushType.SyncSettings: + case PushType.LogOut: var userNotification = - JsonConvert.DeserializeObject>( + JsonConvert.DeserializeObject>( notificationJson); await hubContext.Clients.User(userNotification.Payload.UserId.ToString()) .SendAsync("ReceiveMessage", userNotification, cancellationToken);