add support filling policy handler ...

This commit is contained in:
2026-11-05 23:19:14 +03:30
parent ae7f5920c1
commit 5307fe4d2f
2 changed files with 57 additions and 7 deletions
+47
View File
@@ -1,9 +1,11 @@
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;
@@ -35,6 +37,7 @@ namespace xAiApi.Extensions
)
{
//
var policyHandler = services.GetRegisteredService<IXPolicyHandler>();
services.AddAuthorization(options =>
{
//
@@ -48,6 +51,26 @@ namespace xAiApi.Extensions
{
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())
);
}
}
}
}
}
@@ -62,8 +85,32 @@ namespace xAiApi.Extensions
{
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())
);
}
}
}
}
}
+10 -7
View File
@@ -160,7 +160,7 @@ namespace xAiApi.Providers
//
var tasks = servers
.Select(s =>
ConnectAndValidateAsync(s, cancellationToken));
ConnectAndValidateAsync(s, userInfo, cancellationToken));
await Task.WhenAll(tasks);
}
@@ -330,7 +330,7 @@ namespace xAiApi.Providers
await initLock.WaitAsync(cancellationToken);
try
{
await ConnectAndValidateAsync(result, cancellationToken);
await ConnectAndValidateAsync(result, userInfo, cancellationToken);
}
finally
{
@@ -497,7 +497,7 @@ namespace xAiApi.Providers
//
// Update ...
await ConnectAndValidateAsync(result, cancellationToken);
await ConnectAndValidateAsync(result, userInfo, cancellationToken);
}
finally
{
@@ -774,7 +774,7 @@ namespace xAiApi.Providers
await initLock.WaitAsync(cancellationToken);
try
{
await ConnectAndValidateAsync(exists, cancellationToken);
await ConnectAndValidateAsync(exists, userInfo, cancellationToken);
}
finally
{
@@ -862,7 +862,7 @@ namespace xAiApi.Providers
{
if (exists.Enabled)
{
await ConnectAndValidateAsync(exists, cancellationToken);
await ConnectAndValidateAsync(exists, userInfo, cancellationToken);
}
else
{
@@ -1201,7 +1201,8 @@ namespace xAiApi.Providers
private async Task ConnectAndValidateAsync(
XAiMcpServerDto descriptor,
CancellationToken cancellationToken
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{
//
@@ -1215,7 +1216,9 @@ namespace xAiApi.Providers
var client = await CreateClientAsync(descriptor, cts.Token);
await client.PingAsync(cancellationToken: cts.Token);
var serverTools = await client
.ListToolsAsync(cancellationToken: cts.Token);
.ListToolsAsync(
cancellationToken: cts.Token
);
//
clients[descriptor.Id] = client;