Merge hands/heimdall-callback-signed
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
b2110f53fa
14 changed files with 2765 additions and 164 deletions
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<None Include="Fixtures\*.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="Fixtures\*.cjs" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
64
tests/Bifrost.Web.Tests/Fixtures/complete-page-harness.cjs
Normal file
64
tests/Bifrost.Web.Tests/Fixtures/complete-page-harness.cjs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// Runs the complete page's real inline script against a simulated browser, once per scenario, and prints
|
||||
// what it did as JSON: the ordered events, and the address and form values at the moment of the submit.
|
||||
// Usage: node complete-page-harness.cjs <page.html>
|
||||
const fs = require("fs");
|
||||
const vm = require("vm");
|
||||
|
||||
const html = fs.readFileSync(process.argv[2], "utf8");
|
||||
const scripts = [...html.matchAll(/<script>([\s\S]*?)<\/script>/g)];
|
||||
if (scripts.length !== 1) {
|
||||
throw new Error(`expected exactly one inline script, found ${scripts.length}`);
|
||||
}
|
||||
const script = scripts[0][1];
|
||||
|
||||
function run(name, search, hash) {
|
||||
const events = [];
|
||||
let submit = null;
|
||||
const location = {
|
||||
pathname: "/auth/heimdall/complete",
|
||||
search,
|
||||
hash,
|
||||
get href() {
|
||||
return this.pathname + this.search + this.hash;
|
||||
},
|
||||
replace(url) {
|
||||
events.push(`replace:${url}`);
|
||||
},
|
||||
};
|
||||
const history = {
|
||||
replaceState(_state, _title, url) {
|
||||
events.push(`replaceState:${url}`);
|
||||
const next = new URL(url, "https://bifrost.test" + location.href);
|
||||
location.pathname = next.pathname;
|
||||
location.search = next.search;
|
||||
location.hash = next.hash;
|
||||
},
|
||||
};
|
||||
const form = {
|
||||
elements: { attempt: { value: "" }, code: { value: "" } },
|
||||
submit() {
|
||||
events.push("submit");
|
||||
submit = {
|
||||
attempt: form.elements.attempt.value,
|
||||
code: form.elements.code.value,
|
||||
href: location.href,
|
||||
hash: location.hash,
|
||||
};
|
||||
},
|
||||
};
|
||||
const document = { getElementById: (id) => (id === "complete" ? form : null) };
|
||||
vm.runInNewContext(script, { URLSearchParams, location, history, document });
|
||||
return { name, events, submit };
|
||||
}
|
||||
|
||||
const complete = "&heimdall_completion_code=CODE123";
|
||||
console.log(
|
||||
JSON.stringify([
|
||||
run("success", "?attempt=NONCE", "#heimdall_status=success" + complete),
|
||||
run("success-with-decoy-query-code", "?attempt=NONCE&heimdall_completion_code=QCODE", "#heimdall_status=success" + complete),
|
||||
run("error-status-with-code", "?attempt=NONCE", "#heimdall_status=error&heimdall_error=denied" + complete),
|
||||
run("no-code", "?attempt=NONCE", "#heimdall_status=success"),
|
||||
run("no-attempt", "", "#heimdall_status=success" + complete),
|
||||
run("code-in-query-only", "?attempt=NONCE&heimdall_completion_code=QCODE", ""),
|
||||
]),
|
||||
);
|
||||
107
tests/Bifrost.Web.Tests/HeimdallCompletePageScriptTests.cs
Normal file
107
tests/Bifrost.Web.Tests/HeimdallCompletePageScriptTests.cs
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using Bifrost.Web.Tests.Support;
|
||||
|
||||
namespace Bifrost.Web.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Runs the complete page's inline script, as the server serves it, under Node against a simulated browser
|
||||
/// (Fixtures/complete-page-harness.cjs). Node is required: a missing Node fails these tests, it does not skip them.
|
||||
/// </summary>
|
||||
public sealed class HeimdallCompletePageScriptTests
|
||||
{
|
||||
private const string ErrorTarget = "replace:/?auth=heimdall-error";
|
||||
|
||||
private sealed record Run(string Name, string[] Events, Submit? Submit);
|
||||
|
||||
private sealed record Submit(string Attempt, string Code, string Href, string Hash);
|
||||
|
||||
private static async Task<Dictionary<string, Run>> RunScenariosAsync()
|
||||
{
|
||||
using var host = new HeimdallHost();
|
||||
using var client = host.NewClient();
|
||||
var page = await client.GetStringAsync("/auth/heimdall/complete");
|
||||
var pagePath = Path.Combine(Path.GetTempPath(), $"bifrost-complete-{Guid.NewGuid():N}.html");
|
||||
await File.WriteAllTextAsync(pagePath, page);
|
||||
try
|
||||
{
|
||||
var harness = Path.Combine(AppContext.BaseDirectory, "Fixtures", "complete-page-harness.cjs");
|
||||
var start = new ProcessStartInfo("node") { RedirectStandardOutput = true, RedirectStandardError = true };
|
||||
start.ArgumentList.Add(harness);
|
||||
start.ArgumentList.Add(pagePath);
|
||||
Process process;
|
||||
try
|
||||
{
|
||||
process = Process.Start(start)!;
|
||||
}
|
||||
catch (Win32Exception error)
|
||||
{
|
||||
throw new InvalidOperationException("Node must be on PATH to run the complete page's script: " + error.Message, error);
|
||||
}
|
||||
|
||||
using (process)
|
||||
{
|
||||
var output = process.StandardOutput.ReadToEndAsync();
|
||||
var errors = process.StandardError.ReadToEndAsync();
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(60));
|
||||
await process.WaitForExitAsync(timeout.Token);
|
||||
Assert.True(process.ExitCode == 0, await errors);
|
||||
var runs = JsonSerializer.Deserialize<Run[]>(await output, new JsonSerializerOptions(JsonSerializerDefaults.Web))!;
|
||||
return runs.ToDictionary(x => x.Name);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(pagePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_fragment_is_dropped_from_the_address_before_the_code_is_posted()
|
||||
{
|
||||
var runs = await RunScenariosAsync();
|
||||
|
||||
foreach (var name in new[] { "success", "success-with-decoy-query-code" })
|
||||
{
|
||||
var run = runs[name];
|
||||
Assert.Equal(2, run.Events.Length);
|
||||
Assert.StartsWith("replaceState:/auth/heimdall/complete?attempt=NONCE", run.Events[0]);
|
||||
Assert.DoesNotContain("#", run.Events[0]);
|
||||
Assert.DoesNotContain("CODE123", run.Events[0]);
|
||||
Assert.Equal("submit", run.Events[1]);
|
||||
Assert.Equal("", run.Submit!.Hash);
|
||||
Assert.DoesNotContain("CODE123", run.Submit.Href);
|
||||
Assert.DoesNotContain("#", run.Submit.Href);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task The_code_is_read_only_from_the_fragment()
|
||||
{
|
||||
var runs = await RunScenariosAsync();
|
||||
|
||||
Assert.Equal(new Submit("NONCE", "CODE123", "/auth/heimdall/complete?attempt=NONCE", ""), runs["success"].Submit);
|
||||
Assert.Equal("CODE123", runs["success-with-decoy-query-code"].Submit!.Code);
|
||||
|
||||
var queryOnly = runs["code-in-query-only"];
|
||||
Assert.Null(queryOnly.Submit);
|
||||
Assert.DoesNotContain("submit", queryOnly.Events);
|
||||
Assert.Equal(ErrorTarget, queryOnly.Events[^1]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("error-status-with-code")]
|
||||
[InlineData("no-code")]
|
||||
[InlineData("no-attempt")]
|
||||
public async Task A_failed_or_incomplete_handoff_never_submits_and_leaves_through_the_error_redirect(string scenario)
|
||||
{
|
||||
var run = (await RunScenariosAsync())[scenario];
|
||||
|
||||
Assert.Null(run.Submit);
|
||||
Assert.DoesNotContain("submit", run.Events);
|
||||
Assert.Equal(ErrorTarget, run.Events[^1]);
|
||||
Assert.Contains(run.Events, x => x.StartsWith("replaceState:", StringComparison.Ordinal) && !x.Contains('#'));
|
||||
Assert.DoesNotContain("CODE123", string.Join("\n", run.Events));
|
||||
}
|
||||
}
|
||||
1411
tests/Bifrost.Web.Tests/HeimdallCompletionTests.cs
Normal file
1411
tests/Bifrost.Web.Tests/HeimdallCompletionTests.cs
Normal file
File diff suppressed because it is too large
Load diff
521
tests/Bifrost.Web.Tests/Support/HeimdallTestKit.cs
Normal file
521
tests/Bifrost.Web.Tests/Support/HeimdallTestKit.cs
Normal file
|
|
@ -0,0 +1,521 @@
|
|||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Bifrost.Web.Data;
|
||||
using Bifrost.Web.Features.Heimdall;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Org.BouncyCastle.Math.EC.Rfc8032;
|
||||
|
||||
namespace Bifrost.Web.Tests.Support;
|
||||
|
||||
/// <summary>An Ed25519 key shaped like Heimdall's signing key: a "ed25519-" kid over a thumbprint prefix.</summary>
|
||||
public sealed class HeimdallKey
|
||||
{
|
||||
private readonly byte[] _seed;
|
||||
|
||||
private HeimdallKey(byte[] seed, byte[] publicKey, string kid)
|
||||
{
|
||||
_seed = seed;
|
||||
PublicKey = publicKey;
|
||||
Kid = kid;
|
||||
}
|
||||
|
||||
public string Kid { get; }
|
||||
|
||||
public byte[] PublicKey { get; }
|
||||
|
||||
public static HeimdallKey Generate(string? kid = null)
|
||||
{
|
||||
var seed = RandomNumberGenerator.GetBytes(Ed25519.SecretKeySize);
|
||||
var publicKey = new byte[Ed25519.PublicKeySize];
|
||||
Ed25519.GeneratePublicKey(seed, 0, publicKey, 0);
|
||||
kid ??= "ed25519-" + Convert.ToHexString(SHA256.HashData(publicKey)).ToLowerInvariant()[..12];
|
||||
return new HeimdallKey(seed, publicKey, kid);
|
||||
}
|
||||
|
||||
public object Jwk() => new Dictionary<string, object?>
|
||||
{
|
||||
["kty"] = "OKP",
|
||||
["crv"] = "Ed25519",
|
||||
["x"] = WebEncoders.Base64UrlEncode(PublicKey),
|
||||
["alg"] = "EdDSA",
|
||||
["kid"] = Kid,
|
||||
["use"] = "sig"
|
||||
};
|
||||
|
||||
/// <summary>Signs like Heimdall's signJwt: header {alg, kid, typ}, compact JWS.</summary>
|
||||
public string Sign(IDictionary<string, object?> claims, string alg = "EdDSA", string? kid = "") =>
|
||||
SignParts(HeaderBytes(alg, kid), JsonSerializer.SerializeToUtf8Bytes(claims));
|
||||
|
||||
public byte[] HeaderBytes(string alg = "EdDSA", string? kid = "")
|
||||
{
|
||||
var header = new Dictionary<string, object?> { ["alg"] = alg, ["typ"] = "JWT" };
|
||||
if (kid is not null)
|
||||
{
|
||||
header["kid"] = kid.Length == 0 ? Kid : kid;
|
||||
}
|
||||
|
||||
return JsonSerializer.SerializeToUtf8Bytes(header);
|
||||
}
|
||||
|
||||
/// <summary>Signs exactly these header and payload bytes, whatever they hold.</summary>
|
||||
public string SignParts(byte[] headerBytes, byte[] payloadBytes)
|
||||
{
|
||||
var signingInput = WebEncoders.Base64UrlEncode(headerBytes) + "." + WebEncoders.Base64UrlEncode(payloadBytes);
|
||||
var message = Encoding.ASCII.GetBytes(signingInput);
|
||||
var signature = new byte[Ed25519.SignatureSize];
|
||||
Ed25519.Sign(_seed, 0, message, 0, message.Length, signature, 0);
|
||||
return signingInput + "." + WebEncoders.Base64UrlEncode(signature);
|
||||
}
|
||||
}
|
||||
|
||||
public static class HeimdallShapes
|
||||
{
|
||||
public const string Issuer = "https://heimdall.test";
|
||||
|
||||
public static string NewAccountId() => Guid.NewGuid().ToString();
|
||||
|
||||
/// <summary>The claim set of Heimdall src/claims.ts issueAccessClaim.</summary>
|
||||
public static Dictionary<string, object?> AccessClaims(
|
||||
string accountId,
|
||||
DateTimeOffset now,
|
||||
string[]? capabilities = null,
|
||||
string? displayName = "Genuine Name",
|
||||
Action<Dictionary<string, object?>>? mutate = null)
|
||||
{
|
||||
var issuedAt = now.ToUnixTimeSeconds();
|
||||
var claims = new Dictionary<string, object?>
|
||||
{
|
||||
["iss"] = Issuer,
|
||||
["aud"] = "bifrost",
|
||||
["sub"] = accountId,
|
||||
["sid"] = Guid.NewGuid().ToString(),
|
||||
["jti"] = Guid.NewGuid().ToString(),
|
||||
["iat"] = issuedAt,
|
||||
["nbf"] = issuedAt,
|
||||
["exp"] = issuedAt + 900,
|
||||
["typ"] = "heimdall_access",
|
||||
["account_id"] = accountId,
|
||||
["access_revision"] = 1,
|
||||
["app"] = new Dictionary<string, object?> { ["slug"] = "bifrost", ["profile_version"] = 1 },
|
||||
["facts"] = Array.Empty<string>(),
|
||||
["capabilities"] = capabilities ?? [],
|
||||
["identities"] = new[]
|
||||
{
|
||||
new Dictionary<string, object?> { ["provider"] = "discord", ["providerUserId"] = "1234567890" }
|
||||
}
|
||||
};
|
||||
if (displayName is not null)
|
||||
{
|
||||
claims["display_name"] = displayName;
|
||||
}
|
||||
|
||||
mutate?.Invoke(claims);
|
||||
return claims;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The body Heimdall's redeem route answers with: the completionPayload of src/app.ts, whose unsigned
|
||||
/// fields (loose capabilities, facts, name) Bifrost must not decide anything from.
|
||||
/// </summary>
|
||||
public static Dictionary<string, object?> Completion(
|
||||
string returnTo,
|
||||
string accountId,
|
||||
string accessToken,
|
||||
string[]? looseCapabilities = null,
|
||||
string[]? looseFacts = null,
|
||||
string looseName = "Loose Name") => new()
|
||||
{
|
||||
["status"] = "success",
|
||||
["provider"] = "discord",
|
||||
["mode"] = "sign_in",
|
||||
["appSlug"] = "bifrost",
|
||||
["account"] = new Dictionary<string, object?> { ["id"] = accountId, ["displayName"] = looseName },
|
||||
["entitlements"] = new Dictionary<string, object?>
|
||||
{
|
||||
["facts"] = looseFacts ?? [],
|
||||
["snapshots"] = Array.Empty<string>()
|
||||
},
|
||||
["returnTo"] = returnTo,
|
||||
["connection"] = null,
|
||||
["session"] = new Dictionary<string, object?> { ["accountId"] = accountId, ["appSlug"] = "bifrost" },
|
||||
["accessToken"] = accessToken,
|
||||
["refreshToken"] = "refresh-token-not-read",
|
||||
["refresh"] = new Dictionary<string, object?> { ["expiresAt"] = "2026-11-11T00:00:00.000Z" },
|
||||
["verification"] = new Dictionary<string, object?> { ["issuer"] = Issuer },
|
||||
["sharedCapabilities"] = looseCapabilities ?? [],
|
||||
["hybridCapabilities"] = Array.Empty<string>()
|
||||
};
|
||||
}
|
||||
|
||||
public sealed class TestClock : TimeProvider
|
||||
{
|
||||
private DateTimeOffset _now = new(2026, 10, 11, 12, 0, 0, TimeSpan.Zero);
|
||||
|
||||
public DateTimeOffset Now => _now;
|
||||
|
||||
public void Advance(TimeSpan by) => _now += by;
|
||||
|
||||
public override DateTimeOffset GetUtcNow() => _now;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stands in for Heimdall's HTTP surface: the JWKS, the oauth start endpoint (which, like app.ts, answers
|
||||
/// a policy start only when the app caller's secret is present) and the completion-code redeem route.
|
||||
/// </summary>
|
||||
public sealed class FakeHeimdall : HttpMessageHandler
|
||||
{
|
||||
private readonly Dictionary<string, Dictionary<string, object?>> _codes = new(StringComparer.Ordinal);
|
||||
|
||||
public List<HeimdallKey> Published { get; } = [];
|
||||
|
||||
public List<object> ExtraJwks { get; } = [];
|
||||
|
||||
public int JwksFetches { get; private set; }
|
||||
|
||||
public HttpStatusCode JwksStatus { get; set; } = HttpStatusCode.OK;
|
||||
|
||||
private TaskCompletionSource? _jwksHold;
|
||||
|
||||
/// <summary>Completes when a JWKS request arrives after <see cref="HoldJwks"/>.</summary>
|
||||
public TaskCompletionSource JwksArrived { get; private set; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
/// <summary>Holds the next JWKS responses until the returned source completes.</summary>
|
||||
public TaskCompletionSource HoldJwks()
|
||||
{
|
||||
JwksArrived = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
return _jwksHold = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
}
|
||||
|
||||
public Exception? JwksFailure { get; set; }
|
||||
|
||||
public List<string> StartBodies { get; } = [];
|
||||
|
||||
public List<string?> StartSecrets { get; } = [];
|
||||
|
||||
/// <summary>The secret a policy start must carry in x-heimdall-app-secret; null means Heimdall asks for none.</summary>
|
||||
public string? RequiredAppSecret { get; set; }
|
||||
|
||||
public HttpStatusCode StartStatus { get; set; } = HttpStatusCode.OK;
|
||||
|
||||
public string StartBody { get; set; } =
|
||||
"""{"authorizationUrl":"https://heimdall.test/oauth/discord/authorize?state=fixture"}""";
|
||||
|
||||
public int RedeemCalls { get; private set; }
|
||||
|
||||
public HttpStatusCode? RedeemStatus { get; set; }
|
||||
|
||||
public string? RedeemBody { get; set; }
|
||||
|
||||
/// <summary>Registers a completion code Heimdall will redeem once, answering with this payload.</summary>
|
||||
public string RegisterCode(Dictionary<string, object?> completion)
|
||||
{
|
||||
var code = Convert.ToHexString(RandomNumberGenerator.GetBytes(16)).ToLowerInvariant();
|
||||
_codes[code] = completion;
|
||||
return code;
|
||||
}
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = request.RequestUri!.AbsolutePath;
|
||||
if (request.Method == HttpMethod.Get && path == "/.well-known/jwks.json")
|
||||
{
|
||||
JwksFetches++;
|
||||
JwksArrived.TrySetResult();
|
||||
if (_jwksHold is not null)
|
||||
{
|
||||
await _jwksHold.Task.WaitAsync(cancellationToken);
|
||||
}
|
||||
|
||||
if (JwksFailure is not null)
|
||||
{
|
||||
throw JwksFailure;
|
||||
}
|
||||
|
||||
var keys = Published.Select(x => x.Jwk()).Concat(ExtraJwks).ToArray();
|
||||
return new HttpResponseMessage(JwksStatus)
|
||||
{
|
||||
Content = new StringContent(JsonSerializer.Serialize(new { keys }), Encoding.UTF8, "application/json")
|
||||
};
|
||||
}
|
||||
|
||||
if (request.Method == HttpMethod.Post && path.StartsWith("/v1/oauth/", StringComparison.Ordinal) && path.EndsWith("/start", StringComparison.Ordinal))
|
||||
{
|
||||
var body = await request.Content!.ReadAsStringAsync(cancellationToken);
|
||||
var secret = request.Headers.TryGetValues("x-heimdall-app-secret", out var values) ? values.Single() : null;
|
||||
StartBodies.Add(body);
|
||||
StartSecrets.Add(secret);
|
||||
var wantsPolicy = JsonDocument.Parse(body).RootElement.TryGetProperty("entitlementPolicy", out _);
|
||||
if (wantsPolicy && RequiredAppSecret is not null && secret != RequiredAppSecret)
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.Unauthorized)
|
||||
{
|
||||
Content = new StringContent("""{"error":"app_auth_required"}""", Encoding.UTF8, "application/json")
|
||||
};
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(StartStatus)
|
||||
{
|
||||
Content = new StringContent(StartBody, Encoding.UTF8, "application/json")
|
||||
};
|
||||
}
|
||||
|
||||
if (request.Method == HttpMethod.Post && path == "/v1/apps/bifrost/auth-completions/redeem")
|
||||
{
|
||||
RedeemCalls++;
|
||||
if (RedeemStatus is { } status)
|
||||
{
|
||||
return new HttpResponseMessage(status)
|
||||
{
|
||||
Content = new StringContent(RedeemBody ?? "{}", Encoding.UTF8, "application/json")
|
||||
};
|
||||
}
|
||||
|
||||
var code = JsonDocument.Parse(await request.Content!.ReadAsStringAsync(cancellationToken))
|
||||
.RootElement.GetProperty("completionCode").GetString()!;
|
||||
if (!_codes.Remove(code, out var completion))
|
||||
{
|
||||
return new HttpResponseMessage(HttpStatusCode.Gone)
|
||||
{
|
||||
Content = new StringContent("""{"error":"invalid_or_expired_completion_code"}""", Encoding.UTF8, "application/json")
|
||||
};
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(HttpStatusCode.Created)
|
||||
{
|
||||
Content = new StringContent(RedeemBody ?? JsonSerializer.Serialize(completion), Encoding.UTF8, "application/json")
|
||||
};
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class CapturedLogs : ILoggerProvider
|
||||
{
|
||||
private readonly List<string> _lines = [];
|
||||
|
||||
public IReadOnlyList<string> Lines
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lines)
|
||||
{
|
||||
return _lines.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName) => new Capture(this);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
private sealed class Capture(CapturedLogs owner) : ILogger
|
||||
{
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
lock (owner._lines)
|
||||
{
|
||||
owner._lines.Add(formatter(state, exception) + (exception is null ? string.Empty : "\n" + exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>An attempt Start issued: its nonce, the cookie it set, and the returnTo it sent Heimdall.</summary>
|
||||
public sealed record StartedAttempt(string Nonce, string? CookieName, string? Cookie, string ReturnTo, HttpResponseMessage Response);
|
||||
|
||||
public sealed record CompletionResult(HttpResponseMessage Response, bool SignedIn);
|
||||
|
||||
/// <summary>A local Bifrost with Heimdall faked in-process: a test JWKS, a test clock, captured logs.</summary>
|
||||
public sealed class HeimdallHost : TestWebApplicationFactory
|
||||
{
|
||||
public const string AppSecret = "test-heimdall-app-secret";
|
||||
|
||||
private readonly string _appSecret;
|
||||
private readonly string _appSlug;
|
||||
private readonly string _baseUrl;
|
||||
private readonly string _publicBaseUrl;
|
||||
private readonly bool _discordRoles;
|
||||
|
||||
public TestClock Clock { get; } = new();
|
||||
|
||||
public FakeHeimdall Heimdall { get; } = new();
|
||||
|
||||
public CapturedLogs Logs { get; } = new();
|
||||
|
||||
public HeimdallKey Key { get; } = HeimdallKey.Generate();
|
||||
|
||||
public HeimdallHost(
|
||||
string appSecret = AppSecret,
|
||||
string baseUrl = HeimdallShapes.Issuer,
|
||||
string publicBaseUrl = "https://localhost",
|
||||
bool discordRoles = true,
|
||||
string appSlug = "bifrost")
|
||||
{
|
||||
_appSecret = appSecret;
|
||||
_appSlug = appSlug;
|
||||
_baseUrl = baseUrl;
|
||||
_publicBaseUrl = publicBaseUrl;
|
||||
_discordRoles = discordRoles;
|
||||
Heimdall.Published.Add(Key);
|
||||
Heimdall.RequiredAppSecret = AppSecret;
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureWebHost(builder);
|
||||
// The GitHub OAuth scheme is registered from configuration read while the app builds, which only host settings reach.
|
||||
builder.UseSetting("GitHubOAuth:ClientId", "configured-for-tests");
|
||||
builder.UseSetting("GitHubOAuth:ClientSecret", "configured-for-tests");
|
||||
builder.ConfigureAppConfiguration((_, configuration) =>
|
||||
{
|
||||
configuration.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["Heimdall:BaseUrl"] = _baseUrl,
|
||||
["Heimdall:AppSecret"] = _appSecret,
|
||||
["Heimdall:AppSlug"] = _appSlug,
|
||||
["Heimdall:DiscordGuildId"] = "guild-1",
|
||||
["Host:PublicBaseUrl"] = _publicBaseUrl
|
||||
});
|
||||
if (_discordRoles)
|
||||
{
|
||||
configuration.AddInMemoryCollection(new Dictionary<string, string?> { ["Heimdall:DiscordAllowedRoleIds:0"] = "role-1" });
|
||||
}
|
||||
});
|
||||
builder.ConfigureLogging(logging => logging.AddProvider(Logs));
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.RemoveAll<TimeProvider>();
|
||||
services.AddSingleton<TimeProvider>(Clock);
|
||||
services.AddHttpClient(HeimdallTokenVerifier.ClientName).ConfigurePrimaryHttpMessageHandler(() => Heimdall);
|
||||
services.AddHttpClient<HeimdallAuthService>().ConfigurePrimaryHttpMessageHandler(() => Heimdall);
|
||||
});
|
||||
}
|
||||
|
||||
public HttpClient NewClient() => CreateClient(new WebApplicationFactoryClientOptions
|
||||
{
|
||||
AllowAutoRedirect = false,
|
||||
HandleCookies = false,
|
||||
BaseAddress = new Uri("https://localhost")
|
||||
});
|
||||
|
||||
public string Token(string accountId, string[]? capabilities = null, string? displayName = "Genuine Name",
|
||||
Action<Dictionary<string, object?>>? mutate = null, HeimdallKey? signer = null) =>
|
||||
(signer ?? Key).Sign(HeimdallShapes.AccessClaims(accountId, Clock.Now, capabilities, displayName, mutate));
|
||||
|
||||
public Task<StartedAttempt> StartAsync(
|
||||
bool member = false, string provider = "discord", string returnUrl = "/App/Mill", string? cookieHeader = null) =>
|
||||
StartAtAsync(
|
||||
$"/auth/heimdall/{provider}?returnUrl={Uri.EscapeDataString(returnUrl)}" + (member ? "&access=member" : string.Empty),
|
||||
cookieHeader);
|
||||
|
||||
public async Task<StartedAttempt> StartAtAsync(string url, string? cookieHeader = null)
|
||||
{
|
||||
using var client = NewClient();
|
||||
var before = Heimdall.StartBodies.Count;
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
if (cookieHeader is not null)
|
||||
{
|
||||
request.Headers.Add("Cookie", cookieHeader);
|
||||
}
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
if (Heimdall.StartBodies.Count == before)
|
||||
{
|
||||
return new StartedAttempt(string.Empty, null, null, string.Empty, response);
|
||||
}
|
||||
|
||||
var returnTo = JsonDocument.Parse(Heimdall.StartBodies[^1]).RootElement.GetProperty("returnTo").GetString()!;
|
||||
var nonce = QueryHelpers.ParseQuery(new Uri(returnTo).Query)["attempt"].ToString();
|
||||
var slot = HeimdallAttemptCookie.SlotNames.FirstOrDefault(x => CookieValue(response, x) is { Length: > 0 });
|
||||
return new StartedAttempt(nonce, slot, slot is null ? null : CookieValue(response, slot), returnTo, response);
|
||||
}
|
||||
|
||||
/// <summary>Registers a completion code as Heimdall would mint it after authenticating this attempt.</summary>
|
||||
public string CodeFor(StartedAttempt attempt, string accountId, string? accessToken = null, string[]? looseCapabilities = null,
|
||||
string[]? looseFacts = null, string looseName = "Loose Name", string? returnTo = null)
|
||||
{
|
||||
var completion = HeimdallShapes.Completion(
|
||||
returnTo ?? attempt.ReturnTo, accountId, accessToken ?? Token(accountId), looseCapabilities, looseFacts, looseName);
|
||||
return Heimdall.RegisterCode(completion);
|
||||
}
|
||||
|
||||
/// <summary>The browser's POST from the complete page: the attempt and code in a form, the attempt cookie by name.</summary>
|
||||
public Task<CompletionResult> CompleteAsync(string? nonce, string? code, string? cookie, string? cookieName = null) =>
|
||||
CompleteWithHeaderAsync(
|
||||
nonce, code, cookie is null ? null : $"{cookieName ?? HeimdallAttemptCookie.SlotNames[0]}={cookie}");
|
||||
|
||||
/// <summary>The same POST carrying a browser's whole Cookie header (a jar), not one attempt cookie.</summary>
|
||||
public async Task<CompletionResult> CompleteWithHeaderAsync(string? nonce, string? code, string? cookieHeader)
|
||||
{
|
||||
using var client = NewClient();
|
||||
var fields = new Dictionary<string, string>();
|
||||
if (nonce is not null)
|
||||
{
|
||||
fields["attempt"] = nonce;
|
||||
}
|
||||
|
||||
if (code is not null)
|
||||
{
|
||||
fields["code"] = code;
|
||||
}
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "/auth/heimdall/complete") { Content = new FormUrlEncodedContent(fields) };
|
||||
if (cookieHeader is not null)
|
||||
{
|
||||
request.Headers.Add("Cookie", cookieHeader);
|
||||
}
|
||||
|
||||
var response = await client.SendAsync(request);
|
||||
var session = CookieValue(response, ".AspNetCore.Cookies");
|
||||
return new CompletionResult(response, !string.IsNullOrEmpty(session));
|
||||
}
|
||||
|
||||
public Task<CompletionResult> CompleteAsync(StartedAttempt attempt, string code) =>
|
||||
CompleteAsync(attempt.Nonce, code, attempt.Cookie, attempt.CookieName);
|
||||
|
||||
public static string? CookieValue(HttpResponseMessage response, string name)
|
||||
{
|
||||
if (!response.Headers.TryGetValues("Set-Cookie", out var headers))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var prefix = name + "=";
|
||||
var header = headers.FirstOrDefault(x => x.StartsWith(prefix, StringComparison.Ordinal));
|
||||
return header?[prefix.Length..].Split(';')[0];
|
||||
}
|
||||
|
||||
public static string? SetCookieHeader(HttpResponseMessage response, string name) =>
|
||||
response.Headers.TryGetValues("Set-Cookie", out var headers)
|
||||
? headers.FirstOrDefault(x => x.StartsWith(name + "=", StringComparison.Ordinal))
|
||||
: null;
|
||||
|
||||
public async Task<T> Db<T>(Func<BifrostDbContext, Task<T>> query)
|
||||
{
|
||||
using var scope = Services.CreateScope();
|
||||
return await query(scope.ServiceProvider.GetRequiredService<BifrostDbContext>());
|
||||
}
|
||||
|
||||
public Task<Bifrost.Web.Domain.UserAccount?> Account(string heimdallAccountId) =>
|
||||
Db(db => db.UserAccounts
|
||||
.Include(x => x.Membership!).ThenInclude(x => x.RoleAssignments)
|
||||
.SingleOrDefaultAsync(x => x.HeimdallAccountId == heimdallAccountId));
|
||||
|
||||
public Task<int> AccountCount() => Db(db => db.UserAccounts.CountAsync());
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue