last ...
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Models;
|
||||
@@ -12,7 +11,6 @@ using xIdentityService.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace xIdentityService.Attributes
|
||||
@@ -22,7 +20,7 @@ namespace xIdentityService.Attributes
|
||||
AllowMultiple = false,
|
||||
Inherited = true
|
||||
)]
|
||||
public class XApiKeyAccessAttribute : AuthorizeAttribute, IAuthorizationFilter
|
||||
public class XApiKeyAccessAttribute : AuthorizeAttribute, IAsyncAuthorizationFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// Optional allowed scopes. If empty, no scope enforcement.
|
||||
@@ -41,51 +39,27 @@ namespace xIdentityService.Attributes
|
||||
|
||||
//
|
||||
Scopes = scopes;
|
||||
|
||||
//
|
||||
Policy = $"{XIdentityServiceConstants.PolicyCacheKeyPrefix}:{string.Join(";", Scopes)}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authorization Filtering ...
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public void OnAuthorization(AuthorizationFilterContext context)
|
||||
{
|
||||
Task.FromResult(OnAuthorizationAsync(context));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle Authorization Filtering Using Tasks ...
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task OnAuthorizationAsync(
|
||||
AuthorizationFilterContext context,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
|
||||
{
|
||||
//
|
||||
// Access HttpContext ...
|
||||
var http = context.HttpContext;
|
||||
var cancellationToken = http.RequestAborted;
|
||||
var error = XException.NotAuthorized.ToXError();
|
||||
var apiKeyAuthentication = XAuthenticationScheme.XApiKey.GetStringValue();
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Obtain the endpoint currently being executed
|
||||
var endpointFeature = http.Features.Get<IEndpointFeature>();
|
||||
var endpoint = endpointFeature?.Endpoint;
|
||||
if (endpoint.IsNull() ||
|
||||
endpointFeature.IsNull()
|
||||
)
|
||||
{
|
||||
//
|
||||
error.Message = "Endpoint not Accessible ...";
|
||||
context.Result = new UnauthorizedObjectResult(error);
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Identity Provider Service from DI ...
|
||||
var identityService = http.RequestServices
|
||||
|
||||
Reference in New Issue
Block a user