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