2018-08-16 12:05:01 -04:00
|
|
|
|
using System;
|
2018-08-21 09:29:38 -04:00
|
|
|
|
using System.Collections.Generic;
|
2018-08-16 12:05:01 -04:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2018-08-16 12:05:01 -04:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
|
using Bit.Core.Models;
|
|
|
|
|
|
using Bit.Core.Models.Table;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2018-08-16 12:05:01 -04:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-08-21 09:29:38 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2018-08-16 12:05:01 -04:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
2018-08-16 13:52:11 -04:00
|
|
|
|
public class NotificationsApiPushNotificationService : BaseIdentityClientService, IPushNotificationService
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2018-08-16 13:52:11 -04:00
|
|
|
|
public NotificationsApiPushNotificationService(
|
2018-08-16 12:05:01 -04:00
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2018-08-16 13:52:11 -04:00
|
|
|
|
ILogger<NotificationsApiPushNotificationService> logger)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
: base(
|
2018-08-16 13:50:41 -04:00
|
|
|
|
globalSettings.BaseServiceUri.InternalNotifications,
|
2018-08-16 12:05:01 -04:00
|
|
|
|
globalSettings.BaseServiceUri.InternalIdentity,
|
|
|
|
|
|
"internal",
|
|
|
|
|
|
$"internal.{globalSettings.ProjectName}",
|
|
|
|
|
|
globalSettings.InternalIdentityKey,
|
|
|
|
|
|
logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
_globalSettings = globalSettings;
|
|
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public async Task PushSyncCipherCreateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncCipherCreate, collectionIds);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
public async Task PushSyncCipherUpdateAsync(Cipher cipher, IEnumerable<Guid> collectionIds)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncCipherUpdate, collectionIds);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
|
|
|
|
|
|
{
|
2018-08-21 09:29:38 -04:00
|
|
|
|
await PushCipherAsync(cipher, PushType.SyncLoginDelete, null);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 09:29:38 -04:00
|
|
|
|
private async Task PushCipherAsync(Cipher cipher, PushType type, IEnumerable<Guid> collectionIds)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (cipher.OrganizationId.HasValue)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
var message = new SyncCipherPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
OrganizationId = cipher.OrganizationId,
|
|
|
|
|
|
RevisionDate = cipher.RevisionDate,
|
2018-08-21 09:29:38 -04:00
|
|
|
|
CollectionIds = collectionIds,
|
2018-08-16 12:05:01 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, true);
|
|
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
else if (cipher.UserId.HasValue)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
var message = new SyncCipherPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = cipher.Id,
|
|
|
|
|
|
UserId = cipher.UserId,
|
|
|
|
|
|
RevisionDate = cipher.RevisionDate,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncFolderCreateAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushFolderAsync(folder, PushType.SyncFolderCreate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncFolderUpdateAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushFolderAsync(folder, PushType.SyncFolderUpdate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncFolderDeleteAsync(Folder folder)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushFolderAsync(folder, PushType.SyncFolderDelete);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task PushFolderAsync(Folder folder, PushType type)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = new SyncFolderPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = folder.Id,
|
|
|
|
|
|
UserId = folder.UserId,
|
|
|
|
|
|
RevisionDate = folder.RevisionDate
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncCiphersAsync(Guid userId)
|
|
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncCiphers);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncVaultAsync(Guid userId)
|
|
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncVault);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncOrgKeysAsync(Guid userId)
|
|
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncOrgKeys);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncSettingsAsync(Guid userId)
|
|
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushUserAsync(userId, PushType.SyncSettings);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-28 08:22:49 -04:00
|
|
|
|
public async Task PushLogOutAsync(Guid userId)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
2018-08-28 08:22:49 -04:00
|
|
|
|
await PushUserAsync(userId, PushType.LogOut);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task PushUserAsync(Guid userId, PushType type)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = new UserPushNotification
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
UserId = userId,
|
|
|
|
|
|
Date = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-22 16:16:40 -05:00
|
|
|
|
public async Task PushSyncSendCreateAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushSendAsync(send, PushType.SyncSendCreate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncSendUpdateAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushSendAsync(send, PushType.SyncSendUpdate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task PushSyncSendDeleteAsync(Send send)
|
|
|
|
|
|
{
|
|
|
|
|
|
await PushSendAsync(send, PushType.SyncSendDelete);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task PushSendAsync(Send send, PushType type)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (send.UserId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = new SyncSendPushNotification
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = send.Id,
|
|
|
|
|
|
UserId = send.UserId.Value,
|
|
|
|
|
|
RevisionDate = send.RevisionDate
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await SendMessageAsync(type, message, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-16 12:05:01 -04:00
|
|
|
|
private async Task SendMessageAsync<T>(PushType type, T payload, bool excludeCurrentContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
var contextId = GetContextIdentifier(excludeCurrentContext);
|
|
|
|
|
|
var request = new PushNotificationData<T>(type, payload, contextId);
|
2018-08-24 20:31:17 -04:00
|
|
|
|
await SendAsync(HttpMethod.Post, "send", request);
|
2018-08-16 12:05:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string GetContextIdentifier(bool excludeCurrentContext)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!excludeCurrentContext)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var currentContext = _httpContextAccessor?.HttpContext?.
|
2021-02-04 12:54:21 -06:00
|
|
|
|
RequestServices.GetService(typeof(ICurrentContext)) as ICurrentContext;
|
2018-08-16 12:05:01 -04:00
|
|
|
|
return currentContext?.DeviceIdentifier;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
public Task SendPayloadToUserAsync(string userId, PushType type, object payload, string identifier,
|
|
|
|
|
|
string deviceId = null)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Noop
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 00:39:03 -04:00
|
|
|
|
public Task SendPayloadToOrganizationAsync(string orgId, PushType type, object payload, string identifier,
|
|
|
|
|
|
string deviceId = null)
|
2018-08-16 12:05:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
// Noop
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|