Change Requirements for Authorization and Authentication Handling ...
This commit is contained in:
+86
-11
@@ -5,6 +5,9 @@ using Newtonsoft.Json.Linq;
|
||||
using xAiService.Interfaces;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Models;
|
||||
using xCommons.Extensions;
|
||||
using System.Linq;
|
||||
|
||||
namespace xAiService.Services
|
||||
{
|
||||
@@ -12,16 +15,19 @@ namespace xAiService.Services
|
||||
{
|
||||
//
|
||||
private readonly XAiMcpToolScanner scanner;
|
||||
private readonly IXAiMcpUserContext userContext;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
private readonly Dictionary<string, XAiMcpToolDescriptor> tools = new();
|
||||
|
||||
public XAiMcpHandler(
|
||||
XAiMcpToolScanner scanner,
|
||||
IXAiMcpUserContext userContext,
|
||||
IServiceProvider serviceProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
this.scanner = scanner;
|
||||
this.userContext = userContext;
|
||||
this.serviceProvider = serviceProvider;
|
||||
|
||||
//
|
||||
@@ -32,27 +38,42 @@ namespace xAiService.Services
|
||||
/// Process Specified JSON-RPC Request ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<JObject> ProcessAsync(
|
||||
JObject request,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var method = request["method"]?.ToString();
|
||||
var id = request["id"];
|
||||
// Filling User Context ...
|
||||
userContext.Set(userInfo, connectionId);
|
||||
|
||||
//
|
||||
return method switch
|
||||
try
|
||||
{
|
||||
//
|
||||
"initialize" => HandleInitialize(id),
|
||||
"tools/list" => HandleToolsList(id),
|
||||
"tools/call" => await HandleToolCallAsync(request, id, cancellationToken),
|
||||
"ping" => HandlePing(id),
|
||||
_ => CreateError(id, -32601, $"Method not found: {method}")
|
||||
};
|
||||
var id = request["id"];
|
||||
var method = request["method"]?.ToString();
|
||||
|
||||
//
|
||||
return method switch
|
||||
{
|
||||
"initialize" => HandleInitialize(id),
|
||||
"tools/list" => HandleToolsList(id, userInfo),
|
||||
"tools/call" => await HandleToolCallAsync(request, id, userInfo, cancellationToken),
|
||||
"ping" => HandlePing(id),
|
||||
_ => CreateError(id, -32601, $"Method not found: {method}")
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
userContext.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
@@ -74,11 +95,22 @@ namespace xAiService.Services
|
||||
});
|
||||
}
|
||||
|
||||
private JObject HandleToolsList(JToken id)
|
||||
private JObject HandleToolsList(
|
||||
JToken id,
|
||||
XUserClaimsInfoDto userInfo
|
||||
)
|
||||
{
|
||||
var jsonTools = new JArray();
|
||||
foreach (var tool in tools.Values)
|
||||
{
|
||||
//
|
||||
if (!tool.RequiredPolicy.IsNullOrEmpty() &&
|
||||
!CheckPolicy(userInfo, tool.RequiredPolicy))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
jsonTools.Add(new JObject
|
||||
{
|
||||
["name"] = tool.Name,
|
||||
@@ -94,6 +126,7 @@ namespace xAiService.Services
|
||||
private async Task<JObject> HandleToolCallAsync(
|
||||
JObject request,
|
||||
JToken id,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken ct
|
||||
)
|
||||
{
|
||||
@@ -108,7 +141,28 @@ namespace xAiService.Services
|
||||
}
|
||||
|
||||
//
|
||||
var result = await tools[toolName].Execute(args, ct);
|
||||
var tool = tools[toolName];
|
||||
|
||||
//
|
||||
// Check User Info Required or not ...
|
||||
|
||||
//
|
||||
// Level 1 ...
|
||||
if (userInfo.IsNull() && tool.RequiresAuthentication)
|
||||
{
|
||||
return CreateError(id, -32001, $"Authentication required for tool: {toolName}");
|
||||
}
|
||||
|
||||
//
|
||||
// Level 2 ...
|
||||
if (!tool.RequiredPolicy.IsNullOrEmpty() &&
|
||||
!CheckPolicy(userInfo, tool.RequiredPolicy))
|
||||
{
|
||||
return CreateError(id, -32002, $"Access denied for tool: {toolName}");
|
||||
}
|
||||
|
||||
//
|
||||
var result = await tool.Execute(args, ct);
|
||||
return CreateResult(id, new JObject
|
||||
{
|
||||
["content"] = new JArray
|
||||
@@ -205,6 +259,27 @@ namespace xAiService.Services
|
||||
{
|
||||
return CreateResult(id, new JObject());
|
||||
}
|
||||
|
||||
private bool CheckPolicy(XUserClaimsInfoDto user, string policy)
|
||||
{
|
||||
//
|
||||
var result = false;
|
||||
|
||||
//
|
||||
if (user.IsNullOrDefault()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Authorization ...
|
||||
result = user.Roles?.Any(r =>
|
||||
r.Equals(
|
||||
policy,
|
||||
StringComparison.OrdinalIgnoreCase)) == true;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user