Use PolicyHandler for Validating Authorization in MCP Handler ...

This commit is contained in:
2026-11-05 23:16:27 +03:30
parent 357bf221fa
commit fafc9836a2
+13 -8
View File
@@ -8,6 +8,7 @@ using System.Collections.Generic;
using xIdentityModels.Models;
using xCommons.Extensions;
using System.Linq;
using xIdentityService.Interfaces;
namespace xAiService.Services
{
@@ -15,12 +16,14 @@ namespace xAiService.Services
{
//
private readonly XAiMcpToolScanner scanner;
private readonly IXPolicyHandler policyHandler;
private readonly IXAiMcpUserContext userContext;
private readonly IServiceProvider serviceProvider;
private readonly Dictionary<string, XAiMcpToolDescriptor> tools = new();
public XAiMcpHandler(
XAiMcpToolScanner scanner,
IXPolicyHandler policyHandler,
IXAiMcpUserContext userContext,
IServiceProvider serviceProvider
)
@@ -28,6 +31,7 @@ namespace xAiService.Services
//
this.scanner = scanner;
this.userContext = userContext;
this.policyHandler = policyHandler;
this.serviceProvider = serviceProvider;
//
@@ -142,10 +146,10 @@ namespace xAiService.Services
//
var tool = tools[toolName];
//
// Check User Info Required or not ...
//
// Level 1 ...
if (userInfo.IsNull() && tool.RequiresAuthentication)
@@ -259,23 +263,24 @@ namespace xAiService.Services
{
return CreateResult(id, new JObject());
}
private bool CheckPolicy(XUserClaimsInfoDto user, string policy)
{
//
var result = false;
//
if (user.IsNullOrDefault()) {
if (user.IsNullOrDefault())
{
return result;
}
//
// Handle Authorization ...
result = user.Roles?.Any(r =>
r.Equals(
policy,
StringComparison.OrdinalIgnoreCase)) == true;
result = policyHandler.IsPolicyPassed(
policy: policy,
roles: user.Roles
);
//
return result;