using System; using System.Linq; using System.Threading; using xCommons.Extensions; using xIdentityModels.Dtos; using xExceptions.Constants; using System.Threading.Tasks; using xIdentityService.Models; using System.Collections.Generic; using xIdentityService.Interfaces; using xIdentityService.Extensions; // // TODO: Remove this to xIds ... namespace xIdentityService.Providers { public partial class XAuthorizationProvider // : IXAuthorizationProvider { // // private readonly IXRoleProvider roles; // private readonly IXScopeProvider scopes; // private readonly IXPolicyProvider policies; // private readonly IXAuthorizationCache cache; // private readonly IXRoleHierarchyResolver resolver; // public XAuthorizationProvider( // IXRoleProvider roles, // IXScopeProvider scopes, // IXPolicyProvider policies, // IXAuthorizationCache cache, // IXRoleHierarchyResolver resolver // ) // { // // // this.roles = roles; // this.cache = cache; // this.scopes = scopes; // this.policies = policies; // this.resolver = resolver; // } // // // #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 // // // #region Private ... // private async Task 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 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 FindPolicyOrThrowAsync( // string name, // CancellationToken cancellationToken // ) // { // // // if (name.IsNullOrEmpty()) // { // XException.InvalidArgs.Throw(); // } // // // var normalized = name.ToNormalString(); // var result = await policies.FindOneAsync( // predicate: p => p.Name.ToLower() == normalized, // cancellationToken: cancellationToken // ); // if (result.IsNull()) // { // XException.NotFound.Throw(); // } // // // return result; // } // private async Task ValidateReferencesAsync( // XPolicyDefinitionDto item, // CancellationToken cancellationToken // ) // { // // // // Defensive guard ... // if (item.IsNull()) // { // XException.InvalidArgs.Throw(); // } // // // // The DTO stores RequiredRoles / RequiredScopes as CSV strings. // // Parse them into collections ONCE (ParseListString NREs on a null // // string, so guard first) ... // // // var requiredRoles = item.RequiredRoles.IsNullOrEmpty() // ? [] // : item.RequiredRoles.ParseListString().ToList(); // // // var requiredScopes = item.RequiredScopes.IsNullOrEmpty() // ? [] // : item.RequiredScopes.ParseListString().ToList(); // // // // Referenced Roles must exist ... // if (requiredRoles.HasChild()) // { // // // var existingRoles = new HashSet( // (await roles.GetAllAsync(cancellationToken: cancellationToken)) // .Select(r => r.Name.ToNormalString()), // StringComparer.OrdinalIgnoreCase // ); // if (requiredRoles.Any(r => !existingRoles.Contains(r.ToNormalString()))) // { // XException.NotFound.Throw(); // } // } // // // // Referenced Scopes must exist ... // if (requiredScopes.HasChild()) // { // // // var existingScopes = new HashSet( // (await scopes.GetAllAsync(cancellationToken: cancellationToken)) // .Select(s => s.Name.ToNormalString()), // StringComparer.OrdinalIgnoreCase // ); // if (requiredScopes.Any(s => !existingScopes.Contains(s.ToNormalString()))) // { // XException.NotFound.Throw(); // } // } // } // private async Task 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 // // empty explicitRoles set ... // var explicitRoles = item.RequiredRoles.IsNullOrEmpty() // ? null // : item.RequiredRoles.ParseListString(); // // // // 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 // ); // // // // 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(StringComparer.OrdinalIgnoreCase) // : new HashSet( // item.RequiredScopes.ParseListString(), // StringComparer.OrdinalIgnoreCase // ), // EffectiveRoles = effectiveRoles.HasChild() // ? new HashSet(effectiveRoles, StringComparer.OrdinalIgnoreCase) // : new HashSet(StringComparer.OrdinalIgnoreCase), // }; // // // return result; // } // #endregion } }