Compare commits

..
1 Commits
Author SHA1 Message Date
saherelm fd78ac884c Add XPerson as Owner inside XApiKeyValidation Result ...
Add UserID in XPersonDto ...
handle User Retrievers from Claims ...
Handle User Converters ...
2026-10-30 00:22:02 +03:30
5 changed files with 226 additions and 6 deletions
+4 -3
View File
@@ -111,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; }
} }
} }
+77
View File
@@ -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>
@@ -274,5 +305,51 @@ 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.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;
}
} }
} }
+114 -1
View File
@@ -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;
}
} }
} }
+2
View File
@@ -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; } = [];
} }
+27
View File
@@ -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,