From c4e430a98f7b065853809c1674d60122e3c1399d Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 9 Oct 2026 14:20:47 +0330 Subject: [PATCH] last ... --- .../ApplicationsController+Custom.cs | 259 ++++++++++++++++-- xIds.csproj | 6 +- 2 files changed, 243 insertions(+), 22 deletions(-) diff --git a/Controllers/Applications/ApplicationsController+Custom.cs b/Controllers/Applications/ApplicationsController+Custom.cs index ddbe141..fa9ae51 100644 --- a/Controllers/Applications/ApplicationsController+Custom.cs +++ b/Controllers/Applications/ApplicationsController+Custom.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using xIdentityHelper; using System.Threading; using xCommons.Attributes; @@ -17,6 +18,13 @@ namespace xIds.Controllers { public partial class ApplicationsController { + /// + /// Create Api Key ... + /// + /// + /// + /// + /// [RequireXPowered] [Authorize(LocalApi.PolicyName)] [Authorize(Policy = XPolicies.EnabledUser)] @@ -56,7 +64,6 @@ namespace xIds.Controllers } // - // Create Specified Api Key ... var result = await provider.CreateApiKey( userInfo: userInfo, scopes: request.Scopes, @@ -78,6 +85,13 @@ namespace xIds.Controllers } } + /// + /// Remove Specified Api Key ... + /// + /// + /// + /// + /// [RequireXPowered] [Authorize(LocalApi.PolicyName)] [Authorize(Policy = XPolicies.EnabledUser)] @@ -117,7 +131,6 @@ namespace xIds.Controllers } // - // Create Specified Api Key ... var result = await provider.RemoveApiKeyAsync( id: apiKeyId, softDelete: false, @@ -136,6 +149,14 @@ namespace xIds.Controllers } } + /// + /// Rotate Specified Api Key ... + /// + /// + /// + /// + /// + /// [RequireXPowered] [Authorize(LocalApi.PolicyName)] [Authorize(Policy = XPolicies.EnabledUser)] @@ -192,7 +213,6 @@ namespace xIds.Controllers } // - // Create Specified Api Key ... var result = await provider.RotateApiKey( apiKeyId: apiKeyId, userInfo: userInfo, @@ -211,19 +231,75 @@ namespace xIds.Controllers } } + /// + /// Revoke Specified Api Key ... + /// + /// + /// + /// + /// [RequireXPowered] [Authorize(LocalApi.PolicyName)] [Authorize(Policy = XPolicies.EnabledUser)] [HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Revoke")] - public async Task> RevokeApiKey( + public async Task> RevokeApiKey( [FromRoute] Guid apiKeyId, [FromRoute] int applicationId, CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + try + { + // + // Validate ... + if ( + apiKeyId.IsDefaultGuid() || + !applicationId.IsValidIntId()) + { + XException.InvalidArgs.Throw(); + } + + // + var userInfo = await GetUserInfo(); + + // + // Retrieve Application for Validating ... + var application = await provider.GetApplicationAsync( + id: applicationId, + userInfo: userInfo, + includeBuilder: null, + ignoreSoftDeleteds: true + ); + if (application.IsNullOrDefault()) + { + XException.NotFound.Throw(); + } + + // + var result = await provider.RevokeApiKey( + apiKeyId: apiKeyId, + userInfo: userInfo, + cancellationToken: cancellationToken + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } } + /// + /// Retrieve Specified Application's Api Keys ... + /// + /// + /// + /// [RequireXPowered] [Authorize(LocalApi.PolicyName)] [Authorize(Policy = XPolicies.EnabledUser)] @@ -233,33 +309,176 @@ namespace xIds.Controllers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + try + { + // + // Validate ... + if (!applicationId.IsValidIntId()) + { + XException.InvalidArgs.Throw(); + } + + // + var userInfo = await GetUserInfo(); + + // + // Retrieve Application for Validating ... + var application = await provider.GetApplicationAsync( + id: applicationId, + userInfo: userInfo, + includeBuilder: null, + ignoreSoftDeleteds: true + ); + if (application.IsNullOrDefault()) + { + XException.NotFound.Throw(); + } + + // + var result = await provider.GetApplicationApiKeys( + userInfo: userInfo, + applicationId: applicationId, + cancellationToken: cancellationToken + ); + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } } + /// + /// Retrieve Specified Api Keys Usages ... + /// + /// + /// + /// + /// [RequireXPowered] [Authorize(LocalApi.PolicyName)] [Authorize(Policy = XPolicies.EnabledUser)] [HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Usages")] - public async Task> GetApiKeyUsages( + public async Task>> GetApiKeyUsages( [FromRoute] Guid apiKeyId, [FromRoute] int applicationId, CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); - } - - [RequireXPowered] - [Authorize(LocalApi.PolicyName)] - [HttpPost("Applications/{applicationId}/ApiKeys/{apiKeyId}/Validate")] - public async Task> ValidateApiKey( - [FromRoute] Guid apiKeyId, - [FromRoute] int applicationId, - CancellationToken cancellationToken = default - ) - { - throw new NotImplementedException(); + // + try + { + // + // Validate ... + if ( + apiKeyId.IsDefaultGuid() || + !applicationId.IsValidIntId()) + { + XException.InvalidArgs.Throw(); + } + + // + var userInfo = await GetUserInfo(); + + // + // Retrieve Application for Validating ... + var application = await provider.GetApplicationAsync( + id: applicationId, + userInfo: userInfo, + includeBuilder: null, + ignoreSoftDeleteds: true + ); + if (application.IsNullOrDefault()) + { + XException.NotFound.Throw(); + } + + // + // Retrieve ApiKey for Validating ... + var apiKey = await provider.GetApiKeyAsync( + id: apiKeyId, + userInfo: userInfo, + includeBuilder: null, + ignoreSoftDeleteds: true + ); + if (application.IsNullOrDefault()) + { + XException.NotFound.Throw(); + } + + // + var result = await provider.FindManyApiKeyUsageAsync( + userInfo: userInfo, + includeBuilder: null, + ignoreSoftDeleteds: true, + cancellationToken: cancellationToken, + predicate: x => x.ApiKeyId == apiKeyId, + orderBuilder: x => x.OrderBy(y => y.RequestedOn) + ); + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } } + /// + /// Validate Specifed Api Key ... + /// + /// + /// + /// + [RequireXPowered] + [Authorize(LocalApi.PolicyName)] + [HttpPost("Applications/ApiKeys/Validate")] + public async Task> ValidateApiKey( + [FromBody] XApiKeyValidationRequest request, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(request) + .AddNotEmpty(request.ApiKey) + .AddNotEmpty(request.ClientIP) + .ValidateGroupAsync(); + + // + var userInfo = await GetUserInfo(); + + // + var result = await provider.ValidateApiKey( + userInfo: userInfo, + apiKey: request.ApiKey, + clientIP: request.ClientIP, + requiredScope: request.RequiredScope, + cancellationToken: cancellationToken + ); + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } } } \ No newline at end of file diff --git a/xIds.csproj b/xIds.csproj index 1b4f73b..80fd750 100644 --- a/xIds.csproj +++ b/xIds.csproj @@ -29,13 +29,15 @@ - - + + +