1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-23 05:55:13 -05:00

get request up from cloudflare header

This commit is contained in:
Kyle Spearrin 2019-01-25 00:01:24 -05:00
parent b19628c6f8
commit f8f7c339c3
3 changed files with 18 additions and 19 deletions

View File

@ -12,20 +12,21 @@ namespace Bit.Core
{ {
public class CurrentContext public class CurrentContext
{ {
private const string CloudFlareConnectingIp = "CF-Connecting-IP";
private bool _builtHttpContext; private bool _builtHttpContext;
private bool _builtClaimsPrincipal; private bool _builtClaimsPrincipal;
private string _ip;
public virtual HttpContext HttpContext { get; set; } public virtual HttpContext HttpContext { get; set; }
public virtual Guid? UserId { get; set; } public virtual Guid? UserId { get; set; }
public virtual User User { get; set; } public virtual User User { get; set; }
public virtual string DeviceIdentifier { get; set; } public virtual string DeviceIdentifier { get; set; }
public virtual DeviceType? DeviceType { get; set; } public virtual DeviceType? DeviceType { get; set; }
public virtual string IpAddress => GetRequestIp(); public virtual string IpAddress { get; set; }
public virtual List<CurrentContentOrganization> Organizations { get; set; } public virtual List<CurrentContentOrganization> Organizations { get; set; }
public virtual Guid? InstallationId { get; set; } public virtual Guid? InstallationId { get; set; }
public void Build(HttpContext httpContext) public void Build(HttpContext httpContext, GlobalSettings globalSettings)
{ {
if(_builtHttpContext) if(_builtHttpContext)
{ {
@ -34,7 +35,7 @@ namespace Bit.Core
_builtHttpContext = true; _builtHttpContext = true;
HttpContext = httpContext; HttpContext = httpContext;
Build(httpContext.User); Build(httpContext.User, globalSettings);
if(DeviceIdentifier == null && httpContext.Request.Headers.ContainsKey("Device-Identifier")) if(DeviceIdentifier == null && httpContext.Request.Headers.ContainsKey("Device-Identifier"))
{ {
@ -48,7 +49,7 @@ namespace Bit.Core
} }
} }
public void Build(ClaimsPrincipal user) public void Build(ClaimsPrincipal user, GlobalSettings globalSettings)
{ {
if(_builtClaimsPrincipal) if(_builtClaimsPrincipal)
{ {
@ -56,6 +57,7 @@ namespace Bit.Core
} }
_builtClaimsPrincipal = true; _builtClaimsPrincipal = true;
IpAddress = GetRequestIp(globalSettings);
if(user == null || !user.Claims.Any()) if(user == null || !user.Claims.Any())
{ {
return; return;
@ -158,24 +160,19 @@ namespace Bit.Core
return Organizations; return Organizations;
} }
private string GetRequestIp() private string GetRequestIp(GlobalSettings globalSettings)
{ {
if(!string.IsNullOrWhiteSpace(_ip))
{
return _ip;
}
if(HttpContext == null) if(HttpContext == null)
{ {
return null; return null;
} }
if(string.IsNullOrWhiteSpace(_ip)) if(!globalSettings.SelfHosted && HttpContext.Request.Headers.ContainsKey(CloudFlareConnectingIp))
{ {
_ip = HttpContext.Connection?.RemoteIpAddress?.ToString(); return HttpContext.Request.Headers[CloudFlareConnectingIp].ToString();
} }
return _ip; return HttpContext.Connection?.RemoteIpAddress?.ToString();
} }
private string GetClaimValue(Dictionary<string, IEnumerable<Claim>> claims, string type) private string GetClaimValue(Dictionary<string, IEnumerable<Claim>> claims, string type)

View File

@ -12,9 +12,9 @@ namespace Bit.Core.Utilities
_next = next; _next = next;
} }
public async Task Invoke(HttpContext httpContext, CurrentContext currentContext) public async Task Invoke(HttpContext httpContext, CurrentContext currentContext, GlobalSettings globalSettings)
{ {
currentContext.Build(httpContext); currentContext.Build(httpContext, globalSettings);
await _next.Invoke(httpContext); await _next.Invoke(httpContext);
} }
} }

View File

@ -9,16 +9,18 @@ namespace Bit.Notifications
public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub
{ {
private readonly ConnectionCounter _connectionCounter; private readonly ConnectionCounter _connectionCounter;
private readonly GlobalSettings _globalSettings;
public NotificationsHub(ConnectionCounter connectionCounter) public NotificationsHub(ConnectionCounter connectionCounter, GlobalSettings globalSettings)
{ {
_connectionCounter = connectionCounter; _connectionCounter = connectionCounter;
_globalSettings = globalSettings;
} }
public override async Task OnConnectedAsync() public override async Task OnConnectedAsync()
{ {
var currentContext = new CurrentContext(); var currentContext = new CurrentContext();
currentContext.Build(Context.User); currentContext.Build(Context.User, _globalSettings);
if(currentContext.Organizations != null) if(currentContext.Organizations != null)
{ {
foreach(var org in currentContext.Organizations) foreach(var org in currentContext.Organizations)
@ -33,7 +35,7 @@ namespace Bit.Notifications
public override async Task OnDisconnectedAsync(Exception exception) public override async Task OnDisconnectedAsync(Exception exception)
{ {
var currentContext = new CurrentContext(); var currentContext = new CurrentContext();
currentContext.Build(Context.User); currentContext.Build(Context.User, _globalSettings);
if(currentContext.Organizations != null) if(currentContext.Organizations != null)
{ {
foreach(var org in currentContext.Organizations) foreach(var org in currentContext.Organizations)