diff --git a/Extensions/XModelExtensions.cs b/Extensions/XModelExtensions.cs
index 9a987f4..942357d 100644
--- a/Extensions/XModelExtensions.cs
+++ b/Extensions/XModelExtensions.cs
@@ -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
{
+ ///
+ /// Converts OpenActionUserInfoDto to XPersonDto ...
+ ///
+ ///
+ ///
public static XPersonDto ToXPersonDto(this XOpenActionUserInfoDto source)
{
//
@@ -20,5 +34,260 @@ namespace xIdentityService.Extensions
//
return result;
}
+
+ ///
+ /// Check an Authorization which Can Applyed or not ...
+ ///
+ ///
+ ///
+ 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;
+ }
+
+ ///
+ /// Extract Effective Authorization ...
+ ///
+ ///
+ ///
+ ///
+ public static XEffectiveAuthorizationDto ToEffectiveAuthorization(
+ this XPolicyDefinition source,
+ IEnumerable 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()?
+ .ToHashSet() ?? [],
+ EffectiveClaims = source.RequiredClaims.ToRequiredClaims()
+ };
+
+ //
+ return result;
+ }
+
+ ///
+ /// Extract Effective Authorization ...
+ ///
+ ///
+ ///
+ ///
+ public static XEffectiveAuthorizationDto ToEffectiveAuthorization(
+ this XPolicyDefinitionDto source,
+ IEnumerable effectiveRoles
+ )
+ {
+ //
+ XEffectiveAuthorizationDto result = null;
+ if (source.IsNull() ||
+ source.Name.IsNullOrEmpty())
+ {
+ return result;
+ }
+
+ //
+ var scopes = source.RequiredScopes.ParseListString();
+ 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;
+ }
+
+ ///
+ /// Merge to Effective Authorization Samples ...
+ ///
+ ///
+ ///
+ ///
+ public static XEffectiveAuthorizationDto MergeWith(
+ this XEffectiveAuthorizationDto source,
+ XEffectiveAuthorizationDto other
+ )
+ {
+ //
+ if (source.IsNull()) return other;
+ if (other.IsNull()) return source;
+
+ //
+ // Roles
+ var mergedRoles = new HashSet(source.EffectiveRoles);
+ mergedRoles.UnionWith(other.EffectiveRoles);
+
+ //
+ // Scopes
+ var mergedScopes = new HashSet(source.EffectiveScopes);
+ mergedScopes.UnionWith(other.EffectiveScopes);
+
+ //
+ // Claims
+ var mergedClaims = new List(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;
+ }
+
+ ///
+ /// Make Authorization Policy Based on Dynamically loaded Descriptions ...
+ ///
+ ///
+ ///
+ 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;
+ }
}
}
\ No newline at end of file
diff --git a/Interfaces/IXAuthorizationProvider.cs b/Interfaces/IXAuthorizationProvider.cs
index 99009e6..07e875e 100644
--- a/Interfaces/IXAuthorizationProvider.cs
+++ b/Interfaces/IXAuthorizationProvider.cs
@@ -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
{
diff --git a/Interfaces/IXSyncService.cs b/Interfaces/IXSyncService.cs
index 1442218..509a427 100644
--- a/Interfaces/IXSyncService.cs
+++ b/Interfaces/IXSyncService.cs
@@ -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
{
+ ///
+ /// Implemented on xIds over IdentityServer4 ConfigurationDbContext ...
+ ///
public interface IXScopeSyncService
{
- ///
- /// Implemented on xIds over IdentityServer4 ConfigurationDbContext ...
- ///
- public interface IXScopeSyncService
- {
- Task UpsertAsync(XScopeDefinition scope, CancellationToken cancellationToken = default);
- Task RemoveAsync(string scopeName, CancellationToken cancellationToken = default);
- Task IsApiResourceExistsAsync(string apiResourceName, CancellationToken cancellationToken = default);
- }
+ Task UpsertAsync(
+ XScopeDefinition scope,
+ CancellationToken cancellationToken = default
+ );
+ Task RemoveAsync(
+ string scopeName,
+ CancellationToken cancellationToken = default
+ );
+ Task IsApiResourceExistsAsync(
+ string apiResourceName,
+ CancellationToken cancellationToken = default
+ );
+ }
- ///
- /// Implemented on xIds over RoleManager<IdentityRole> ...
- ///
- public interface IXRoleSyncService
- {
- Task UpsertAsync(XRoleDefinition role, CancellationToken cancellationToken = default);
- Task RemoveAsync(string roleName, CancellationToken cancellationToken = default);
- Task CountUsersInRoleAsync(string roleName, CancellationToken cancellationToken = default);
- Task> GetUserRolesAsync(string userSelectByParam, CancellationToken cancellationToken = default);
- }
+ ///
+ /// Implemented on xIds over RoleManager<IdentityRole> ...
+ ///
+ public interface IXRoleSyncService
+ {
+ Task UpsertAsync(
+ XRoleDefinition role,
+ CancellationToken cancellationToken = default
+ );
+ Task RemoveAsync(
+ string roleName,
+ CancellationToken cancellationToken = default
+ );
+ Task CountUsersInRoleAsync(
+ string roleName,
+ CancellationToken cancellationToken = default
+ );
+ Task> GetUserRolesAsync(
+ string userSelectByParam,
+ CancellationToken cancellationToken = default
+ );
}
}
\ No newline at end of file
diff --git a/Models/XEffectiveAuthorizationDto.cs b/Models/XEffectiveAuthorizationDto.cs
new file mode 100644
index 0000000..dabaa23
--- /dev/null
+++ b/Models/XEffectiveAuthorizationDto.cs
@@ -0,0 +1,38 @@
+using System.Collections.Generic;
+
+namespace xIdentityService.Models
+{
+ public class XEffectiveAuthorizationDto
+ {
+ ///
+ /// Policy Name ...
+ ///
+ public string PolicyName { get; set; }
+
+ ///
+ /// Required Authenticated User ...
+ ///
+ public bool RequireAuthenticatedUser { get; set; }
+
+ ///
+ /// Check Policy has Enabled feature or not ...
+ /// (IsEnabled/IsBanned) ...
+ ///
+ public bool RequireEnabledUser { get; set; }
+
+ ///
+ /// Effective Roles ...
+ ///
+ public HashSet EffectiveRoles { get; set; } = [];
+
+ ///
+ /// Effective Scopes ...
+ ///
+ public HashSet EffectiveScopes { get; set; } = [];
+
+ ///
+ /// Effective Claims ...
+ ///
+ public List EffectiveClaims { get; set; } = [];
+ }
+}
\ No newline at end of file
diff --git a/Models/XPolicyEvaluationResultDto.cs b/Models/XPolicyEvaluationResultDto.cs
index 982dc1f..db68025 100644
--- a/Models/XPolicyEvaluationResultDto.cs
+++ b/Models/XPolicyEvaluationResultDto.cs
@@ -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 FailedRequirements { get; set; } = new List();
+ public List FailedRequirements { get; set; } = [];
}
}
\ No newline at end of file
diff --git a/Models/XRequiredClaimDto.cs b/Models/XRequiredClaimDto.cs
index ca9df0f..d507845 100644
--- a/Models/XRequiredClaimDto.cs
+++ b/Models/XRequiredClaimDto.cs
@@ -1,3 +1,5 @@
+using System.Collections.Generic;
+
namespace xIdentityService.Models
{
public class XRequiredClaimDto
diff --git a/Partial/XAuthorizationProvider+Policy.cs b/Partial/XAuthorizationProvider+Policy.cs
new file mode 100644
index 0000000..5843d03
--- /dev/null
+++ b/Partial/XAuthorizationProvider+Policy.cs
@@ -0,0 +1,9 @@
+namespace xIdentityService.Providers
+{
+ public partial class XAuthorizationProvider
+ {
+ //
+ #region Policy ...
+ #endregion
+ }
+}
\ No newline at end of file
diff --git a/Providers/XAuthorizationProvider.cs b/Providers/XAuthorizationProvider.cs
index 7d45e39..c6580a3 100644
--- a/Providers/XAuthorizationProvider.cs
+++ b/Providers/XAuthorizationProvider.cs
@@ -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 ...
+ ///
+ /// Clear Local Cache and Notify other Instances ...
+ ///
+ public async Task InvalidateAsync(string policyName = null)
+ {
+ //
+ if (policyName.IsNullOrEmpty())
+ {
+ cache.Clear();
+ }
+ else
+ {
+ cache.RemovePolicy(policyName);
+ }
+ }
+ #endregion
}
}
\ No newline at end of file