Refactor and Test Application Controller and ApiKey Access Authorization Handling ...
This commit is contained in:
@@ -90,14 +90,16 @@ namespace xIds.Controllers
|
||||
/// }
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("")]
|
||||
[HttpPut("{id}")]
|
||||
[RequireXPowered]
|
||||
[Authorize(LocalApi.PolicyName)]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XApplicationDto>> 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
|
||||
);
|
||||
|
||||
@@ -18,6 +18,8 @@ namespace xIds.Controllers
|
||||
{
|
||||
public partial class ApplicationsController
|
||||
{
|
||||
//
|
||||
#region Custom ...
|
||||
/// <summary>
|
||||
/// Show Api Key ...
|
||||
/// </summary>
|
||||
@@ -510,6 +512,7 @@ namespace xIds.Controllers
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[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
|
||||
}
|
||||
}
|
||||
@@ -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<ApplicationsController> logger,
|
||||
XValidationProvider validationProvider,
|
||||
XApiKeyConfiguration apiKeyConfiguration
|
||||
@@ -31,7 +33,9 @@ namespace xIds.Controllers
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
this.provider = provider;
|
||||
this.securityProvider = securityProvider;
|
||||
this.apiKeyConfiguration = apiKeyConfiguration;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ...
|
||||
/// <summary>
|
||||
/// Extract Checksum From Headers ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract ApiKey From Headers ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert an Exception to Propper Error Result
|
||||
/// </summary>
|
||||
|
||||
@@ -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 ...
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
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<IXApplicationProvider>();
|
||||
if (applicationProvider.IsNull())
|
||||
{
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user