2024-11-21 21:39:28 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
using Bit.Core.Auth.Entities;
|
2023-04-14 13:25:56 -04:00
|
|
|
|
using Bit.Core.Context;
|
2018-08-16 12:05:01 -04:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
|
using Bit.Core.Models;
|
2024-10-21 14:58:57 +01:00
|
|
|
|
using Bit.Core.NotificationCenter.Entities;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2023-04-18 14:05:17 +02:00
|
|
|
|
using Bit.Core.Tools.Entities;
|
2023-03-02 13:23:38 -05:00
|
|
|
|
using Bit.Core.Vault.Entities;
|
2018-08-16 12:05:01 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2018-08-16 12:05:01 -04:00
|
|
|
|
public class NotificationsApiPushNotificationService : BaseIdentityClientService, IPushNotificationService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2018-08-16 13:52:11 -04:00
|
|
|
|
public NotificationsApiPushNotificationService(
|
2022-05-10 17:12:09 -04:00
|
|
|
|
IHttpClientFactory httpFactory,
|
2018-08-16 12:05:01 -04:00
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2018-08-16 13:52:11 -04:00
|
|
|
|
ILogger<NotificationsApiPushNotificationService> logger)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
: base(
|
2022-05-10 17:12:09 -04:00
|
|
|
|
httpFactory,
|
|
|
|
|
|
globalSettings.BaseServiceUri.InternalNotifications,
|
|
|
|
|
|
globalSettings.BaseServiceUri.InternalIdentity,
|
|
|
|
|
|
"internal",
|
|
|
|
|
|
$"internal.{globalSettings.ProjectName}",
|
|
|
|
|
|
globalSettings.InternalIdentityKey,
|
|
|
|
|
|
logger)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public async Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncCipherCreate, collectionIds);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public async Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncCipherUpdate, collectionIds);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
|
|
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncLoginDelete, null);
|
2018-08-16 12:05:01 -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
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = new SyncCipherPushNotification
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
OrganizationId = cipher.OrganizationId,
|
|
|
|
|
|
RevisionDate = cipher.RevisionDate,
|
2022-06-13 13:50:44 -04:00
|
|
|
|
CollectionIds = collectionIds,
|
2018-08-16 12:05:01 -04:00
|
|
|
|
};
|
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-16 12:05:01 -04:00
|
|
|
|
else if (cipher.UserId.HasValue)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
var message = new SyncCipherPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
UserId = cipher.UserId,
|
|
|
|
|
|
RevisionDate = cipher.RevisionDate,
|
|
|
|
|
|
CollectionIds = collectionIds,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, true);
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-08-16 12:05:01 -04:00
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncFolderCreateAsync(Folder folder)
|
|
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushFolderAsync(folder, PushType.SyncFolderCreate);
|
2018-08-16 12:05:01 -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-16 12:05:01 -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-16 12:05:01 -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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-16 12:05:01 -04:00
|
|
|
|
public async Task PushSyncSettingsAsync(Guid userId)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncSettings);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-24 11:25:16 -05:00
|
|
|
|
public async Task PushLogOutAsync(Guid userId, bool excludeCurrentContext)
|
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-16 12:05:01 -04:00
|
|
|
|
public async Task PushSyncSendCreateAsync(Send send)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
await PushSendAsync(send, PushType.SyncSendCreate);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-16 12:05:01 -04:00
|
|
|
|
public async Task PushSyncSendUpdateAsync(Send send)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
await PushSendAsync(send, PushType.SyncSendUpdate);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-16 12:05:01 -04:00
|
|
|
|
public async Task PushSyncSendDeleteAsync(Send send)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
await PushSendAsync(send, PushType.SyncSendDelete);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 22:43:14 +00:00
|
|
|
|
public async Task PushSyncNotificationCreateAsync(Notification notification)
|
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 22:43:14 +00:00
|
|
|
|
RevisionDate = notification.RevisionDate
|
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-16 12:05:01 -04:00
|
|
|
|
private async Task PushSendAsync(Send send, PushType type)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-08-16 12:05:01 -04:00
|
|
|
|
if (send.UserId.HasValue)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
var message = new SyncSendPushNotification
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
Id = send.Id,
|
2019-03-19 00:39:03 -04:00
|
|
|
|
UserId = send.UserId.Value,
|
2018-08-16 12:05:01 -04:00
|
|
|
|
RevisionDate = send.RevisionDate
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-02-04 12:54:21 -06:00
|
|
|
|
await SendMessageAsync(type, message, false);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-08-16 12:05:01 -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 request = new PushNotificationData<T>(type, payload, contextId);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
await SendAsync(HttpMethod.Post, "send", request);
|
|
|
|
|
|
}
|
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-16 12:05:01 -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 =
|
2024-11-21 21:39:28 +00:00
|
|
|
|
_httpContextAccessor.HttpContext?.RequestServices.GetService(typeof(ICurrentContext)) as ICurrentContext;
|
2018-08-16 12:05:01 -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-16 12:05:01 -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-16 12:05:01 -04:00
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|