1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

support message pack protocol for signalr

This commit is contained in:
Kyle Spearrin 2018-08-23 21:56:48 -04:00
parent d458d77511
commit 68c349f72f
6 changed files with 62 additions and 37 deletions

View File

@ -68,9 +68,11 @@ namespace Bit.Notifications
{
foreach(var message in messages)
{
var notificationJson = message.AsString;
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(
message.AsString);
await HubHelpers.SendNotificationToHubAsync(notification, _hubContext, cancellationToken);
notificationJson);
await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson,
_hubContext, cancellationToken);
await _queue.DeleteMessageAsync(message);
}
}

View File

@ -1,9 +1,12 @@
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Bit.Core.Models;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
namespace Bit.Notifications
{
@ -19,9 +22,17 @@ namespace Bit.Notifications
}
[HttpPost("~/notifications")]
public async Task PostNotification([FromBody]PushNotificationData<object> model)
public async Task PostNotification()
{
await HubHelpers.SendNotificationToHubAsync(model, _hubContext);
using(var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var notificationJson = await reader.ReadToEndAsync();
if(!string.IsNullOrWhiteSpace(notificationJson))
{
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(notificationJson);
await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson, _hubContext);
}
}
}
}
}

View File

@ -1,5 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Bit.Core.Enums;
using Bit.Core.Models;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
@ -8,45 +9,48 @@ namespace Bit.Notifications
{
public static class HubHelpers
{
public static async Task SendNotificationToHubAsync(PushNotificationData<object> notification,
public static async Task SendNotificationToHubAsync(PushType type, string notificationJson,
IHubContext<NotificationsHub> hubContext, CancellationToken cancellationToken = default(CancellationToken))
{
switch(notification.Type)
switch(type)
{
case Core.Enums.PushType.SyncCipherUpdate:
case Core.Enums.PushType.SyncCipherCreate:
case Core.Enums.PushType.SyncCipherDelete:
case Core.Enums.PushType.SyncLoginDelete:
var cipherPayload = JsonConvert.DeserializeObject<SyncCipherPushNotification>(
JsonConvert.SerializeObject(notification.Payload));
if(cipherPayload.UserId.HasValue)
case PushType.SyncCipherUpdate:
case PushType.SyncCipherCreate:
case PushType.SyncCipherDelete:
case PushType.SyncLoginDelete:
var cipherNotification =
JsonConvert.DeserializeObject<PushNotificationData<SyncCipherPushNotification>>(
notificationJson);
if(cipherNotification.Payload.UserId.HasValue)
{
await hubContext.Clients.User(cipherPayload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
await hubContext.Clients.User(cipherNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", cipherNotification, cancellationToken);
}
else if(cipherPayload.OrganizationId.HasValue)
else if(cipherNotification.Payload.OrganizationId.HasValue)
{
await hubContext.Clients.Group(
$"Organization_{cipherPayload.OrganizationId}")
.SendAsync("ReceiveMessage", notification, cancellationToken);
$"Organization_{cipherNotification.Payload.OrganizationId}")
.SendAsync("ReceiveMessage", cipherNotification, cancellationToken);
}
break;
case Core.Enums.PushType.SyncFolderUpdate:
case Core.Enums.PushType.SyncFolderCreate:
case Core.Enums.PushType.SyncFolderDelete:
var folderPayload = JsonConvert.DeserializeObject<SyncFolderPushNotification>(
JsonConvert.SerializeObject(notification.Payload));
await hubContext.Clients.User(folderPayload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
case PushType.SyncFolderUpdate:
case PushType.SyncFolderCreate:
case PushType.SyncFolderDelete:
var folderNotification =
JsonConvert.DeserializeObject<PushNotificationData<SyncFolderPushNotification>>(
notificationJson);
await hubContext.Clients.User(folderNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", folderNotification, cancellationToken);
break;
case Core.Enums.PushType.SyncCiphers:
case Core.Enums.PushType.SyncVault:
case Core.Enums.PushType.SyncOrgKeys:
case Core.Enums.PushType.SyncSettings:
var userPayload = JsonConvert.DeserializeObject<SyncUserPushNotification>(
JsonConvert.SerializeObject(notification.Payload));
await hubContext.Clients.User(userPayload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
case PushType.SyncCiphers:
case PushType.SyncVault:
case PushType.SyncOrgKeys:
case PushType.SyncSettings:
var userNotification =
JsonConvert.DeserializeObject<PushNotificationData<SyncUserPushNotification>>(
notificationJson);
await hubContext.Clients.User(userNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", userNotification, cancellationToken);
break;
default:
break;

View File

@ -21,7 +21,7 @@ namespace Bit.Notifications.Jobs
{
var everyFiveMinutesTrigger = TriggerBuilder.Create()
.StartNow()
.WithCronSchedule("0 */5 * * * ?")
.WithCronSchedule("0 */30 * * * ?")
.Build();
Jobs = new List<Tuple<Type, ITrigger>>

View File

@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.0.3" />
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.0.0-preview1-10015" />
</ItemGroup>

View File

@ -1,4 +1,5 @@
using Bit.Core;
using System.Collections.Generic;
using Bit.Core;
using Bit.Core.Utilities;
using IdentityModel;
using Microsoft.AspNetCore.Builder;
@ -54,7 +55,13 @@ namespace Bit.Notifications
}
else
{
services.AddSignalR();
services.AddSignalR().AddMessagePackProtocol(options =>
{
options.FormatterResolvers = new List<MessagePack.IFormatterResolver>()
{
MessagePack.Resolvers.ContractlessStandardResolver.Instance
};
});
}
services.AddSingleton<IUserIdProvider, SubjectUserIdProvider>();
services.AddSingleton<ConnectionCounter>();