Files
server/src/Core/IdentityServer/ProfileService.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
3.3 KiB
C#
Raw Normal View History

using System.Security.Claims;
using Bit.Core.Context;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Utilities;
using IdentityServer4.Models;
using IdentityServer4.Services;
2017-05-05 16:11:50 -04:00
namespace Bit.Core.IdentityServer;
2022-08-29 14:53:16 -04:00
public class ProfileService : IProfileService
{
private readonly IUserService _userService;
2017-04-05 15:31:33 -04:00
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly IProviderUserRepository _providerUserRepository;
private readonly IProviderOrganizationRepository _providerOrganizationRepository;
private readonly ILicensingService _licensingService;
private readonly ICurrentContext _currentContext;
2022-08-29 14:53:16 -04:00
public ProfileService(
IUserService userService,
2017-04-05 15:31:33 -04:00
IOrganizationUserRepository organizationUserRepository,
2021-06-30 09:35:26 +02:00
IProviderUserRepository providerUserRepository,
IProviderOrganizationRepository providerOrganizationRepository,
2018-04-03 14:31:33 -04:00
ILicensingService licensingService,
ICurrentContext currentContext)
{
_userService = userService;
2017-04-05 15:31:33 -04:00
_organizationUserRepository = organizationUserRepository;
2021-06-30 09:35:26 +02:00
_providerUserRepository = providerUserRepository;
_providerOrganizationRepository = providerOrganizationRepository;
_licensingService = licensingService;
_currentContext = currentContext;
2022-08-29 14:53:16 -04:00
}
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
2018-04-03 14:31:33 -04:00
var existingClaims = context.Subject.Claims;
var newClaims = new List<Claim>();
var user = await _userService.GetUserByPrincipalAsync(context.Subject);
if (user != null)
{
var isPremium = await _licensingService.ValidateUserPremiumAsync(user);
var orgs = await _currentContext.OrganizationMembershipAsync(_organizationUserRepository, user.Id);
2017-01-25 00:38:09 -05:00
var providers = await _currentContext.ProviderMembershipAsync(_providerUserRepository, user.Id);
foreach (var claim in CoreHelpers.BuildIdentityClaims(user, orgs, providers, isPremium))
{
var upperValue = claim.Value.ToUpperInvariant();
var isBool = upperValue == "TRUE" || upperValue == "FALSE";
newClaims.Add(isBool ?
new Claim(claim.Key, claim.Value, ClaimValueTypes.Boolean) :
new Claim(claim.Key, claim.Value)
);
}
2022-08-29 14:53:16 -04:00
}
// filter out any of the new claims
var existingClaimsToKeep = existingClaims
.Where(c => !c.Type.StartsWith("org") &&
(newClaims.Count == 0 || !newClaims.Any(nc => nc.Type == c.Type)))
2017-04-05 15:31:33 -04:00
.ToList();
newClaims.AddRange(existingClaimsToKeep);
if (newClaims.Any())
{
context.IssuedClaims.AddRange(newClaims);
}
2022-08-29 14:53:16 -04:00
}
public async Task IsActiveAsync(IsActiveContext context)
{
var securityTokenClaim = context.Subject?.Claims.FirstOrDefault(c => c.Type == "sstamp");
2017-01-25 00:38:09 -05:00
var user = await _userService.GetUserByPrincipalAsync(context.Subject);
if (user != null && securityTokenClaim != null)
{
context.IsActive = string.Equals(user.SecurityStamp, securityTokenClaim.Value,
StringComparison.InvariantCultureIgnoreCase);
return;
}
else
{
context.IsActive = true;
}
}
}