1
0
mirror of https://github.com/bitwarden/server synced 2026-01-05 18:13:31 +00:00

[PM-19659] Clean up Notifications code (#6244)

* Move PushType to Platform Folder

- Move the PushType next to the rest of push notification code
- Specifically exclude it from needing Platform code review
- Add tests establishing rules Platform has for usage of this enum, making it safe to have no owner

* Move NotificationHub code into Platform/Push directory

* Update NotificationHub namespace imports

* Add attribute for storing push type metadata

* Rename Push Engines to have PushEngine suffix

* Move Push Registration items to their own directory

* Push code move

* Add expected usage comment

* Add Push feature registration method

- Make method able to be called multipes times with no ill effects

* Add Push Registration service entrypoint and tests

* Use new service entrypoints

* Test changes
This commit is contained in:
Justin Baur
2025-08-26 13:30:37 -04:00
committed by GitHub
parent 7a63ae6315
commit e5159a3ba2
51 changed files with 849 additions and 205 deletions

View File

@@ -1,35 +0,0 @@
namespace Bit.Core.Enums;
public enum PushType : byte
{
SyncCipherUpdate = 0,
SyncCipherCreate = 1,
SyncLoginDelete = 2,
SyncFolderDelete = 3,
SyncCiphers = 4,
SyncVault = 5,
SyncOrgKeys = 6,
SyncFolderCreate = 7,
SyncFolderUpdate = 8,
SyncCipherDelete = 9,
SyncSettings = 10,
LogOut = 11,
SyncSendCreate = 12,
SyncSendUpdate = 13,
SyncSendDelete = 14,
AuthRequest = 15,
AuthRequestResponse = 16,
SyncOrganizations = 17,
SyncOrganizationStatusChanged = 18,
SyncOrganizationCollectionSettingChanged = 19,
Notification = 20,
NotificationStatus = 21,
RefreshSecurityTasks = 22
}

View File

@@ -1,9 +1,11 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.Enums;
using Bit.Core.NotificationCenter.Enums;
namespace Bit.Core.Models;
// New push notification payload models should not be defined in this file
// they should instead be defined in file owned by your team.
public class PushNotificationData<T>
{
public PushNotificationData(PushType type, T payload, string? contextId)

View File

@@ -1,5 +1,4 @@
#nullable enable
using System.Text.Json;
using System.Text.Json;
using Azure.Storage.Queues;
using Bit.Core.Context;
using Bit.Core.Enums;
@@ -13,17 +12,16 @@ using Microsoft.Extensions.Logging;
namespace Bit.Core.Platform.Push.Internal;
public class AzureQueuePushNotificationService : IPushEngine
public class AzureQueuePushEngine : IPushEngine
{
private readonly QueueClient _queueClient;
private readonly IHttpContextAccessor _httpContextAccessor;
public AzureQueuePushNotificationService(
public AzureQueuePushEngine(
[FromKeyedServices("notifications")] QueueClient queueClient,
IHttpContextAccessor httpContextAccessor,
IGlobalSettings globalSettings,
ILogger<AzureQueuePushNotificationService> logger,
TimeProvider timeProvider)
ILogger<AzureQueuePushEngine> logger)
{
_queueClient = queueClient;
_httpContextAccessor = httpContextAccessor;

View File

@@ -1,5 +1,4 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.Enums;
using Bit.Core.Settings;
using Bit.Core.Vault.Entities;
using Microsoft.Extensions.Logging;
@@ -8,7 +7,7 @@ namespace Bit.Core.Platform.Push.Internal;
public class MultiServicePushNotificationService : IPushNotificationService
{
private readonly IEnumerable<IPushEngine> _services;
private readonly IPushEngine[] _services;
public Guid InstallationId { get; }
@@ -22,7 +21,8 @@ public class MultiServicePushNotificationService : IPushNotificationService
GlobalSettings globalSettings,
TimeProvider timeProvider)
{
_services = services;
// Filter out any NoopPushEngine's
_services = [.. services.Where(engine => engine is not NoopPushEngine)];
Logger = logger;
Logger.LogInformation("Hub services: {Services}", _services.Count());

View File

@@ -1,10 +1,9 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.Enums;
using Bit.Core.Vault.Entities;
namespace Bit.Core.Platform.Push.Internal;
internal class NoopPushNotificationService : IPushEngine
internal class NoopPushEngine : IPushEngine
{
public Task PushCipherAsync(Cipher cipher, PushType pushType, IEnumerable<Guid>? collectionIds) => Task.CompletedTask;

View File

@@ -1,5 +1,4 @@
#nullable enable
using Bit.Core.Context;
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.Services;
@@ -8,23 +7,22 @@ using Bit.Core.Vault.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
// This service is not in the `Internal` namespace because it has direct external references.
namespace Bit.Core.Platform.Push;
namespace Bit.Core.Platform.Push.Internal;
/// <summary>
/// Sends non-mobile push notifications to the Azure Queue Api, later received by Notifications Api.
/// Used by Cloud-Hosted environments.
/// Received by AzureQueueHostedService message receiver in Notifications project.
/// </summary>
public class NotificationsApiPushNotificationService : BaseIdentityClientService, IPushEngine
public class NotificationsApiPushEngine : BaseIdentityClientService, IPushEngine
{
private readonly IHttpContextAccessor _httpContextAccessor;
public NotificationsApiPushNotificationService(
public NotificationsApiPushEngine(
IHttpClientFactory httpFactory,
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor,
ILogger<NotificationsApiPushNotificationService> logger)
ILogger<NotificationsApiPushEngine> logger)
: base(
httpFactory,
globalSettings.BaseServiceUri.InternalNotifications,

View File

@@ -1,5 +1,4 @@
#nullable enable
using Bit.Core.Context;
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.IdentityServer;
using Bit.Core.Models;
@@ -19,18 +18,18 @@ namespace Bit.Core.Platform.Push.Internal;
/// Used by Self-Hosted environments.
/// Received by PushController endpoint in Api project.
/// </summary>
public class RelayPushNotificationService : BaseIdentityClientService, IPushEngine
public class RelayPushEngine : BaseIdentityClientService, IPushEngine
{
private readonly IDeviceRepository _deviceRepository;
private readonly IHttpContextAccessor _httpContextAccessor;
public RelayPushNotificationService(
public RelayPushEngine(
IHttpClientFactory httpFactory,
IDeviceRepository deviceRepository,
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor,
ILogger<RelayPushNotificationService> logger)
ILogger<RelayPushEngine> logger)
: base(
httpFactory,
globalSettings.PushRelayBaseUri,

View File

@@ -1,8 +1,7 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.Enums;
using Bit.Core.Vault.Entities;
namespace Bit.Core.Platform.Push;
namespace Bit.Core.Platform.Push.Internal;
public interface IPushEngine
{

View File

@@ -1,5 +1,4 @@
#nullable enable
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Auth.Entities;
using Bit.Core.Enums;
using Bit.Core.Models;
@@ -10,10 +9,27 @@ using Microsoft.Extensions.Logging;
namespace Bit.Core.Platform.Push;
/// <summary>
/// Used to Push notifications to end-user devices.
/// </summary>
/// <remarks>
/// New notifications should not be wired up inside this service. You may either directly call the
/// <see cref="PushAsync"/> method in your service to send your notification or if you want your notification
/// sent by other teams you can make an extension method on this service with a well typed definition
/// of your notification. You may also make your own service that injects this and exposes methods for each of
/// your notifications.
/// </remarks>
public interface IPushNotificationService
{
private const string ServiceDeprecation = "Do not use the services exposed here, instead use your own services injected in your service.";
[Obsolete(ServiceDeprecation, DiagnosticId = "BWP0001")]
Guid InstallationId { get; }
[Obsolete(ServiceDeprecation, DiagnosticId = "BWP0001")]
TimeProvider TimeProvider { get; }
[Obsolete(ServiceDeprecation, DiagnosticId = "BWP0001")]
ILogger Logger { get; }
#region Legacy method, to be removed soon.
@@ -80,7 +96,9 @@ public interface IPushNotificationService
Payload = new UserPushNotification
{
UserId = userId,
#pragma warning disable BWP0001 // Type or member is obsolete
Date = TimeProvider.GetUtcNow().UtcDateTime,
#pragma warning restore BWP0001 // Type or member is obsolete
},
ExcludeCurrentContext = false,
});
@@ -94,7 +112,9 @@ public interface IPushNotificationService
Payload = new UserPushNotification
{
UserId = userId,
#pragma warning disable BWP0001 // Type or member is obsolete
Date = TimeProvider.GetUtcNow().UtcDateTime,
#pragma warning restore BWP0001 // Type or member is obsolete
},
ExcludeCurrentContext = false,
});
@@ -108,7 +128,9 @@ public interface IPushNotificationService
Payload = new UserPushNotification
{
UserId = userId,
#pragma warning disable BWP0001 // Type or member is obsolete
Date = TimeProvider.GetUtcNow().UtcDateTime,
#pragma warning restore BWP0001 // Type or member is obsolete
},
ExcludeCurrentContext = false,
});
@@ -122,7 +144,9 @@ public interface IPushNotificationService
Payload = new UserPushNotification
{
UserId = userId,
#pragma warning disable BWP0001 // Type or member is obsolete
Date = TimeProvider.GetUtcNow().UtcDateTime,
#pragma warning restore BWP0001 // Type or member is obsolete
},
ExcludeCurrentContext = false,
});
@@ -136,7 +160,9 @@ public interface IPushNotificationService
Payload = new UserPushNotification
{
UserId = userId,
#pragma warning disable BWP0001 // Type or member is obsolete
Date = TimeProvider.GetUtcNow().UtcDateTime,
#pragma warning restore BWP0001 // Type or member is obsolete
},
ExcludeCurrentContext = false,
});
@@ -150,7 +176,9 @@ public interface IPushNotificationService
Payload = new UserPushNotification
{
UserId = userId,
#pragma warning disable BWP0001 // Type or member is obsolete
Date = TimeProvider.GetUtcNow().UtcDateTime,
#pragma warning restore BWP0001 // Type or member is obsolete
},
ExcludeCurrentContext = excludeCurrentContextFromPush,
});
@@ -231,7 +259,9 @@ public interface IPushNotificationService
ClientType = notification.ClientType,
UserId = notification.UserId,
OrganizationId = notification.OrganizationId,
#pragma warning disable BWP0001 // Type or member is obsolete
InstallationId = notification.Global ? InstallationId : null,
#pragma warning restore BWP0001 // Type or member is obsolete
TaskId = notification.TaskId,
Title = notification.Title,
Body = notification.Body,
@@ -246,7 +276,9 @@ public interface IPushNotificationService
{
// TODO: Think about this a bit more
target = NotificationTarget.Installation;
#pragma warning disable BWP0001 // Type or member is obsolete
targetId = InstallationId;
#pragma warning restore BWP0001 // Type or member is obsolete
}
else if (notification.UserId.HasValue)
{
@@ -260,7 +292,9 @@ public interface IPushNotificationService
}
else
{
#pragma warning disable BWP0001 // Type or member is obsolete
Logger.LogWarning("Invalid notification id {NotificationId} push notification", notification.Id);
#pragma warning restore BWP0001 // Type or member is obsolete
return Task.CompletedTask;
}
@@ -285,7 +319,9 @@ public interface IPushNotificationService
ClientType = notification.ClientType,
UserId = notification.UserId,
OrganizationId = notification.OrganizationId,
#pragma warning disable BWP0001 // Type or member is obsolete
InstallationId = notification.Global ? InstallationId : null,
#pragma warning restore BWP0001 // Type or member is obsolete
TaskId = notification.TaskId,
Title = notification.Title,
Body = notification.Body,
@@ -302,7 +338,9 @@ public interface IPushNotificationService
{
// TODO: Think about this a bit more
target = NotificationTarget.Installation;
#pragma warning disable BWP0001 // Type or member is obsolete
targetId = InstallationId;
#pragma warning restore BWP0001 // Type or member is obsolete
}
else if (notification.UserId.HasValue)
{
@@ -316,7 +354,9 @@ public interface IPushNotificationService
}
else
{
#pragma warning disable BWP0001 // Type or member is obsolete
Logger.LogWarning("Invalid notification status id {NotificationId} push notification", notification.Id);
#pragma warning restore BWP0001 // Type or member is obsolete
return Task.CompletedTask;
}
@@ -398,7 +438,9 @@ public interface IPushNotificationService
Payload = new UserPushNotification
{
UserId = userId,
#pragma warning disable BWP0001 // Type or member is obsolete
Date = TimeProvider.GetUtcNow().UtcDateTime,
#pragma warning restore BWP0001 // Type or member is obsolete
},
ExcludeCurrentContext = false,
});
@@ -406,6 +448,12 @@ public interface IPushNotificationService
Task PushCipherAsync(Cipher cipher, PushType pushType, IEnumerable<Guid>? collectionIds);
/// <summary>
/// Pushes a notification to devices based on the settings given to us in <see cref="PushNotification{T}"/>.
/// </summary>
/// <typeparam name="T">The type of the payload to be sent along with the notification.</typeparam>
/// <param name="pushNotification"></param>
/// <returns>A task that is NOT guarunteed to have sent the notification by the time the task resolves.</returns>
Task PushAsync<T>(PushNotification<T> pushNotification)
where T : class;
}

View File

@@ -1,6 +1,4 @@
#nullable enable
using System.Text.Json;
using System.Text.Json;
using Bit.Core.Enums;
namespace Bit.Core.Platform.Push.Internal;

View File

@@ -1,8 +1,6 @@
using Microsoft.Azure.NotificationHubs;
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.Push.Internal;
public interface INotificationHubProxy
{

View File

@@ -1,8 +1,6 @@
using Microsoft.Azure.NotificationHubs;
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.Push.Internal;
public interface INotificationHubPool
{

View File

@@ -1,8 +1,6 @@
using Microsoft.Azure.NotificationHubs;
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.Push.Internal;
public class NotificationHubClientProxy : INotificationHubProxy
{

View File

@@ -6,9 +6,7 @@ using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.Azure.NotificationHubs;
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.Push.Internal;
public class NotificationHubConnection
{

View File

@@ -3,9 +3,7 @@ using Bit.Core.Utilities;
using Microsoft.Azure.NotificationHubs;
using Microsoft.Extensions.Logging;
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.Push.Internal;
public class NotificationHubPool : INotificationHubPool
{

View File

@@ -1,28 +1,23 @@
#nullable enable
using System.Text.Json;
using System.Text.Json;
using System.Text.RegularExpressions;
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.Models;
using Bit.Core.Models.Data;
using Bit.Core.Platform.Push;
using Bit.Core.Platform.Push.Internal;
using Bit.Core.Repositories;
using Bit.Core.Settings;
using Bit.Core.Vault.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.Push.Internal;
/// <summary>
/// Sends mobile push notifications to the Azure Notification Hub.
/// Used by Cloud-Hosted environments.
/// Received by Firebase for Android or APNS for iOS.
/// </summary>
public class NotificationHubPushNotificationService : IPushEngine, IPushRelayer
public class NotificationHubPushEngine : IPushEngine, IPushRelayer
{
private readonly IInstallationDeviceRepository _installationDeviceRepository;
private readonly IHttpContextAccessor _httpContextAccessor;
@@ -30,11 +25,11 @@ public class NotificationHubPushNotificationService : IPushEngine, IPushRelayer
private readonly INotificationHubPool _notificationHubPool;
private readonly ILogger _logger;
public NotificationHubPushNotificationService(
public NotificationHubPushEngine(
IInstallationDeviceRepository installationDeviceRepository,
INotificationHubPool notificationHubPool,
IHttpContextAccessor httpContextAccessor,
ILogger<NotificationHubPushNotificationService> logger,
ILogger<NotificationHubPushEngine> logger,
IGlobalSettings globalSettings)
{
_installationDeviceRepository = installationDeviceRepository;

View File

@@ -0,0 +1,44 @@
using Bit.Core.Enums;
namespace Bit.Core.Platform.Push;
/// <summary>
/// Used to annotate information about a given <see cref="PushType"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class NotificationInfoAttribute : Attribute
{
// Once upon a time we can feed this information into a C# analyzer to make sure that we validate
// the callsites of IPushNotificationService.PushAsync uses the correct payload type for the notification type
// for now this only exists as forced documentation to teams who create a push type.
// It's especially on purpose that we allow ourselves to take a type name via just the string,
// this allows teams to make a push type that is only sent with a payload that exists in a separate assembly than
// this one.
public NotificationInfoAttribute(string team, Type payloadType)
// It should be impossible to reference an unnamed type for an attributes constructor so this assertion should be safe.
: this(team, payloadType.FullName!)
{
Team = team;
}
public NotificationInfoAttribute(string team, string payloadTypeName)
{
ArgumentException.ThrowIfNullOrWhiteSpace(team);
ArgumentException.ThrowIfNullOrWhiteSpace(payloadTypeName);
Team = team;
PayloadTypeName = payloadTypeName;
}
/// <summary>
/// The name of the team that owns this <see cref="PushType"/>.
/// </summary>
public string Team { get; }
/// <summary>
/// The fully qualified type name of the payload that should be used when sending a notification of this type.
/// </summary>
public string PayloadTypeName { get; }
}

View File

@@ -6,6 +6,9 @@ namespace Bit.Core.Platform.Push;
/// <summary>
/// Contains constants for all the available targets for a given notification.
/// </summary>
/// <remarks>
/// Please reach out to the Platform team if you need a new target added.
/// </remarks>
public enum NotificationTarget
{
/// <summary>

View File

@@ -0,0 +1,82 @@
using Azure.Storage.Queues;
using Bit.Core.Platform.Push;
using Bit.Core.Platform.Push.Internal;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Extension methods for adding the Push feature.
/// </summary>
public static class PushServiceCollectionExtensions
{
/// <summary>
/// Adds a <see cref="IPushNotificationService"/> to the services that can be used to send push notifications to
/// end user devices. This method is safe to be ran multiple time provided <see cref="GlobalSettings"/> does not
/// change between calls.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <param name="globalSettings">The <see cref="GlobalSettings"/> to use to configure services.</param>
/// <returns>The <see cref="IServiceCollection"/> for additional chaining.</returns>
public static IServiceCollection AddPush(this IServiceCollection services, GlobalSettings globalSettings)
{
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(globalSettings);
services.TryAddSingleton(TimeProvider.System);
services.TryAddSingleton<IPushNotificationService, MultiServicePushNotificationService>();
if (globalSettings.SelfHosted)
{
if (globalSettings.Installation.Id == Guid.Empty)
{
throw new InvalidOperationException("Installation Id must be set for self-hosted installations.");
}
if (CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
CoreHelpers.SettingHasValue(globalSettings.Installation.Key))
{
// TODO: We should really define the HttpClient we will use here
services.AddHttpClient();
services.AddHttpContextAccessor();
// We also depend on IDeviceRepository but don't explicitly add it right now.
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPushEngine, RelayPushEngine>());
}
if (CoreHelpers.SettingHasValue(globalSettings.InternalIdentityKey) &&
CoreHelpers.SettingHasValue(globalSettings.BaseServiceUri.InternalNotifications))
{
// TODO: We should really define the HttpClient we will use here
services.AddHttpClient();
services.AddHttpContextAccessor();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPushEngine, NotificationsApiPushEngine>());
}
}
else
{
services.TryAddSingleton<INotificationHubPool, NotificationHubPool>();
services.AddHttpContextAccessor();
// We also depend on IInstallationDeviceRepository but don't explicitly add it right now.
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPushEngine, NotificationHubPushEngine>());
services.TryAddSingleton<IPushRelayer, NotificationHubPushEngine>();
if (CoreHelpers.SettingHasValue(globalSettings.Notifications?.ConnectionString))
{
services.TryAddKeyedSingleton("notifications", static (sp, _) =>
{
var gs = sp.GetRequiredService<GlobalSettings>();
return new QueueClient(gs.Notifications.ConnectionString, "notifications");
});
// We not IHttpContextAccessor will be added above, no need to do it here.
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPushEngine, AzureQueuePushEngine>());
}
}
return services;
}
}

View File

@@ -0,0 +1,93 @@
using Bit.Core.Platform.Push;
// TODO: This namespace should change to `Bit.Core.Platform.Push`
namespace Bit.Core.Enums;
/// <summary>
///
/// </summary>
/// <remarks>
/// <para>
/// When adding a new enum member you must annotate it with a <see cref="NotificationInfoAttribute"/>
/// this is enforced with a unit test. It is preferred that you do NOT add new usings for the type referenced
/// in <see cref="NotificationInfoAttribute"/>.
/// </para>
/// <para>
/// You may and are
/// </para>
/// </remarks>
public enum PushType : byte
{
// When adding a new enum member you must annotate it with a NotificationInfoAttribute this is enforced with a unit
// test. It is preferred that you do NOT add new usings for the type referenced for the payload. You are also
// encouraged to define the payload type in your own teams owned code.
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.SyncCipherPushNotification))]
SyncCipherUpdate = 0,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.SyncCipherPushNotification))]
SyncCipherCreate = 1,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.SyncCipherPushNotification))]
SyncLoginDelete = 2,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.SyncFolderPushNotification))]
SyncFolderDelete = 3,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.UserPushNotification))]
SyncCiphers = 4,
[NotificationInfo("not-specified", typeof(Models.UserPushNotification))]
SyncVault = 5,
[NotificationInfo("@bitwarden/team-admin-console-dev", typeof(Models.UserPushNotification))]
SyncOrgKeys = 6,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.SyncFolderPushNotification))]
SyncFolderCreate = 7,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.SyncFolderPushNotification))]
SyncFolderUpdate = 8,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.SyncCipherPushNotification))]
SyncCipherDelete = 9,
[NotificationInfo("not-specified", typeof(Models.UserPushNotification))]
SyncSettings = 10,
[NotificationInfo("not-specified", typeof(Models.UserPushNotification))]
LogOut = 11,
[NotificationInfo("@bitwarden/team-tools-dev", typeof(Models.SyncSendPushNotification))]
SyncSendCreate = 12,
[NotificationInfo("@bitwarden/team-tools-dev", typeof(Models.SyncSendPushNotification))]
SyncSendUpdate = 13,
[NotificationInfo("@bitwarden/team-tools-dev", typeof(Models.SyncSendPushNotification))]
SyncSendDelete = 14,
[NotificationInfo("@bitwarden/team-auth-dev", typeof(Models.AuthRequestPushNotification))]
AuthRequest = 15,
[NotificationInfo("@bitwarden/team-auth-dev", typeof(Models.AuthRequestPushNotification))]
AuthRequestResponse = 16,
[NotificationInfo("not-specified", typeof(Models.UserPushNotification))]
SyncOrganizations = 17,
[NotificationInfo("@bitwarden/team-billing-dev", typeof(Models.OrganizationStatusPushNotification))]
SyncOrganizationStatusChanged = 18,
[NotificationInfo("@bitwarden/team-admin-console-dev", typeof(Models.OrganizationCollectionManagementPushNotification))]
SyncOrganizationCollectionSettingChanged = 19,
[NotificationInfo("not-specified", typeof(Models.NotificationPushNotification))]
Notification = 20,
[NotificationInfo("not-specified", typeof(Models.NotificationPushNotification))]
NotificationStatus = 21,
[NotificationInfo("@bitwarden/team-vault-dev", typeof(Models.UserPushNotification))]
RefreshSecurityTasks = 22,
}

View File

@@ -1,10 +1,10 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.NotificationHub;
using Bit.Core.Enums;
using Bit.Core.Platform.PushRegistration;
// TODO: Change this namespace to `Bit.Core.Platform.PushRegistration
namespace Bit.Core.Platform.Push;
public interface IPushRegistrationService
{
Task CreateOrUpdateRegistrationAsync(PushRegistrationData data, string deviceId, string userId, string identifier, DeviceType type, IEnumerable<string> organizationIds, Guid installationId);

View File

@@ -1,9 +1,7 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.Platform.Push;
using Bit.Core.Enums;
using Bit.Core.NotificationHub;
namespace Bit.Core.Platform.Push.Internal;
namespace Bit.Core.Platform.PushRegistration.Internal;
public class NoopPushRegistrationService : IPushRegistrationService
{

View File

@@ -6,14 +6,13 @@ using System.Text.Json;
using Bit.Core.Enums;
using Bit.Core.Models.Data;
using Bit.Core.Platform.Push;
using Bit.Core.Platform.Push.Internal;
using Bit.Core.Repositories;
using Bit.Core.Utilities;
using Microsoft.Azure.NotificationHubs;
using Microsoft.Extensions.Logging;
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.PushRegistration.Internal;
public class NotificationHubPushRegistrationService : IPushRegistrationService
{

View File

@@ -1,6 +1,4 @@
namespace Bit.Core.NotificationHub;
#nullable enable
namespace Bit.Core.Platform.PushRegistration;
public record struct WebPushRegistrationData
{

View File

@@ -0,0 +1,54 @@
using Bit.Core.Platform.Push;
using Bit.Core.Platform.Push.Internal;
using Bit.Core.Platform.PushRegistration.Internal;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Extension methods for adding the Push Registration feature.
/// </summary>
public static class PushRegistrationServiceCollectionExtensions
{
/// <summary>
/// Adds a <see cref="IPushRegistrationService"/> to the service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> for chaining.</returns>
public static IServiceCollection AddPushRegistration(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
// TODO: Should add feature that brings in IInstallationDeviceRepository once that is featurized
// Register all possible variants under there concrete type.
services.TryAddSingleton<RelayPushRegistrationService>();
services.TryAddSingleton<NoopPushRegistrationService>();
services.AddHttpClient();
services.TryAddSingleton<INotificationHubPool, NotificationHubPool>();
services.TryAddSingleton<NotificationHubPushRegistrationService>();
services.TryAddSingleton<IPushRegistrationService>(static sp =>
{
var globalSettings = sp.GetRequiredService<GlobalSettings>();
if (globalSettings.SelfHosted)
{
if (CoreHelpers.SettingHasValue(globalSettings.PushRelayBaseUri) &&
CoreHelpers.SettingHasValue(globalSettings.Installation.Key))
{
return sp.GetRequiredService<RelayPushRegistrationService>();
}
return sp.GetRequiredService<NoopPushRegistrationService>();
}
return sp.GetRequiredService<NotificationHubPushRegistrationService>();
});
return services;
}
}

View File

@@ -1,14 +1,12 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.Enums;
using Bit.Core.IdentityServer;
using Bit.Core.Models.Api;
using Bit.Core.NotificationHub;
using Bit.Core.Platform.Push;
using Bit.Core.Services;
using Bit.Core.Settings;
using Microsoft.Extensions.Logging;
namespace Bit.Core.Platform.Push.Internal;
namespace Bit.Core.Platform.PushRegistration.Internal;
public class RelayPushRegistrationService : BaseIdentityClientService, IPushRegistrationService
{

View File

@@ -1,6 +1,6 @@
using Bit.Core.Auth.Models.Api.Request;
using Bit.Core.Entities;
using Bit.Core.NotificationHub;
using Bit.Core.Platform.PushRegistration;
namespace Bit.Core.Services;

View File

@@ -3,8 +3,8 @@ using Bit.Core.Auth.Utilities;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.NotificationHub;
using Bit.Core.Platform.Push;
using Bit.Core.Platform.PushRegistration;
using Bit.Core.Repositories;
using Bit.Core.Settings;