2024-11-21 21:39:28 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
using System.Text.Json;
|
2020-01-10 08:33:13 -05:00
|
|
|
|
using Azure.Storage.Queues;
|
2023-04-14 13:25:56 -04:00
|
|
|
|
using Bit.Core.Auth.Entities;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
|
using Bit.Core.Models;
|
2024-10-21 14:58:57 +01:00
|
|
|
|
using Bit.Core.NotificationCenter.Entities;
|
2023-04-18 14:05:17 +02:00
|
|
|
|
using Bit.Core.Tools.Entities;
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using Bit.Core.Utilities;
|
2023-03-02 13:23:38 -05:00
|
|
|
|
using Bit.Core.Vault.Entities;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2024-10-23 14:15:10 +01:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public class AzureQueuePushNotificationService : IPushNotificationService
|
|
|
|
|
|
{
|
2020-01-10 08:33:13 -05:00
|
|
|
|
private readonly QueueClient _queueClient;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public AzureQueuePushNotificationService(
|
2024-10-23 14:15:10 +01:00
|
|
|
|
[FromKeyedServices("notifications")] QueueClient queueClient,
|
2018-08-21 09:29:38 -04:00
|
|
|
|
IHttpContextAccessor httpContextAccessor)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2024-10-23 14:15:10 +01:00
|
|
|
|
_queueClient = queueClient;
|
2018-08-21 09:29:38 -04:00
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public async Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncCipherCreate, collectionIds);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
|
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncCipherUpdate, collectionIds);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncLoginDelete, null);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 21:39:28 +00:00
|
|
|
|
private async Task PushCipherAsync(Cipher cipher, PushType type, IEnumerable<Guid>? collectionIds)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue)
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
var message = new SyncCipherPushNotification
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
OrganizationId = cipher.OrganizationId,
|
|
|
|
|
|
RevisionDate = cipher.RevisionDate,
|
|
|
|
|
|
CollectionIds = collectionIds,
|
2022-08-29 15:53:48 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await SendMessageAsync(type, message, true);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2018-08-02 17:23:37 -04:00
|
|
|
|
else if (cipher.UserId.HasValue)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
var message = new SyncCipherPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
UserId = cipher.UserId,
|
|
|
|
|
|
RevisionDate = cipher.RevisionDate,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, true);
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-08-02 17:23:37 -04:00
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncFolderCreateAsync(Folder folder)
|
|
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushFolderAsync(folder, PushType.SyncFolderCreate);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-28 08:22:49 -04:00
|
|
|
|
public async Task PushSyncFolderUpdateAsync(Folder folder)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushFolderAsync(folder, PushType.SyncFolderUpdate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncFolderDeleteAsync(Folder folder)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushFolderAsync(folder, PushType.SyncFolderDelete);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-28 08:22:49 -04:00
|
|
|
|
private async Task PushFolderAsync(Folder folder, PushType type)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
var message = new SyncFolderPushNotification
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
Id = folder.Id,
|
|
|
|
|
|
UserId = folder.UserId,
|
|
|
|
|
|
RevisionDate = folder.RevisionDate
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-01-22 16:16:40 -05:00
|
|
|
|
await SendMessageAsync(type, message, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncCiphersAsync(Guid userId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-01-22 16:16:40 -05:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncCiphers);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncVaultAsync(Guid userId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2021-01-22 16:16:40 -05:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncVault);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-14 04:15:07 +10:00
|
|
|
|
public async Task PushSyncOrganizationsAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushUserAsync(userId, PushType.SyncOrganizations);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-22 16:16:40 -05:00
|
|
|
|
public async Task PushSyncOrgKeysAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushUserAsync(userId, PushType.SyncOrgKeys);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-10 08:33:13 -05:00
|
|
|
|
public async Task PushSyncSettingsAsync(Guid userId)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2020-01-10 08:33:13 -05:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncSettings);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-24 11:25:16 -05:00
|
|
|
|
public async Task PushLogOutAsync(Guid userId, bool excludeCurrentContext = false)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2022-11-24 11:25:16 -05:00
|
|
|
|
await PushUserAsync(userId, PushType.LogOut, excludeCurrentContext);
|
2022-08-29 14:53:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-24 11:25:16 -05:00
|
|
|
|
private async Task PushUserAsync(Guid userId, PushType type, bool excludeCurrentContext = false)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2024-10-21 14:58:57 +01:00
|
|
|
|
var message = new UserPushNotification { UserId = userId, Date = DateTime.UtcNow };
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2022-11-24 11:25:16 -05:00
|
|
|
|
await SendMessageAsync(type, message, excludeCurrentContext);
|
2022-08-29 14:53:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-26 13:21:13 -04:00
|
|
|
|
public async Task PushAuthRequestAsync(AuthRequest authRequest)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushAuthRequestAsync(authRequest, PushType.AuthRequest);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushAuthRequestResponseAsync(AuthRequest authRequest)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushAuthRequestAsync(authRequest, PushType.AuthRequestResponse);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task PushAuthRequestAsync(AuthRequest authRequest, PushType type)
|
|
|
|
|
|
{
|
2024-10-21 14:58:57 +01:00
|
|
|
|
var message = new AuthRequestPushNotification { Id = authRequest.Id, UserId = authRequest.UserId };
|
2022-09-26 13:21:13 -04:00
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public async Task PushSyncSendCreateAsync(Send send)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
await PushSendAsync(send, PushType.SyncSendCreate);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public async Task PushSyncSendUpdateAsync(Send send)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
await PushSendAsync(send, PushType.SyncSendUpdate);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
public async Task PushSyncSendDeleteAsync(Send send)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
await PushSendAsync(send, PushType.SyncSendDelete);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 21:39:28 +00:00
|
|
|
|
public async Task PushSyncNotificationCreateAsync(Notification notification, NotificationStatus? notificationStatus)
|
2024-10-21 14:58:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
var message = new SyncNotificationPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = notification.Id,
|
2024-10-22 14:42:27 +01:00
|
|
|
|
UserId = notification.UserId,
|
|
|
|
|
|
OrganizationId = notification.OrganizationId,
|
2024-10-22 11:11:55 +01:00
|
|
|
|
ClientType = notification.ClientType,
|
2024-11-21 21:39:28 +00:00
|
|
|
|
RevisionDate = notification.RevisionDate,
|
|
|
|
|
|
ReadDate = notificationStatus?.ReadDate,
|
|
|
|
|
|
DeletedDate = notificationStatus?.DeletedDate
|
2024-10-21 14:58:57 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-11-21 21:39:28 +00:00
|
|
|
|
await SendMessageAsync(PushType.SyncNotificationCreate, message, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncNotificationUpdateAsync(Notification notification, NotificationStatus? notificationStatus)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = new SyncNotificationPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = notification.Id,
|
|
|
|
|
|
UserId = notification.UserId,
|
|
|
|
|
|
OrganizationId = notification.OrganizationId,
|
|
|
|
|
|
ClientType = notification.ClientType,
|
|
|
|
|
|
RevisionDate = notification.RevisionDate,
|
|
|
|
|
|
ReadDate = notificationStatus?.ReadDate,
|
|
|
|
|
|
DeletedDate = notificationStatus?.DeletedDate
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(PushType.SyncNotificationUpdate, message, true);
|
2024-10-21 14:58:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-02 17:23:37 -04:00
|
|
|
|
private async Task PushSendAsync(Send send, PushType type)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-02 17:23:37 -04:00
|
|
|
|
if (send.UserId.HasValue)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
var message = new SyncSendPushNotification
|
2018-08-02 17:23:37 -04:00
|
|
|
|
{
|
|
|
|
|
|
Id = send.Id,
|
2019-03-19 00:39:03 -04:00
|
|
|
|
UserId = send.UserId.Value,
|
2018-08-02 17:23:37 -04:00
|
|
|
|
RevisionDate = send.RevisionDate
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-02-04 12:54:21 -06:00
|
|
|
|
await SendMessageAsync(type, message, true);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-08-02 17:23:37 -04:00
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-03-19 00:39:03 -04:00
|
|
|
|
var contextId = GetContextIdentifier(excludeCurrentContext);
|
|
|
|
|
|
var message = JsonSerializer.Serialize(new PushNotificationData<T>(type, payload, contextId),
|
|
|
|
|
|
JsonHelpers.IgnoreWritingNull);
|
2018-08-02 21:03:04 -04:00
|
|
|
|
await _queueClient.SendMessageAsync(message);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2024-11-21 21:39:28 +00:00
|
|
|
|
private string? GetContextIdentifier(bool excludeCurrentContext)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-03-19 00:39:03 -04:00
|
|
|
|
if (!excludeCurrentContext)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2018-08-02 21:03:04 -04:00
|
|
|
|
return null;
|
2022-08-29 14:53:16 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2024-10-21 14:58:57 +01:00
|
|
|
|
var currentContext =
|
|
|
|
|
|
_httpContextAccessor?.HttpContext?.RequestServices.GetService(typeof(ICurrentContext)) as ICurrentContext;
|
2018-08-02 17:23:37 -04:00
|
|
|
|
return currentContext?.DeviceIdentifier;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
|
2024-11-21 21:39:28 +00:00
|
|
|
|
string? deviceId = null, ClientType? clientType = null)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Noop
|
2018-08-02 21:03:04 -04:00
|
|
|
|
return Task.FromResult(0);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
|
2024-11-21 21:39:28 +00:00
|
|
|
|
string? deviceId = null, ClientType? clientType = null)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Noop
|
2018-08-02 21:03:04 -04:00
|
|
|
|
return Task.FromResult(0);
|
2018-08-02 17:23:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|