Files
server/src/Core/Services/Implementations/MultiServicePushNotificationService.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

184 lines
6.1 KiB
C#
Raw Normal View History

#nullable enable
using Bit.Core.Auth.Entities;
2018-08-02 17:23:37 -04:00
using Bit.Core.Enums;
using Bit.Core.NotificationCenter.Entities;
using Bit.Core.Settings;
using Bit.Core.Tools.Entities;
using Bit.Core.Vault.Entities;
Shard notification hub (#4450) * Allow for binning of comb IDs by date and value * Introduce notification hub pool * Replace device type sharding with comb + range sharding * Fix proxy interface * Use enumerable services for multiServiceNotificationHub * Fix push interface usage * Fix push notification service dependencies * Fix push notification keys * Fixup documentation * Remove deprecated settings * Fix tests * PascalCase method names * Remove unused request model properties * Remove unused setting * Improve DateFromComb precision * Prefer readonly service enumerable * Pascal case template holes * Name TryParse methods TryParse * Apply suggestions from code review Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * AllClients is a set of clients and must be deduplicated * Fix registration start time * Add logging to initialization of a notification hub * more logging * Add lower level logging for hub settings * Log when connection is resolved * Improve log message * Log pushes to notification hub * temporarily elevate log messages for visibility * Log in multi-service when relaying to another push service * Revert to more reasonable logging free of user information * Fixup merge Deleting user was extracted to a command in #4803, this updates that work to use just the device ids as I did elsewhere in abd67e8ec * Do not use bouncy castle exception types * Add required services for logging --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
2024-10-22 09:20:57 -07:00
using Microsoft.Extensions.DependencyInjection;
2018-08-02 17:23:37 -04:00
using Microsoft.Extensions.Logging;
namespace Bit.Core.Services;
2022-08-29 16:06:55 -04:00
2018-08-02 17:23:37 -04:00
public class MultiServicePushNotificationService : IPushNotificationService
{
private readonly IEnumerable<IPushNotificationService>? _services;
2019-03-19 22:47:53 -04:00
private readonly ILogger<MultiServicePushNotificationService> _logger;
2022-08-29 16:06:55 -04:00
2018-08-02 17:23:37 -04:00
public MultiServicePushNotificationService(
Shard notification hub (#4450) * Allow for binning of comb IDs by date and value * Introduce notification hub pool * Replace device type sharding with comb + range sharding * Fix proxy interface * Use enumerable services for multiServiceNotificationHub * Fix push interface usage * Fix push notification service dependencies * Fix push notification keys * Fixup documentation * Remove deprecated settings * Fix tests * PascalCase method names * Remove unused request model properties * Remove unused setting * Improve DateFromComb precision * Prefer readonly service enumerable * Pascal case template holes * Name TryParse methods TryParse * Apply suggestions from code review Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * AllClients is a set of clients and must be deduplicated * Fix registration start time * Add logging to initialization of a notification hub * more logging * Add lower level logging for hub settings * Log when connection is resolved * Improve log message * Log pushes to notification hub * temporarily elevate log messages for visibility * Log in multi-service when relaying to another push service * Revert to more reasonable logging free of user information * Fixup merge Deleting user was extracted to a command in #4803, this updates that work to use just the device ids as I did elsewhere in abd67e8ec * Do not use bouncy castle exception types * Add required services for logging --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
2024-10-22 09:20:57 -07:00
[FromKeyedServices("implementation")] IEnumerable<IPushNotificationService> services,
2019-03-19 22:47:53 -04:00
ILogger<MultiServicePushNotificationService> logger,
Shard notification hub (#4450) * Allow for binning of comb IDs by date and value * Introduce notification hub pool * Replace device type sharding with comb + range sharding * Fix proxy interface * Use enumerable services for multiServiceNotificationHub * Fix push interface usage * Fix push notification service dependencies * Fix push notification keys * Fixup documentation * Remove deprecated settings * Fix tests * PascalCase method names * Remove unused request model properties * Remove unused setting * Improve DateFromComb precision * Prefer readonly service enumerable * Pascal case template holes * Name TryParse methods TryParse * Apply suggestions from code review Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * AllClients is a set of clients and must be deduplicated * Fix registration start time * Add logging to initialization of a notification hub * more logging * Add lower level logging for hub settings * Log when connection is resolved * Improve log message * Log pushes to notification hub * temporarily elevate log messages for visibility * Log in multi-service when relaying to another push service * Revert to more reasonable logging free of user information * Fixup merge Deleting user was extracted to a command in #4803, this updates that work to use just the device ids as I did elsewhere in abd67e8ec * Do not use bouncy castle exception types * Add required services for logging --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
2024-10-22 09:20:57 -07:00
GlobalSettings globalSettings)
2022-08-29 16:06:55 -04:00
{
Shard notification hub (#4450) * Allow for binning of comb IDs by date and value * Introduce notification hub pool * Replace device type sharding with comb + range sharding * Fix proxy interface * Use enumerable services for multiServiceNotificationHub * Fix push interface usage * Fix push notification service dependencies * Fix push notification keys * Fixup documentation * Remove deprecated settings * Fix tests * PascalCase method names * Remove unused request model properties * Remove unused setting * Improve DateFromComb precision * Prefer readonly service enumerable * Pascal case template holes * Name TryParse methods TryParse * Apply suggestions from code review Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * AllClients is a set of clients and must be deduplicated * Fix registration start time * Add logging to initialization of a notification hub * more logging * Add lower level logging for hub settings * Log when connection is resolved * Improve log message * Log pushes to notification hub * temporarily elevate log messages for visibility * Log in multi-service when relaying to another push service * Revert to more reasonable logging free of user information * Fixup merge Deleting user was extracted to a command in #4803, this updates that work to use just the device ids as I did elsewhere in abd67e8ec * Do not use bouncy castle exception types * Add required services for logging --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
2024-10-22 09:20:57 -07:00
_services = services;
2018-08-02 17:23:37 -04:00
_logger = logger;
Shard notification hub (#4450) * Allow for binning of comb IDs by date and value * Introduce notification hub pool * Replace device type sharding with comb + range sharding * Fix proxy interface * Use enumerable services for multiServiceNotificationHub * Fix push interface usage * Fix push notification service dependencies * Fix push notification keys * Fixup documentation * Remove deprecated settings * Fix tests * PascalCase method names * Remove unused request model properties * Remove unused setting * Improve DateFromComb precision * Prefer readonly service enumerable * Pascal case template holes * Name TryParse methods TryParse * Apply suggestions from code review Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * AllClients is a set of clients and must be deduplicated * Fix registration start time * Add logging to initialization of a notification hub * more logging * Add lower level logging for hub settings * Log when connection is resolved * Improve log message * Log pushes to notification hub * temporarily elevate log messages for visibility * Log in multi-service when relaying to another push service * Revert to more reasonable logging free of user information * Fixup merge Deleting user was extracted to a command in #4803, this updates that work to use just the device ids as I did elsewhere in abd67e8ec * Do not use bouncy castle exception types * Add required services for logging --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
2024-10-22 09:20:57 -07:00
_logger.LogInformation("Hub services: {Services}", _services.Count());
globalSettings.NotificationHubPool?.NotificationHubs?.ForEach(hub =>
Shard notification hub (#4450) * Allow for binning of comb IDs by date and value * Introduce notification hub pool * Replace device type sharding with comb + range sharding * Fix proxy interface * Use enumerable services for multiServiceNotificationHub * Fix push interface usage * Fix push notification service dependencies * Fix push notification keys * Fixup documentation * Remove deprecated settings * Fix tests * PascalCase method names * Remove unused request model properties * Remove unused setting * Improve DateFromComb precision * Prefer readonly service enumerable * Pascal case template holes * Name TryParse methods TryParse * Apply suggestions from code review Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> * AllClients is a set of clients and must be deduplicated * Fix registration start time * Add logging to initialization of a notification hub * more logging * Add lower level logging for hub settings * Log when connection is resolved * Improve log message * Log pushes to notification hub * temporarily elevate log messages for visibility * Log in multi-service when relaying to another push service * Revert to more reasonable logging free of user information * Fixup merge Deleting user was extracted to a command in #4803, this updates that work to use just the device ids as I did elsewhere in abd67e8ec * Do not use bouncy castle exception types * Add required services for logging --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com> Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
2024-10-22 09:20:57 -07:00
{
_logger.LogInformation("HubName: {HubName}, EnableSendTracing: {EnableSendTracing}, RegistrationStartDate: {RegistrationStartDate}, RegistrationEndDate: {RegistrationEndDate}", hub.HubName, hub.EnableSendTracing, hub.RegistrationStartDate, hub.RegistrationEndDate);
});
2018-08-02 17:23:37 -04:00
}
public Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
{
PushToServices((s) => s.PushSyncCipherCreateAsync(cipher, collectionIds));
return Task.FromResult(0);
}
public Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
{
PushToServices((s) => s.PushSyncCipherUpdateAsync(cipher, collectionIds));
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);
}
2018-08-28 08:22:49 -04:00
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 PushSyncOrganizationsAsync(Guid userId)
{
PushToServices((s) => s.PushSyncOrganizationsAsync(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 PushLogOutAsync(Guid userId, bool excludeCurrentContext = false)
2018-08-02 17:23:37 -04:00
{
PushToServices((s) => s.PushLogOutAsync(userId, excludeCurrentContext));
2018-08-02 21:03:04 -04:00
return Task.FromResult(0);
2018-08-02 17:23:37 -04:00
}
public Task PushSyncSendCreateAsync(Send send)
2022-08-29 14:53:16 -04:00
{
PushToServices((s) => s.PushSyncSendCreateAsync(send));
2018-08-02 17:23:37 -04:00
return Task.FromResult(0);
2022-08-29 14:53:16 -04:00
}
2019-03-19 00:39:03 -04:00
public Task PushSyncSendUpdateAsync(Send send)
2018-08-02 17:23:37 -04:00
{
2019-03-19 00:39:03 -04:00
PushToServices((s) => s.PushSyncSendUpdateAsync(send));
2018-08-02 21:03:04 -04:00
return Task.FromResult(0);
[SG-167] Implement Passwordless Authentication via Notifications (#2276) * [SG-549] Commit Initial AuthRequest Repository (#2174) * Model Passwordless * Scaffold database for Passwordless * Implement SQL Repository * [SG-167] Base Passwordless API (#2185) * Implement Passwordless notifications * Implement Controller * Add documentation to BaseRequestValidator * Register AuthRequestRepo * Remove ExpirationDate from the AuthRequest table * [SG-407] Create job to delete expired requests (#2187) * chore: init * remove exp date * fix: log name * [SG-167] Added fingerprint phrase to response model. (#2233) * Remove FailedLoginAttempt logic * Block unknown devices * Add EF Support for passwordless * Got SignalR working for responses * Added delete job method to EF repo * Implement a GetMany API endpoint for AuthRequests * Ran dotnet format * Fix a merge issues * Redated migration scripts * tried sorting sqlproj * Remove FailedLoginAttempts from SQL * Groom Postgres script * Remove extra commas from migration script * Correct isSpent() * [SG-167] Adde identity validation for passwordless requests. Registered IAuthRepository. * [SG-167] Added origin of the request to response model * Use display name for device identifier in response * Add datetime conversions back to postgres migration script * [SG-655] Add anonymous endpoint for checking if a device & user combo match * [review] Consolidate error conditions Co-authored-by: Brandon Maharaj <107377945+BrandonM-Bitwarden@users.noreply.github.com> Co-authored-by: André Filipe da Silva Bispo <andrefsbispo@hotmail.com> Co-authored-by: André Bispo <abispo@bitwarden.com>
2022-09-26 13:21:13 -04:00
}
public Task PushAuthRequestAsync(AuthRequest authRequest)
{
PushToServices((s) => s.PushAuthRequestAsync(authRequest));
return Task.FromResult(0);
}
public Task PushAuthRequestResponseAsync(AuthRequest authRequest)
{
PushToServices((s) => s.PushAuthRequestResponseAsync(authRequest));
return Task.FromResult(0);
2018-08-02 17:23:37 -04:00
}
public Task PushSyncSendDeleteAsync(Send send)
2022-08-29 16:06:55 -04:00
{
2018-08-02 17:23:37 -04:00
PushToServices((s) => s.PushSyncSendDeleteAsync(send));
return Task.FromResult(0);
2022-08-29 16:06:55 -04:00
}
public Task SendPayloadToInstallationAsync(string installationId, PushType type, object payload, string? identifier,
string? deviceId = null, ClientType? clientType = null)
{
PushToServices((s) =>
s.SendPayloadToInstallationAsync(installationId, type, payload, identifier, deviceId, clientType));
return Task.CompletedTask;
}
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string? identifier,
string? deviceId = null, ClientType? clientType = null)
2022-08-29 14:53:16 -04:00
{
PushToServices((s) => s.SendPayloadToUserAsync(userId, type, payload, identifier, deviceId, clientType));
2018-08-02 17:23:37 -04:00
return Task.FromResult(0);
2022-08-29 14:53:16 -04:00
}
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string? identifier,
string? deviceId = null, ClientType? clientType = null)
2022-08-29 16:06:55 -04:00
{
PushToServices((s) => s.SendPayloadToOrganizationAsync(orgId, type, payload, identifier, deviceId, clientType));
2018-08-02 17:23:37 -04:00
return Task.FromResult(0);
2022-08-29 16:06:55 -04:00
}
public Task PushSyncNotificationCreateAsync(Notification notification)
{
PushToServices((s) => s.PushSyncNotificationCreateAsync(notification));
return Task.CompletedTask;
}
public Task PushSyncNotificationUpdateAsync(Notification notification, NotificationStatus? notificationStatus)
{
PushToServices((s) => s.PushSyncNotificationUpdateAsync(notification, notificationStatus));
return Task.CompletedTask;
}
2018-08-02 17:23:37 -04:00
private void PushToServices(Func<IPushNotificationService, Task> pushFunc)
2022-08-29 16:06:55 -04:00
{
if (_services == null)
{
_logger.LogWarning("No services found to push notification");
return;
}
foreach (var service in _services)
2018-08-02 17:23:37 -04:00
{
_logger.LogDebug("Pushing notification to service {}", service.GetType().Name);
pushFunc(service);
2018-08-02 17:23:37 -04:00
}
}
}