1
0
mirror of https://github.com/bitwarden/server synced 2025-12-17 08:43:27 +00:00

store device type and ip address on events

This commit is contained in:
Kyle Spearrin
2017-12-15 10:50:06 -05:00
parent 39f6516ca8
commit a9f232746e
11 changed files with 123 additions and 44 deletions

View File

@@ -3,14 +3,21 @@ using System.Collections.Generic;
using System.Linq;
using Bit.Core.Models.Table;
using Bit.Core.Enums;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace Bit.Core
{
public class CurrentContext
{
private string _ip;
public virtual HttpContext HttpContext { get; set; }
public virtual Guid? UserId { get; set; }
public virtual User User { get; set; }
public virtual string DeviceIdentifier { get; set; }
public virtual DeviceType? DeviceType { get; set; }
public virtual string IpAddress => GetRequestIp();
public virtual List<CurrentContentOrganization> Organizations { get; set; } = new List<CurrentContentOrganization>();
public virtual Guid? InstallationId { get; set; }
@@ -28,6 +35,31 @@ namespace Bit.Core
return Organizations.Any(o => o.Id == orgId && o.Type == OrganizationUserType.Owner);
}
private string GetRequestIp()
{
if(!string.IsNullOrWhiteSpace(_ip))
{
return _ip;
}
if(HttpContext == null)
{
return null;
}
if(HttpContext.Request?.Headers?.TryGetValue("X-Forwarded-For", out StringValues forwardHeader) ?? false)
{
_ip = forwardHeader.FirstOrDefault()?.Trim();
}
if(string.IsNullOrWhiteSpace(_ip))
{
_ip = HttpContext.Connection?.RemoteIpAddress?.ToString();
}
return _ip;
}
public class CurrentContentOrganization
{
public Guid Id { get; set; }