From b99b182a1ac484763dc29d5cfb0ad71524ae8870 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 30 Oct 2026 00:20:01 +0330 Subject: [PATCH] Sort Ordering Usings ... Using ApiKey Validations and Claims Parsing from Extensions ... --- Attributes/XApiKeyAccessAttribute.cs | 35 +------------- Controllers/XBaseIdentityController.cs | 66 +++++++++++++++++++++++++- Extensions/XModelExtensions.cs | 1 + 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/Attributes/XApiKeyAccessAttribute.cs b/Attributes/XApiKeyAccessAttribute.cs index 88b97e1..678da5c 100644 --- a/Attributes/XApiKeyAccessAttribute.cs +++ b/Attributes/XApiKeyAccessAttribute.cs @@ -1,7 +1,5 @@ using System; -using IdentityModel; using System.Threading; -using xCommons.Constants; using xCommons.Extensions; using xExceptions.Constants; using xIdentityModels.Models; @@ -9,7 +7,7 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using xIdentityModels.Constants; -using System.Collections.Generic; +using xIdentityModels.Extensions; using xIdentityService.Constants; using xIdentityService.Interfaces; using Microsoft.AspNetCore.Mvc.Filters; @@ -156,38 +154,9 @@ namespace xIdentityService.Attributes return; } - // - var device = identityService.GetDevice(); - var userInfo = await identityService.GetUserInfo( - device: device, - userSelectByParam: validationResponse.OwnerId - ); - // // Creating Claims ... - var claims = new List { - // - // Owner Identifier of API Key ... - new(ClaimTypes.Name, validationResponse.OwnerId), - new(JwtClaimTypes.Subject, validationResponse.OwnerId), - // - // Application Id ... - new(XCustomClaims.ApplicationId, validationResponse.ApplicationId.ToString()), - // - // Authentication Type ... - new(XCustomClaims.AuthType, apiKeyAuthentication), - }; - - // - // Add Scopes ... - if (validationResponse.Scopes != null) - { - // - foreach (var scope in validationResponse.Scopes) - { - claims.Add(new Claim(JwtClaimTypes.Scope, scope)); - } - } + var claims = validationResponse.ToXClaims(); // // Create Principal and Ticket ... diff --git a/Controllers/XBaseIdentityController.cs b/Controllers/XBaseIdentityController.cs index 81ce817..d66a185 100644 --- a/Controllers/XBaseIdentityController.cs +++ b/Controllers/XBaseIdentityController.cs @@ -1,3 +1,4 @@ +using xModels.Dtos; using xCommons.Constants; using xCommons.Providers; using xCommons.Extensions; @@ -10,6 +11,7 @@ using Microsoft.AspNetCore.Mvc; using xIdentityModels.Constants; using System.Collections.Generic; using xIdentityService.Constants; +using xIdentityModels.Extensions; using xIdentityService.Interfaces; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Authentication; @@ -82,7 +84,7 @@ namespace xIdentityService.Controllers } // - return result; + return result; } /// @@ -106,7 +108,7 @@ namespace xIdentityService.Controllers } // - return result; + return result; } /// @@ -303,6 +305,66 @@ namespace xIdentityService.Controllers // return result; } + + /// + /// Get Authenticated ApiKey Person ... + /// + /// + [NonAction] + public XPersonDto GetApiKeyPerson() + { + // + var result = User.ExtractApiKeyUser(); + return result; + } + + /// + /// Validate and Get Authenticated ApiKey Person ... + /// + /// + [NonAction] + public XPersonDto ValidateAndGetApiKeyPerson() + { + // + var result = User.ExtractApiKeyUser(); + if (result.IsNullOrDefault()) + { + XException.NotAuthorized.Throw(); + } + + // + return result; + } + + /// + /// Get Authenticated JWT Person ... + /// + /// + [NonAction] + public XPersonDto GetAuthenticatedPerson() + { + // + var result = User.ExtractAuthenticatedUser(); + return result; + } + + /// + /// Validate and Get Authenticated JWT Person ... + /// + /// + [NonAction] + public XPersonDto ValidateAndGetAuthenticatedPerson() + { + // + var result = User.ExtractAuthenticatedUser(); + if (result.IsNullOrDefault()) + { + XException.NotAuthorized.Throw(); + } + + // + return result; + } #endregion } } \ No newline at end of file diff --git a/Extensions/XModelExtensions.cs b/Extensions/XModelExtensions.cs index 602f3f9..9a987f4 100644 --- a/Extensions/XModelExtensions.cs +++ b/Extensions/XModelExtensions.cs @@ -10,6 +10,7 @@ namespace xIdentityService.Extensions // var result = new XPersonDto { + UserId = source.UserId, Avatar = source.Avatar, Lastname = source.Lastname, Username = source.Username,