This commit is contained in:
2026-11-16 23:11:33 +03:30
parent cd9ee26c0f
commit 19fece89bd
8 changed files with 420 additions and 32 deletions
+269
View File
@@ -1,10 +1,24 @@
using System.Linq;
using xModels.Dtos;
using xCommons.Extensions;
using xIdentityModels.Dtos;
using xIdentityService.Models;
using xIdentityModels.Models;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using xCommons.Authorization;
using IdentityModel;
using xCommons.Constants;
namespace xIdentityService.Extensions
{
public static class XModelExtensions
{
/// <summary>
/// Converts OpenActionUserInfoDto to XPersonDto ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XPersonDto ToXPersonDto(this XOpenActionUserInfoDto source)
{
//
@@ -20,5 +34,260 @@ namespace xIdentityService.Extensions
//
return result;
}
/// <summary>
/// Check an Authorization which Can Applyed or not ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool HasRequirements(
this XEffectiveAuthorizationDto source
)
{
//
var result =
!source.IsNullOrDefault() &&
!source.PolicyName.IsNullOrEmpty() &&
source.RequireEnabledUser ||
source.RequireAuthenticatedUser ||
source.EffectiveRoles.HasChild() ||
source.EffectiveScopes.HasChild() ||
source.EffectiveClaims.HasChild();
//
return result;
}
/// <summary>
/// Extract Effective Authorization ...
/// </summary>
/// <param name="source"></param>
/// <param name="effectiveRoles"></param>
/// <returns></returns>
public static XEffectiveAuthorizationDto ToEffectiveAuthorization(
this XPolicyDefinition source,
IEnumerable<string> effectiveRoles
)
{
//
XEffectiveAuthorizationDto result = null;
if (source.IsNull() ||
source.Name.IsNullOrEmpty())
{
return result;
}
//
result = new XEffectiveAuthorizationDto
{
//
PolicyName = source.Name,
EffectiveRoles = [.. effectiveRoles],
RequireEnabledUser = source.RequireEnabledUser,
RequireAuthenticatedUser = source.RequireAuthenticatedUser,
EffectiveScopes = source.RequiredScopes
.ParseListString<string>()?
.ToHashSet() ?? [],
EffectiveClaims = source.RequiredClaims.ToRequiredClaims()
};
//
return result;
}
/// <summary>
/// Extract Effective Authorization ...
/// </summary>
/// <param name="source"></param>
/// <param name="effectiveRoles"></param>
/// <returns></returns>
public static XEffectiveAuthorizationDto ToEffectiveAuthorization(
this XPolicyDefinitionDto source,
IEnumerable<string> effectiveRoles
)
{
//
XEffectiveAuthorizationDto result = null;
if (source.IsNull() ||
source.Name.IsNullOrEmpty())
{
return result;
}
//
var scopes = source.RequiredScopes.ParseListString<string>();
result = new XEffectiveAuthorizationDto
{
PolicyName = source.Name,
EffectiveScopes = scopes.HasChild()
? [.. scopes]
: [],
EffectiveRoles = effectiveRoles.HasChild()
? [.. effectiveRoles]
: [],
RequireEnabledUser = source.RequireEnabledUser,
EffectiveClaims = source.RequiredClaims.ToRequiredClaims(),
RequireAuthenticatedUser = source.RequireAuthenticatedUser
};
//
return result;
}
/// <summary>
/// Merge to Effective Authorization Samples ...
/// </summary>
/// <param name="source"></param>
/// <param name="other"></param>
/// <returns></returns>
public static XEffectiveAuthorizationDto MergeWith(
this XEffectiveAuthorizationDto source,
XEffectiveAuthorizationDto other
)
{
//
if (source.IsNull()) return other;
if (other.IsNull()) return source;
//
// Roles
var mergedRoles = new HashSet<string>(source.EffectiveRoles);
mergedRoles.UnionWith(other.EffectiveRoles);
//
// Scopes
var mergedScopes = new HashSet<string>(source.EffectiveScopes);
mergedScopes.UnionWith(other.EffectiveScopes);
//
// Claims
var mergedClaims = new List<XRequiredClaimDto>(source.EffectiveClaims);
mergedClaims.AddRange(other.EffectiveClaims);
//
var result = new XEffectiveAuthorizationDto
{
//
EffectiveRoles = mergedRoles,
EffectiveScopes = mergedScopes,
EffectiveClaims = mergedClaims,
PolicyName = source.PolicyName ?? other.PolicyName,
RequireEnabledUser =
source.RequireEnabledUser || other.RequireEnabledUser,
RequireAuthenticatedUser =
source.RequireAuthenticatedUser || other.RequireAuthenticatedUser,
};
//
return result;
}
/// <summary>
/// Make Authorization Policy Based on Dynamically loaded Descriptions ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static AuthorizationPolicy ToAuthorizationPolicy(
this XEffectiveAuthorizationDto source
)
{
//
AuthorizationPolicy result = null;
if (source.IsNull() ||
source.PolicyName.IsNullOrEmpty())
{
return result;
}
//
var builder = new AuthorizationPolicyBuilder();
//
// Check and Handle Authenticated User ...
if (source.RequireAuthenticatedUser)
{
builder.RequireAuthenticatedUser();
}
//
// Check and Apply Effective Roles ...
if (source.EffectiveRoles.HasChild())
{
//
builder
.AddRequirements(new RequiredRolesRequirement([.. source.EffectiveRoles]));
}
//
// Check and Apply Effective Scopes ...
if (source.EffectiveScopes.HasChild())
{
//
builder.RequireClaim(
JwtClaimTypes.Scope,
[.. source.EffectiveScopes]);
}
//
// Check and Apply Effective Claims ...
foreach (var claim in source.EffectiveClaims)
{
//
// Validate Type ...
if (claim.Type.IsNullOrEmpty())
{
continue;
}
//
if (claim.Values.HasChild())
{
//
builder
.RequireClaim(
claim.Type,
[.. claim.Values]);
}
else
{
builder.RequireClaim(claim.Type);
}
}
//
// Check, Apply and Handle
// IsBanned and IsEnabled Claims ...
if (source.RequireEnabledUser &&
source.RequireAuthenticatedUser)
{
//
builder.RequireClaim(
XCustomClaims.IsBanned,
false.ToString(),
false.ToString()
.ToNormalString());
//
builder.RequireClaim(
XCustomClaims.IsEnabled,
true.ToString(),
true.ToString()
.ToNormalString());
}
//
// Last Validation ...
if (!builder.Requirements.HasChild())
{
builder.RequireAuthenticatedUser();
}
//
// Build and Create Result ...
result = builder.Build();
//
return result;
}
}
}
+6 -7
View File
@@ -1,11 +1,10 @@
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;
using System.Threading;
using xIdentityModels.Dtos;
using System.Threading.Tasks;
using xIdentityModels.Models;
using System.Collections.Generic;
using xIdentityService.Models;
namespace xIdentityService.Interfaces
{
+40 -22
View File
@@ -1,31 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using xIdentityModels.Models;
using System.Collections.Generic;
namespace xIdentityService.Interfaces
{
/// <summary>
/// Implemented on xIds over IdentityServer4 ConfigurationDbContext ...
/// </summary>
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);
}
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&lt;IdentityRole&gt; ...
/// </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);
}
/// <summary>
/// Implemented on xIds over RoleManager&lt;IdentityRole&gt; ...
/// </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
);
}
}
+38
View File
@@ -0,0 +1,38 @@
using System.Collections.Generic;
namespace xIdentityService.Models
{
public class XEffectiveAuthorizationDto
{
/// <summary>
/// Policy Name ...
/// </summary>
public string PolicyName { get; set; }
/// <summary>
/// Required Authenticated User ...
/// </summary>
public bool RequireAuthenticatedUser { get; set; }
/// <summary>
/// Check Policy has Enabled feature or not ...
/// (IsEnabled/IsBanned) ...
/// </summary>
public bool RequireEnabledUser { get; set; }
/// <summary>
/// Effective Roles ...
/// </summary>
public HashSet<string> EffectiveRoles { get; set; } = [];
/// <summary>
/// Effective Scopes ...
/// </summary>
public HashSet<string> EffectiveScopes { get; set; } = [];
/// <summary>
/// Effective Claims ...
/// </summary>
public List<XRequiredClaimDto> EffectiveClaims { get; set; } = [];
}
}
+3 -1
View File
@@ -1,9 +1,11 @@
using System.Collections.Generic;
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>();
public List<string> FailedRequirements { get; set; } = [];
}
}
+2
View File
@@ -1,3 +1,5 @@
using System.Collections.Generic;
namespace xIdentityService.Models
{
public class XRequiredClaimDto
+9
View File
@@ -0,0 +1,9 @@
namespace xIdentityService.Providers
{
public partial class XAuthorizationProvider
{
//
#region Policy ...
#endregion
}
}
+53 -2
View File
@@ -1,9 +1,60 @@
using xCommons.Extensions;
using System.Threading.Tasks;
using xIdentityService.Interfaces;
//
// TODO: Remove this to xIds ...
namespace xIdentityService.Providers
{
public class XAuthorizationProvider : IXAuthorizationProvider
public partial class XAuthorizationProvider : IXAuthorizationProvider
{
//
private readonly IXRoleProvider roles;
private readonly IXScopeProvider scopes;
private readonly IXPolicyProvider policies;
private readonly IXRoleSyncService roleSync;
private readonly IXAuthorizationCache cache;
private readonly IXScopeSyncService scopeSync;
private readonly IXRoleHierarchyResolver resolver;
// private readonly IXEventServiceProvider eventService;
public XAuthorizationProvider(
IXRoleProvider roles,
IXScopeProvider scopes,
IXPolicyProvider policies,
IXRoleSyncService roleSync,
IXAuthorizationCache cache,
IXScopeSyncService scopeSync,
IXRoleHierarchyResolver resolver
)
{
//
this.roles = roles;
this.cache = cache;
this.scopes = scopes;
this.policies = policies;
this.roleSync = roleSync;
this.resolver = resolver;
this.scopeSync = scopeSync;
}
//
#region Commons ...
/// <summary>
/// Clear Local Cache and Notify other Instances ...
/// </summary>
public async Task InvalidateAsync(string policyName = null)
{
//
if (policyName.IsNullOrEmpty())
{
cache.Clear();
}
else
{
cache.RemovePolicy(policyName);
}
}
#endregion
}
}