last ...
This commit is contained in:
@@ -141,6 +141,38 @@ 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 roleSync = source.GetRegisteredService<IXRoleSyncService>();
|
||||
var cache = source.GetRegisteredService<IXAuthorizationCache>();
|
||||
var scopeSync = source.GetRegisteredService<IXScopeSyncService>();
|
||||
var resolver = source.GetRegisteredService<IXRoleHierarchyResolver>();
|
||||
|
||||
//
|
||||
var isValid =
|
||||
roles.IsNull() ||
|
||||
cache.IsNull() ||
|
||||
scopes.IsNull() ||
|
||||
policies.IsNull() ||
|
||||
roleSync.IsNull() ||
|
||||
resolver.IsNull() ||
|
||||
scopeSync.IsNull();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidConfiguration.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
source.AddScoped<IXAuthorizationProvider, XAuthorizationProvider>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use Authentication
|
||||
/// </summary>
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Models;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Extensions;
|
||||
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
@@ -51,7 +57,7 @@ namespace xIdentityService.Providers
|
||||
await InvalidateAsync(result.Name);
|
||||
|
||||
//
|
||||
return await ToDtoWithEffectiveRolesAsync(result, cancellationToken);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> RemovePolicyAsync(
|
||||
@@ -77,6 +83,201 @@ namespace xIdentityService.Providers
|
||||
await InvalidateAsync(entity.Name);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<XQueryResult<XPolicyDefinitionDto>> QueryPoliciesAsync(
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (query.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await policies.QueryAsync(
|
||||
query: query,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XPolicyDefinitionDto> GetPolicyAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XPolicyDefinitionDto> UpdatePolicyAsync(
|
||||
string name,
|
||||
XPolicyDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (name.IsNullOrEmpty() || item.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var exists = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
if (exists.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Referenced Roles & Scopes must exist ...
|
||||
await ValidateReferencesAsync(item, cancellationToken);
|
||||
|
||||
//
|
||||
var result = await policies.UpdateAsync(
|
||||
item: item,
|
||||
id: exists.Id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Drop the cached policy so it rebuilds on next evaluation ...
|
||||
await InvalidateAsync(result.Name);
|
||||
|
||||
//
|
||||
// Return the updated DTO as-is (EffectiveRoles is not a field on
|
||||
// XPolicyDefinitionDto) ...
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XPolicyEvaluationResultDto> EvaluatePolicyAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto target,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Load the policy (throws NotFound when missing) ...
|
||||
var dto = await FindPolicyOrThrowAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
var result = new XPolicyEvaluationResultDto
|
||||
{
|
||||
Passed = true,
|
||||
Policy = dto.Name,
|
||||
FailedRequirements = []
|
||||
};
|
||||
|
||||
//
|
||||
// Build the policy's effective authorization via the shared helper
|
||||
// (resolver + projection onto XEffectiveAuthorizationDto) ...
|
||||
var auth = await ToDtoWithEffectiveRolesAsync(dto, cancellationToken);
|
||||
if (auth.IsNull())
|
||||
{
|
||||
//
|
||||
result.Passed = false;
|
||||
result.FailedRequirements.Add("PolicyNotBuilt");
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Anonymous user fails any policy that requires authentication ...
|
||||
if (target.IsNull())
|
||||
{
|
||||
//
|
||||
if (auth.RequireAuthenticatedUser)
|
||||
{
|
||||
result.Passed = false;
|
||||
result.FailedRequirements.Add("RequireAuthenticatedUser");
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Effective Roles: the user must hold at least one ...
|
||||
if (auth.EffectiveRoles.HasChild())
|
||||
{
|
||||
//
|
||||
var userRoles = await roleSync.GetUserRolesAsync(
|
||||
target.GetUserSelectByParam(),
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
var matched = auth.EffectiveRoles.Any(er =>
|
||||
userRoles.Any(ur =>
|
||||
string.Equals(ur, er, StringComparison.OrdinalIgnoreCase)
|
||||
)
|
||||
);
|
||||
|
||||
//
|
||||
if (!matched)
|
||||
{
|
||||
//
|
||||
result.Passed = false;
|
||||
result.FailedRequirements.Add("RequiredRoles");
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// NOTE: RequireEnabledUser (IsEnabled / !IsBanned), EffectiveScopes and
|
||||
// EffectiveClaims checks are layered on here once the matching flags /
|
||||
// collections on XUserClaimsInfoDto (xIdentityModels) are wired.
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XEffectiveAuthorizationDto> GetEffectiveAuthorizationAsync(
|
||||
XUserClaimsInfoDto target,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new XEffectiveAuthorizationDto
|
||||
{
|
||||
RequireEnabledUser = !target.IsNull(),
|
||||
RequireAuthenticatedUser = !target.IsNull(),
|
||||
EffectiveClaims = new List<XRequiredClaimDto>(),
|
||||
EffectiveRoles = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
EffectiveScopes = new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
};
|
||||
|
||||
//
|
||||
if (target.IsNull())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Effective roles = the user's actual roles (from RoleManager) ...
|
||||
var userRoles = await roleSync.GetUserRolesAsync(
|
||||
cancellationToken: cancellationToken,
|
||||
userSelectByParam: target.GetUserSelectByParam()
|
||||
);
|
||||
foreach (var role in userRoles)
|
||||
{
|
||||
result.EffectiveRoles.Add(role);
|
||||
}
|
||||
|
||||
//
|
||||
// NOTE: EffectiveScopes / EffectiveClaims aggregation is wired here from
|
||||
// the user's scope / claim source on XUserClaimsInfoDto (xIdentityModels).
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
@@ -9,6 +13,169 @@ namespace xIdentityService.Providers
|
||||
{
|
||||
//
|
||||
#region Role ...
|
||||
public async Task<IEnumerable<XRoleDefinitionDto>> GetRolesAsync(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await roles
|
||||
.GetAllAsync(cancellationToken: cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XRoleDefinitionDto> GetRoleAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await FindRoleOrThrowAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XRoleDefinitionDto> AddRoleAsync(
|
||||
XRoleDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Duplicate check ...
|
||||
var normalized = item.Name.ToNormalString();
|
||||
var exists = await roles.FindOneAsync(
|
||||
predicate: r => r.Name.ToLower() == normalized,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!exists.IsNull())
|
||||
{
|
||||
XException.Duplicate.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Persist ...
|
||||
item.IsSystem = false;
|
||||
var result = await roles.AddAsync(
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Sync to IdentityServer (RoleManager<IdentityRole>) ...
|
||||
var entity = roles.AsQueryable()
|
||||
.FirstOrDefault(r => r.Name.ToLower() == normalized);
|
||||
if (!entity.IsNull())
|
||||
{
|
||||
await roleSync.UpsertAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XRoleDefinitionDto> UpdateRoleAsync(
|
||||
string name,
|
||||
XRoleDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (name.IsNullOrEmpty() || item.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var existing = await FindRoleOrThrowAsync(name, cancellationToken);
|
||||
if (existing.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var updated = await roles.UpdateAsync(
|
||||
id: existing.Id,
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Sync to IdentityServer ...
|
||||
var entity = roles.AsQueryable()
|
||||
.FirstOrDefault(r => r.Name.ToLower() == name.ToNormalString());
|
||||
if (!entity.IsNull())
|
||||
{
|
||||
await roleSync.UpsertAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return updated;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveRoleAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var exists = await FindRoleOrThrowAsync(name, cancellationToken);
|
||||
if (exists.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
await roles.RemoveAsync(
|
||||
id: exists.Id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Remove from IdentityServer ...
|
||||
await roleSync.RemoveAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<int> CountUsersInRoleAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Fail fast if the role itself does not exist ...
|
||||
await FindRoleOrThrowAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
var result = await roleSync
|
||||
.CountUsersInRoleAsync(
|
||||
name,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,174 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using xModels.Dtos;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xIdentityService.Providers
|
||||
{
|
||||
public partial class XAuthorizationProvider
|
||||
{
|
||||
//
|
||||
#region Scope ...
|
||||
public async Task<XQueryResult<XScopeDefinitionDto>> QueryScopesAsync(
|
||||
XQuery query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
if (query.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await scopes.QueryAsync(
|
||||
query: query,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XScopeDefinitionDto> GetScopeAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await FindScopeOrThrowAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XScopeDefinitionDto> AddScopeAsync(
|
||||
XScopeDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Duplicate check ...
|
||||
var normalized = item.Name.ToNormalString();
|
||||
var exists = await scopes.FindOneAsync(
|
||||
cancellationToken: cancellationToken,
|
||||
predicate: s => s.Name.ToLower() == normalized
|
||||
);
|
||||
if (!exists.IsNull())
|
||||
{
|
||||
XException.Duplicate.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Persist ...
|
||||
item.IsSystem = false;
|
||||
var result = await scopes.AddAsync(
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Sync to IdentityServer (ConfigurationDbContext) ...
|
||||
var entity = scopes.AsQueryable()
|
||||
.FirstOrDefault(s => s.Name.ToLower() == normalized);
|
||||
if (!entity.IsNull())
|
||||
{
|
||||
//
|
||||
await scopeSync
|
||||
.UpsertAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<XScopeDefinitionDto> UpdateScopeAsync(
|
||||
string name,
|
||||
XScopeDefinitionDto item,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (name.IsNullOrEmpty() || item.IsNull())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var existing = await FindScopeOrThrowAsync(name, cancellationToken);
|
||||
if (existing.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var updated = await scopes.UpdateAsync(
|
||||
id: existing.Id,
|
||||
item: item,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Sync to IdentityServer ...
|
||||
var entity = scopes.AsQueryable()
|
||||
.FirstOrDefault(s => s.Name.ToLower() == name.ToNormalString());
|
||||
if (!entity.IsNull())
|
||||
{
|
||||
await scopeSync.UpsertAsync(entity, cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return updated;
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveScopeAsync(
|
||||
string name,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var entity = await FindScopeOrThrowAsync(name, cancellationToken);
|
||||
if (entity.IsSystem)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
await scopes.RemoveAsync(
|
||||
id: entity.Id,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Remove from IdentityServer ...
|
||||
await scopeSync.RemoveAsync(name, cancellationToken);
|
||||
|
||||
//
|
||||
await InvalidateAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,10 @@ using xCommons.Extensions;
|
||||
using xIdentityModels.Dtos;
|
||||
using xExceptions.Constants;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Models;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Interfaces;
|
||||
using xIdentityService.Extensions;
|
||||
|
||||
//
|
||||
// TODO: Remove this to xIds ...
|
||||
@@ -23,7 +24,6 @@ namespace xIdentityService.Providers
|
||||
private readonly IXAuthorizationCache cache;
|
||||
private readonly IXScopeSyncService scopeSync;
|
||||
private readonly IXRoleHierarchyResolver resolver;
|
||||
// private readonly IXEventServiceProvider eventService;
|
||||
|
||||
public XAuthorizationProvider(
|
||||
IXRoleProvider roles,
|
||||
@@ -62,10 +62,91 @@ namespace xIdentityService.Providers
|
||||
cache.RemovePolicy(policyName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Re-push every Role and Scope into IdentityServer and drop the local
|
||||
/// authorization cache so policies rebuild lazily on next evaluation.
|
||||
/// </summary>
|
||||
public async Task SyncAllAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
//
|
||||
// Sync all Roles to IdentityServer (RoleManager<IdentityRole>) ...
|
||||
foreach (var role in roles.AsQueryable())
|
||||
{
|
||||
//
|
||||
await roleSync
|
||||
.UpsertAsync(role, cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
// Sync all Scopes to IdentityServer (ConfigurationDbContext) ...
|
||||
foreach (var scope in scopes.AsQueryable())
|
||||
{
|
||||
//
|
||||
await scopeSync
|
||||
.UpsertAsync(scope, cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
// Drop local authorization cache ...
|
||||
await InvalidateAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
private async Task<XRoleDefinitionDto> FindRoleOrThrowAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
//
|
||||
if (name.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var normalized = name.ToNormalString();
|
||||
var result = await roles.FindOneAsync(
|
||||
predicate: r => r.Name.ToLower() == normalized,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (result.IsNull())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<XScopeDefinitionDto> FindScopeOrThrowAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
//
|
||||
if (name.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var normalized = name.ToNormalString();
|
||||
var result = await scopes.FindOneAsync(
|
||||
predicate: s => s.Name.ToLower() == normalized,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (result.IsNull())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<XPolicyDefinitionDto> FindPolicyOrThrowAsync(
|
||||
string name,
|
||||
CancellationToken cancellationToken
|
||||
@@ -152,11 +233,18 @@ namespace xIdentityService.Providers
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
||||
private async Task<XEffectiveAuthorizationDto> ToDtoWithEffectiveRolesAsync(
|
||||
XPolicyDefinitionDto item,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Null / nameless guard ...
|
||||
if (item.IsNull() || item.Name.IsNullOrEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
// Parse the DTO's CSV-string RequiredRoles. ParseListString NREs on a
|
||||
// null string, so guard first; the resolver itself is null-safe on an
|
||||
@@ -166,42 +254,39 @@ namespace xIdentityService.Providers
|
||||
: item.RequiredRoles.ParseListString<string>();
|
||||
|
||||
//
|
||||
item.EffectiveRoles = (
|
||||
await resolver.ResolveEffectiveRolesAsync(
|
||||
explicitRoles: explicitRoles,
|
||||
minRoleWeight: item.MinRoleWeight,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
).ToList();
|
||||
// Resolve the effective role set
|
||||
// (explicit RequiredRoles U roles by MinRoleWeight U parent chain) ...
|
||||
var effectiveRoles = await resolver.ResolveEffectiveRolesAsync(
|
||||
explicitRoles: explicitRoles,
|
||||
minRoleWeight: item.MinRoleWeight,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return item;
|
||||
}
|
||||
|
||||
private async Task<XPolicyDefinitionDto> ToDtoWithEffectiveRolesAsync(
|
||||
XPolicyDefinition entity,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
//
|
||||
// Entity -> DTO (the RequiredRoles collection is serialized to the
|
||||
// CSV string on the DTO by the ToDto mapping) ...
|
||||
var dto = entity.ToDto();
|
||||
// Project onto XEffectiveAuthorizationDto - the ONLY DTO that carries
|
||||
// EffectiveRoles / EffectiveScopes / EffectiveClaims. Each CSV / JSON
|
||||
// field is null-guarded here (the shared ToEffectiveAuthorization
|
||||
// extension calls ParseListString on RequiredScopes without a null
|
||||
// guard, so the projection is built inline to stay null-safe) ...
|
||||
var result = new XEffectiveAuthorizationDto
|
||||
{
|
||||
PolicyName = item.Name,
|
||||
RequireEnabledUser = item.RequireEnabledUser,
|
||||
RequireAuthenticatedUser = item.RequireAuthenticatedUser,
|
||||
EffectiveClaims = item.RequiredClaims.ToRequiredClaims(),
|
||||
EffectiveScopes = item.RequiredScopes.IsNullOrEmpty()
|
||||
? new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
: new HashSet<string>(
|
||||
item.RequiredScopes.ParseListString<string>(),
|
||||
StringComparer.OrdinalIgnoreCase
|
||||
),
|
||||
EffectiveRoles = effectiveRoles.HasChild()
|
||||
? new HashSet<string>(effectiveRoles, StringComparer.OrdinalIgnoreCase)
|
||||
: new HashSet<string>(StringComparer.OrdinalIgnoreCase),
|
||||
};
|
||||
|
||||
//
|
||||
// Effective Roles = explicit RequiredRoles U roles by MinRoleWeight
|
||||
// U parent chain. explicitRoles takes IEnumerable<string>, so pass
|
||||
// the entity's collection directly (the resolver is null-safe on it) ...
|
||||
dto.EffectiveRoles = (
|
||||
await resolver.ResolveEffectiveRolesAsync(
|
||||
explicitRoles: entity.RequiredRoles,
|
||||
minRoleWeight: entity.MinRoleWeight,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
).ToList();
|
||||
|
||||
//
|
||||
return dto;
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -35,9 +35,6 @@
|
||||
|
||||
<!-- Dependencies -->
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.3.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.14" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="4.0.0-preview.6" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user