Compare commits

..
1 Commits
Author SHA1 Message Date
saherelm b7a00244df last ... 2026-11-27 19:21:58 +03:30
4 changed files with 130 additions and 62 deletions
-32
View File
@@ -141,38 +141,6 @@ namespace xIdentityService.DI
source.AddScoped<IXRoleHierarchyResolver, XRoleHierarchyResolver>();
}
public static void AddXAuthorizationProvider(
this IServiceCollection source
)
{
//
// Validate Requirements ...
var roles = source.GetRegisteredService<IXRoleProvider>();
var scopes = source.GetRegisteredService<IXScopeProvider>();
var policies = source.GetRegisteredService<IXPolicyProvider>();
var cache = source.GetRegisteredService<IXAuthorizationCache>();
var resolver = source.GetRegisteredService<IXRoleHierarchyResolver>();
// var roleSync = source.GetRegisteredService<IXRoleSyncService>();
// var scopeSync = source.GetRegisteredService<IXScopeSyncService>();
//
var isInvalid =
roles.IsNull() ||
cache.IsNull() ||
scopes.IsNull() ||
policies.IsNull() ||
resolver.IsNull();
// roleSync.IsNull() ||
// scopeSync.IsNull();
if (isInvalid)
{
XException.InvalidConfiguration.Throw();
}
//
source.AddScoped<IXAuthorizationProvider, XAuthorizationProvider>();
}
/// <summary>
/// Use Authentication
/// </summary>
+20
View File
@@ -0,0 +1,20 @@
using xIdentityService.Models;
using xIdentityService.Interfaces;
using Microsoft.Extensions.DependencyInjection;
namespace xIdentityService.Extensions
{
public static class XAuthorizationExtensions
{
/// <summary>
/// Add Authorization Cache ...
/// </summary>
/// <param name="source"></param>
public static void AddXAuthorizationCache(
this IServiceCollection source
)
{
source.AddSingleton<IXAuthorizationCache, XAuthorizationCache>();
}
}
}
+4
View File
@@ -67,6 +67,10 @@ namespace xIdentityService.Interfaces
string name,
CancellationToken cancellationToken = default
);
Task<XQueryResult<XRoleDefinitionDto>> QueryRolesAsync(
XQuery query,
CancellationToken cancellationToken = default
);
#endregion
//
+76
View File
@@ -0,0 +1,76 @@
using System;
using xCommons.Extensions;
using xExceptions.Constants;
using xIdentityService.Interfaces;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;
using Microsoft.AspNetCore.Authorization;
namespace xIdentityService.Models
{
public class XAuthorizationCache : IXAuthorizationCache
{
private readonly ILogger<XAuthorizationCache> logger;
private readonly ConcurrentDictionary<string, AuthorizationPolicy> policies;
public XAuthorizationCache(
ILogger<XAuthorizationCache> logger
)
{
//
this.logger = logger;
policies = new ConcurrentDictionary<string, AuthorizationPolicy>(StringComparer.Ordinal);
}
public void Clear()
{
policies.Clear();
}
public void SetPolicy(
string name,
AuthorizationPolicy policy
)
{
//
if (name.IsNullOrEmpty() ||
policy.IsNullOrDefault())
{
XException.InvalidArgs.Throw();
}
//
policies.AddOrUpdate(name, policy, (_, __) => policy);
}
public bool TryGetPolicy(
string name,
out AuthorizationPolicy policy
)
{
//
if (name.IsNullOrEmpty())
{
//
policy = null!;
return false;
}
//
var result = policies.TryGetValue(name, out policy!);
return result;
}
public void RemovePolicy(string name)
{
//
if (name.IsNullOrEmpty())
{
return;
}
//
if (policies.TryRemove(name, out _)) { }
}
}
}