-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMcpClaimTestFixture.cs
More file actions
73 lines (62 loc) · 2.85 KB
/
Copy pathMcpClaimTestFixture.cs
File metadata and controls
73 lines (62 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json.Nodes;
using NpgsqlRest.Mcp;
namespace NpgsqlRestTests.Setup;
[CollectionDefinition("McpClaimFixture")]
public class McpClaimFixtureCollection : ICollectionFixture<McpClaimTestFixture> { }
/// <summary>
/// Confirms that a claim-mapped routine parameter binds from the forwarded principal on tools/call (the
/// point of forwarding the ClaimsPrincipal), and that such a parameter is hidden from inputSchema. The
/// isolated <c>mcp_claim</c> schema holds <c>claim_echo(_user_id)</c> mapped to the <c>name_identifier</c>
/// claim. "/login-as?uid=" signs the caller in with that claim.
/// </summary>
public class McpClaimTestFixture : IDisposable
{
private readonly WebApplication _app;
private readonly Mcp _mcp = new(new McpOptions { Enabled = true });
public string ServerAddress { get; }
public IReadOnlyDictionary<string, JsonObject> Tools => _mcp.Tools;
public McpClaimTestFixture()
{
Database.Create();
var connectionString = Database.CreateAdditional("mcp_claim_test");
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://127.0.0.1:0");
builder.Services.AddAuthentication().AddCookie();
_app = builder.Build();
_app.MapGet("/login-as", (string uid) => Results.SignIn(new ClaimsPrincipal(new ClaimsIdentity(
claims: [new Claim("name_identifier", uid)],
authenticationType: CookieAuthenticationDefaults.AuthenticationScheme))));
_app.UseNpgsqlRest(new NpgsqlRestOptions(connectionString)
{
IncludeSchemas = ["mcp_claim"],
CommentsMode = CommentsMode.OnlyAnnotated,
AuthenticationOptions = new()
{
DefaultUserIdClaimType = "name_identifier",
UseUserParameters = true,
ParameterNameClaimsMapping = new() { { "_user_id", "name_identifier" } },
},
EndpointCreateHandlers = [_mcp],
});
_app.StartAsync().GetAwaiter().GetResult();
ServerAddress = _app.Urls.First();
}
public HttpClient CreateClient()
{
var handler = new HttpClientHandler { UseCookies = true, CookieContainer = new System.Net.CookieContainer() };
return new HttpClient(handler) { BaseAddress = new Uri(ServerAddress), Timeout = TimeSpan.FromHours(1) };
}
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
public void Dispose()
#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
{
_app.StopAsync().GetAwaiter().GetResult();
_app.DisposeAsync().GetAwaiter().GetResult();
}
}