mirror of
https://github.com/bitwarden/server.git
synced 2025-04-24 06:25:09 -05:00
143 lines
4.3 KiB
C#
143 lines
4.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System.Net.Http;
|
|
using System;
|
|
using Bit.Core.Models.Api;
|
|
using Bit.Core.Enums;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Bit.Core.Services
|
|
{
|
|
public class RelayPushRegistrationService : BaseRelayPushNotificationService, IPushRegistrationService
|
|
{
|
|
private readonly ILogger<RelayPushRegistrationService> _logger;
|
|
|
|
public RelayPushRegistrationService(
|
|
GlobalSettings globalSettings,
|
|
ILogger<RelayPushRegistrationService> logger)
|
|
: base(globalSettings, logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
|
string identifier, DeviceType type)
|
|
{
|
|
var tokenStateResponse = await HandleTokenStateAsync();
|
|
if(!tokenStateResponse)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var requestModel = new PushRegistrationRequestModel
|
|
{
|
|
DeviceId = deviceId,
|
|
Identifier = identifier,
|
|
PushToken = pushToken,
|
|
Type = type,
|
|
UserId = userId
|
|
};
|
|
|
|
var message = new TokenHttpRequestMessage(requestModel, AccessToken)
|
|
{
|
|
Method = HttpMethod.Post,
|
|
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/register"))
|
|
};
|
|
|
|
try
|
|
{
|
|
await PushClient.SendAsync(message);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
_logger.LogError(12335, e, "Unable to create push registration.");
|
|
}
|
|
}
|
|
|
|
public async Task DeleteRegistrationAsync(string deviceId)
|
|
{
|
|
var tokenStateResponse = await HandleTokenStateAsync();
|
|
if(!tokenStateResponse)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var message = new TokenHttpRequestMessage(AccessToken)
|
|
{
|
|
Method = HttpMethod.Delete,
|
|
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/", deviceId))
|
|
};
|
|
|
|
try
|
|
{
|
|
await PushClient.SendAsync(message);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
_logger.LogError(12336, e, "Unable to delete push registration.");
|
|
}
|
|
}
|
|
|
|
public async Task AddUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
|
|
{
|
|
if(!deviceIds.Any())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var tokenStateResponse = await HandleTokenStateAsync();
|
|
if(!tokenStateResponse)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var requestModel = new PushUpdateRequestModel(deviceIds, organizationId);
|
|
var message = new TokenHttpRequestMessage(requestModel, AccessToken)
|
|
{
|
|
Method = HttpMethod.Put,
|
|
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/add-organization"))
|
|
};
|
|
|
|
try
|
|
{
|
|
await PushClient.SendAsync(message);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
_logger.LogError(12337, e, "Unable to add user org push registration.");
|
|
}
|
|
}
|
|
|
|
public async Task DeleteUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId)
|
|
{
|
|
if(!deviceIds.Any())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var tokenStateResponse = await HandleTokenStateAsync();
|
|
if(!tokenStateResponse)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var requestModel = new PushUpdateRequestModel(deviceIds, organizationId);
|
|
var message = new TokenHttpRequestMessage(requestModel, AccessToken)
|
|
{
|
|
Method = HttpMethod.Put,
|
|
RequestUri = new Uri(string.Concat(PushClient.BaseAddress, "/push/delete-organization"))
|
|
};
|
|
|
|
try
|
|
{
|
|
await PushClient.SendAsync(message);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
_logger.LogError(12338, e, "Unable to delete user org push registration.");
|
|
}
|
|
}
|
|
}
|
|
}
|