1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 16:12:49 -05:00

notification hub push registration service

This commit is contained in:
Kyle Spearrin
2017-05-26 00:50:27 -04:00
parent e3cba6204b
commit c95d39f563
18 changed files with 163 additions and 11 deletions

View File

@ -8,11 +8,14 @@ namespace Bit.Core.Services
public class DeviceService : IDeviceService
{
private readonly IDeviceRepository _deviceRepository;
private readonly IPushRegistrationService _pushRegistrationService;
public DeviceService(
IDeviceRepository deviceRepository)
IDeviceRepository deviceRepository,
IPushRegistrationService pushRegistrationService)
{
_deviceRepository = deviceRepository;
_pushRegistrationService = pushRegistrationService;
}
public async Task SaveAsync(Device device)
@ -26,6 +29,20 @@ namespace Bit.Core.Services
device.RevisionDate = DateTime.UtcNow;
await _deviceRepository.ReplaceAsync(device);
}
await _pushRegistrationService.CreateOrUpdateRegistrationAsync(device);
}
public async Task ClearTokenAsync(Device device)
{
await _deviceRepository.ClearPushTokenByIdentifierAsync(device.Identifier);
await _pushRegistrationService.DeleteRegistrationAsync(device.Id);
}
public async Task DeleteAsync(Device device)
{
await _deviceRepository.DeleteAsync(device);
await _pushRegistrationService.DeleteRegistrationAsync(device.Id);
}
}
}

View File

@ -1,13 +0,0 @@
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public class NoopBlockIpService : IBlockIpService
{
public Task BlockIpAsync(string ipAddress, bool permanentBlock)
{
// Do nothing
return Task.FromResult(0);
}
}
}

View File

@ -1,50 +0,0 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
namespace Bit.Core.Services
{
public class NoopMailService : IMailService
{
public Task SendChangeEmailAlreadyExistsEmailAsync(string fromEmail, string toEmail)
{
return Task.FromResult(0);
}
public Task SendChangeEmailEmailAsync(string newEmailAddress, string token)
{
return Task.FromResult(0);
}
public Task SendMasterPasswordHintEmailAsync(string email, string hint)
{
return Task.FromResult(0);
}
public Task SendNoMasterPasswordHintEmailAsync(string email)
{
return Task.FromResult(0);
}
public Task SendOrganizationAcceptedEmailAsync(string organizationName, string userEmail, IEnumerable<string> adminEmails)
{
return Task.FromResult(0);
}
public Task SendOrganizationConfirmedEmailAsync(string organizationName, string email)
{
return Task.FromResult(0);
}
public Task SendOrganizationInviteEmailAsync(string organizationName, OrganizationUser orgUser, string token)
{
return Task.FromResult(0);
}
public Task SendWelcomeEmailAsync(User user)
{
return Task.FromResult(0);
}
}
}

View File

@ -1,59 +0,0 @@
using System;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
namespace Bit.Core.Services
{
public class NoopPushService : IPushService
{
public Task PushSyncCipherCreateAsync(Cipher cipher)
{
return Task.FromResult(0);
}
public Task PushSyncCipherDeleteAsync(Cipher cipher)
{
return Task.FromResult(0);
}
public Task PushSyncCiphersAsync(Guid userId)
{
return Task.FromResult(0);
}
public Task PushSyncCipherUpdateAsync(Cipher cipher)
{
return Task.FromResult(0);
}
public Task PushSyncFolderCreateAsync(Folder folder)
{
return Task.FromResult(0);
}
public Task PushSyncFolderDeleteAsync(Folder folder)
{
return Task.FromResult(0);
}
public Task PushSyncFolderUpdateAsync(Folder folder)
{
return Task.FromResult(0);
}
public Task PushSyncOrgKeysAsync(Guid userId)
{
return Task.FromResult(0);
}
public Task PushSyncSettingsAsync(Guid userId)
{
return Task.FromResult(0);
}
public Task PushSyncVaultAsync(Guid userId)
{
return Task.FromResult(0);
}
}
}

View File

@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.NotificationHubs;
using Bit.Core.Models.Table;
namespace Bit.Core.Services
{
public class NotificationHubPushRegistrationService : IPushRegistrationService
{
private readonly NotificationHubClient _client;
public NotificationHubPushRegistrationService(GlobalSettings globalSettings)
{
_client = NotificationHubClient.CreateClientFromConnectionString(globalSettings.NotificationHub.ConnectionString,
globalSettings.NotificationHub.HubName);
}
public async Task CreateOrUpdateRegistrationAsync(Device device)
{
if(string.IsNullOrWhiteSpace(device.PushToken))
{
return;
}
var installation = new Installation
{
InstallationId = device.Id.ToString(),
PushChannel = device.PushToken
};
installation.Tags = new List<string>
{
"userId:" + device.UserId.ToString()
};
if(!string.IsNullOrWhiteSpace(device.Identifier))
{
installation.Tags.Add("identifier:" + device.Identifier);
}
switch(device.Type)
{
case Enums.DeviceType.Android:
installation.Platform = NotificationPlatform.Gcm;
break;
case Enums.DeviceType.iOS:
installation.Platform = NotificationPlatform.Apns;
break;
case Enums.DeviceType.AndroidAmazon:
installation.Platform = NotificationPlatform.Adm;
break;
default:
break;
}
await _client.CreateOrUpdateInstallationAsync(installation);
}
public async Task DeleteRegistrationAsync(Guid deviceId)
{
await _client.DeleteInstallationAsync(deviceId.ToString());
}
}
}