2019-07-03 08:58:18 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2017-12-04 10:59:07 -05:00
|
|
|
|
using Bit.Core;
|
2019-03-19 17:26:34 -04:00
|
|
|
|
using Bit.Core.Enums;
|
2017-12-04 10:59:07 -05:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Bit.Events.Models;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2017-12-04 10:12:11 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
2019-03-19 17:45:31 -04:00
|
|
|
|
namespace Bit.Events.Controllers
|
2017-12-04 10:12:11 -05:00
|
|
|
|
{
|
2019-03-19 17:45:31 -04:00
|
|
|
|
[Route("collect")]
|
2017-12-04 10:59:07 -05:00
|
|
|
|
[Authorize("Application")]
|
2019-03-19 17:45:31 -04:00
|
|
|
|
public class CollectController : Controller
|
2017-12-04 10:12:11 -05:00
|
|
|
|
{
|
2017-12-04 10:59:07 -05:00
|
|
|
|
private readonly CurrentContext _currentContext;
|
|
|
|
|
|
private readonly IEventService _eventService;
|
|
|
|
|
|
private readonly ICipherRepository _cipherRepository;
|
|
|
|
|
|
|
2019-03-19 17:45:31 -04:00
|
|
|
|
public CollectController(
|
2017-12-04 10:59:07 -05:00
|
|
|
|
CurrentContext currentContext,
|
|
|
|
|
|
IEventService eventService,
|
|
|
|
|
|
ICipherRepository cipherRepository)
|
|
|
|
|
|
{
|
|
|
|
|
|
_currentContext = currentContext;
|
|
|
|
|
|
_eventService = eventService;
|
|
|
|
|
|
_cipherRepository = cipherRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-03 08:58:18 -04:00
|
|
|
|
[HttpPost]
|
2019-07-03 12:11:40 -04:00
|
|
|
|
public async Task<IActionResult> Post([FromBody]IEnumerable<EventModel> model)
|
2019-07-03 08:58:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
if(model == null || !model.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
return new BadRequestResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach(var eventModel in model)
|
|
|
|
|
|
{
|
|
|
|
|
|
await LogEventAsync(eventModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
return new OkResult();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<bool> LogEventAsync(EventModel model)
|
2017-12-04 10:12:11 -05:00
|
|
|
|
{
|
2019-03-19 17:26:34 -04:00
|
|
|
|
switch(model.Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
// User events
|
2019-07-03 08:58:18 -04:00
|
|
|
|
case EventType.User_ClientExportedVault:
|
2019-03-19 17:26:34 -04:00
|
|
|
|
await _eventService.LogUserEventAsync(_currentContext.UserId.Value, model.Type);
|
|
|
|
|
|
break;
|
|
|
|
|
|
// Cipher events
|
2019-06-20 08:56:10 -04:00
|
|
|
|
case EventType.Cipher_ClientAutofilled:
|
|
|
|
|
|
case EventType.Cipher_ClientCopedHiddenField:
|
|
|
|
|
|
case EventType.Cipher_ClientCopiedPassword:
|
|
|
|
|
|
case EventType.Cipher_ClientToggledHiddenFieldVisible:
|
|
|
|
|
|
case EventType.Cipher_ClientToggledPasswordVisible:
|
|
|
|
|
|
case EventType.Cipher_ClientViewed:
|
2019-03-19 17:26:34 -04:00
|
|
|
|
if(!model.CipherId.HasValue)
|
|
|
|
|
|
{
|
2019-07-03 08:58:18 -04:00
|
|
|
|
return false;
|
2019-03-19 17:26:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
var cipher = await _cipherRepository.GetByIdAsync(model.CipherId.Value,
|
|
|
|
|
|
_currentContext.UserId.Value);
|
|
|
|
|
|
if(cipher == null)
|
|
|
|
|
|
{
|
2019-07-03 08:58:18 -04:00
|
|
|
|
return false;
|
2019-03-19 17:26:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
await _eventService.LogCipherEventAsync(cipher, model.Type);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2019-07-03 08:58:18 -04:00
|
|
|
|
return false;
|
2019-03-19 17:26:34 -04:00
|
|
|
|
}
|
2019-07-03 08:58:18 -04:00
|
|
|
|
return true;
|
2017-12-04 10:12:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|