mirror of
https://github.com/bitwarden/server.git
synced 2025-07-04 09:32:48 -05:00
azure queue notification service
This commit is contained in:
@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Enums;
|
||||
using Newtonsoft.Json;
|
||||
using Bit.Core.Models;
|
||||
using Microsoft.WindowsAzure.Storage.Queue;
|
||||
using Microsoft.WindowsAzure.Storage;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class AzureQueuePushNotificationService : IPushNotificationService
|
||||
{
|
||||
private readonly CloudQueue _queue;
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
|
||||
public AzureQueuePushNotificationService(
|
||||
GlobalSettings globalSettings,
|
||||
IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
var storageAccount = CloudStorageAccount.Parse(globalSettings.Events.ConnectionString);
|
||||
var queueClient = storageAccount.CreateCloudQueueClient();
|
||||
_queue = queueClient.GetQueueReference("sync");
|
||||
_globalSettings = globalSettings;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public async Task PushSyncCipherCreateAsync(Cipher cipher)
|
||||
{
|
||||
await PushCipherAsync(cipher, PushType.SyncCipherCreate);
|
||||
}
|
||||
|
||||
public async Task PushSyncCipherUpdateAsync(Cipher cipher)
|
||||
{
|
||||
await PushCipherAsync(cipher, PushType.SyncCipherUpdate);
|
||||
}
|
||||
|
||||
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
|
||||
{
|
||||
await PushCipherAsync(cipher, PushType.SyncLoginDelete);
|
||||
}
|
||||
|
||||
private async Task PushCipherAsync(Cipher cipher, PushType type)
|
||||
{
|
||||
if(cipher.OrganizationId.HasValue)
|
||||
{
|
||||
var message = new SyncCipherPushNotification
|
||||
{
|
||||
Id = cipher.Id,
|
||||
OrganizationId = cipher.OrganizationId,
|
||||
RevisionDate = cipher.RevisionDate,
|
||||
};
|
||||
|
||||
await SendMessageAsync(type, message, true);
|
||||
}
|
||||
else if(cipher.UserId.HasValue)
|
||||
{
|
||||
var message = new SyncCipherPushNotification
|
||||
{
|
||||
Id = cipher.Id,
|
||||
UserId = cipher.UserId,
|
||||
RevisionDate = cipher.RevisionDate,
|
||||
};
|
||||
|
||||
await SendMessageAsync(type, message, true);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task PushSyncFolderCreateAsync(Folder folder)
|
||||
{
|
||||
await PushFolderAsync(folder, PushType.SyncFolderCreate);
|
||||
}
|
||||
|
||||
public async Task PushSyncFolderUpdateAsync(Folder folder)
|
||||
{
|
||||
await PushFolderAsync(folder, PushType.SyncFolderUpdate);
|
||||
}
|
||||
|
||||
public async Task PushSyncFolderDeleteAsync(Folder folder)
|
||||
{
|
||||
await PushFolderAsync(folder, PushType.SyncFolderDelete);
|
||||
}
|
||||
|
||||
private async Task PushFolderAsync(Folder folder, PushType type)
|
||||
{
|
||||
var message = new SyncFolderPushNotification
|
||||
{
|
||||
Id = folder.Id,
|
||||
UserId = folder.UserId,
|
||||
RevisionDate = folder.RevisionDate
|
||||
};
|
||||
|
||||
await SendMessageAsync(type, message, true);
|
||||
}
|
||||
|
||||
public async Task PushSyncCiphersAsync(Guid userId)
|
||||
{
|
||||
await PushSyncUserAsync(userId, PushType.SyncCiphers);
|
||||
}
|
||||
|
||||
public async Task PushSyncVaultAsync(Guid userId)
|
||||
{
|
||||
await PushSyncUserAsync(userId, PushType.SyncVault);
|
||||
}
|
||||
|
||||
public async Task PushSyncOrgKeysAsync(Guid userId)
|
||||
{
|
||||
await PushSyncUserAsync(userId, PushType.SyncOrgKeys);
|
||||
}
|
||||
|
||||
public async Task PushSyncSettingsAsync(Guid userId)
|
||||
{
|
||||
await PushSyncUserAsync(userId, PushType.SyncSettings);
|
||||
}
|
||||
|
||||
private async Task PushSyncUserAsync(Guid userId, PushType type)
|
||||
{
|
||||
var message = new SyncUserPushNotification
|
||||
{
|
||||
UserId = userId,
|
||||
Date = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await SendMessageAsync(type, message, false);
|
||||
}
|
||||
|
||||
private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext)
|
||||
{
|
||||
var contextId = GetContextIdentifier(excludeCurrentContext);
|
||||
var message = JsonConvert.SerializeObject(new PushNotificationData<T>(type, payload, contextId),
|
||||
_jsonSettings);
|
||||
var queueMessage = new CloudQueueMessage(message);
|
||||
await _queue.AddMessageAsync(queueMessage);
|
||||
}
|
||||
|
||||
private string GetContextIdentifier(bool excludeCurrentContext)
|
||||
{
|
||||
if(!excludeCurrentContext)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var currentContext = _httpContextAccessor?.HttpContext?.
|
||||
RequestServices.GetService(typeof(CurrentContext)) as CurrentContext;
|
||||
return currentContext?.DeviceIdentifier;
|
||||
}
|
||||
|
||||
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Enums;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class MultiServicePushNotificationService : IPushNotificationService
|
||||
{
|
||||
private readonly List<IPushNotificationService> _services = new List<IPushNotificationService>();
|
||||
|
||||
public MultiServicePushNotificationService(
|
||||
GlobalSettings globalSettings,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
ILogger<RelayPushNotificationService> relayLogger)
|
||||
{
|
||||
if(globalSettings.SelfHosted)
|
||||
{
|
||||
if(CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
|
||||
globalSettings.Installation?.Id != null &&
|
||||
CoreHelpers.SettingHasValue(globalSettings.Installation?.Key))
|
||||
{
|
||||
_services.Add(new RelayPushNotificationService(globalSettings, httpContextAccessor, relayLogger));
|
||||
}
|
||||
// TODO: ApiPushNotificationService for SignalR
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NET471
|
||||
_services.Add(new NotificationHubPushNotificationService(globalSettings, httpContextAccessor));
|
||||
#endif
|
||||
_services.Add(new AzureQueuePushNotificationService(globalSettings, httpContextAccessor));
|
||||
}
|
||||
}
|
||||
|
||||
public Task PushSyncCipherCreateAsync(Cipher cipher)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncCipherCreateAsync(cipher));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncCipherUpdateAsync(Cipher cipher)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncCipherUpdateAsync(cipher));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncCipherDeleteAsync(Cipher cipher)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncCipherDeleteAsync(cipher));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncFolderCreateAsync(Folder folder)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncFolderCreateAsync(folder));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncFolderUpdateAsync(Folder folder)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncFolderUpdateAsync(folder));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncFolderDeleteAsync(Folder folder)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncFolderDeleteAsync(folder));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncCiphersAsync(Guid userId)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncCiphersAsync(userId));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncVaultAsync(Guid userId)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncVaultAsync(userId));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncOrgKeysAsync(Guid userId)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncOrgKeysAsync(userId));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task PushSyncSettingsAsync(Guid userId)
|
||||
{
|
||||
PushToServices((s) => s.PushSyncSettingsAsync(userId));
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private void PushToServices(Func<IPushNotificationService, Task> pushFunc)
|
||||
{
|
||||
if(_services != null)
|
||||
{
|
||||
foreach(var service in _services)
|
||||
{
|
||||
pushFunc(service);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user