From d43428e0b6375136d2ded0151b97971705ec977f Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 23 Oct 2026 19:52:31 +0330 Subject: [PATCH] - Fix HttpContext RetrieveTokenAsXTokenResponse Extension Method; - Add Extract Api Key Extension Method from HttpContext; - Add Support for Token Introspect Validation in XIdentityService; --- Extensions/HttpContextExtensions.cs | 2 +- Extensions/XRequestExtensions.cs | 10 + Interfaces/IXIdentityProvider.cs | 1 + Partial/XIdentityProvider+Authentication.cs | 272 +++++++++++++------- 4 files changed, 186 insertions(+), 99 deletions(-) diff --git a/Extensions/HttpContextExtensions.cs b/Extensions/HttpContextExtensions.cs index 0482c0a..c16d81d 100644 --- a/Extensions/HttpContextExtensions.cs +++ b/Extensions/HttpContextExtensions.cs @@ -141,7 +141,7 @@ namespace xIdentityService.Extensions /// Retrieve All Required Tokens /// /// - public static async Task RetrieveTokensAsXTokenResponse(HttpContext context) + public static async Task RetrieveTokensAsXTokenResponse(this HttpContext context) { // var accessToken = await GetAccessToken(context); diff --git a/Extensions/XRequestExtensions.cs b/Extensions/XRequestExtensions.cs index edb1b1c..ee42598 100644 --- a/Extensions/XRequestExtensions.cs +++ b/Extensions/XRequestExtensions.cs @@ -6,6 +6,16 @@ namespace xIdentityService.Extensions { public static class XRequestExtensions { + /// + /// Extract ApiKey from Request Headers ... + /// + /// + /// + public static string ExtractApiKey(this HttpContext source) + { + return source.Request.ExtractApiKey(); + } + /// /// Extract ApiKey from Request Headers ... /// diff --git a/Interfaces/IXIdentityProvider.cs b/Interfaces/IXIdentityProvider.cs index 38fa2e8..89bcae5 100644 --- a/Interfaces/IXIdentityProvider.cs +++ b/Interfaces/IXIdentityProvider.cs @@ -44,6 +44,7 @@ namespace xIdentityService.Interfaces #region Authentication ... Task RequestDiscoveryDocument(); Task RequestScopeAccessToken(string scope); + Task Validate(XTokenResponse model); Task Introspection(XTokenResponse model); Task Login(XLoginRequest model); Task Logout(XTokenResponse model); diff --git a/Partial/XIdentityProvider+Authentication.cs b/Partial/XIdentityProvider+Authentication.cs index 9bcee0d..30133cc 100644 --- a/Partial/XIdentityProvider+Authentication.cs +++ b/Partial/XIdentityProvider+Authentication.cs @@ -9,33 +9,40 @@ using System.Collections.Generic; using xIdentityModels.Extensions; using xIdentityService.Constants; -namespace xIdentityService.Providers { - public partial class XIdentityProvider { +namespace xIdentityService.Providers +{ + public partial class XIdentityProvider + { // #region Authentication Handlers ... /// /// Request for Discovery Document /// /// an instance of DiscoveryDocumentResponse - public async Task RequestDiscoveryDocument () { + public async Task RequestDiscoveryDocument() + { // - var httpClient = GetHttpClient (); + var httpClient = GetHttpClient(); // - try { + try + { // var result = httpClient - .GetDiscoveryDocumentAsync (identityConfiguration.Authority) - .ContinueWith (docTask => { + .GetDiscoveryDocumentAsync(identityConfiguration.Authority) + .ContinueWith(docTask => + { // - httpClient.Dispose (); + httpClient.Dispose(); return docTask.Result; }); // return await result; - } catch { - throw XException.ActionFailed.ToException (); + } + catch + { + throw XException.ActionFailed.ToException(); } } @@ -44,25 +51,29 @@ namespace xIdentityService.Providers { /// /// a member of XApiScope /// an instance of XTokenResponse - public async Task RequestScopeAccessToken (string scope) { + public async Task RequestScopeAccessToken(string scope) + { // // Validate Args ... var scopeName = string.Empty; - try { + try + { scopeName = scope; // scope.GetStringValue (); - } catch { } - if (scopeName.IsNullOrEmpty ()) { - XException.InvalidArgs.Throw (); + } + catch { } + if (scopeName.IsNullOrEmpty()) + { + XException.InvalidArgs.Throw(); } // - var client = GetRestClient (); - var request = GetRestRequet ( + var client = GetRestClient(); + var request = GetRestRequet( XApiAccountEndpoint.RequestScopeAccessToken, - headers : new Dictionary { { XParam.XScope.GetStringValue (), scope.ToString () } }); + headers: new Dictionary { { XParam.XScope.GetStringValue(), scope.ToString() } }); // - var response = await RunRestRequest ( + var response = await RunRestRequest( client, request, XHttpMethod.POST @@ -70,8 +81,9 @@ namespace xIdentityService.Providers { // // Save Token ... - if (!response.IsNull ()) { - await tokenProvider.AddOrUpdateToken (response); + if (!response.IsNull()) + { + await tokenProvider.AddOrUpdateToken(response); } // @@ -83,45 +95,100 @@ namespace xIdentityService.Providers { /// /// Authentication Tokens /// an instance of TokenIntrospectionResponse - public async Task Introspection (XTokenResponse model) { + public async Task Introspection(XTokenResponse model) + { // // Validate Args ... await validationProvider - .GroupValidationBuilder () - .AddNotNull (model) - .AddNotEmpty (model.AccessToken) - .ValidateGroupAsync (); + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotEmpty(model.AccessToken) + .ValidateGroupAsync(); // - var httpClient = GetHttpClient (); + var httpClient = GetHttpClient(); // - try { + try + { // - var discoDoc = await RequestDiscoveryDocument (); - if (discoDoc.IsError) { - XException.ActionFailed.Throw (); + var discoDoc = await RequestDiscoveryDocument(); + if (discoDoc.IsError) + { + XException.ActionFailed.Throw(); } // - var response = await httpClient.IntrospectTokenAsync (new TokenIntrospectionRequest { + var response = await httpClient.IntrospectTokenAsync(new TokenIntrospectionRequest + { // Address = discoDoc.IntrospectionEndpoint, - ClientId = identityConfiguration.ClientId, - ClientSecret = identityConfiguration.ClientSecret, + ClientId = identityConfiguration.ClientId, + ClientSecret = identityConfiguration.ClientSecret, - // - Token = model.AccessToken + // + Token = model.AccessToken }); - if (response.IsError) { - throw new Exception (response.Error); + if (response.IsError) + { + throw new Exception(response.Error); } // return response; - } catch { - throw XException.ActionFailed.ToException (); } + catch + { + throw XException.ActionFailed.ToException(); + } + } + + /// + /// Validate Specified Token Model ... + /// + /// + /// + public async Task Validate(XTokenResponse model) + { + // + // Validate Args ... + await validationProvider + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotEmpty(model.AccessToken) + .ValidateGroupAsync(); + + // + var httpClient = GetHttpClient(); + + // + TokenIntrospectionResponse response = null; + try + { + // + var discoDoc = await RequestDiscoveryDocument(); + if (discoDoc.IsError) + { + XException.ActionFailed.Throw(); + } + + // + response = await httpClient.IntrospectTokenAsync(new TokenIntrospectionRequest + { + // + Address = discoDoc.IntrospectionEndpoint, + ClientId = identityConfiguration.ClientId, + ClientSecret = identityConfiguration.ClientSecret, + + // + Token = model.AccessToken + }); + } + catch + { } + + // + return response; } /// @@ -129,25 +196,26 @@ namespace xIdentityService.Providers { /// /// User Login Required Info, an instance of XLoginRequest /// an instance of XTokenResponse - public async Task Authenticate (XLoginRequest model) { + public async Task Authenticate(XLoginRequest model) + { // // Validate Args ... validationProvider - .GroupValidationBuilder () - .AddNotNull (model) - .AddNotEmpty ( + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotEmpty( model.UserSelectBy, model.Password ) - .ValidateGroup (); + .ValidateGroup(); // - var client = GetRestClient (); - var request = GetRestRequet (XApiAccountEndpoint.Authenticate); - request.AddJsonBody (model); + var client = GetRestClient(); + var request = GetRestRequet(XApiAccountEndpoint.Authenticate); + request.AddJsonBody(model); // - var response = await RunRestRequest ( + var response = await RunRestRequest( client, request, XHttpMethod.POST @@ -155,8 +223,9 @@ namespace xIdentityService.Providers { // // Save Token ... - if (!response.IsNull ()) { - await tokenProvider.AddOrUpdateToken (response); + if (!response.IsNull()) + { + await tokenProvider.AddOrUpdateToken(response); } // @@ -168,35 +237,37 @@ namespace xIdentityService.Providers { /// /// User Login Required Info, an instance of XLoginRequest /// an instance of XTokenResponse - public async Task Login (XLoginRequest model) { + public async Task Login(XLoginRequest model) + { // // Validate Args ... validationProvider - .GroupValidationBuilder () - .AddNotNull (model, model.Device) - .AddNotEmpty ( + .GroupValidationBuilder() + .AddNotNull(model, model.Device) + .AddNotEmpty( model.UserSelectBy, model.Password ) - .ValidateGroup (); + .ValidateGroup(); // - var client = GetRestClient (); - var request = GetRestRequet (XApiAccountEndpoint.Login); - request.AddJsonBody (model); + var client = GetRestClient(); + var request = GetRestRequet(XApiAccountEndpoint.Login); + request.AddJsonBody(model); // - var response = await RunRestRequest ( + var response = await RunRestRequest( client, request, XHttpMethod.POST ); // - if (!response.IsNull ()) { + if (!response.IsNull()) + { // - var tokens = response.ToXTokenResponse (); - await tokenProvider.AddOrUpdateToken (tokens); + var tokens = response.ToXTokenResponse(); + await tokenProvider.AddOrUpdateToken(tokens); } // @@ -208,20 +279,22 @@ namespace xIdentityService.Providers { /// /// /// - public async Task Logout (XTokenResponse model) { + public async Task Logout(XTokenResponse model) + { // await validationProvider - .GroupValidationBuilder () - .AddNotNull (model) - .AddNotEmpty (model.AccessToken) - .ValidateGroupAsync (); + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotEmpty(model.AccessToken) + .ValidateGroupAsync(); // var isExists = await tokenProvider - .IsTokenExists (model.AccessToken); - if (isExists) { + .IsTokenExists(model.AccessToken); + if (isExists) + { await tokenProvider - .RemoveToken (model.AccessToken); + .RemoveToken(model.AccessToken); } } @@ -230,35 +303,37 @@ namespace xIdentityService.Providers { /// /// Authentication Tokens /// an instance of XTokenResponse - public async Task RefreshTokens (XTokenResponse model) { + public async Task RefreshTokens(XTokenResponse model) + { // // Validate Args ... validationProvider - .GroupValidationBuilder () - .AddNotNull (model) - .AddNotEmpty ( + .GroupValidationBuilder() + .AddNotNull(model) + .AddNotEmpty( model.AccessToken, model.RefreshToken ) - .ValidateGroup (); + .ValidateGroup(); // - var client = GetRestClient (model); + var client = GetRestClient(model); // - var request = GetRestRequet ( + var request = GetRestRequet( XApiAccountEndpoint.RefreshTokens, - headers : model.ToHeaderDictionary ()); - request.AddJsonBody (model); + headers: model.ToHeaderDictionary()); + request.AddJsonBody(model); // - var response = await RunRestRequest ( + var response = await RunRestRequest( client, request, XHttpMethod.POST ); - if (!response.IsNull ()) { - await tokenProvider.UpdateToken (response); + if (!response.IsNull()) + { + await tokenProvider.UpdateToken(response); } // @@ -274,39 +349,40 @@ namespace xIdentityService.Providers { /// Authentication Tokens /// an instance of XActionRequest class which provider requirement for action /// - public async Task ChangePassword ( + public async Task ChangePassword( XTokenResponse tokens, XActionRequest model - ) { + ) + { // // Validate Args ... await validationProvider - .GroupValidationBuilder () - .AddNotNull (tokens, model) - .AddNotEmpty ( + .GroupValidationBuilder() + .AddNotNull(tokens, model) + .AddNotEmpty( model.Lang, model.Password, model.NewPassword, model.ReturnUrl) - .AddNotNull (model.Device) - .ValidateGroupAsync (); + .AddNotNull(model.Device) + .ValidateGroupAsync(); // // Validate User Select By Param ... - var userSelectByParam = GetUserSelectByParam ( + var userSelectByParam = GetUserSelectByParam( model, - forceNotNull : true, - excludes : null); + forceNotNull: true, + excludes: null); // - var client = GetRestClient (tokens); - var request = GetRestRequet ( + var client = GetRestClient(tokens); + var request = GetRestRequet( XApiAccountEndpoint.ChangePassword, - headers : tokens.ToHeaderDictionary ()); - request.AddJsonBody (model); + headers: tokens.ToHeaderDictionary()); + request.AddJsonBody(model); // - var response = await RunRestRequest ( + var response = await RunRestRequest( client, request, XHttpMethod.POST