1
0
mirror of https://github.com/bitwarden/server synced 2026-01-15 06:53:26 +00:00

hub api notifications

This commit is contained in:
Kyle Spearrin
2018-08-16 12:05:01 -04:00
parent ff01ce5ca7
commit 28e6783a00
22 changed files with 320 additions and 179 deletions

View File

@@ -70,49 +70,7 @@ namespace Bit.Hub
{
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(
message.AsString);
switch(notification.Type)
{
case Core.Enums.PushType.SyncCipherUpdate:
case Core.Enums.PushType.SyncCipherCreate:
case Core.Enums.PushType.SyncCipherDelete:
case Core.Enums.PushType.SyncLoginDelete:
var cipherNotification =
JsonConvert.DeserializeObject<PushNotificationData<SyncCipherPushNotification>>(
message.AsString);
if(cipherNotification.Payload.UserId.HasValue)
{
await _hubContext.Clients.User(cipherNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
}
else if(cipherNotification.Payload.OrganizationId.HasValue)
{
await _hubContext.Clients.Group(
$"Organization_{cipherNotification.Payload.OrganizationId}")
.SendAsync("ReceiveMessage", notification, cancellationToken);
}
break;
case Core.Enums.PushType.SyncFolderUpdate:
case Core.Enums.PushType.SyncFolderCreate:
case Core.Enums.PushType.SyncFolderDelete:
var folderNotification =
JsonConvert.DeserializeObject<PushNotificationData<SyncFolderPushNotification>>(
message.AsString);
await _hubContext.Clients.User(folderNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
break;
case Core.Enums.PushType.SyncCiphers:
case Core.Enums.PushType.SyncVault:
case Core.Enums.PushType.SyncOrgKeys:
case Core.Enums.PushType.SyncSettings:
var userNotification =
JsonConvert.DeserializeObject<PushNotificationData<SyncUserPushNotification>>(
message.AsString);
await _hubContext.Clients.User(userNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
break;
default:
break;
}
await HubHelpers.SendNotificationToHubAsync(notification, _hubContext, cancellationToken);
await _queue.DeleteMessageAsync(message);
}
}

View File

@@ -1,25 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
namespace Bit.Hub
{
[Authorize("Internal")]
public class EventsController : Controller
{
private readonly IHubContext<SyncHub> _syncHubContext;
public EventsController(IHubContext<SyncHub> syncHubContext)
{
_syncHubContext = syncHubContext;
}
[HttpGet("~/events")]
public async Task GetTest()
{
await _syncHubContext.Clients.All.SendAsync("ReceiveMessage", "From API.");
}
}
}

View File

@@ -0,0 +1,27 @@
using System.Threading.Tasks;
using Bit.Core.Models;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
namespace Bit.Hub
{
[Authorize("Internal")]
[SelfHosted(SelfHostedOnly = true)]
public class NotificationController : Controller
{
private readonly IHubContext<SyncHub> _syncHubContext;
public NotificationController(IHubContext<SyncHub> syncHubContext)
{
_syncHubContext = syncHubContext;
}
[HttpPost("~/notification")]
public async Task PostNotification([FromBody]PushNotificationData<object> model)
{
await HubHelpers.SendNotificationToHubAsync(model, _syncHubContext);
}
}
}

56
src/Hub/HubHelpers.cs Normal file
View File

@@ -0,0 +1,56 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Bit.Core.Models;
using Microsoft.AspNetCore.SignalR;
namespace Bit.Hub
{
public static class HubHelpers
{
public static async Task SendNotificationToHubAsync(PushNotificationData<object> notification,
IHubContext<SyncHub> hubContext, CancellationToken cancellationToken = default(CancellationToken))
{
switch(notification.Type)
{
case Core.Enums.PushType.SyncCipherUpdate:
case Core.Enums.PushType.SyncCipherCreate:
case Core.Enums.PushType.SyncCipherDelete:
case Core.Enums.PushType.SyncLoginDelete:
var cipherPayload = (SyncCipherPushNotification)Convert.ChangeType(
notification.Payload, typeof(SyncCipherPushNotification));
if(cipherPayload.UserId.HasValue)
{
await hubContext.Clients.User(cipherPayload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
}
else if(cipherPayload.OrganizationId.HasValue)
{
await hubContext.Clients.Group(
$"Organization_{cipherPayload.OrganizationId}")
.SendAsync("ReceiveMessage", notification, cancellationToken);
}
break;
case Core.Enums.PushType.SyncFolderUpdate:
case Core.Enums.PushType.SyncFolderCreate:
case Core.Enums.PushType.SyncFolderDelete:
var folderPayload = (SyncFolderPushNotification)Convert.ChangeType(
notification.Payload, typeof(SyncFolderPushNotification));
await hubContext.Clients.User(folderPayload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
break;
case Core.Enums.PushType.SyncCiphers:
case Core.Enums.PushType.SyncVault:
case Core.Enums.PushType.SyncOrgKeys:
case Core.Enums.PushType.SyncSettings:
var userPayload = (SyncUserPushNotification)Convert.ChangeType(
notification.Payload, typeof(SyncUserPushNotification));
await hubContext.Clients.User(userPayload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken);
break;
default:
break;
}
}
}
}

View File

@@ -5,6 +5,8 @@
"api": "https://api.bitwarden.com",
"identity": "https://identity.bitwarden.com",
"admin": "https://admin.bitwarden.com",
"hub": "https://hub.bitwarden.com",
"internalHub": "https://hub.bitwarden.com",
"internalAdmin": "https://admin.bitwarden.com",
"internalIdentity": "https://identity.bitwarden.com",
"internalApi": "https://api.bitwarden.com",

View File

@@ -7,6 +7,8 @@
"api": "http://localhost:4000",
"identity": "http://localhost:33656",
"admin": "http://localhost:62911",
"hub": "http://localhost:61840",
"internalHub": "http://localhost:61840",
"internalAdmin": "http://localhost:62911",
"internalIdentity": "http://localhost:33656",
"internalApi": "http://localhost:4000",