From 42d3a2d8e3492a26c126ec5d8a4a7f89d2932eaa Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 16 Aug 2018 13:35:16 -0400 Subject: [PATCH] hub configurations --- src/Core/GlobalSettings.cs | 1 + .../AzureQueuePushNotificationService.cs | 4 ++-- .../MultiServicePushNotificationService.cs | 19 +++++++++++++++---- src/Hub/AzureQueueHostedService.cs | 2 +- ...ntroller.cs => NotificationsController.cs} | 6 +++--- src/Hub/HubHelpers.cs | 16 ++++++++-------- 6 files changed, 30 insertions(+), 18 deletions(-) rename src/Hub/Controllers/{NotificationController.cs => NotificationsController.cs} (78%) diff --git a/src/Core/GlobalSettings.cs b/src/Core/GlobalSettings.cs index 4db96e373..dfe40e4fa 100644 --- a/src/Core/GlobalSettings.cs +++ b/src/Core/GlobalSettings.cs @@ -19,6 +19,7 @@ namespace Bit.Core public virtual MailSettings Mail { get; set; } = new MailSettings(); public virtual StorageSettings Storage { get; set; } = new StorageSettings(); public virtual StorageSettings Events { get; set; } = new StorageSettings(); + public virtual StorageSettings Notifications { get; set; } = new StorageSettings(); public virtual AttachmentSettings Attachment { get; set; } = new AttachmentSettings(); public virtual IdentityServerSettings IdentityServer { get; set; } = new IdentityServerSettings(); public virtual DataProtectionSettings DataProtection { get; set; } = new DataProtectionSettings(); diff --git a/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs b/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs index 84bef2c01..820c5af54 100644 --- a/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs +++ b/src/Core/Services/Implementations/AzureQueuePushNotificationService.cs @@ -25,9 +25,9 @@ namespace Bit.Core.Services GlobalSettings globalSettings, IHttpContextAccessor httpContextAccessor) { - var storageAccount = CloudStorageAccount.Parse(globalSettings.Events.ConnectionString); + var storageAccount = CloudStorageAccount.Parse(globalSettings.Notifications.ConnectionString); var queueClient = storageAccount.CreateCloudQueueClient(); - _queue = queueClient.GetQueueReference("sync"); + _queue = queueClient.GetQueueReference("notifications"); _globalSettings = globalSettings; _httpContextAccessor = httpContextAccessor; } diff --git a/src/Core/Services/Implementations/MultiServicePushNotificationService.cs b/src/Core/Services/Implementations/MultiServicePushNotificationService.cs index c718f55e0..7c83e45bc 100644 --- a/src/Core/Services/Implementations/MultiServicePushNotificationService.cs +++ b/src/Core/Services/Implementations/MultiServicePushNotificationService.cs @@ -16,7 +16,8 @@ namespace Bit.Core.Services public MultiServicePushNotificationService( GlobalSettings globalSettings, IHttpContextAccessor httpContextAccessor, - ILogger relayLogger) + ILogger relayLogger, + ILogger hubLogger) { if(globalSettings.SelfHosted) { @@ -26,12 +27,22 @@ namespace Bit.Core.Services { _services.Add(new RelayPushNotificationService(globalSettings, httpContextAccessor, relayLogger)); } - // TODO: ApiPushNotificationService for SignalR + if(CoreHelpers.SettingHasValue(globalSettings.InternalIdentityKey) && + CoreHelpers.SettingHasValue(globalSettings.BaseServiceUri.InternalHub)) + { + // _services.Add(new HubApiPushNotificationService(globalSettings, httpContextAccessor, hubLogger)); + } } else { - _services.Add(new NotificationHubPushNotificationService(globalSettings, httpContextAccessor)); - // _services.Add(new AzureQueuePushNotificationService(globalSettings, httpContextAccessor)); + if(CoreHelpers.SettingHasValue(globalSettings.NotificationHub.ConnectionString)) + { + _services.Add(new NotificationHubPushNotificationService(globalSettings, httpContextAccessor)); + } + if(CoreHelpers.SettingHasValue(globalSettings.Notifications?.ConnectionString)) + { + // _services.Add(new AzureQueuePushNotificationService(globalSettings, httpContextAccessor)); + } } } diff --git a/src/Hub/AzureQueueHostedService.cs b/src/Hub/AzureQueueHostedService.cs index 04d665923..799b05b6b 100644 --- a/src/Hub/AzureQueueHostedService.cs +++ b/src/Hub/AzureQueueHostedService.cs @@ -58,7 +58,7 @@ namespace Bit.Hub { var storageAccount = CloudStorageAccount.Parse(_globalSettings.Events.ConnectionString); var queueClient = storageAccount.CreateCloudQueueClient(); - _queue = queueClient.GetQueueReference("sync"); + _queue = queueClient.GetQueueReference("notifications"); while(!cancellationToken.IsCancellationRequested) { diff --git a/src/Hub/Controllers/NotificationController.cs b/src/Hub/Controllers/NotificationsController.cs similarity index 78% rename from src/Hub/Controllers/NotificationController.cs rename to src/Hub/Controllers/NotificationsController.cs index b7b8627c8..f31ff8be7 100644 --- a/src/Hub/Controllers/NotificationController.cs +++ b/src/Hub/Controllers/NotificationsController.cs @@ -9,16 +9,16 @@ namespace Bit.Hub { [Authorize("Internal")] [SelfHosted(SelfHostedOnly = true)] - public class NotificationController : Controller + public class NotificationsController : Controller { private readonly IHubContext _syncHubContext; - public NotificationController(IHubContext syncHubContext) + public NotificationsController(IHubContext syncHubContext) { _syncHubContext = syncHubContext; } - [HttpPost("~/notification")] + [HttpPost("~/notifications")] public async Task PostNotification([FromBody]PushNotificationData model) { await HubHelpers.SendNotificationToHubAsync(model, _syncHubContext); diff --git a/src/Hub/HubHelpers.cs b/src/Hub/HubHelpers.cs index 89f5e7415..0c265161c 100644 --- a/src/Hub/HubHelpers.cs +++ b/src/Hub/HubHelpers.cs @@ -1,8 +1,8 @@ -using System; -using System.Threading; +using System.Threading; using System.Threading.Tasks; using Bit.Core.Models; using Microsoft.AspNetCore.SignalR; +using Newtonsoft.Json; namespace Bit.Hub { @@ -17,8 +17,8 @@ namespace Bit.Hub case Core.Enums.PushType.SyncCipherCreate: case Core.Enums.PushType.SyncCipherDelete: case Core.Enums.PushType.SyncLoginDelete: - var cipherPayload = (SyncCipherPushNotification)Convert.ChangeType( - notification.Payload, typeof(SyncCipherPushNotification)); + var cipherPayload = JsonConvert.DeserializeObject( + JsonConvert.SerializeObject(notification.Payload)); if(cipherPayload.UserId.HasValue) { await hubContext.Clients.User(cipherPayload.UserId.ToString()) @@ -34,8 +34,8 @@ namespace Bit.Hub case Core.Enums.PushType.SyncFolderUpdate: case Core.Enums.PushType.SyncFolderCreate: case Core.Enums.PushType.SyncFolderDelete: - var folderPayload = (SyncFolderPushNotification)Convert.ChangeType( - notification.Payload, typeof(SyncFolderPushNotification)); + var folderPayload = JsonConvert.DeserializeObject( + JsonConvert.SerializeObject(notification.Payload)); await hubContext.Clients.User(folderPayload.UserId.ToString()) .SendAsync("ReceiveMessage", notification, cancellationToken); break; @@ -43,8 +43,8 @@ namespace Bit.Hub case Core.Enums.PushType.SyncVault: case Core.Enums.PushType.SyncOrgKeys: case Core.Enums.PushType.SyncSettings: - var userPayload = (SyncUserPushNotification)Convert.ChangeType( - notification.Payload, typeof(SyncUserPushNotification)); + var userPayload = JsonConvert.DeserializeObject( + JsonConvert.SerializeObject(notification.Payload)); await hubContext.Clients.User(userPayload.UserId.ToString()) .SendAsync("ReceiveMessage", notification, cancellationToken); break;