2017-08-11 08:57:31 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-08-10 14:39:11 -04:00
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2017-08-10 15:26:05 -04:00
|
|
|
|
using Bit.Core.Exceptions;
|
2017-08-11 08:57:31 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2021-12-14 15:05:07 +00:00
|
|
|
|
using Bit.Core.Models.Api;
|
2018-03-23 09:44:48 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2020-01-10 08:47:58 -05:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2017-08-10 14:39:11 -04:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("push")]
|
|
|
|
|
|
[Authorize("Push")]
|
2017-08-15 16:20:46 -04:00
|
|
|
|
[SelfHosted(NotSelfHostedOnly = true)]
|
2017-08-10 14:39:11 -04:00
|
|
|
|
public class PushController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPushRegistrationService _pushRegistrationService;
|
2017-08-11 10:04:59 -04:00
|
|
|
|
private readonly IPushNotificationService _pushNotificationService;
|
2020-01-10 08:47:58 -05:00
|
|
|
|
private readonly IWebHostEnvironment _environment;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
private readonly ICurrentContext _currentContext;
|
2017-08-11 08:57:31 -04:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2017-08-10 14:39:11 -04:00
|
|
|
|
|
|
|
|
|
|
public PushController(
|
2017-08-10 15:26:05 -04:00
|
|
|
|
IPushRegistrationService pushRegistrationService,
|
2017-08-11 10:04:59 -04:00
|
|
|
|
IPushNotificationService pushNotificationService,
|
2020-01-10 08:47:58 -05:00
|
|
|
|
IWebHostEnvironment environment,
|
2021-02-04 12:54:21 -06:00
|
|
|
|
ICurrentContext currentContext,
|
2017-08-11 08:57:31 -04:00
|
|
|
|
GlobalSettings globalSettings)
|
2017-08-10 14:39:11 -04:00
|
|
|
|
{
|
2017-08-10 15:26:05 -04:00
|
|
|
|
_currentContext = currentContext;
|
2017-08-11 08:57:31 -04:00
|
|
|
|
_environment = environment;
|
2017-08-10 14:39:11 -04:00
|
|
|
|
_pushRegistrationService = pushRegistrationService;
|
2017-08-11 10:04:59 -04:00
|
|
|
|
_pushNotificationService = pushNotificationService;
|
2017-08-11 08:57:31 -04:00
|
|
|
|
_globalSettings = globalSettings;
|
2017-08-10 14:39:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-11 08:57:31 -04:00
|
|
|
|
[HttpPost("register")]
|
2017-08-11 12:22:59 -04:00
|
|
|
|
public async Task PostRegister([FromBody]PushRegistrationRequestModel model)
|
2017-08-10 14:39:11 -04:00
|
|
|
|
{
|
2017-08-11 08:57:31 -04:00
|
|
|
|
CheckUsage();
|
|
|
|
|
|
await _pushRegistrationService.CreateOrUpdateRegistrationAsync(model.PushToken, Prefix(model.DeviceId),
|
|
|
|
|
|
Prefix(model.UserId), Prefix(model.Identifier), model.Type);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
|
public async Task Delete(string id)
|
|
|
|
|
|
{
|
|
|
|
|
|
CheckUsage();
|
|
|
|
|
|
await _pushRegistrationService.DeleteRegistrationAsync(Prefix(id));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("add-organization")]
|
2017-08-11 12:22:59 -04:00
|
|
|
|
public async Task PutAddOrganization([FromBody]PushUpdateRequestModel model)
|
2017-08-11 08:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
CheckUsage();
|
|
|
|
|
|
await _pushRegistrationService.AddUserRegistrationOrganizationAsync(
|
|
|
|
|
|
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("delete-organization")]
|
2017-08-11 12:22:59 -04:00
|
|
|
|
public async Task PutDeleteOrganization([FromBody]PushUpdateRequestModel model)
|
2017-08-11 08:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
CheckUsage();
|
|
|
|
|
|
await _pushRegistrationService.DeleteUserRegistrationOrganizationAsync(
|
|
|
|
|
|
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-11 10:04:59 -04:00
|
|
|
|
[HttpPost("send")]
|
2017-08-11 12:22:59 -04:00
|
|
|
|
public async Task PostSend([FromBody]PushSendRequestModel model)
|
2017-08-11 10:04:59 -04:00
|
|
|
|
{
|
|
|
|
|
|
CheckUsage();
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.UserId))
|
2017-08-11 10:04:59 -04:00
|
|
|
|
{
|
2017-08-11 12:22:59 -04:00
|
|
|
|
await _pushNotificationService.SendPayloadToUserAsync(Prefix(model.UserId),
|
2019-03-19 00:39:03 -04:00
|
|
|
|
model.Type.Value, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId));
|
2017-08-11 10:04:59 -04:00
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
else if (!string.IsNullOrWhiteSpace(model.OrganizationId))
|
2017-08-11 10:04:59 -04:00
|
|
|
|
{
|
|
|
|
|
|
await _pushNotificationService.SendPayloadToOrganizationAsync(Prefix(model.OrganizationId),
|
2019-03-19 00:39:03 -04:00
|
|
|
|
model.Type.Value, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId));
|
2017-08-11 10:04:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-11 08:57:31 -04:00
|
|
|
|
private string Prefix(string value)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
2017-08-11 10:04:59 -04:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-11 08:57:31 -04:00
|
|
|
|
return $"{_currentContext.InstallationId.Value}_{value}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CheckUsage()
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (CanUse())
|
2017-08-11 08:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new BadRequestException("Not correctly configured for push relays.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private bool CanUse()
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (_environment.IsDevelopment())
|
2017-08-10 15:26:05 -04:00
|
|
|
|
{
|
2017-08-11 08:57:31 -04:00
|
|
|
|
return true;
|
2017-08-10 15:26:05 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-18 21:07:56 -04:00
|
|
|
|
return _currentContext.InstallationId.HasValue && !_globalSettings.SelfHosted;
|
2017-08-10 14:39:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|