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

added push events and moved cipher writing to cipher service.

This commit is contained in:
Kyle Spearrin
2016-06-29 01:15:37 -04:00
parent afa37f5ab1
commit ef0a808687
8 changed files with 120 additions and 17 deletions

View File

@ -10,11 +10,40 @@ namespace Bit.Core.Services
public class CipherService : ICipherService
{
private readonly ICipherRepository _cipherRepository;
private readonly IPushService _pushService;
public CipherService(
ICipherRepository cipherRepository)
ICipherRepository cipherRepository,
IPushService pushService)
{
_cipherRepository = cipherRepository;
_pushService = pushService;
}
public async Task SaveAsync(Cipher cipher)
{
if(cipher.Id == default(Guid))
{
await _cipherRepository.CreateAsync(cipher);
// push
await _pushService.PushSyncCipherCreateAsync(cipher);
}
else
{
await _cipherRepository.ReplaceAsync(cipher);
// push
await _pushService.PushSyncCipherUpdateAsync(cipher);
}
}
public async Task DeleteAsync(Cipher cipher)
{
await _cipherRepository.DeleteAsync(cipher);
// push
await _pushService.PushSyncCipherDeleteAsync(cipher);
}
public async Task ImportCiphersAsync(
@ -46,6 +75,13 @@ namespace Bit.Core.Services
// create all the ciphers
await _cipherRepository.CreateAsync(ciphers);
// push
var userId = folders.FirstOrDefault()?.UserId ?? ciphers.FirstOrDefault()?.UserId;
if(userId.HasValue)
{
await _pushService.PushSyncCiphersAsync(userId.Value);
}
}
}
}

View File

@ -6,6 +6,8 @@ namespace Bit.Core.Services
{
public interface ICipherService
{
Task SaveAsync(Cipher cipher);
Task DeleteAsync(Cipher cipher);
Task ImportCiphersAsync(List<Cipher> folders, List<Cipher> ciphers, IEnumerable<KeyValuePair<int, int>> folderRelationships);
}
}

View File

@ -1,7 +1,14 @@
namespace Bit.Core.Services
using System;
using System.Threading.Tasks;
using Bit.Core.Domains;
namespace Bit.Core.Services
{
public interface IPushService
{
Task PushSyncCipherCreateAsync(Cipher cipher);
Task PushSyncCipherUpdateAsync(Cipher cipher);
Task PushSyncCipherDeleteAsync(Cipher cipher);
Task PushSyncCiphersAsync(Guid userId);
}
}

View File

@ -9,6 +9,8 @@ using PushSharp.Apple;
using Microsoft.AspNetCore.Hosting;
using PushSharp.Core;
using System.Security.Cryptography.X509Certificates;
using Bit.Core.Domains;
using Bit.Core.Enums;
namespace Bit.Core.Services
{
@ -29,6 +31,44 @@ namespace Bit.Core.Services
InitApnsBroker(globalSettings, hostingEnvironment);
}
public async Task PushSyncCipherCreateAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncCipherCreate);
}
public async Task PushSyncCipherUpdateAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncCipherUpdate);
}
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncCipherDelete);
}
private async Task PushCipherAsync(Cipher cipher, PushType type)
{
var message = new
{
Type = type,
Id = cipher.Id,
UserId = cipher.UserId
};
await PushToAllUserDevicesAsync(cipher.UserId, new JObject(message));
}
public async Task PushSyncCiphersAsync(Guid userId)
{
var message = new
{
Type = PushType.SyncCiphers,
UserId = userId
};
await PushToAllUserDevicesAsync(userId, new JObject(message));
}
private void InitGcmBroker(GlobalSettings globalSettings)
{
if(string.IsNullOrWhiteSpace(globalSettings.Push.GcmSenderId) || string.IsNullOrWhiteSpace(globalSettings.Push.GcmApiKey)
@ -115,7 +155,7 @@ namespace Bit.Core.Services
private void InitApnsBroker(GlobalSettings globalSettings, IHostingEnvironment hostingEnvironment)
{
if(string.IsNullOrWhiteSpace(globalSettings.Push.ApnsCertificatePassword)
if(string.IsNullOrWhiteSpace(globalSettings.Push.ApnsCertificatePassword)
|| string.IsNullOrWhiteSpace(globalSettings.Push.ApnsCertificateThumbprint))
{
return;
@ -192,7 +232,7 @@ namespace Bit.Core.Services
private async Task PushToAllUserDevicesAsync(Guid userId, JObject message)
{
var devices = (await _deviceRepository.GetManyByUserIdAsync(userId)).Where(d => d.PushToken != null);
var devices = (await _deviceRepository.GetManyByUserIdAsync(userId)).Where(d => !string.IsNullOrWhiteSpace(d.PushToken));
if(devices.Count() == 0)
{
return;
@ -201,7 +241,7 @@ namespace Bit.Core.Services
if(_apnsBroker != null)
{
// Send to each iOS device
foreach(var device in devices.Where(d => d.Type == Enums.DeviceType.iOS && d.PushToken != null))
foreach(var device in devices.Where(d => d.Type == DeviceType.iOS))
{
_apnsBroker.QueueNotification(new ApnsNotification
{
@ -212,11 +252,11 @@ namespace Bit.Core.Services
}
// Android can send to many devices at once
if(_gcmBroker != null && devices.Any(d => d.Type == Enums.DeviceType.Android))
if(_gcmBroker != null && devices.Any(d => d.Type == DeviceType.Android))
{
_gcmBroker.QueueNotification(new GcmNotification
{
RegistrationIds = devices.Where(d => d.Type == Enums.DeviceType.Android && d.PushToken != null)
RegistrationIds = devices.Where(d => d.Type == DeviceType.Android)
.Select(d => d.PushToken).ToList(),
Data = message
});