From ac5517358e2a5f4bd5e9eb57cf63f84f4ee90b2c Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 31 Oct 2026 21:40:44 +0330 Subject: [PATCH] add support for person service ... --- DI/XDIHelperExtension.cs | 138 +++++++++++++++++++++------------- Interfaces/IXPersonService.cs | 20 +++++ Providers/XPersonService.cs | 92 +++++++++++++++++++++++ 3 files changed, 199 insertions(+), 51 deletions(-) create mode 100644 Interfaces/IXPersonService.cs create mode 100644 Providers/XPersonService.cs diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index acea0b6..0eb77e3 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -12,18 +12,22 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using IdentityModel.AspNetCore.OAuth2Introspection; +using Microsoft.IdentityModel.Tokens; -namespace xIdentityService.DI { - public static partial class XDIHelperExtension { +namespace xIdentityService.DI +{ + public static partial class XDIHelperExtension + { /// /// Extract Module Configuration Structure from IConfiguration interface /// /// /// - public static XIdentityServiceConfiguration GetXIdentityServiceConfiguration (this IConfiguration config) { + public static XIdentityServiceConfiguration GetXIdentityServiceConfiguration(this IConfiguration config) + { // - var xIdentityServiceConfigSection = config.GetSection (ConfigurationNodeNames.IDENTITY_SERVICE_NODE_NAME); - return xIdentityServiceConfigSection.Get (); + var xIdentityServiceConfigSection = config.GetSection(ConfigurationNodeNames.IDENTITY_SERVICE_NODE_NAME); + return xIdentityServiceConfigSection.Get(); } /// @@ -32,14 +36,15 @@ namespace xIdentityService.DI { /// /// /// - public static void AddXIdentityService ( + public static void AddXIdentityService( this IServiceCollection services, IConfiguration configuration, ServiceLifetime lifeTime = ServiceLifetime.Singleton - ) { + ) + { // - var identityConfiguration = configuration.GetXIdentityServiceConfiguration (); - AddIdentityService ( + var identityConfiguration = configuration.GetXIdentityServiceConfiguration(); + AddIdentityService( services, identityConfiguration, lifeTime @@ -52,12 +57,13 @@ namespace xIdentityService.DI { /// /// /// - public static void AddXIdentityService ( + public static void AddXIdentityService( this IServiceCollection services, XIdentityServiceConfiguration configuration, ServiceLifetime lifeTime = ServiceLifetime.Singleton - ) { - AddIdentityService (services, configuration, lifeTime); + ) + { + AddIdentityService(services, configuration, lifeTime); } /// @@ -66,60 +72,81 @@ namespace xIdentityService.DI { /// /// /// - public static void AddXTokenProvider ( + public static void AddXTokenProvider( this IServiceCollection services, - ServiceLifetime lifeTime = ServiceLifetime.Singleton) - where T : IXTokenProvider { + ServiceLifetime lifeTime = ServiceLifetime.Singleton + ) + where T : IXTokenProvider + { // - Console.WriteLine ("IdentityService: Add Custom Token Provider ..."); - services.Add (new ServiceDescriptor (typeof (IXTokenProvider), typeof (T), lifeTime)); + Console.WriteLine("IdentityService: Add Custom Token Provider ..."); + services.Add(new ServiceDescriptor(typeof(IXTokenProvider), typeof(T), lifeTime)); + } + + /// + /// Register Person Service ... + /// + /// + /// + public static void AddXPersonService( + this IServiceCollection source, + ServiceLifetime lifeTime = ServiceLifetime.Singleton + ) + { + source.Add(new ServiceDescriptor(typeof(IXPersonService), typeof(XPersonService), lifeTime)); + Console.WriteLine("IdentityService: Add PersonService ..."); } /// /// Use Authentication /// /// - public static void UseXIdentityService (this IApplicationBuilder app) { - app.UseAuthentication (); + public static void UseXIdentityService(this IApplicationBuilder app) + { + app.UseAuthentication(); } // #region Private ... - private static void AddIdentityService ( + private static void AddIdentityService( this IServiceCollection services, XIdentityServiceConfiguration config, ServiceLifetime lifeTime = ServiceLifetime.Singleton - ) { + ) + { // - services.AddSingleton (config); + services.AddSingleton(config); // var builder = services - .AddAuthentication (XAuthenticationScheme.XToken.GetStringValue ()); + .AddAuthentication(XAuthenticationScheme.XToken.GetStringValue()); // // Add JWT Handler ... - builder.AddJwtBearer ( - XAuthenticationScheme.XToken.GetStringValue (), options => { + builder.AddJwtBearer( + XAuthenticationScheme.XToken.GetStringValue(), options => + { // options.Authority = config.Authority; options.TokenValidationParameters.ValidateAudience = false; - options.TokenValidationParameters.ValidTypes = new [] { "at+jwt" }; + options.TokenValidationParameters.ValidTypes = new[] { "at+jwt" }; // - options.ForwardDefaultSelector = context => { + options.ForwardDefaultSelector = context => + { // var path = context.Request.Path.Value; // - var fromHeader = TokenRetrieval.FromAuthorizationHeader (); - var fromQuery = TokenRetrieval.FromQueryString (); - var bearerToken = fromHeader (context.Request) ?? fromQuery (context.Request); + var fromHeader = TokenRetrieval.FromAuthorizationHeader(); + var fromQuery = TokenRetrieval.FromQueryString(); + var bearerToken = fromHeader(context.Request) ?? fromQuery(context.Request); // - var result = XAuthenticationScheme.XToken.GetStringValue (); - if (!bearerToken.IsNull () && !bearerToken.Contains (".")) { - result = XAuthenticationScheme.XIntrospection.GetStringValue (); + var result = XAuthenticationScheme.XToken.GetStringValue(); + if (!bearerToken.IsNull() && !bearerToken.Contains(".")) + { + result = XAuthenticationScheme.XIntrospection.GetStringValue(); } // @@ -129,21 +156,23 @@ namespace xIdentityService.DI { // // Add OAuthIntrospect ... - builder.AddOAuth2Introspection ( - XAuthenticationScheme.XIntrospection.GetStringValue (), options => { + builder.AddOAuth2Introspection( + XAuthenticationScheme.XIntrospection.GetStringValue(), options => + { // options.Authority = config.Authority; options.ClientId = config.ApiName; options.ClientSecret = config.ClientSecret; // - options.TokenRetriever = new Func (req => { + options.TokenRetriever = new Func(req => + { // - var fromHeader = TokenRetrieval.FromAuthorizationHeader (); - var fromQuery = TokenRetrieval.FromQueryString (); + var fromHeader = TokenRetrieval.FromAuthorizationHeader(); + var fromQuery = TokenRetrieval.FromQueryString(); // - var bearerToken = fromHeader (req) ?? fromQuery (req); + var bearerToken = fromHeader(req) ?? fromQuery(req); // return bearerToken; @@ -152,30 +181,37 @@ namespace xIdentityService.DI { // // Register Authorization Handler ... - services.AddSingleton (); - Console.WriteLine ("IdentityService: register Authorization Handlers ..."); + services.AddSingleton(); + Console.WriteLine("IdentityService: register Authorization Handlers ..."); // // Register Identity Provider ... - services.Add (new ServiceDescriptor (typeof (IXIdentityProvider), typeof (XIdentityProvider), lifeTime)); - Console.WriteLine ("IdentityService: Register IdentityProvider ..."); + services.Add(new ServiceDescriptor(typeof(IXIdentityProvider), typeof(XIdentityProvider), lifeTime)); + Console.WriteLine("IdentityService: Register IdentityProvider ..."); + + // + // Register Person Service ... + services.AddXPersonService(lifeTime); // // Try To Register InMemoryTokenProvier if it's not Provided ... IXTokenProvider xTokenProvider = null; - try { - xTokenProvider = services.GetRegisteredService (); - } catch { } - if (xTokenProvider.IsNull ()) { + try + { + xTokenProvider = services.GetRegisteredService(); + } + catch { } + if (xTokenProvider.IsNull()) + { // - Console.WriteLine ("IdentityService: there is no Custom Token Provider, Adding InMemoryTokenProvider instead ..."); - services.Add (new ServiceDescriptor (typeof (IXTokenProvider), typeof (XInMemoryTokenProvider), lifeTime)); + Console.WriteLine("IdentityService: there is no Custom Token Provider, Adding InMemoryTokenProvider instead ..."); + services.Add(new ServiceDescriptor(typeof(IXTokenProvider), typeof(XInMemoryTokenProvider), lifeTime)); } // // Adding Security Provider ... - services.Add (new ServiceDescriptor (typeof (IXSecurityProvider), typeof (XSecurityProvider), lifeTime)); - Console.WriteLine ("IdentityService: Register Security Provider ..."); + services.Add(new ServiceDescriptor(typeof(IXSecurityProvider), typeof(XSecurityProvider), lifeTime)); + Console.WriteLine("IdentityService: Register Security Provider ..."); } #endregion } diff --git a/Interfaces/IXPersonService.cs b/Interfaces/IXPersonService.cs new file mode 100644 index 0000000..37ce80c --- /dev/null +++ b/Interfaces/IXPersonService.cs @@ -0,0 +1,20 @@ +using xModels.Dtos; +using System.Threading.Tasks; + +namespace xIdentityService.Interfaces +{ + public interface IXPersonService + { + /// + /// Retrieve Person ... + /// + /// + /// + Task GetPerson(string userId); + + /// + /// Handle Cleanup ... + /// + void Cleanup(); + } +} \ No newline at end of file diff --git a/Providers/XPersonService.cs b/Providers/XPersonService.cs new file mode 100644 index 0000000..23fdee3 --- /dev/null +++ b/Providers/XPersonService.cs @@ -0,0 +1,92 @@ +using System; +using System.Linq; +using xModels.Dtos; +using xCommons.Extensions; +using xExceptions.Constants; +using System.Threading.Tasks; +using xIdentityService.Extensions; +using xIdentityService.Interfaces; +using System.Collections.Concurrent; + +namespace xIdentityService.Providers +{ + public class XPersonService : IXPersonService + { + // + private readonly IXIdentityProvider identityProvider; + private readonly ConcurrentDictionary persons = new ConcurrentDictionary(); + + public XPersonService( + IXIdentityProvider identityProvider + ) + { + this.identityProvider = identityProvider; + } + + public async Task GetPerson(string userId) + { + // + // Validate ... + if (userId.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); + } + + // + XPersonDto result = null; + + // + // if Persons has Child, Check it ... + if (persons.HasChild()) + { + // + var personEntry = persons + .FirstOrDefault(p => p.Key.Equals(userId, StringComparison.InvariantCultureIgnoreCase)); + if (!personEntry.IsNull()) + { + result = personEntry.Value; + } + } + + // + // Check if Person not Exists ... + if (result.IsNullOrDefault()) + { + // + // Prepare Device ... + var device = identityProvider.GetDevice(); + + // + // Retrieve User Info in OpenAction ... + var userInfo = await identityProvider + .GetUserInfo( + device: device, + userSelectByParam: userId + ); + + // + // Converts to Person Dto ... + result = userInfo.ToXPersonDto(); + + // + // Update Persons Dictionary ... + if (!result.IsNullOrDefault()) + { + persons[userId] = result; + } + } + + // + Cleanup(); + + // + return result; + } + + public void Cleanup() + { + // + // TODO: Implement Cleanup Pesons ... + } + } +} \ No newline at end of file