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

push registration through relay apis

This commit is contained in:
Kyle Spearrin
2017-08-11 08:57:31 -04:00
parent 0ad76a5487
commit 0f37920de2
12 changed files with 463 additions and 69 deletions

View File

@ -1,9 +1,12 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Bit.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Bit.Core;
using Bit.Core.Exceptions;
using Bit.Core.Models.Api;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
namespace Bit.Api.Controllers
{
@ -12,25 +15,76 @@ namespace Bit.Api.Controllers
public class PushController : Controller
{
private readonly IPushRegistrationService _pushRegistrationService;
private readonly IHostingEnvironment _environment;
private readonly CurrentContext _currentContext;
private readonly GlobalSettings _globalSettings;
public PushController(
IPushRegistrationService pushRegistrationService,
CurrentContext currentContext)
IHostingEnvironment environment,
CurrentContext currentContext,
GlobalSettings globalSettings)
{
_currentContext = currentContext;
_environment = environment;
_pushRegistrationService = pushRegistrationService;
_globalSettings = globalSettings;
}
[HttpGet("register")]
public Object Register()
[HttpPost("register")]
public async Task PostRegister(PushRegistrationRequestModel model)
{
if(!_currentContext.InstallationId.HasValue)
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")]
public async Task PutAddOrganization(PushUpdateRequestModel model)
{
CheckUsage();
await _pushRegistrationService.AddUserRegistrationOrganizationAsync(
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
}
[HttpPut("delete-organization")]
public async Task PutDeleteOrganization(PushUpdateRequestModel model)
{
CheckUsage();
await _pushRegistrationService.DeleteUserRegistrationOrganizationAsync(
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
}
private string Prefix(string value)
{
return $"{_currentContext.InstallationId.Value}_{value}";
}
private void CheckUsage()
{
if(CanUse())
{
throw new BadRequestException("bad request.");
return;
}
return new { Foo = "bar" };
throw new BadRequestException("Not correctly configured for push relays.");
}
private bool CanUse()
{
if(_environment.IsDevelopment())
{
return true;
}
return _currentContext.InstallationId.HasValue && _globalSettings.SelfHosted;
}
}
}