Heimdall token verifier: one process-wide instance owns the key cache

The verifier was registered as a typed client, which is transient, so each request
would have started with an empty key cache. It is now a singleton that takes its
HTTP client from the factory per fetch. Test hosts share one in-memory database
root so EF keeps a single service provider.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-11 02:55:35 +02:00
commit e99eea8769
4 changed files with 9 additions and 5 deletions

View file

@ -14,14 +14,15 @@ public sealed record HeimdallVerifiedIdentity(string AccountId, string? DisplayN
/// <summary>
/// Verifies Heimdall's access tokens (EdDSA) against Heimdall's published JWKS. Heimdall is the issuer
/// at <see cref="HeimdallOptions.BaseUrl"/>; the audience is this app's slug. Failures name a rule and
/// never echo the token or any of its contents.
/// never echo the token or any of its contents. One instance serves the process: it owns the key cache.
/// </summary>
public sealed class HeimdallTokenVerifier(
HttpClient httpClient,
IHttpClientFactory httpClientFactory,
IOptions<HeimdallOptions> heimdallOptions,
TimeProvider timeProvider)
{
public const string TokenType = "heimdall_access";
public const string ClientName = "heimdall-jwks";
internal static readonly TimeSpan ClockSkew = TimeSpan.FromSeconds(60);
internal static readonly TimeSpan KeyRefreshInterval = TimeSpan.FromSeconds(30);
@ -193,6 +194,7 @@ public sealed class HeimdallTokenVerifier(
{
try
{
var httpClient = httpClientFactory.CreateClient(ClientName);
using var response = await httpClient.GetAsync("/.well-known/jwks.json", cancellationToken);
response.EnsureSuccessStatusCode();
await using var body = await response.Content.ReadAsStreamAsync(cancellationToken);

View file

@ -130,7 +130,8 @@ builder.Services.AddHttpClient<HeimdallAuthService>((serviceProvider, client) =>
var options = serviceProvider.GetRequiredService<IOptions<HeimdallOptions>>().Value;
client.BaseAddress = new Uri(options.BaseUrl.TrimEnd('/'));
});
builder.Services.AddHttpClient<HeimdallTokenVerifier>((serviceProvider, client) =>
builder.Services.AddSingleton<HeimdallTokenVerifier>();
builder.Services.AddHttpClient(HeimdallTokenVerifier.ClientName, (serviceProvider, client) =>
{
var options = serviceProvider.GetRequiredService<IOptions<HeimdallOptions>>().Value;
client.BaseAddress = new Uri(options.BaseUrl.TrimEnd('/'));

View file

@ -280,7 +280,7 @@ public sealed class HeimdallHost : TestWebApplicationFactory
{
services.RemoveAll<TimeProvider>();
services.AddSingleton<TimeProvider>(Clock);
services.AddHttpClient<HeimdallTokenVerifier>().ConfigurePrimaryHttpMessageHandler(() => Heimdall);
services.AddHttpClient(HeimdallTokenVerifier.ClientName).ConfigurePrimaryHttpMessageHandler(() => Heimdall);
services.AddHttpClient<HeimdallAuthService>().ConfigurePrimaryHttpMessageHandler(() => Heimdall);
});
}

View file

@ -16,7 +16,8 @@ namespace Bifrost.Web.Tests.Support;
public class TestWebApplicationFactory : WebApplicationFactory<Program>
{
private readonly InMemoryDatabaseRoot _databaseRoot = new();
// One root for every factory: EF caches a service provider per root, and warns past twenty.
private static readonly InMemoryDatabaseRoot _databaseRoot = new();
private readonly string _databaseName = $"bifrost-tests-{Guid.NewGuid():N}";
protected override void ConfigureWebHost(IWebHostBuilder builder)