mirror of
https://github.com/bitwarden/server.git
synced 2025-07-05 18:12:48 -05:00
stub out signalr sync hub
This commit is contained in:
@ -6,11 +6,14 @@ using Bit.Core.Enums;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Bit.Core.Repositories;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Bit.Core
|
||||
{
|
||||
public class CurrentContext
|
||||
{
|
||||
private bool _builtHttpContext;
|
||||
private bool _builtClaimsPrincipal;
|
||||
private string _ip;
|
||||
private Dictionary<Guid, ICollection<OrganizationUser>> _orgUsers =
|
||||
new Dictionary<Guid, ICollection<OrganizationUser>>();
|
||||
@ -25,6 +28,93 @@ namespace Bit.Core
|
||||
new List<CurrentContentOrganization>();
|
||||
public virtual Guid? InstallationId { get; set; }
|
||||
|
||||
public void Build(HttpContext httpContext)
|
||||
{
|
||||
if(_builtHttpContext)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_builtHttpContext = true;
|
||||
HttpContext = httpContext;
|
||||
Build(httpContext.User);
|
||||
|
||||
if(DeviceIdentifier == null && httpContext.Request.Headers.ContainsKey("Device-Identifier"))
|
||||
{
|
||||
DeviceIdentifier = httpContext.Request.Headers["Device-Identifier"];
|
||||
}
|
||||
|
||||
if(httpContext.Request.Headers.ContainsKey("Device-Type") &&
|
||||
Enum.TryParse(httpContext.Request.Headers["Device-Type"].ToString(), out DeviceType dType))
|
||||
{
|
||||
DeviceType = dType;
|
||||
}
|
||||
}
|
||||
|
||||
public void Build(ClaimsPrincipal user)
|
||||
{
|
||||
if(_builtClaimsPrincipal)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_builtClaimsPrincipal = true;
|
||||
if(user == null || !user.Claims.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var claimsDict = user.Claims.GroupBy(c => c.Type).ToDictionary(c => c.Key, c => c.Select(v => v));
|
||||
|
||||
var subject = GetClaimValue(claimsDict, "sub");
|
||||
if(Guid.TryParse(subject, out var subIdGuid))
|
||||
{
|
||||
UserId = subIdGuid;
|
||||
}
|
||||
|
||||
var clientId = GetClaimValue(claimsDict, "client_id");
|
||||
var clientSubject = GetClaimValue(claimsDict, "client_sub");
|
||||
if((clientId?.StartsWith("installation.") ?? false) && clientSubject != null)
|
||||
{
|
||||
if(Guid.TryParse(clientSubject, out var idGuid))
|
||||
{
|
||||
InstallationId = idGuid;
|
||||
}
|
||||
}
|
||||
|
||||
DeviceIdentifier = GetClaimValue(claimsDict, "device");
|
||||
|
||||
if(claimsDict.ContainsKey("orgowner"))
|
||||
{
|
||||
Organizations.AddRange(claimsDict["orgowner"].Select(c =>
|
||||
new CurrentContentOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
Type = OrganizationUserType.Owner
|
||||
}));
|
||||
}
|
||||
|
||||
if(claimsDict.ContainsKey("orgadmin"))
|
||||
{
|
||||
Organizations.AddRange(claimsDict["orgadmin"].Select(c =>
|
||||
new CurrentContentOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
Type = OrganizationUserType.Admin
|
||||
}));
|
||||
}
|
||||
|
||||
if(claimsDict.ContainsKey("orguser"))
|
||||
{
|
||||
Organizations.AddRange(claimsDict["orguser"].Select(c =>
|
||||
new CurrentContentOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
Type = OrganizationUserType.User
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
public bool OrganizationUser(Guid orgId)
|
||||
{
|
||||
return Organizations.Any(o => o.Id == orgId);
|
||||
@ -70,6 +160,16 @@ namespace Bit.Core
|
||||
return _ip;
|
||||
}
|
||||
|
||||
private string GetClaimValue(Dictionary<string, IEnumerable<Claim>> claims, string type)
|
||||
{
|
||||
if(!claims.ContainsKey(type))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return claims[type].FirstOrDefault()?.Value;
|
||||
}
|
||||
|
||||
public class CurrentContentOrganization
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
@ -1,9 +1,4 @@
|
||||
using Bit.Core.Enums;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.Core.Utilities
|
||||
@ -19,85 +14,8 @@ namespace Bit.Core.Utilities
|
||||
|
||||
public async Task Invoke(HttpContext httpContext, CurrentContext currentContext)
|
||||
{
|
||||
currentContext.HttpContext = httpContext;
|
||||
|
||||
if(httpContext.User != null && httpContext.User.Claims.Any())
|
||||
{
|
||||
var claimsDict = httpContext.User.Claims
|
||||
.GroupBy(c => c.Type)
|
||||
.ToDictionary(c => c.Key, c => c.Select(v => v));
|
||||
|
||||
var subject = GetClaimValue(claimsDict, "sub");
|
||||
if(Guid.TryParse(subject, out var subIdGuid))
|
||||
{
|
||||
currentContext.UserId = subIdGuid;
|
||||
}
|
||||
|
||||
var clientId = GetClaimValue(claimsDict, "client_id");
|
||||
var clientSubject = GetClaimValue(claimsDict, "client_sub");
|
||||
if((clientId?.StartsWith("installation.") ?? false) && clientSubject != null)
|
||||
{
|
||||
if(Guid.TryParse(clientSubject, out var idGuid))
|
||||
{
|
||||
currentContext.InstallationId = idGuid;
|
||||
}
|
||||
}
|
||||
|
||||
currentContext.DeviceIdentifier = GetClaimValue(claimsDict, "device");
|
||||
|
||||
if(claimsDict.ContainsKey("orgowner"))
|
||||
{
|
||||
currentContext.Organizations.AddRange(claimsDict["orgowner"].Select(c =>
|
||||
new CurrentContext.CurrentContentOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
Type = OrganizationUserType.Owner
|
||||
}));
|
||||
}
|
||||
|
||||
if(claimsDict.ContainsKey("orgadmin"))
|
||||
{
|
||||
currentContext.Organizations.AddRange(claimsDict["orgadmin"].Select(c =>
|
||||
new CurrentContext.CurrentContentOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
Type = OrganizationUserType.Admin
|
||||
}));
|
||||
}
|
||||
|
||||
if(claimsDict.ContainsKey("orguser"))
|
||||
{
|
||||
currentContext.Organizations.AddRange(claimsDict["orguser"].Select(c =>
|
||||
new CurrentContext.CurrentContentOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
Type = OrganizationUserType.User
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
if(currentContext.DeviceIdentifier == null && httpContext.Request.Headers.ContainsKey("Device-Identifier"))
|
||||
{
|
||||
currentContext.DeviceIdentifier = httpContext.Request.Headers["Device-Identifier"];
|
||||
}
|
||||
|
||||
if(httpContext.Request.Headers.ContainsKey("Device-Type") &&
|
||||
Enum.TryParse(httpContext.Request.Headers["Device-Type"].ToString(), out DeviceType dType))
|
||||
{
|
||||
currentContext.DeviceType = dType;
|
||||
}
|
||||
|
||||
currentContext.Build(httpContext);
|
||||
await _next.Invoke(httpContext);
|
||||
}
|
||||
|
||||
private string GetClaimValue(Dictionary<string, IEnumerable<Claim>> claims, string type)
|
||||
{
|
||||
if(!claims.ContainsKey(type))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return claims[type].FirstOrDefault()?.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user