mirror of
https://github.com/bitwarden/server
synced 2026-01-06 10:34:01 +00:00
added installation id to current context.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using Bit.Core;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.Api.Middleware
|
||||
@@ -18,38 +21,49 @@ namespace Bit.Api.Middleware
|
||||
{
|
||||
if(httpContext.User != null)
|
||||
{
|
||||
var securityStampClaim = httpContext.User.Claims.FirstOrDefault(c => c.Type == "device");
|
||||
currentContext.DeviceIdentifier = securityStampClaim?.Value;
|
||||
var claimsDict = httpContext.User.Claims
|
||||
.GroupBy(c => c.Type)
|
||||
.ToDictionary(c => c.Key, c => c.Select(v => v));
|
||||
|
||||
var orgOwnerClaims = httpContext.User.Claims.Where(c => c.Type == "orgowner");
|
||||
if(orgOwnerClaims.Any())
|
||||
var clientId = GetClaimValue(claimsDict, "client_id");
|
||||
var clientSubject = GetClaimValue(claimsDict, "client_sub");
|
||||
if((clientId?.StartsWith("installation.") ?? false) && clientSubject != null)
|
||||
{
|
||||
currentContext.Organizations.AddRange(orgOwnerClaims.Select(c =>
|
||||
Guid idGuid;
|
||||
if(Guid.TryParse(clientSubject, out 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 System.Guid(c.Value),
|
||||
Id = new Guid(c.Value),
|
||||
Type = Core.Enums.OrganizationUserType.Owner
|
||||
}));
|
||||
}
|
||||
|
||||
var orgAdminClaims = httpContext.User.Claims.Where(c => c.Type == "orgadmin");
|
||||
if(orgAdminClaims.Any())
|
||||
if(claimsDict.ContainsKey("orgadmin"))
|
||||
{
|
||||
currentContext.Organizations.AddRange(orgAdminClaims.Select(c =>
|
||||
currentContext.Organizations.AddRange(claimsDict["orgadmin"].Select(c =>
|
||||
new CurrentContext.CurrentContentOrganization
|
||||
{
|
||||
Id = new System.Guid(c.Value),
|
||||
Id = new Guid(c.Value),
|
||||
Type = Core.Enums.OrganizationUserType.Admin
|
||||
}));
|
||||
}
|
||||
|
||||
var orgUserClaims = httpContext.User.Claims.Where(c => c.Type == "orguser");
|
||||
if(orgUserClaims.Any())
|
||||
if(claimsDict.ContainsKey("orguser"))
|
||||
{
|
||||
currentContext.Organizations.AddRange(orgUserClaims.Select(c =>
|
||||
currentContext.Organizations.AddRange(claimsDict["orguser"].Select(c =>
|
||||
new CurrentContext.CurrentContentOrganization
|
||||
{
|
||||
Id = new System.Guid(c.Value),
|
||||
Id = new Guid(c.Value),
|
||||
Type = Core.Enums.OrganizationUserType.User
|
||||
}));
|
||||
}
|
||||
@@ -62,5 +76,15 @@ namespace Bit.Api.Middleware
|
||||
|
||||
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