146 lines
5.6 KiB
C#
146 lines
5.6 KiB
C#
using System.Linq;
|
|
using xIdentityHelper;
|
|
using xAiApi.Providers;
|
|
using xAiApi.Interfaces;
|
|
using xCommons.Extensions;
|
|
using xCommons.Authorization;
|
|
using System.Collections.Generic;
|
|
using xIdentityService.Interfaces;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace xAiApi.Extensions
|
|
{
|
|
public static class XProgramExtensions
|
|
{
|
|
/// <summary>
|
|
/// Register MCP Server Provider ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void AddXAiMcpServerProvider(
|
|
this IServiceCollection source
|
|
)
|
|
{
|
|
source.AddScoped<IXAiMcpServerProvider, XAiMcpServerProvider>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Register Authorization Policies
|
|
/// </summary>
|
|
/// <param name="services"></param>
|
|
/// <param name="policies"></param>
|
|
public static void AddXAuthorization(
|
|
this IServiceCollection services,
|
|
IDictionary<string, AuthorizationPolicy> policies = null
|
|
)
|
|
{
|
|
//
|
|
var policyHandler = services.GetRegisteredService<IXPolicyHandler>();
|
|
services.AddAuthorization(options =>
|
|
{
|
|
//
|
|
if (!policies.IsNull())
|
|
{
|
|
//
|
|
// Fill Policies ...
|
|
using (var policiesEnumerator = policies.GetEnumerator())
|
|
{
|
|
while (policiesEnumerator.MoveNext())
|
|
{
|
|
var policyDescriptor = policiesEnumerator.Current;
|
|
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
|
|
|
//
|
|
// Add Policy to Policy Handler ...
|
|
if (!policyHandler.IsNull())
|
|
{
|
|
//
|
|
// Check Policy is RequiredRole Policy ...
|
|
var roleRequirements = policyDescriptor.Value.Requirements
|
|
.OfType<RequiredRolesRequirement>()
|
|
.FirstOrDefault();
|
|
if (!roleRequirements.IsNull() &&
|
|
roleRequirements.Roles.HasChild())
|
|
{
|
|
//
|
|
policyHandler.AddPolicy(
|
|
policy: policyDescriptor.Key.ToNormalString(),
|
|
requiredRoles: roleRequirements.Roles.Select(r => r.ToNormalString())
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
var xPolicies = XAuthorizationHelper.GetXAuthorizationPolicies();
|
|
if (!xPolicies.IsNull())
|
|
{
|
|
//
|
|
// Fill Policies ...
|
|
using (var policiesEnumerator = xPolicies.GetEnumerator())
|
|
{
|
|
while (policiesEnumerator.MoveNext())
|
|
{
|
|
//
|
|
var policyDescriptor = policiesEnumerator.Current;
|
|
|
|
//
|
|
// Add Policy to Authorization ...
|
|
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value);
|
|
|
|
//
|
|
// Add Policy to Policy Handler ...
|
|
if (!policyHandler.IsNull())
|
|
{
|
|
//
|
|
// Check Policy is RequiredRole Policy ...
|
|
var roleRequirements = policyDescriptor.Value.Requirements
|
|
.OfType<RequiredRolesRequirement>()
|
|
.FirstOrDefault();
|
|
if (!roleRequirements.IsNull() &&
|
|
roleRequirements.Roles.HasChild())
|
|
{
|
|
//
|
|
policyHandler.AddPolicy(
|
|
policy: policyDescriptor.Key.ToNormalString(),
|
|
requiredRoles: roleRequirements.Roles.Select(r => r.ToNormalString())
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
//
|
|
services.AddSingleton<IAuthorizationHandler, RequiredRolesHandler>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use Forward Headers Options for resolving behind a proxy issues ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static void UseXForwardOptions(this IApplicationBuilder source)
|
|
{
|
|
//
|
|
var forwardOptions = new ForwardedHeadersOptions
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
|
|
RequireHeaderSymmetry = false
|
|
};
|
|
|
|
//
|
|
forwardOptions.KnownNetworks.Clear();
|
|
forwardOptions.KnownProxies.Clear();
|
|
|
|
//
|
|
// ref: https://github.com/aspnet/Docs/issues/2384
|
|
source.UseForwardedHeaders(forwardOptions);
|
|
}
|
|
}
|
|
} |