diff --git a/Controllers/Applications/ApplicationsController+Application.cs b/Controllers/Applications/ApplicationsController+Application.cs index 82cbce0..59aac60 100644 --- a/Controllers/Applications/ApplicationsController+Application.cs +++ b/Controllers/Applications/ApplicationsController+Application.cs @@ -90,14 +90,16 @@ namespace xIds.Controllers /// } /// /// + /// /// /// /// - [HttpPut("")] + [HttpPut("{id}")] [RequireXPowered] [Authorize(LocalApi.PolicyName)] [Authorize(Policy = XPolicies.EnabledUser)] public async Task> UpdateApplication( + [FromRoute] int id, [FromBody] XApplicationDto item, CancellationToken cancellationToken = default ) @@ -107,7 +109,10 @@ namespace xIds.Controllers { // // Validate ... - if (!item.IsValidForAdd()) + if (!id.IsValidIntId() || + !item.IsValidForAdd() || + id != item.Id + ) { XException.InvalidArgs.Throw(); } @@ -117,8 +122,8 @@ namespace xIds.Controllers // var result = await provider.UpdateApplicationAsync( + id: id, item: item, - id: item.Id, userInfo: userInfo, cancellationToken: cancellationToken ); diff --git a/Controllers/Applications/ApplicationsController+Custom.cs b/Controllers/Applications/ApplicationsController+Custom.cs index a4810aa..d2e67c5 100644 --- a/Controllers/Applications/ApplicationsController+Custom.cs +++ b/Controllers/Applications/ApplicationsController+Custom.cs @@ -18,6 +18,8 @@ namespace xIds.Controllers { public partial class ApplicationsController { + // + #region Custom ... /// /// Show Api Key ... /// @@ -510,6 +512,7 @@ namespace xIds.Controllers /// /// /// + [AllowAnonymous] [RequireXPowered] [HttpPost("ApiKeys/Validate")] [Authorize(LocalApi.PolicyName)] @@ -532,6 +535,19 @@ namespace xIds.Controllers // var userInfo = await GetUserInfo(); + var checksum = GetRequestChecksum(); + if (checksum.IsNullOrEmpty()) + { + XException.NotAuthorized.Throw(); + } + + // + // Validate Checksum ... + var requestChecksum = securityProvider.Encrypt(request.ToJSON()).ToMd5String(); + if (checksum.ToNormalString() != requestChecksum.ToNormalString()) + { + XException.NotAuthorized.Throw(); + } // var result = await provider.ValidateApiKey( @@ -552,5 +568,6 @@ namespace xIds.Controllers return result; } } + #endregion } } \ No newline at end of file diff --git a/Controllers/ApplicationsController.cs b/Controllers/ApplicationsController.cs index e3ca40d..6d8adc2 100644 --- a/Controllers/ApplicationsController.cs +++ b/Controllers/ApplicationsController.cs @@ -15,12 +15,14 @@ namespace xIds.Controllers public partial class ApplicationsController : XIBaseController { private readonly IXApplicationProvider provider; + private readonly IXSecurityProvider securityProvider; private readonly XApiKeyConfiguration apiKeyConfiguration; public ApplicationsController( IXApplicationProvider provider, IXIdentityManager identityManager, XAppConfiguration appConfiguration, + IXSecurityProvider securityProvider, ILogger logger, XValidationProvider validationProvider, XApiKeyConfiguration apiKeyConfiguration @@ -31,7 +33,9 @@ namespace xIds.Controllers validationProvider ) { + // this.provider = provider; + this.securityProvider = securityProvider; this.apiKeyConfiguration = apiKeyConfiguration; } } diff --git a/Controllers/Base/XIBaseController.cs b/Controllers/Base/XIBaseController.cs index a594e0d..ac6ef0d 100644 --- a/Controllers/Base/XIBaseController.cs +++ b/Controllers/Base/XIBaseController.cs @@ -1,18 +1,19 @@ using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; -using xCommons.Configurations; -using xCommons.Constants; -using xCommons.Controllers; -using xCommons.Extensions; -using xCommons.Providers; -using xExceptions.Constants; -using xIdentityModels.Constants; -using xIdentityModels.Models; using xIds.Interfaces; +using xCommons.Providers; +using xCommons.Constants; +using xCommons.Extensions; +using xCommons.Controllers; +using xExceptions.Constants; +using System.Threading.Tasks; +using xIdentityModels.Models; +using xCommons.Configurations; +using Microsoft.AspNetCore.Mvc; +using xIdentityModels.Constants; +using System.Collections.Generic; +using xIdentityService.Constants; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Authentication; namespace xIds.Controllers.Base { @@ -226,6 +227,54 @@ namespace xIds.Controllers.Base // #region NonActions ... + /// + /// Extract Checksum From Headers ... + /// + /// + [NonAction] + public string GetRequestChecksum() + { + // + var result = string.Empty; + + // + var headerKey = XHeader.ChecksumDigest.GetStringValue(); + + // + // Check if Connection ID is Provided ... + if (Request.Headers.ContainsKey(headerKey)) + { + result = Request.Headers[headerKey]; + } + + // + return result; + } + + /// + /// Extract ApiKey From Headers ... + /// + /// + [NonAction] + public string GetRequestApiKey() + { + // + var result = string.Empty; + + // + var headerKey = XHeader.ApiKey.GetStringValue(); + + // + // Check if Connection ID is Provided ... + if (Request.Headers.ContainsKey(headerKey)) + { + result = Request.Headers[headerKey]; + } + + // + return result; + } + /// /// Convert an Exception to Propper Error Result /// diff --git a/Providers/XApiKeyAccessAttribute.cs b/Providers/XApiKeyAccessAttribute.cs index f833874..407472b 100644 --- a/Providers/XApiKeyAccessAttribute.cs +++ b/Providers/XApiKeyAccessAttribute.cs @@ -14,6 +14,7 @@ using xIdentityService.Constants; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Authorization; +using Microsoft.Extensions.DependencyInjection; namespace xIds.Providers { @@ -56,6 +57,7 @@ namespace xIds.Providers /// Handle Authorization Filtering Using Tasks ... /// /// + /// /// private async Task OnAuthorizationAsync( AuthorizationFilterContext context, @@ -78,8 +80,7 @@ namespace xIds.Providers // // Retrieve the application provider from DI ... var applicationProvider = http.RequestServices - .GetService(typeof(IXApplicationProvider)) - as IXApplicationProvider; + .GetService(); if (applicationProvider.IsNull()) { //