Compare commits
6
Commits
b5848b96e7
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efe4c39ab8 | ||
|
|
6b5f498511 | ||
|
|
d7c80f2532 | ||
|
|
f24cfb2fc1 | ||
|
|
fd78ac884c | ||
|
|
d48743578f |
@@ -0,0 +1,22 @@
|
|||||||
|
using xExceptions.Attributes;
|
||||||
|
|
||||||
|
namespace xIdentityModels.Constants
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Kind of a Policy Definition ...
|
||||||
|
/// </summary>
|
||||||
|
public enum XPolicyKind
|
||||||
|
{
|
||||||
|
[StringValue("roleBased")]
|
||||||
|
RoleBased = 0,
|
||||||
|
|
||||||
|
[StringValue("scopeBased")]
|
||||||
|
ScopeBased = 1,
|
||||||
|
|
||||||
|
[StringValue("claimBased")]
|
||||||
|
ClaimBased = 2,
|
||||||
|
|
||||||
|
[StringValue("composite")]
|
||||||
|
Composite = 3
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
-6
@@ -40,10 +40,17 @@ namespace xIdentityModels.Dtos
|
|||||||
!Device.IsNull() &&
|
!Device.IsNull() &&
|
||||||
!Timestamp.IsNull() &&
|
!Timestamp.IsNull() &&
|
||||||
!Action.IsNullOrEmpty() &&
|
!Action.IsNullOrEmpty() &&
|
||||||
(Action == XOpenActions.HandShake ||
|
(
|
||||||
|
//
|
||||||
|
Action == XOpenActions.HandShake ||
|
||||||
|
Action == XOpenActions.GetApiKey ||
|
||||||
|
Action == XOpenActions.UseApiKey ||
|
||||||
Action == XOpenActions.GetUserInfo ||
|
Action == XOpenActions.GetUserInfo ||
|
||||||
Action == XOpenActions.GetUserInfos) &&
|
Action == XOpenActions.GetUserInfos
|
||||||
!Int64.Parse(Timestamp).FromTimestamp().IsExPired();
|
) &&
|
||||||
|
!Int64.Parse(Timestamp)
|
||||||
|
.FromTimestamp()
|
||||||
|
.IsExPired();
|
||||||
|
|
||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
@@ -104,9 +111,10 @@ namespace xIdentityModels.Dtos
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class XOpenActionUserInfoDto : XBaseDto
|
public class XOpenActionUserInfoDto : XBaseDto
|
||||||
{
|
{
|
||||||
public string Username { get; set; }
|
public string UserId { get; set; }
|
||||||
public string Firstname { get; set; }
|
|
||||||
public string Lastname { get; set; }
|
|
||||||
public string Avatar { get; set; }
|
public string Avatar { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string Lastname { get; set; }
|
||||||
|
public string Firstname { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
using xModels.Base;
|
||||||
|
using xIdentityModels.Constants;
|
||||||
|
|
||||||
|
namespace xIdentityModels.Dtos
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a Policy Definition Model which can be converted to AuthorizationPolicy ...
|
||||||
|
/// </summary>
|
||||||
|
public class XPolicyDefinitionDto : XBaseIntIDEntityDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique Policy Name, e.g. enabledEditorPolicy ...
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Display Name ...
|
||||||
|
/// </summary>
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Policy Kind ...
|
||||||
|
/// </summary>
|
||||||
|
public XPolicyKind Kind { get; set; } = XPolicyKind.RoleBased;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Comma Separated Role Names (explicit list) ...
|
||||||
|
/// </summary>
|
||||||
|
public string RequiredRoles { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// if provided, all Roles with Weight >= MinRoleWeight will be Injected ...
|
||||||
|
/// </summary>
|
||||||
|
public int? MinRoleWeight { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Comma Separated Scope Names ...
|
||||||
|
/// </summary>
|
||||||
|
public string RequiredScopes { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Json Array of { "Type": "...", "Values": ["..."] } ...
|
||||||
|
/// </summary>
|
||||||
|
public string RequiredClaims { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Requires IsBanned=false and IsEnabled=true Claims ...
|
||||||
|
/// </summary>
|
||||||
|
public bool RequireEnabledUser { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Requires Authenticated User ...
|
||||||
|
/// </summary>
|
||||||
|
public bool RequireAuthenticatedUser { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is Enabled ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Seeded from Constants, cannot be removed ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSystem { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using xModels.Base;
|
||||||
|
|
||||||
|
namespace xIdentityModels.Dtos
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a Role Definition Model (mirror of ASP.NET IdentityRole + Weight) ...
|
||||||
|
/// </summary>
|
||||||
|
public class XRoleDefinitionDto : XBaseIntIDEntityDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique Normalized Role Name, e.g. chief_clerk ...
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Persian Display Name, e.g. سر دبیر ...
|
||||||
|
/// </summary>
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Role Weight, same as XUserRole enum (user=0 ... admin=10) ...
|
||||||
|
/// </summary>
|
||||||
|
public int Weight { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional Parent Role for Inheritance ...
|
||||||
|
/// </summary>
|
||||||
|
public string ParentRoleName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is Enabled ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Seeded from Constants, cannot be removed ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSystem { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using xModels.Base;
|
||||||
|
|
||||||
|
namespace xIdentityModels.Dtos
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a Scope Definition Model (mirror of IdentityServer ApiScope) ...
|
||||||
|
/// </summary>
|
||||||
|
public class XScopeDefinitionDto : XBaseIntIDEntityDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique Scope Name, e.g. XAPI.read ...
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Display Name ...
|
||||||
|
/// </summary>
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Description ...
|
||||||
|
/// </summary>
|
||||||
|
public string Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Comma Separated List of User Claims ...
|
||||||
|
/// </summary>
|
||||||
|
public string UserClaims { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional ApiResource Name on IdentityServer ...
|
||||||
|
/// </summary>
|
||||||
|
public string ApiResourceName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is Enabled ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Seeded from Constants, cannot be removed ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSystem { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using xModels.Dtos;
|
using xModels.Dtos;
|
||||||
|
using IdentityModel;
|
||||||
|
using xCommons.Constants;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xIdentityModels.Dtos;
|
using xIdentityModels.Dtos;
|
||||||
using xIdentityModels.Models;
|
using xIdentityModels.Models;
|
||||||
|
using System.Security.Claims;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using xIdentityModels.Descriptors;
|
using xIdentityModels.Descriptors;
|
||||||
using xIdentityModels.Navigations;
|
using xIdentityModels.Navigations;
|
||||||
@@ -51,6 +54,7 @@ namespace xIdentityModels.Extensions
|
|||||||
//
|
//
|
||||||
result = new XPersonDto
|
result = new XPersonDto
|
||||||
{
|
{
|
||||||
|
UserId = source.Id,
|
||||||
Avatar = source.Avatar,
|
Avatar = source.Avatar,
|
||||||
Username = source.UserName,
|
Username = source.UserName,
|
||||||
Lastname = source.LastName,
|
Lastname = source.LastName,
|
||||||
@@ -157,6 +161,33 @@ namespace xIdentityModels.Extensions
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts XPersonDto to XUserClaimsInfoDto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XUserClaimsInfoDto ToXUserClaimsInfoDto(this XPersonDto source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XUserClaimsInfoDto result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!source.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = new XUserClaimsInfoDto(
|
||||||
|
userid: source.UserId,
|
||||||
|
avatar: source.Avatar,
|
||||||
|
username: source.Username,
|
||||||
|
lastname: source.Lastname,
|
||||||
|
firstname: source.Firstname
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validate ...
|
/// Validate ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -220,6 +251,198 @@ namespace xIdentityModels.Extensions
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Policy Kind ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsKindValid(this XPolicyDefinition source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault();
|
||||||
|
switch (source.Kind)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.Composite:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!(source.RequiredRoles.IsNullOrEmpty() &&
|
||||||
|
source.RequiredClaims.IsNullOrEmpty() &&
|
||||||
|
source.RequiredScopes.IsNullOrEmpty());
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.ClaimBased:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!source.RequiredClaims.IsNullOrEmpty();
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.ScopeBased:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!source.RequiredScopes.IsNullOrEmpty();
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.RoleBased:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!source.RequiredRoles.IsNullOrEmpty();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Policy Kind ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsKindValid(this XPolicyDefinitionDto source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault();
|
||||||
|
switch (source.Kind)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.Composite:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!(source.RequiredRoles.IsNullOrEmpty() &&
|
||||||
|
source.RequiredClaims.IsNullOrEmpty() &&
|
||||||
|
source.RequiredScopes.IsNullOrEmpty());
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.ClaimBased:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!source.RequiredClaims.IsNullOrEmpty();
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.ScopeBased:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!source.RequiredScopes.IsNullOrEmpty();
|
||||||
|
break;
|
||||||
|
|
||||||
|
//
|
||||||
|
case Constants.XPolicyKind.RoleBased:
|
||||||
|
//
|
||||||
|
result =
|
||||||
|
!source.RequiredRoles.IsNullOrEmpty();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Item for Add or Update ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidForAdd(this XPolicyDefinition source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
source.IsKindValid() &&
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
!source.Name.IsNullOrEmpty();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Item for Add or Update ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidForAdd(this XPolicyDefinitionDto source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
source.IsKindValid() &&
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
!source.Name.IsNullOrEmpty();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Item for Add or Update ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidForAdd(this XRoleDefinition source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
!source.Name.IsNullOrEmpty();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Item for Add or Update ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidForAdd(this XRoleDefinitionDto source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
!source.Name.IsNullOrEmpty();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Item for Add or Update ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidForAdd(this XScopeDefinition source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
!source.Name.IsNullOrEmpty();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate Item for Add or Update ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsValidForAdd(this XScopeDefinitionDto source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result =
|
||||||
|
!source.IsNullOrDefault() &&
|
||||||
|
!source.Name.IsNullOrEmpty();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Validate Item for Add or Update ...
|
/// Validate Item for Add or Update ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -274,5 +497,56 @@ namespace xIdentityModels.Extensions
|
|||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts XApiKeyValidationResult to Claims ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IList<Claim> ToXClaims(
|
||||||
|
this XApiKeyValidationResult source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new List<Claim>();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!source.IsNullOrDefault() &&
|
||||||
|
!source.Owner.IsNullOrDefault() &&
|
||||||
|
!source.OwnerId.IsNullOrEmpty() &&
|
||||||
|
source.ApplicationId.IsValidIntId()
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result.Add(new(ClaimTypes.Name, source.OwnerId));
|
||||||
|
result.Add(new(XCustomClaims.UserId, source.OwnerId));
|
||||||
|
result.Add(new(JwtClaimTypes.Subject, source.OwnerId));
|
||||||
|
result.Add(new(XCustomClaims.UserName, source.Owner.Username));
|
||||||
|
result.Add(new(XCustomClaims.LastName, source.Owner.Lastname));
|
||||||
|
result.Add(new(XCustomClaims.AuthType, XAuthorization.XApiKey));
|
||||||
|
result.Add(new(XCustomClaims.FirstName, source.Owner.Firstname));
|
||||||
|
result.Add(new(XCustomClaims.ApplicationId, source.ApplicationId.ToString()));
|
||||||
|
|
||||||
|
//
|
||||||
|
if (source.Scopes.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
foreach (var scope in source.Scopes)
|
||||||
|
{
|
||||||
|
result.Add(new(JwtClaimTypes.Scope, scope));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!source.Owner.Avatar.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
result.Add(new(XCustomClaims.Picture, source.Owner.Avatar));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using xModels.Dtos;
|
||||||
|
using xCommons.Constants;
|
||||||
using xCommons.Extensions;
|
using xCommons.Extensions;
|
||||||
using xIdentityModels.Dtos;
|
using xIdentityModels.Dtos;
|
||||||
|
using xExceptions.Constants;
|
||||||
using xIdentityModels.Models;
|
using xIdentityModels.Models;
|
||||||
|
using System.Security.Claims;
|
||||||
using xIdentityModels.Constants;
|
using xIdentityModels.Constants;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using xIdentityModels.Configurations;
|
using xIdentityModels.Configurations;
|
||||||
using xExceptions.Constants;
|
|
||||||
|
|
||||||
namespace xIdentityModels.Extensions
|
namespace xIdentityModels.Extensions
|
||||||
{
|
{
|
||||||
@@ -502,5 +505,115 @@ namespace xIdentityModels.Extensions
|
|||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extract ApiKey Validated User ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XPersonDto ExtractApiKeyUser(
|
||||||
|
this ClaimsPrincipal source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XPersonDto result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!source.IsNull() &&
|
||||||
|
source.Claims.HasChild())
|
||||||
|
{
|
||||||
|
source.Claims.ExtractApiKeyUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extract ApiKey Validated User ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XPersonDto ExtractApiKeyUser(
|
||||||
|
this IEnumerable<Claim> source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XPersonDto result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (source.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = new XPersonDto
|
||||||
|
{
|
||||||
|
UserId = source.FirstOrDefault(c => c.Type == XCustomClaims.UserId)?.Value ?? "",
|
||||||
|
Avatar = source.FirstOrDefault(c => c.Type == XCustomClaims.Picture)?.Value ?? "",
|
||||||
|
Username = source.FirstOrDefault(c => c.Type == XCustomClaims.UserName)?.Value ?? "",
|
||||||
|
Lastname = source.FirstOrDefault(c => c.Type == XCustomClaims.LastName)?.Value ?? "",
|
||||||
|
Firstname = source.FirstOrDefault(c => c.Type == XCustomClaims.FirstName)?.Value ?? "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extract JWT Validated User ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XPersonDto ExtractAuthenticatedUser(
|
||||||
|
this ClaimsPrincipal source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XPersonDto result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!source.IsNull() &&
|
||||||
|
source.Claims.HasChild())
|
||||||
|
{
|
||||||
|
source.Claims.ExtractAuthenticatedUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extract JWT Validated User ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XPersonDto ExtractAuthenticatedUser(
|
||||||
|
this IEnumerable<Claim> source
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XPersonDto result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (source.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = new XPersonDto
|
||||||
|
{
|
||||||
|
UserId = source.FirstOrDefault(c => c.Type == XCustomClaims.UserId)?.Value ?? "",
|
||||||
|
Avatar = source.FirstOrDefault(c => c.Type == XCustomClaims.Picture)?.Value ?? "",
|
||||||
|
Username = source.FirstOrDefault(c => c.Type == XCustomClaims.UserName)?.Value ?? "",
|
||||||
|
Lastname = source.FirstOrDefault(c => c.Type == XCustomClaims.LastName)?.Value ?? "",
|
||||||
|
Firstname = source.FirstOrDefault(c => c.Type == XCustomClaims.FirstName)?.Value ?? "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using xModels.Dtos;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace xIdentityModels.Models
|
namespace xIdentityModels.Models
|
||||||
@@ -6,6 +7,7 @@ namespace xIdentityModels.Models
|
|||||||
{
|
{
|
||||||
public string OwnerId { get; set; }
|
public string OwnerId { get; set; }
|
||||||
public int ApplicationId { get; set; }
|
public int ApplicationId { get; set; }
|
||||||
|
public XPersonDto Owner {get; set; } = new();
|
||||||
public IList<string> Scopes {get; set;} = [];
|
public IList<string> Scopes {get; set;} = [];
|
||||||
public IList<string> Errors { get; set; } = [];
|
public IList<string> Errors { get; set; } = [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
using xModels.Base;
|
||||||
|
using xIdentityModels.Constants;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace xIdentityModels.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a Policy Definition Model which can be converted to AuthorizationPolicy ...
|
||||||
|
/// </summary>
|
||||||
|
public class XPolicyDefinition : XBaseIntIDEntity
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique Policy Name, e.g. enabledEditorPolicy ...
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
[StringLength(200)]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Display Name ...
|
||||||
|
/// </summary>
|
||||||
|
[StringLength(255)]
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Policy Kind ...
|
||||||
|
/// </summary>
|
||||||
|
public XPolicyKind Kind { get; set; } = XPolicyKind.RoleBased;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Comma Separated Role Names (explicit list) ...
|
||||||
|
/// </summary>
|
||||||
|
public string RequiredRoles { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// if provided, all Roles with Weight >= MinRoleWeight will be Injected ...
|
||||||
|
/// </summary>
|
||||||
|
public int? MinRoleWeight { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Comma Separated Scope Names ...
|
||||||
|
/// </summary>
|
||||||
|
public string RequiredScopes { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Json Array of { "Type": "...", "Values": ["..."] } ...
|
||||||
|
/// </summary>
|
||||||
|
public string RequiredClaims { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Requires IsBanned=false and IsEnabled=true Claims ...
|
||||||
|
/// </summary>
|
||||||
|
public bool RequireEnabledUser { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Requires Authenticated User ...
|
||||||
|
/// </summary>
|
||||||
|
public bool RequireAuthenticatedUser { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is Enabled ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Seeded from Constants, cannot be removed ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSystem { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using xModels.Base;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace xIdentityModels.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a Role Definition Model (mirror of ASP.NET IdentityRole + Weight) ...
|
||||||
|
/// </summary>
|
||||||
|
public class XRoleDefinition : XBaseIntIDEntity
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique Normalized Role Name, e.g. chief_clerk ...
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
[StringLength(256)]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Persian Display Name, e.g. سر دبیر ...
|
||||||
|
/// </summary>
|
||||||
|
[StringLength(255)]
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Role Weight, same as XUserRole enum (user=0 ... admin=10) ...
|
||||||
|
/// </summary>
|
||||||
|
public int Weight { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional Parent Role for Inheritance ...
|
||||||
|
/// </summary>
|
||||||
|
[StringLength(256)]
|
||||||
|
public string ParentRoleName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is Enabled ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Seeded from Constants, cannot be removed ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSystem { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
using xModels.Base;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace xIdentityModels.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a Scope Definition Model (mirror of IdentityServer ApiScope) ...
|
||||||
|
/// </summary>
|
||||||
|
public class XScopeDefinition : XBaseIntIDEntity
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Unique Scope Name, e.g. XAPI.read ...
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
[StringLength(200)]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Display Name ...
|
||||||
|
/// </summary>
|
||||||
|
[StringLength(255)]
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Description ...
|
||||||
|
/// </summary>
|
||||||
|
[StringLength(1000)]
|
||||||
|
public string Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Comma Separated List of User Claims ...
|
||||||
|
/// </summary>
|
||||||
|
public string UserClaims { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Optional ApiResource Name on IdentityServer ...
|
||||||
|
/// </summary>
|
||||||
|
[StringLength(200)]
|
||||||
|
public string ApiResourceName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is Enabled ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Seeded from Constants, cannot be removed ...
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSystem { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,6 +46,33 @@ namespace xIdentityModels.Models
|
|||||||
//
|
//
|
||||||
public XUserClaimsInfoDto() { }
|
public XUserClaimsInfoDto() { }
|
||||||
|
|
||||||
|
public XUserClaimsInfoDto(
|
||||||
|
string userid,
|
||||||
|
string username,
|
||||||
|
string firstname,
|
||||||
|
string lastname,
|
||||||
|
string avatar
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var isValid =
|
||||||
|
!userid.IsNullOrEmpty() &&
|
||||||
|
!username.IsNullOrEmpty() &&
|
||||||
|
!firstname.IsNullOrEmpty() &&
|
||||||
|
!lastname.IsNullOrEmpty();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
UserId = userid;
|
||||||
|
Picture = avatar;
|
||||||
|
UserName = username;
|
||||||
|
LastName = lastname;
|
||||||
|
FirstName = firstname;
|
||||||
|
}
|
||||||
|
|
||||||
public XUserClaimsInfoDto(
|
public XUserClaimsInfoDto(
|
||||||
string accessToken,
|
string accessToken,
|
||||||
string refreshToken,
|
string refreshToken,
|
||||||
|
|||||||
Reference in New Issue
Block a user