293 lines
8.9 KiB
C#
293 lines
8.9 KiB
C#
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)
|
|
{
|
|
//
|
|
var result = new XPersonDto
|
|
{
|
|
UserId = source.UserId,
|
|
Avatar = source.Avatar,
|
|
Lastname = source.Lastname,
|
|
Username = source.Username,
|
|
Firstname = source.Firstname,
|
|
};
|
|
|
|
//
|
|
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;
|
|
}
|
|
}
|
|
} |