2022-06-29 19:46:41 -04:00
|
|
|
|
using System.Security.Claims;
|
2023-10-27 03:38:29 +10:00
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2023-01-13 15:02:53 +01:00
|
|
|
|
using Bit.Core.Identity;
|
2017-01-11 00:34:16 -05:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
2020-11-10 15:15:29 -05:00
|
|
|
|
using Bit.Core.Utilities;
|
2023-11-20 16:32:23 -05:00
|
|
|
|
using Duende.IdentityServer.Models;
|
|
|
|
|
|
using Duende.IdentityServer.Services;
|
2017-01-11 00:34:16 -05:00
|
|
|
|
|
2022-09-27 18:30:37 +02:00
|
|
|
|
namespace Bit.Identity.IdentityServer;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-01-11 00:34:16 -05:00
|
|
|
|
public class ProfileService : IProfileService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserService _userService;
|
2017-04-05 15:31:33 -04:00
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
2017-01-11 00:34:16 -05:00
|
|
|
|
private readonly IProviderUserRepository _providerUserRepository;
|
2021-07-08 17:05:32 +02:00
|
|
|
|
private readonly IProviderOrganizationRepository _providerOrganizationRepository;
|
2017-01-11 00:34:16 -05:00
|
|
|
|
private readonly ILicensingService _licensingService;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
private readonly ICurrentContext _currentContext;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-01-11 00:34:16 -05:00
|
|
|
|
public ProfileService(
|
2017-01-24 22:15:21 -05:00
|
|
|
|
IUserService userService,
|
2017-04-05 15:31:33 -04:00
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
2021-06-30 09:35:26 +02:00
|
|
|
|
IProviderUserRepository providerUserRepository,
|
2021-02-04 12:54:21 -06:00
|
|
|
|
IProviderOrganizationRepository providerOrganizationRepository,
|
|
|
|
|
|
ILicensingService licensingService,
|
2017-01-11 00:34:16 -05:00
|
|
|
|
ICurrentContext currentContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
_userService = userService;
|
2017-04-05 15:31:33 -04:00
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
_providerUserRepository = providerUserRepository;
|
2021-07-08 17:05:32 +02:00
|
|
|
|
_providerOrganizationRepository = providerOrganizationRepository;
|
2017-08-16 17:08:20 -04:00
|
|
|
|
_licensingService = licensingService;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
_currentContext = currentContext;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-01-11 00:34:16 -05:00
|
|
|
|
|
2021-02-04 12:54:21 -06:00
|
|
|
|
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
|
2017-01-11 00:34:16 -05:00
|
|
|
|
{
|
2018-04-03 14:31:33 -04:00
|
|
|
|
var existingClaims = context.Subject.Claims;
|
2017-01-25 22:31:14 -05:00
|
|
|
|
var newClaims = new List<Claim>();
|
2017-01-11 00:34:16 -05:00
|
|
|
|
|
2017-01-24 22:15:21 -05:00
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(context.Subject);
|
|
|
|
|
|
if (user != null)
|
2017-01-11 00:34:16 -05:00
|
|
|
|
{
|
2017-01-25 22:31:14 -05:00
|
|
|
|
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);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var claim in CoreHelpers.BuildIdentityClaims(user, orgs, providers, isPremium))
|
2017-01-24 22:15:21 -05:00
|
|
|
|
{
|
2020-11-10 15:15:29 -05:00
|
|
|
|
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)
|
|
|
|
|
|
);
|
2017-01-24 22:15:21 -05:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-01-24 22:15:21 -05:00
|
|
|
|
|
2017-01-25 22:31:14 -05:00
|
|
|
|
// filter out any of the new claims
|
|
|
|
|
|
var existingClaimsToKeep = existingClaims
|
2020-07-30 17:00:13 -04:00
|
|
|
|
.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();
|
2017-01-25 22:31:14 -05:00
|
|
|
|
|
|
|
|
|
|
newClaims.AddRange(existingClaimsToKeep);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (newClaims.Any())
|
2017-01-24 22:15:21 -05:00
|
|
|
|
{
|
2020-07-30 17:00:13 -04:00
|
|
|
|
context.IssuedClaims.AddRange(newClaims);
|
2017-01-24 22:15:21 -05:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-01-24 22:15:21 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task IsActiveAsync(IsActiveContext context)
|
|
|
|
|
|
{
|
2023-01-13 15:02:53 +01:00
|
|
|
|
var securityTokenClaim = context.Subject?.Claims.FirstOrDefault(c => c.Type == Claims.SecurityStamp);
|
2017-01-25 00:38:09 -05:00
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(context.Subject);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (user != null && securityTokenClaim != null)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2017-01-24 22:15:21 -05:00
|
|
|
|
context.IsActive = string.Equals(user.SecurityStamp, securityTokenClaim.Value,
|
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-01-24 22:15:21 -05:00
|
|
|
|
context.IsActive = true;
|
2017-01-11 00:34:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|