1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-22 02:51:33 +01:00

Added device service. Added API for clearing out token for a device identifier (used for push unregister).

This commit is contained in:
Kyle Spearrin 2016-08-05 23:59:59 -04:00
parent f456a4fca8
commit 0727b75a83
4 changed files with 68 additions and 3 deletions

View File

@ -9,6 +9,7 @@ using Bit.Api.Models;
using Bit.Core.Exceptions;
using Bit.Core.Domains;
using Microsoft.AspNetCore.Identity;
using Bit.Core.Services;
namespace Bit.Api.Controllers
{
@ -17,13 +18,16 @@ namespace Bit.Api.Controllers
public class DevicesController : Controller
{
private readonly IDeviceRepository _deviceRepository;
private readonly IDeviceService _deviceService;
private readonly UserManager<User> _userManager;
public DevicesController(
IDeviceRepository deviceRepository,
IDeviceService deviceService,
UserManager<User> userManager)
{
_deviceRepository = deviceRepository;
_deviceService = deviceService;
_userManager = userManager;
}
@ -65,7 +69,7 @@ namespace Bit.Api.Controllers
public async Task<DeviceResponseModel> Post([FromBody]DeviceRequestModel model)
{
var device = model.ToDevice(_userManager.GetUserId(User));
await _deviceRepository.CreateAsync(device);
await _deviceService.SaveAsync(device);
var response = new DeviceResponseModel(device);
return response;
@ -81,7 +85,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException();
}
await _deviceRepository.ReplaceAsync(model.ToDevice(device));
await _deviceService.SaveAsync(model.ToDevice(device));
var response = new DeviceResponseModel(device);
return response;
@ -97,7 +101,26 @@ namespace Bit.Api.Controllers
throw new NotFoundException();
}
await _deviceRepository.ReplaceAsync(model.ToDevice(device));
await _deviceService.SaveAsync(model.ToDevice(device));
var response = new DeviceResponseModel(device);
return response;
}
[AllowAnonymous]
[HttpPut("identifier/{identifier}/clear-token")]
[HttpPost("identifier/{identifier}/clear-token")]
public async Task<DeviceResponseModel> PutClearToken(string identifier)
{
var device = await _deviceRepository.GetByIdentifierAsync(identifier, new Guid(_userManager.GetUserId(User)));
if(device == null)
{
await Task.Delay(2000);
throw new NotFoundException();
}
device.PushToken = null;
await _deviceService.SaveAsync(device);
var response = new DeviceResponseModel(device);
return response;

View File

@ -123,6 +123,7 @@ namespace Bit.Api
services.AddSingleton<ICipherService, CipherService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IPushService, PushService>();
services.AddScoped<IDeviceService, DeviceService>();
// Cors
services.AddCors(config =>

View File

@ -0,0 +1,31 @@
using System;
using System.Threading.Tasks;
using Bit.Core.Domains;
using Bit.Core.Repositories;
namespace Bit.Core.Services
{
public class DeviceService : IDeviceService
{
private readonly IDeviceRepository _deviceRepository;
public DeviceService(
IDeviceRepository deviceRepository)
{
_deviceRepository = deviceRepository;
}
public async Task SaveAsync(Device device)
{
if(device.Id == default(Guid))
{
await _deviceRepository.CreateAsync(device);
}
else
{
device.RevisionDate = DateTime.UtcNow;
await _deviceRepository.ReplaceAsync(device);
}
}
}
}

View File

@ -0,0 +1,10 @@
using System.Threading.Tasks;
using Bit.Core.Domains;
namespace Bit.Core.Services
{
public interface IDeviceService
{
Task SaveAsync(Device device);
}
}