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

record installation devices

This commit is contained in:
Kyle Spearrin
2019-03-19 00:39:03 -04:00
parent 11fafa4a92
commit 01a293cf76
15 changed files with 255 additions and 36 deletions

View File

@ -8,15 +8,18 @@ using System.Net.Http;
using Bit.Core.Models.Api;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using Bit.Core.Repositories;
namespace Bit.Core.Services
{
public class RelayPushNotificationService : BaseIdentityClientService, IPushNotificationService
{
private readonly IDeviceRepository _deviceRepository;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILogger<RelayPushNotificationService> _logger;
public RelayPushNotificationService(
IDeviceRepository deviceRepository,
GlobalSettings globalSettings,
IHttpContextAccessor httpContextAccessor,
ILogger<RelayPushNotificationService> logger)
@ -28,6 +31,7 @@ namespace Bit.Core.Services
globalSettings.Installation.Key,
logger)
{
_deviceRepository = deviceRepository;
_httpContextAccessor = httpContextAccessor;
_logger = logger;
}
@ -143,12 +147,8 @@ namespace Bit.Core.Services
Type = type,
Payload = payload
};
if(excludeCurrentContext)
{
ExcludeCurrentContext(request);
}
await AddCurrentContextAsync(request, excludeCurrentContext);
await SendAsync(HttpMethod.Post, "push/send", request);
}
@ -161,30 +161,36 @@ namespace Bit.Core.Services
Payload = payload
};
if(excludeCurrentContext)
{
ExcludeCurrentContext(request);
}
await AddCurrentContextAsync(request, excludeCurrentContext);
await SendAsync(HttpMethod.Post, "push/send", request);
}
private void ExcludeCurrentContext(PushSendRequestModel request)
private async Task AddCurrentContextAsync(PushSendRequestModel request, bool addIdentifier)
{
var currentContext = _httpContextAccessor?.HttpContext?.
RequestServices.GetService(typeof(CurrentContext)) as CurrentContext;
RequestServices.GetService(typeof(CurrentContext)) as CurrentContext;
if(!string.IsNullOrWhiteSpace(currentContext?.DeviceIdentifier))
{
request.Identifier = currentContext.DeviceIdentifier;
var device = await _deviceRepository.GetByIdentifierAsync(currentContext.DeviceIdentifier);
if(device != null)
{
request.DeviceId = device.Id.ToString();
}
if(addIdentifier)
{
request.Identifier = currentContext.DeviceIdentifier;
}
}
}
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier)
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
string deviceId = null)
{
throw new NotImplementedException();
}
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier)
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
string deviceId = null)
{
throw new NotImplementedException();
}