last ...
This commit is contained in:
@@ -13,6 +13,7 @@ using Microsoft.Extensions.Configuration;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using IdentityModel.AspNetCore.OAuth2Introspection;
|
using IdentityModel.AspNetCore.OAuth2Introspection;
|
||||||
using xHttpService.Interfaces;
|
using xHttpService.Interfaces;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
|
||||||
namespace xIdentityService.DI
|
namespace xIdentityService.DI
|
||||||
{
|
{
|
||||||
@@ -124,6 +125,22 @@ namespace xIdentityService.DI
|
|||||||
source.AddSingleton<IXPolicyHandler, XPolicyHandler>();
|
source.AddSingleton<IXPolicyHandler, XPolicyHandler>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void AddXRoleHierarchyResolver(
|
||||||
|
this IServiceCollection source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Check Provider ...
|
||||||
|
var providerService = source.GetRegisteredService<IXRoleProvider>();
|
||||||
|
if (providerService.IsNull())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
source.AddScoped<IXRoleHierarchyResolver, XRoleHierarchyResolver>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Use Authentication
|
/// Use Authentication
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
using System;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using IdentityModel;
|
using IdentityModel;
|
||||||
using xCommons.Constants;
|
using xCommons.Constants;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
|
using xIdentityModels.Dtos;
|
||||||
using xIdentityModels.Models;
|
using xIdentityModels.Models;
|
||||||
using xCommons.Authorization;
|
using xCommons.Authorization;
|
||||||
|
using xIdentityService.Models;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
@@ -12,73 +13,41 @@ namespace xIdentityService.Extensions
|
|||||||
{
|
{
|
||||||
public static class XPolicyDefinitionExtensions
|
public static class XPolicyDefinitionExtensions
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Split Comma Separated Values (same style as XTag.References) ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="source"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
// public static List<string> ToCsvList(this string source)
|
|
||||||
// {
|
|
||||||
// //
|
|
||||||
// if (source.IsNullOrEmpty())
|
|
||||||
// {
|
|
||||||
// return [];
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //
|
|
||||||
// return source
|
|
||||||
// .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
|
|
||||||
// .Select(s => s.Trim())
|
|
||||||
// .Where(s => !s.IsNullOrEmpty())
|
|
||||||
// .Distinct()
|
|
||||||
// .ToList();
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Join Values as Comma Separated String ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="source"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
// public static string ToCsv(this IEnumerable<string> source)
|
|
||||||
// {
|
|
||||||
// //
|
|
||||||
// if (!source.HasChild())
|
|
||||||
// {
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //
|
|
||||||
// return string.Join(",", source
|
|
||||||
// .Where(s => !s.IsNullOrEmpty())
|
|
||||||
// .Select(s => s.Trim())
|
|
||||||
// .Distinct());
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deserialize Json Array of Required Claims ...
|
/// Deserialize Json Array of Required Claims ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="json"></param>
|
/// <param name="json"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
// public static List<XRequiredClaimDto> ToRequiredClaims(this string json)
|
public static List<XRequiredClaimDto> ToRequiredClaims(this string json)
|
||||||
// {
|
{
|
||||||
// }
|
//
|
||||||
|
var result = new List<XRequiredClaimDto>();
|
||||||
|
|
||||||
/// <summary>
|
//
|
||||||
/// Serialize Required Claims to Json ...
|
// Validate ...
|
||||||
/// </summary>
|
if (json.IsNullOrEmpty())
|
||||||
/// <param name="source"></param>
|
{
|
||||||
/// <returns></returns>
|
return result;
|
||||||
// public static string ToRequiredClaimsJson(this IEnumerable<XRequiredClaimDto> source)
|
}
|
||||||
// {
|
|
||||||
// //
|
|
||||||
// if (!source.HasChild())
|
|
||||||
// {
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //
|
//
|
||||||
// return JsonConvert.SerializeObject(source);
|
try
|
||||||
// }
|
{
|
||||||
|
//
|
||||||
|
result = json.FromJSON<List<XRequiredClaimDto>>();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{ }
|
||||||
|
|
||||||
|
//
|
||||||
|
if (result.IsNull())
|
||||||
|
{
|
||||||
|
result = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Build AuthorizationPolicy exactly like XAuthorizationHelper does ...
|
/// Build AuthorizationPolicy exactly like XAuthorizationHelper does ...
|
||||||
@@ -86,91 +55,184 @@ namespace xIdentityService.Extensions
|
|||||||
/// <param name="source"></param>
|
/// <param name="source"></param>
|
||||||
/// <param name="effectiveRoles">already resolved by IXRoleHierarchyResolver</param>
|
/// <param name="effectiveRoles">already resolved by IXRoleHierarchyResolver</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
// public static AuthorizationPolicy ToAuthorizationPolicy(
|
public static AuthorizationPolicy ToAuthorizationPolicy(
|
||||||
// this XPolicyDefinition source,
|
this XPolicyDefinition source,
|
||||||
// IEnumerable<string> effectiveRoles
|
IEnumerable<string> effectiveRoles
|
||||||
// )
|
)
|
||||||
// {
|
{
|
||||||
// //
|
//
|
||||||
// if (source.IsNull() || source.Name.IsNullOrEmpty())
|
if (source.IsNull() || source.Name.IsNullOrEmpty())
|
||||||
// {
|
{
|
||||||
// return null;
|
return null;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// var builder = new AuthorizationPolicyBuilder();
|
var builder = new AuthorizationPolicyBuilder();
|
||||||
|
|
||||||
// //
|
//
|
||||||
// // Authenticated User ...
|
// Authenticated User ...
|
||||||
// if (source.RequireAuthenticatedUser)
|
if (source.RequireAuthenticatedUser)
|
||||||
// {
|
{
|
||||||
// builder.RequireAuthenticatedUser();
|
builder.RequireAuthenticatedUser();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// // Roles (RequiredRolesRequirement, handled by RequiredRolesHandler) ...
|
// Roles (RequiredRolesRequirement, handled by RequiredRolesHandler) ...
|
||||||
// if (effectiveRoles.HasChild())
|
if (effectiveRoles.HasChild())
|
||||||
// {
|
{
|
||||||
// builder.AddRequirements(new RequiredRolesRequirement(effectiveRoles.ToArray()));
|
builder.AddRequirements(new RequiredRolesRequirement(effectiveRoles.ToArray()));
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// // Scopes ...
|
// Scopes ...
|
||||||
// var scopes = source.RequiredScopes.ToCsvList();
|
var scopes = source.RequiredScopes.ParseListString<string>();
|
||||||
// if (scopes.HasChild())
|
if (scopes.HasChild())
|
||||||
// {
|
{
|
||||||
// builder.RequireClaim(JwtClaimTypes.Scope, scopes.ToArray());
|
builder.RequireClaim(JwtClaimTypes.Scope, [.. scopes]);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// // Custom Claims ...
|
// Custom Claims ...
|
||||||
// foreach (var claim in source.RequiredClaims.ToRequiredClaims())
|
foreach (var claim in source.RequiredClaims.ToRequiredClaims())
|
||||||
// {
|
{
|
||||||
// //
|
//
|
||||||
// if (claim.Type.IsNullOrEmpty())
|
if (claim.Type.IsNullOrEmpty())
|
||||||
// {
|
{
|
||||||
// continue;
|
continue;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// if (claim.Values.HasChild())
|
if (claim.Values.HasChild())
|
||||||
// {
|
{
|
||||||
// builder.RequireClaim(claim.Type, claim.Values.ToArray());
|
builder.RequireClaim(claim.Type, claim.Values.ToArray());
|
||||||
// }
|
}
|
||||||
// else
|
else
|
||||||
// {
|
{
|
||||||
// builder.RequireClaim(claim.Type);
|
builder.RequireClaim(claim.Type);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// // Enabled User (IsBanned=false & IsEnabled=true) ...
|
// Enabled User (IsBanned=false & IsEnabled=true) ...
|
||||||
// if (source.RequireEnabledUser)
|
if (source.RequireEnabledUser)
|
||||||
// {
|
{
|
||||||
// //
|
//
|
||||||
// builder.RequireClaim(
|
builder.RequireClaim(
|
||||||
// XCustomClaims.IsBanned,
|
XCustomClaims.IsBanned,
|
||||||
// false.ToString(),
|
false.ToString(),
|
||||||
// false.ToString().ToNormalString()
|
false.ToString().ToNormalString()
|
||||||
// );
|
);
|
||||||
|
|
||||||
// //
|
//
|
||||||
// builder.RequireClaim(
|
builder.RequireClaim(
|
||||||
// XCustomClaims.IsEnabled,
|
XCustomClaims.IsEnabled,
|
||||||
// true.ToString(),
|
true.ToString(),
|
||||||
// true.ToString().ToNormalString()
|
true.ToString().ToNormalString()
|
||||||
// );
|
);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// // Nothing Required => at least Deny Anonymous to avoid an Empty Policy ...
|
// Nothing Required => at least Deny Anonymous to avoid an Empty Policy ...
|
||||||
// if (!builder.Requirements.HasChild())
|
if (!builder.Requirements.HasChild())
|
||||||
// {
|
{
|
||||||
// builder.RequireAuthenticatedUser();
|
builder.RequireAuthenticatedUser();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// //
|
//
|
||||||
// return builder.Build();
|
return builder.Build();
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Build AuthorizationPolicy exactly like XAuthorizationHelper does ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="effectiveRoles">already resolved by IXRoleHierarchyResolver</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static AuthorizationPolicy ToAuthorizationPolicy(
|
||||||
|
this XPolicyDefinitionDto source,
|
||||||
|
IEnumerable<string> effectiveRoles
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (source.IsNull() || source.Name.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var builder = new AuthorizationPolicyBuilder();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Authenticated User ...
|
||||||
|
if (source.RequireAuthenticatedUser)
|
||||||
|
{
|
||||||
|
builder.RequireAuthenticatedUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Roles (RequiredRolesRequirement, handled by RequiredRolesHandler) ...
|
||||||
|
if (effectiveRoles.HasChild())
|
||||||
|
{
|
||||||
|
builder.AddRequirements(new RequiredRolesRequirement(effectiveRoles.ToArray()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Scopes ...
|
||||||
|
var scopes = source.RequiredScopes.ParseListString<string>();
|
||||||
|
if (scopes.HasChild())
|
||||||
|
{
|
||||||
|
builder.RequireClaim(JwtClaimTypes.Scope, [.. scopes]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Custom Claims ...
|
||||||
|
foreach (var claim in source.RequiredClaims.ToRequiredClaims())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (claim.Type.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (claim.Values.HasChild())
|
||||||
|
{
|
||||||
|
builder.RequireClaim(claim.Type, claim.Values.ToArray());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
builder.RequireClaim(claim.Type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Enabled User (IsBanned=false & IsEnabled=true) ...
|
||||||
|
if (source.RequireEnabledUser)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
builder.RequireClaim(
|
||||||
|
XCustomClaims.IsBanned,
|
||||||
|
false.ToString(),
|
||||||
|
false.ToString().ToNormalString()
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
builder.RequireClaim(
|
||||||
|
XCustomClaims.IsEnabled,
|
||||||
|
true.ToString(),
|
||||||
|
true.ToString().ToNormalString()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Nothing Required => at least Deny Anonymous to avoid an Empty Policy ...
|
||||||
|
if (!builder.Requirements.HasChild())
|
||||||
|
{
|
||||||
|
builder.RequireAuthenticatedUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return builder.Build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xIdentityModels.Dtos;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
using xModels.Dtos;
|
||||||
|
|
||||||
|
namespace xIdentityService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXAuthorizationProvider
|
||||||
|
{
|
||||||
|
//
|
||||||
|
#region Scope ...
|
||||||
|
Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
|
||||||
|
XQuery query,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XScopeDefinitionDto> GetScopeAsync(
|
||||||
|
string name,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XScopeDefinitionDto> AddScopeAsync(
|
||||||
|
XScopeDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XScopeDefinitionDto> UpdateScopeAsync(
|
||||||
|
string name,
|
||||||
|
XScopeDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<bool> RemoveScopeAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Role ...
|
||||||
|
Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XRoleDefinitionDto> GetRoleAsync(
|
||||||
|
string name,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XRoleDefinitionDto> AddRoleAsync(
|
||||||
|
XRoleDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XRoleDefinitionDto> UpdateRoleAsync(
|
||||||
|
string name,
|
||||||
|
XRoleDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<bool> RemoveRoleAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<int> CountUsersInRoleAsync(
|
||||||
|
string name,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Policy ...
|
||||||
|
Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
||||||
|
XQuery query,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||||
|
string name,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XPolicyDefinitionDto> AddPolicyAsync(
|
||||||
|
XPolicyDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||||
|
string name,
|
||||||
|
XPolicyDefinitionDto item,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<bool> RemovePolicyAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto userInfo,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
||||||
|
string name,
|
||||||
|
XUserClaimsInfoDto target,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||||
|
XUserClaimsInfoDto target,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Utilities ...
|
||||||
|
Task SyncAllAsync(CancellationToken cancellationToken = default);
|
||||||
|
Task InvalidateAsync(string policyName = null);
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace xIdentityService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXRoleHierarchyResolver
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve All Enabled Role Names which Weight >= minWeight ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="minWeight"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<string>> GetRolesByMinWeightAsync(
|
||||||
|
int minWeight,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Effective Roles for a Policy
|
||||||
|
/// (explicit RequiredRoles ∪ roles by MinRoleWeight ∪ parents chain) ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="explicitRoles"></param>
|
||||||
|
/// <param name="minRoleWeight"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<string>> ResolveEffectiveRolesAsync(
|
||||||
|
IEnumerable<string> explicitRoles,
|
||||||
|
int? minRoleWeight,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace xIdentityService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXScopeSyncService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implemented on xIds over IdentityServer4 ConfigurationDbContext ...
|
||||||
|
/// </summary>
|
||||||
|
public interface IXScopeSyncService
|
||||||
|
{
|
||||||
|
Task UpsertAsync(XScopeDefinition scope, CancellationToken cancellationToken = default);
|
||||||
|
Task RemoveAsync(string scopeName, CancellationToken cancellationToken = default);
|
||||||
|
Task<bool> IsApiResourceExistsAsync(string apiResourceName, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Implemented on xIds over RoleManager<IdentityRole> ...
|
||||||
|
/// </summary>
|
||||||
|
public interface IXRoleSyncService
|
||||||
|
{
|
||||||
|
Task UpsertAsync(XRoleDefinition role, CancellationToken cancellationToken = default);
|
||||||
|
Task RemoveAsync(string roleName, CancellationToken cancellationToken = default);
|
||||||
|
Task<int> CountUsersInRoleAsync(string roleName, CancellationToken cancellationToken = default);
|
||||||
|
Task<IEnumerable<string>> GetUserRolesAsync(string userSelectByParam, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace xIdentityService.Models
|
||||||
|
{
|
||||||
|
public class XPolicyEvaluationResultDto
|
||||||
|
{
|
||||||
|
public bool Passed { get; set; }
|
||||||
|
public string Policy { get; set; }
|
||||||
|
public List<string> FailedRequirements { get; set; } = new List<string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace xIdentityService.Models
|
||||||
|
{
|
||||||
|
public class XRequiredClaimDto
|
||||||
|
{
|
||||||
|
public string Type { get; set; }
|
||||||
|
public List<string> Values { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using xIdentityService.Interfaces;
|
||||||
|
|
||||||
|
namespace xIdentityService.Providers
|
||||||
|
{
|
||||||
|
public class XAuthorizationProvider : IXAuthorizationProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
using xCommons.Extensions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using xIdentityService.Extensions;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace xIdentityService.Providers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Resolve Policies from Cache → Repository → Default (static) Provider ...
|
||||||
|
/// </summary>
|
||||||
|
public class XDynamicPolicyProvider : IAuthorizationPolicyProvider
|
||||||
|
{
|
||||||
|
private readonly IXAuthorizationCache cache;
|
||||||
|
private readonly IServiceScopeFactory scopeFactory;
|
||||||
|
private readonly DefaultAuthorizationPolicyProvider fallback;
|
||||||
|
|
||||||
|
public XDynamicPolicyProvider(
|
||||||
|
IXAuthorizationCache cache,
|
||||||
|
IServiceScopeFactory scopeFactory,
|
||||||
|
IOptions<AuthorizationOptions> options
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
this.cache = cache;
|
||||||
|
this.scopeFactory = scopeFactory;
|
||||||
|
fallback = new DefaultAuthorizationPolicyProvider(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<AuthorizationPolicy> GetDefaultPolicyAsync() =>
|
||||||
|
fallback.GetDefaultPolicyAsync();
|
||||||
|
|
||||||
|
public async Task<AuthorizationPolicy> GetPolicyAsync(string policyName)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (policyName.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
return await fallback.GetPolicyAsync(policyName);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 1. Cache ...
|
||||||
|
if (cache.TryGetPolicy(policyName, out var cached))
|
||||||
|
{
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 2. Repository (needs a Scope because Provider is Singleton) ...
|
||||||
|
using (var scope = scopeFactory.CreateScope())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var repository = scope.ServiceProvider.GetService<IXPolicyProvider>();
|
||||||
|
var resolver = scope.ServiceProvider.GetService<IXRoleHierarchyResolver>();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!repository.IsNull() && !resolver.IsNull())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var normalized = policyName.ToNormalString();
|
||||||
|
var definition = await repository.FindOneAsync(
|
||||||
|
predicate: p => p.IsEnabled && p.Name.ToLower() == normalized
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var requiredRoles = definition.RequiredRoles
|
||||||
|
.ParseListString<string>();
|
||||||
|
if (!definition.IsNull())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var roles = await resolver.ResolveEffectiveRolesAsync(
|
||||||
|
explicitRoles: requiredRoles,
|
||||||
|
minRoleWeight: definition.MinRoleWeight
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var policy = definition.ToAuthorizationPolicy(roles);
|
||||||
|
if (!policy.IsNull())
|
||||||
|
{
|
||||||
|
cache.SetPolicy(policyName, policy);
|
||||||
|
return policy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 3. Fallback to Static Policies (LocalApi.PolicyName, XPolicies.*) ...
|
||||||
|
return await fallback.GetPolicyAsync(policyName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
|
||||||
|
namespace xIdentityService.Providers
|
||||||
|
{
|
||||||
|
public class XRoleHierarchyResolver : IXRoleHierarchyResolver
|
||||||
|
{
|
||||||
|
private readonly IXRoleProvider provider;
|
||||||
|
|
||||||
|
public XRoleHierarchyResolver(
|
||||||
|
IXRoleProvider provider
|
||||||
|
)
|
||||||
|
{
|
||||||
|
this.provider = provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<string>> GetRolesByMinWeightAsync(
|
||||||
|
int minWeight,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var roles = await provider.FindManyAsync(
|
||||||
|
predicate: r => r.IsEnabled && r.Weight >= minWeight,
|
||||||
|
orderBuilder: q => q.OrderBy(r => r.Weight),
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = roles
|
||||||
|
.Select(r => r.Name.ToNormalString())
|
||||||
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<string>> ResolveEffectiveRolesAsync(
|
||||||
|
IEnumerable<string> explicitRoles,
|
||||||
|
int? minRoleWeight,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Explicit Roles ...
|
||||||
|
if (explicitRoles.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
foreach (var role in explicitRoles)
|
||||||
|
{
|
||||||
|
result.Add(role.ToNormalString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Roles by Weight ...
|
||||||
|
if (minRoleWeight.HasValue)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var byWeight = await GetRolesByMinWeightAsync(
|
||||||
|
minRoleWeight.Value,
|
||||||
|
cancellationToken
|
||||||
|
);
|
||||||
|
foreach (var role in byWeight)
|
||||||
|
{
|
||||||
|
result.Add(role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Parent Chain: any Role whose ParentRoleName is in result inherits it ...
|
||||||
|
var all = await provider.GetAllAsync(cancellationToken: cancellationToken);
|
||||||
|
bool changed;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
//
|
||||||
|
changed = false;
|
||||||
|
foreach (var role in all.Where(r => r.IsEnabled && !r.ParentRoleName.IsNullOrEmpty()))
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (result.Contains(role.ParentRoleName) &&
|
||||||
|
result.Add(role.Name.ToNormalString()))
|
||||||
|
{
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (changed);
|
||||||
|
|
||||||
|
//
|
||||||
|
return [.. result];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user