1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

update to new 2.0.0 preview for notification hub

This commit is contained in:
Kyle Spearrin 2018-08-06 09:04:31 -04:00
parent 58d29cc4a8
commit b2715503c3
5 changed files with 50 additions and 28 deletions

View File

@ -21,6 +21,7 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.1.1" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="2.0.0-preview2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" /> <PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" />
@ -44,10 +45,6 @@
<PackageReference Include="YubicoDotNetClient" Version="1.2.0" /> <PackageReference Include="YubicoDotNetClient" Version="1.2.0" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net471' ">
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="1.0.9" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' "> <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1' ">
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" /> <PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup> </ItemGroup>

View File

@ -30,9 +30,7 @@ namespace Bit.Core.Services
} }
else else
{ {
#if NET471
_services.Add(new NotificationHubPushNotificationService(globalSettings, httpContextAccessor)); _services.Add(new NotificationHubPushNotificationService(globalSettings, httpContextAccessor));
#endif
// _services.Add(new AzureQueuePushNotificationService(globalSettings, httpContextAccessor)); // _services.Add(new AzureQueuePushNotificationService(globalSettings, httpContextAccessor));
} }
} }

View File

@ -1,5 +1,4 @@
#if NET471 using System;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;
using Microsoft.Azure.NotificationHubs; using Microsoft.Azure.NotificationHubs;
@ -13,16 +12,17 @@ namespace Bit.Core.Services
{ {
public class NotificationHubPushNotificationService : IPushNotificationService public class NotificationHubPushNotificationService : IPushNotificationService
{ {
private readonly NotificationHubClient _client; private readonly GlobalSettings _globalSettings;
private readonly IHttpContextAccessor _httpContextAccessor; private readonly IHttpContextAccessor _httpContextAccessor;
private NotificationHubClient _client = null;
private DateTime? _clientExpires = null;
public NotificationHubPushNotificationService( public NotificationHubPushNotificationService(
GlobalSettings globalSettings, GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor) IHttpContextAccessor httpContextAccessor)
{ {
_client = NotificationHubClient.CreateClientFromConnectionString(globalSettings.NotificationHub.ConnectionString, _globalSettings = globalSettings;
globalSettings.NotificationHub.HubName);
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
} }
@ -170,13 +170,25 @@ namespace Bit.Core.Services
private async Task SendPayloadAsync(string tag, PushType type, object payload) private async Task SendPayloadAsync(string tag, PushType type, object payload)
{ {
await _client.SendTemplateNotificationAsync( await RenewClientAndExecuteAsync(async client => await client.SendTemplateNotificationAsync(
new Dictionary<string, string> new Dictionary<string, string>
{ {
{ "type", ((byte)type).ToString() }, { "type", ((byte)type).ToString() },
{ "payload", JsonConvert.SerializeObject(payload) } { "payload", JsonConvert.SerializeObject(payload) }
}, tag); }, tag));
}
private async Task RenewClientAndExecuteAsync(Func<NotificationHubClient, Task> task)
{
var now = DateTime.UtcNow;
if(_client == null || !_clientExpires.HasValue || _clientExpires.Value < now)
{
_clientExpires = now.Add(TimeSpan.FromMinutes(30));
_client = NotificationHubClient.CreateClientFromConnectionString(
_globalSettings.NotificationHub.ConnectionString,
_globalSettings.NotificationHub.HubName);
}
await task(_client);
} }
} }
} }
#endif

View File

@ -1,5 +1,4 @@
#if NET471 using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Azure.NotificationHubs; using Microsoft.Azure.NotificationHubs;
using Bit.Core.Enums; using Bit.Core.Enums;
@ -10,13 +9,15 @@ namespace Bit.Core.Services
{ {
public class NotificationHubPushRegistrationService : IPushRegistrationService public class NotificationHubPushRegistrationService : IPushRegistrationService
{ {
private readonly NotificationHubClient _client; private readonly GlobalSettings _globalSettings;
private NotificationHubClient _client = null;
private DateTime? _clientExpires = null;
public NotificationHubPushRegistrationService( public NotificationHubPushRegistrationService(
GlobalSettings globalSettings) GlobalSettings globalSettings)
{ {
_client = NotificationHubClient.CreateClientFromConnectionString(globalSettings.NotificationHub.ConnectionString, _globalSettings = globalSettings;
globalSettings.NotificationHub.HubName);
} }
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId, public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
@ -76,9 +77,11 @@ namespace Bit.Core.Services
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier); BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier);
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier); BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier);
BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate, userId, identifier); BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate,
userId, identifier);
await _client.CreateOrUpdateInstallationAsync(installation); await RenewClientAndExecuteAsync(async client =>
await client.CreateOrUpdateInstallationAsync(installation));
} }
private void BuildInstallationTemplate(Installation installation, string templateId, string templateBody, private void BuildInstallationTemplate(Installation installation, string templateId, string templateBody,
@ -113,7 +116,7 @@ namespace Bit.Core.Services
{ {
try try
{ {
await _client.DeleteInstallationAsync(deviceId); await RenewClientAndExecuteAsync(async client => await client.DeleteInstallationAsync(deviceId));
} }
catch(Exception e) catch(Exception e)
{ {
@ -135,7 +138,8 @@ namespace Bit.Core.Services
$"organizationId:{organizationId}"); $"organizationId:{organizationId}");
} }
private async Task PatchTagsForUserDevicesAsync(IEnumerable<string> deviceIds, UpdateOperationType op, string tag) private async Task PatchTagsForUserDevicesAsync(IEnumerable<string> deviceIds, UpdateOperationType op,
string tag)
{ {
if(!deviceIds.Any()) if(!deviceIds.Any())
{ {
@ -161,7 +165,8 @@ namespace Bit.Core.Services
{ {
try try
{ {
await _client.PatchInstallationAsync(id, new List<PartialUpdateOperation> { operation }); await RenewClientAndExecuteAsync(async client =>
await client.PatchInstallationAsync(id, new List<PartialUpdateOperation> { operation }));
} }
catch(Exception e) catch(Exception e)
{ {
@ -172,6 +177,18 @@ namespace Bit.Core.Services
} }
} }
} }
private async Task RenewClientAndExecuteAsync(Func<NotificationHubClient, Task> task)
{
var now = DateTime.UtcNow;
if(_client == null || !_clientExpires.HasValue || _clientExpires.Value < now)
{
_clientExpires = now.Add(TimeSpan.FromMinutes(30));
_client = NotificationHubClient.CreateClientFromConnectionString(
_globalSettings.NotificationHub.ConnectionString,
_globalSettings.NotificationHub.HubName);
}
await task(_client);
}
} }
} }
#endif

View File

@ -91,12 +91,10 @@ namespace Bit.Core.Utilities
{ {
services.AddSingleton<IPushRegistrationService, RelayPushRegistrationService>(); services.AddSingleton<IPushRegistrationService, RelayPushRegistrationService>();
} }
#if NET471
else if(!globalSettings.SelfHosted) else if(!globalSettings.SelfHosted)
{ {
services.AddSingleton<IPushRegistrationService, NotificationHubPushRegistrationService>(); services.AddSingleton<IPushRegistrationService, NotificationHubPushRegistrationService>();
} }
#endif
else else
{ {
services.AddSingleton<IPushRegistrationService, NoopPushRegistrationService>(); services.AddSingleton<IPushRegistrationService, NoopPushRegistrationService>();