Add XPerson as Owner inside XApiKeyValidation Result ...

Add UserID in XPersonDto ...
handle User Retrievers from Claims ...
Handle User Converters ...
This commit is contained in:
2026-10-30 00:22:02 +03:30
parent d48743578f
commit fd78ac884c
5 changed files with 226 additions and 6 deletions
+4 -3
View File
@@ -111,9 +111,10 @@ namespace xIdentityModels.Dtos
/// </summary>
public class XOpenActionUserInfoDto : XBaseDto
{
public string Username { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string UserId { get; set; }
public string Avatar { get; set; }
public string Username { get; set; }
public string Lastname { get; set; }
public string Firstname { get; set; }
}
}
+77
View File
@@ -1,8 +1,11 @@
using System;
using xModels.Dtos;
using IdentityModel;
using xCommons.Constants;
using xCommons.Extensions;
using xIdentityModels.Dtos;
using xIdentityModels.Models;
using System.Security.Claims;
using System.Collections.Generic;
using xIdentityModels.Descriptors;
using xIdentityModels.Navigations;
@@ -51,6 +54,7 @@ namespace xIdentityModels.Extensions
//
result = new XPersonDto
{
UserId = source.Id,
Avatar = source.Avatar,
Username = source.UserName,
Lastname = source.LastName,
@@ -157,6 +161,33 @@ namespace xIdentityModels.Extensions
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>
/// Validate ...
/// </summary>
@@ -274,5 +305,51 @@ namespace xIdentityModels.Extensions
//
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.Picture, source.Owner.Avatar));
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));
}
}
}
//
return result;
}
}
}
+116 -3
View File
@@ -1,12 +1,15 @@
using System;
using System.Linq;
using xModels.Dtos;
using xCommons.Constants;
using xCommons.Extensions;
using xIdentityModels.Dtos;
using xExceptions.Constants;
using xIdentityModels.Models;
using System.Security.Claims;
using xIdentityModels.Constants;
using System.Collections.Generic;
using xIdentityModels.Configurations;
using xExceptions.Constants;
namespace xIdentityModels.Extensions
{
@@ -398,7 +401,7 @@ namespace xIdentityModels.Extensions
{
//
// if Allowed IPs Empty means all Client IPs is Valid ...
var result =
var result =
!source.HasChild();
if (result)
{
@@ -420,7 +423,7 @@ namespace xIdentityModels.Extensions
if (dest == "::1")
{
//
result = source.Any(s =>
result = source.Any(s =>
s.ToNormalString() == "127.0.0.1" ||
s.ToNormalString() == "localhost");
}
@@ -502,5 +505,115 @@ namespace xIdentityModels.Extensions
//
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;
}
}
}
+2
View File
@@ -1,3 +1,4 @@
using xModels.Dtos;
using System.Collections.Generic;
namespace xIdentityModels.Models
@@ -6,6 +7,7 @@ namespace xIdentityModels.Models
{
public string OwnerId { get; set; }
public int ApplicationId { get; set; }
public XPersonDto Owner {get; set; } = new();
public IList<string> Scopes {get; set;} = [];
public IList<string> Errors { get; set; } = [];
}
+27
View File
@@ -46,6 +46,33 @@ namespace xIdentityModels.Models
//
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(
string accessToken,
string refreshToken,