158 lines
5.1 KiB
C#
158 lines
5.1 KiB
C#
using System;
|
|
using xIds.Interfaces;
|
|
using System.Threading;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Http;
|
|
using xIdentityModels.Constants;
|
|
using xIdentityService.Constants;
|
|
using xIdentityModels.Extensions;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace xIds.Providers
|
|
{
|
|
[AttributeUsage(
|
|
AttributeTargets.Class | AttributeTargets.Method,
|
|
AllowMultiple = false,
|
|
Inherited = true
|
|
)]
|
|
public class XApiKeyAccessAttribute : AuthorizeAttribute, IAsyncAuthorizationFilter
|
|
{
|
|
/// <summary>
|
|
/// Optional allowed scopes. If empty, no scope enforcement.
|
|
/// </summary>
|
|
public XApiKeyScope[] Scopes { get; set; }
|
|
|
|
public XApiKeyAccessAttribute(
|
|
XApiKeyScope[] scopes = null
|
|
)
|
|
{
|
|
//
|
|
if (!scopes.HasChild())
|
|
{
|
|
scopes = [XApiKeyScope.Read];
|
|
}
|
|
|
|
//
|
|
Scopes = scopes;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authorization Filtering ...
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
|
|
{
|
|
//
|
|
// Access HttpContext ...
|
|
var http = context.HttpContext;
|
|
var error = XException.NotAuthorized.ToXError();
|
|
var apiKeyAuthentication = XAuthenticationScheme.XApiKey.GetStringValue();
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
// Obtain the endpoint currently being executed
|
|
var endpoint = http.GetEndpoint();
|
|
|
|
//
|
|
// Retrieve the application provider from DI ...
|
|
var applicationProvider = http.RequestServices
|
|
.GetService<IXApplicationProvider>();
|
|
if (applicationProvider.IsNull())
|
|
{
|
|
//
|
|
error.Message = "Api Key Service not Registered ...";
|
|
context.Result = new UnauthorizedObjectResult(error);
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Checking Header ...
|
|
var apiKeyHeader = XHeader.ApiKey.GetStringValue();
|
|
if (!http.Request.Headers.ContainsKey(apiKeyHeader))
|
|
{
|
|
//
|
|
error.Message = "ApiKey not Exists ...";
|
|
context.Result = new UnauthorizedObjectResult(error);
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Extract ApiKey from Header ...
|
|
var apiKey = http.Request.Headers[apiKeyHeader].ToString();
|
|
if (apiKey.IsNullOrEmpty())
|
|
{
|
|
//
|
|
error.Message = "ApiKey not Provided ...";
|
|
context.Result = new UnauthorizedObjectResult(error);
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Extract Client IP ...
|
|
var clientIP = http.Request.HttpContext.Connection.RemoteIpAddress?.ToString();
|
|
var scopes =
|
|
!Scopes.HasChild()
|
|
? string.Empty
|
|
: Scopes.ToListString();
|
|
|
|
//
|
|
// Validating ApiKey ...
|
|
var validationResult = await applicationProvider
|
|
.ValidateApiKey(
|
|
apiKey: apiKey,
|
|
userInfo: null,
|
|
clientIP: clientIP,
|
|
requiredScope: scopes,
|
|
cancellationToken: default
|
|
);
|
|
if (validationResult.Errors.HasChild())
|
|
{
|
|
//
|
|
var message = validationResult.Errors.ToListString(',');
|
|
error.Message = message;
|
|
context.Result = new UnauthorizedObjectResult(error);
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Creating Claims ...
|
|
var claims = validationResult.ToXClaims();
|
|
|
|
//
|
|
// Create Principal and Ticket ...
|
|
var identity = new ClaimsIdentity(claims, apiKeyAuthentication);
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
//
|
|
http.User = principal;
|
|
}
|
|
catch
|
|
{
|
|
//
|
|
error.Message = "ApiKey authorization failed ...";
|
|
context.Result = new UnauthorizedObjectResult(error);
|
|
}
|
|
}
|
|
|
|
/// <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
|
|
)
|
|
{
|
|
}
|
|
}
|
|
} |