mirror of
https://github.com/bitwarden/server.git
synced 2024-11-23 12:25:16 +01:00
move ip address method to core helpers
This commit is contained in:
parent
4982c21c37
commit
72310701d2
@ -7,14 +7,12 @@ using Microsoft.AspNetCore.Http;
|
||||
using Bit.Core.Repositories;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Claims;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core
|
||||
{
|
||||
public class CurrentContext
|
||||
{
|
||||
private const string CloudFlareConnectingIp = "CF-Connecting-IP";
|
||||
private const string RealIp = "X-Real-IP";
|
||||
|
||||
private bool _builtHttpContext;
|
||||
private bool _builtClaimsPrincipal;
|
||||
|
||||
@ -59,7 +57,7 @@ namespace Bit.Core
|
||||
}
|
||||
|
||||
_builtClaimsPrincipal = true;
|
||||
IpAddress = GetRequestIp(globalSettings);
|
||||
IpAddress = HttpContext.GetIpAddress(globalSettings);
|
||||
if(user == null || !user.Claims.Any())
|
||||
{
|
||||
return;
|
||||
@ -172,25 +170,6 @@ namespace Bit.Core
|
||||
return Organizations;
|
||||
}
|
||||
|
||||
private string GetRequestIp(GlobalSettings globalSettings)
|
||||
{
|
||||
if(HttpContext == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!globalSettings.SelfHosted && HttpContext.Request.Headers.ContainsKey(CloudFlareConnectingIp))
|
||||
{
|
||||
return HttpContext.Request.Headers[CloudFlareConnectingIp].ToString();
|
||||
}
|
||||
if(globalSettings.SelfHosted && HttpContext.Request.Headers.ContainsKey(RealIp))
|
||||
{
|
||||
return HttpContext.Request.Headers[RealIp].ToString();
|
||||
}
|
||||
|
||||
return HttpContext.Connection?.RemoteIpAddress?.ToString();
|
||||
}
|
||||
|
||||
private string GetClaimValue(Dictionary<string, IEnumerable<Claim>> claims, string type)
|
||||
{
|
||||
if(!claims.ContainsKey(type))
|
||||
|
@ -33,6 +33,8 @@ namespace Bit.Core.Utilities
|
||||
"RL?+AOEUIDHTNS_:QJKXBMWVZ";
|
||||
private static readonly string _qwertyColemakMap = "qwertyuiopasdfghjkl;zxcvbnmQWERTYUIOPASDFGHJKL:ZXCVBNM";
|
||||
private static readonly string _colemakMap = "qwfpgjluy;arstdhneiozxcvbkmQWFPGJLUY:ARSTDHNEIOZXCVBKM";
|
||||
private static readonly string CloudFlareConnectingIp = "CF-Connecting-IP";
|
||||
private static readonly string RealIp = "X-Real-IP";
|
||||
|
||||
/// <summary>
|
||||
/// Generate sequential Guid for Sql Server.
|
||||
@ -569,5 +571,25 @@ namespace Bit.Core.Utilities
|
||||
}
|
||||
return subName;
|
||||
}
|
||||
|
||||
public static string GetIpAddress(this Microsoft.AspNetCore.Http.HttpContext httpContext,
|
||||
GlobalSettings globalSettings)
|
||||
{
|
||||
if(httpContext == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!globalSettings.SelfHosted && httpContext.Request.Headers.ContainsKey(CloudFlareConnectingIp))
|
||||
{
|
||||
return httpContext.Request.Headers[CloudFlareConnectingIp].ToString();
|
||||
}
|
||||
if(globalSettings.SelfHosted && httpContext.Request.Headers.ContainsKey(RealIp))
|
||||
{
|
||||
return httpContext.Request.Headers[RealIp].ToString();
|
||||
}
|
||||
|
||||
return httpContext.Connection?.RemoteIpAddress?.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user