From fd78ac884c694226aba07cd34c4414b62fd48350 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 30 Oct 2026 00:22:02 +0330 Subject: [PATCH] Add XPerson as Owner inside XApiKeyValidation Result ... Add UserID in XPersonDto ... handle User Retrievers from Claims ... Handle User Converters ... --- Dtos/XOpenActionDto.cs | 7 +- Extensions/XModelExtensions.cs | 77 +++++++++++++++ Extensions/xIdentityModelsExtensions.cs | 119 +++++++++++++++++++++++- Models/XApiKeyValidationResult.cs | 2 + Models/XUserClaimsInfoDto.cs | 27 ++++++ 5 files changed, 226 insertions(+), 6 deletions(-) diff --git a/Dtos/XOpenActionDto.cs b/Dtos/XOpenActionDto.cs index e90245a..e8bae85 100644 --- a/Dtos/XOpenActionDto.cs +++ b/Dtos/XOpenActionDto.cs @@ -111,9 +111,10 @@ namespace xIdentityModels.Dtos /// 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; } } } \ No newline at end of file diff --git a/Extensions/XModelExtensions.cs b/Extensions/XModelExtensions.cs index 91b0868..39d30bd 100644 --- a/Extensions/XModelExtensions.cs +++ b/Extensions/XModelExtensions.cs @@ -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; } + /// + /// Converts XPersonDto to XUserClaimsInfoDto ... + /// + /// + /// + 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; + } + /// /// Validate ... /// @@ -274,5 +305,51 @@ namespace xIdentityModels.Extensions // return result; } + + /// + /// Converts XApiKeyValidationResult to Claims ... + /// + /// + /// + public static IList ToXClaims( + this XApiKeyValidationResult source + ) + { + // + var result = new List(); + + // + // 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; + } } } \ No newline at end of file diff --git a/Extensions/xIdentityModelsExtensions.cs b/Extensions/xIdentityModelsExtensions.cs index 2314eff..8fd812a 100644 --- a/Extensions/xIdentityModelsExtensions.cs +++ b/Extensions/xIdentityModelsExtensions.cs @@ -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; } + + /// + /// Extract ApiKey Validated User ... + /// + /// + /// + public static XPersonDto ExtractApiKeyUser( + this ClaimsPrincipal source + ) + { + // + XPersonDto result = null; + + // + // Validate ... + if (!source.IsNull() && + source.Claims.HasChild()) + { + source.Claims.ExtractApiKeyUser(); + } + + // + return result; + } + + /// + /// Extract ApiKey Validated User ... + /// + /// + /// + public static XPersonDto ExtractApiKeyUser( + this IEnumerable 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; + } + + /// + /// Extract JWT Validated User ... + /// + /// + /// + public static XPersonDto ExtractAuthenticatedUser( + this ClaimsPrincipal source + ) + { + // + XPersonDto result = null; + + // + // Validate ... + if (!source.IsNull() && + source.Claims.HasChild()) + { + source.Claims.ExtractAuthenticatedUser(); + } + + // + return result; + } + + /// + /// Extract JWT Validated User ... + /// + /// + /// + public static XPersonDto ExtractAuthenticatedUser( + this IEnumerable 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; + } } } \ No newline at end of file diff --git a/Models/XApiKeyValidationResult.cs b/Models/XApiKeyValidationResult.cs index 272fc3e..3b8d40e 100644 --- a/Models/XApiKeyValidationResult.cs +++ b/Models/XApiKeyValidationResult.cs @@ -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 Scopes {get; set;} = []; public IList Errors { get; set; } = []; } diff --git a/Models/XUserClaimsInfoDto.cs b/Models/XUserClaimsInfoDto.cs index 0b57249..e463316 100644 --- a/Models/XUserClaimsInfoDto.cs +++ b/Models/XUserClaimsInfoDto.cs @@ -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,