last ...
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Linq;
|
||||
using xIds.Helpers;
|
||||
using IdentityModel;
|
||||
using xIds.Interfaces;
|
||||
using xCommons.Constants;
|
||||
@@ -5,10 +7,12 @@ using xCommons.Extensions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using xIdentityService.Constants;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
|
||||
namespace xIds.Providers
|
||||
@@ -41,18 +45,49 @@ namespace xIds.Providers
|
||||
/// <returns></returns>
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
//
|
||||
// Obtain the endpoint currently being executed
|
||||
var endpoint = Context.GetEndpoint();
|
||||
|
||||
//
|
||||
// Retrieve all AuthorizeAttribute metadata from the endpoint
|
||||
var hasApiKeyScheme = false;
|
||||
var apiKeyScopes = XApiKeyHelper.GetXApiKeyScopes();
|
||||
var apiKeyPolicies = XApiKeyHelper.GetXApiKeyPolicies();
|
||||
var authorizeAttributes = endpoint?.Metadata.GetOrderedMetadata<IAuthorizeData>();
|
||||
if (
|
||||
apiKeyPolicies.HasChild() &&
|
||||
authorizeAttributes.HasChild()
|
||||
)
|
||||
{
|
||||
//
|
||||
// Retrieve a List of ApiKey Policies ...
|
||||
hasApiKeyScheme = authorizeAttributes.Any(attr =>
|
||||
apiKeyPolicies.Any(asc => asc.ToNormalString() == attr.Policy.ToNormalString()) ||
|
||||
attr.AuthenticationSchemes.ToNormalString() == XAuthenticationScheme.XApiKey.GetStringValue().ToNormalString()
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// if there is not any Policies for ApiKey Authentication
|
||||
// or Authorization, pass no Result ...
|
||||
if (!hasApiKeyScheme)
|
||||
{
|
||||
return AuthenticateResult.NoResult();
|
||||
}
|
||||
|
||||
//
|
||||
var apiKeyHeader = HeaderName;
|
||||
if (!Request.Headers.ContainsKey(apiKeyHeader))
|
||||
{
|
||||
return AuthenticateResult.NoResult();
|
||||
return AuthenticateResult.Fail("ApiKey not Exists ...");
|
||||
}
|
||||
|
||||
//
|
||||
var apiKey = Request.Headers[apiKeyHeader].ToString();
|
||||
if (apiKey.IsNullOrEmpty())
|
||||
{
|
||||
return AuthenticateResult.NoResult();
|
||||
return AuthenticateResult.Fail("ApiKey not Provided ...");
|
||||
}
|
||||
|
||||
// Extract Client IP ...
|
||||
|
||||
Reference in New Issue
Block a user