Compare commits

..
1 Commits
Author SHA1 Message Date
saherelm 5307fe4d2f add support filling policy handler ... 2026-11-05 23:19:14 +03:30
2 changed files with 57 additions and 7 deletions
+47
View File
@@ -1,9 +1,11 @@
using System.Linq;
using xIdentityHelper; using xIdentityHelper;
using xAiApi.Providers; using xAiApi.Providers;
using xAiApi.Interfaces; using xAiApi.Interfaces;
using xCommons.Extensions; using xCommons.Extensions;
using xCommons.Authorization; using xCommons.Authorization;
using System.Collections.Generic; using System.Collections.Generic;
using xIdentityService.Interfaces;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
@@ -35,6 +37,7 @@ namespace xAiApi.Extensions
) )
{ {
// //
var policyHandler = services.GetRegisteredService<IXPolicyHandler>();
services.AddAuthorization(options => services.AddAuthorization(options =>
{ {
// //
@@ -48,6 +51,26 @@ namespace xAiApi.Extensions
{ {
var policyDescriptor = policiesEnumerator.Current; var policyDescriptor = policiesEnumerator.Current;
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value); 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()) while (policiesEnumerator.MoveNext())
{ {
//
var policyDescriptor = policiesEnumerator.Current; var policyDescriptor = policiesEnumerator.Current;
//
// Add Policy to Authorization ...
options.AddPolicy(policyDescriptor.Key, policyDescriptor.Value); 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 var tasks = servers
.Select(s => .Select(s =>
ConnectAndValidateAsync(s, cancellationToken)); ConnectAndValidateAsync(s, userInfo, cancellationToken));
await Task.WhenAll(tasks); await Task.WhenAll(tasks);
} }
@@ -330,7 +330,7 @@ namespace xAiApi.Providers
await initLock.WaitAsync(cancellationToken); await initLock.WaitAsync(cancellationToken);
try try
{ {
await ConnectAndValidateAsync(result, cancellationToken); await ConnectAndValidateAsync(result, userInfo, cancellationToken);
} }
finally finally
{ {
@@ -497,7 +497,7 @@ namespace xAiApi.Providers
// //
// Update ... // Update ...
await ConnectAndValidateAsync(result, cancellationToken); await ConnectAndValidateAsync(result, userInfo, cancellationToken);
} }
finally finally
{ {
@@ -774,7 +774,7 @@ namespace xAiApi.Providers
await initLock.WaitAsync(cancellationToken); await initLock.WaitAsync(cancellationToken);
try try
{ {
await ConnectAndValidateAsync(exists, cancellationToken); await ConnectAndValidateAsync(exists, userInfo, cancellationToken);
} }
finally finally
{ {
@@ -862,7 +862,7 @@ namespace xAiApi.Providers
{ {
if (exists.Enabled) if (exists.Enabled)
{ {
await ConnectAndValidateAsync(exists, cancellationToken); await ConnectAndValidateAsync(exists, userInfo, cancellationToken);
} }
else else
{ {
@@ -1201,7 +1201,8 @@ namespace xAiApi.Providers
private async Task ConnectAndValidateAsync( private async Task ConnectAndValidateAsync(
XAiMcpServerDto descriptor, XAiMcpServerDto descriptor,
CancellationToken cancellationToken XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
) )
{ {
// //
@@ -1215,7 +1216,9 @@ namespace xAiApi.Providers
var client = await CreateClientAsync(descriptor, cts.Token); var client = await CreateClientAsync(descriptor, cts.Token);
await client.PingAsync(cancellationToken: cts.Token); await client.PingAsync(cancellationToken: cts.Token);
var serverTools = await client var serverTools = await client
.ListToolsAsync(cancellationToken: cts.Token); .ListToolsAsync(
cancellationToken: cts.Token
);
// //
clients[descriptor.Id] = client; clients[descriptor.Id] = client;