diff --git a/src/Bifrost.Web/Features/Heimdall/HeimdallIdentityMoveClient.cs b/src/Bifrost.Web/Features/Heimdall/HeimdallIdentityMoveClient.cs new file mode 100644 index 0000000..b670aee --- /dev/null +++ b/src/Bifrost.Web/Features/Heimdall/HeimdallIdentityMoveClient.cs @@ -0,0 +1,267 @@ +using System.Net; +using System.Text; +using System.Text.Json; +using System.Text.RegularExpressions; +using Bifrost.Web.Configuration; +using Microsoft.Extensions.Options; + +namespace Bifrost.Web.Features.Heimdall; + +/// +/// What Bifrost asks Heimdall to move (docs/service-contract.md, POST /v1/apps/{app}/identity-moves). Exactly one of +/// (a forward move, optionally of ) and (the undo). The body +/// never names an account: the proofs, live Heimdall access tokens, say which accounts are meant. +/// +public sealed record HeimdallMoveRequest( + string MoveId, IReadOnlyList FromProofs, string? ToProof = null, IReadOnlyList? IdentityIds = null, string? ReversesMoveId = null); + +public enum HeimdallProofTier +{ + Strong, + Weak +} + +/// The receipt of a move: the destination account, the logins that moved and the tier of the proofs that carried it. +public sealed record HeimdallMoveReceipt( + string MoveId, string AccountId, IReadOnlyList MovedIdentityIds, HeimdallProofTier ProofFrom, HeimdallProofTier? ProofTo, string? ReversesMoveId); + +/// Heimdall's fixed refusal codes for a move, plus the two local classes: a 400, and a 4xx this client does not know. +public enum HeimdallMoveRefusal +{ + AppAuthRequired, + NotAllowedForApp, + NotReversible, + ProofInvalid, + SameAccount, + ProofStale, + IdentityNotProved, + AlreadyMoved, + ProviderConflict, + MoveIdConflict, + IdentityNotFound, + RequestInvalid, + Unrecognized +} + +public enum HeimdallProofSide +{ + From, + To +} + +/// +/// The closed answer to a move. Moved: the receipt. Refused: Heimdall answered a 4xx before applying anything, so nothing changed. +/// Unknown: anything else (a 5xx may follow a committed move, a timeout may hide one): the caller replays the same request. +/// +public abstract record HeimdallMoveOutcome +{ + private HeimdallMoveOutcome() + { + } + + public sealed record Moved(HeimdallMoveReceipt Receipt) : HeimdallMoveOutcome; + + public sealed record Refused(HeimdallMoveRefusal Code, HeimdallProofSide? Side = null, string? Provider = null) : HeimdallMoveOutcome; + + public sealed record Unknown(string Reason) : HeimdallMoveOutcome; +} + +/// +/// Heimdall's move route under Bifrost's app secret (HeimdallAuthService.ConfigureClient adds the header). Each call sends one request and +/// retries nothing: replay with the same is the caller's, and Heimdall answers it with the stored +/// receipt. Logs name the outcome class and refusal code only. +/// +public sealed partial class HeimdallIdentityMoveClient( + HttpClient httpClient, IOptions heimdallOptions, ILogger logger) +{ + public static readonly TimeSpan Timeout = TimeSpan.FromSeconds(15); + + private const int MaxMovedIdentities = 256; + + [GeneratedRegex("^[A-Za-z0-9_-]{16,64}$")] + private static partial Regex MoveIdShape(); + + public async Task MoveAsync(HeimdallMoveRequest request, CancellationToken cancellationToken) + { + var body = Serialize(request); + var outcome = await SendAsync(request, body, cancellationToken); + logger.LogInformation("Heimdall identity move: {Outcome} {Code}", outcome.GetType().Name, outcome is HeimdallMoveOutcome.Refused refused ? refused.Code : null); + return outcome; + } + + private async Task SendAsync(HeimdallMoveRequest request, string body, CancellationToken cancellationToken) + { + try + { + using var message = new HttpRequestMessage( + HttpMethod.Post, $"/v1/apps/{Uri.EscapeDataString(heimdallOptions.Value.AppSlug)}/identity-moves") + { + Content = new StringContent(body, Encoding.UTF8, "application/json") + }; + using var response = await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + var status = (int)response.StatusCode; + if (response.StatusCode == HttpStatusCode.OK) + { + return await ReadReceiptAsync(response, request, cancellationToken) is { } receipt + ? new HeimdallMoveOutcome.Moved(receipt) + : new HeimdallMoveOutcome.Unknown("malformed or mismatched receipt"); + } + + if (status is < 400 or >= 500 || status == 429) + { + return new HeimdallMoveOutcome.Unknown($"HTTP {status}"); + } + + var refusal = await HeimdallRefusal.ReadAsync(response, cancellationToken); + return status == 400 ? new HeimdallMoveOutcome.Refused(HeimdallMoveRefusal.RequestInvalid) : Refused(refusal); + } + catch (Exception error) when (error is HttpRequestException or JsonException or NotSupportedException or TaskCanceledException + && !cancellationToken.IsCancellationRequested) + { + return new HeimdallMoveOutcome.Unknown("transport"); + } + } + + private static HeimdallMoveOutcome.Refused Refused(HeimdallRefusalBody? refusal) + { + var code = refusal?.Code switch + { + "app_auth_required" => HeimdallMoveRefusal.AppAuthRequired, + "not_allowed_for_app" => HeimdallMoveRefusal.NotAllowedForApp, + "not_reversible" => HeimdallMoveRefusal.NotReversible, + "proof_invalid" => HeimdallMoveRefusal.ProofInvalid, + "same_account" => HeimdallMoveRefusal.SameAccount, + "proof_stale" => HeimdallMoveRefusal.ProofStale, + "identity_not_proved" => HeimdallMoveRefusal.IdentityNotProved, + "already_moved" => HeimdallMoveRefusal.AlreadyMoved, + "provider_conflict" => HeimdallMoveRefusal.ProviderConflict, + "move_id_conflict" => HeimdallMoveRefusal.MoveIdConflict, + "identity_not_found" => HeimdallMoveRefusal.IdentityNotFound, + _ => HeimdallMoveRefusal.Unrecognized + }; + return code switch + { + HeimdallMoveRefusal.ProofInvalid or HeimdallMoveRefusal.ProofStale => new(code, Side: refusal?.Proof switch + { + "from" => HeimdallProofSide.From, + "to" => HeimdallProofSide.To, + _ => null + }), + HeimdallMoveRefusal.ProviderConflict => new(code, Provider: refusal?.Provider), + _ => new(code) + }; + } + + /// + /// A 200 body is a receipt only when it is whole and answers this request: its moveId (and reversesMoveId) equal the request's, + /// a forward move names a destination proof tier and a reversal does not. Anything else is not trusted as a move. + /// + private static async Task ReadReceiptAsync(HttpResponseMessage response, HeimdallMoveRequest request, CancellationToken cancellationToken) + { + using var document = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(cancellationToken), cancellationToken: cancellationToken); + var root = document.RootElement; + if (root.ValueKind != JsonValueKind.Object + || Text(root, "moveId") != request.MoveId + || Text(root, "reversesMoveId") != request.ReversesMoveId + || Text(root, "accountId") is not { Length: > 0 } account + || !root.TryGetProperty("movedIdentityIds", out var moved) + || moved.ValueKind != JsonValueKind.Array + || moved.GetArrayLength() is 0 or > MaxMovedIdentities + || !root.TryGetProperty("proof", out var proof) + || proof.ValueKind != JsonValueKind.Object + || Tier(proof, "from") is not { } from) + { + return null; + } + + var ids = new List(); + foreach (var item in moved.EnumerateArray()) + { + if (item.ValueKind != JsonValueKind.String || !Guid.TryParse(item.GetString(), out _)) + { + return null; + } + + ids.Add(item.GetString()!); + } + + HeimdallProofTier? to = null; + if (request.ReversesMoveId is null) + { + to = Tier(proof, "to"); + if (to is null) + { + return null; + } + } + else if (!proof.TryGetProperty("to", out var none) || none.ValueKind != JsonValueKind.Null) + { + return null; + } + + return new HeimdallMoveReceipt(request.MoveId, account, ids, from, to, request.ReversesMoveId); + } + + private static HeimdallProofTier? Tier(JsonElement proof, string name) => Text(proof, name) switch + { + "strong" => HeimdallProofTier.Strong, + "weak" => HeimdallProofTier.Weak, + _ => null + }; + + private static string? Text(JsonElement item, string name) => + item.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String ? value.GetString() : null; + + /// The contract's closed body, or ArgumentException before any I/O when the request breaks it. + private static string Serialize(HeimdallMoveRequest request) + { + if (request.MoveId is null || !MoveIdShape().IsMatch(request.MoveId)) + { + throw new ArgumentException("moveId must match ^[A-Za-z0-9_-]{16,64}$", nameof(request)); + } + + if (request.FromProofs is not { Count: >= 1 and <= 8 } || request.FromProofs.Any(string.IsNullOrWhiteSpace)) + { + throw new ArgumentException("fromProofs must hold 1 to 8 proofs", nameof(request)); + } + + if ((request.ToProof is null) == (request.ReversesMoveId is null)) + { + throw new ArgumentException("exactly one of toProof and reversesMoveId is required", nameof(request)); + } + + if (request.ToProof is not null && string.IsNullOrWhiteSpace(request.ToProof)) + { + throw new ArgumentException("toProof is empty", nameof(request)); + } + + if (request.ReversesMoveId is not null && !MoveIdShape().IsMatch(request.ReversesMoveId)) + { + throw new ArgumentException("reversesMoveId must match the moveId shape", nameof(request)); + } + + if (request.IdentityIds is not null + && (request.ToProof is null || request.IdentityIds.Count is < 1 or > 32 || request.IdentityIds.Any(string.IsNullOrWhiteSpace))) + { + throw new ArgumentException("identityIds are 1 to 32 and only with toProof", nameof(request)); + } + + var body = new Dictionary { ["moveId"] = request.MoveId, ["fromProofs"] = request.FromProofs }; + if (request.ToProof is not null) + { + body["toProof"] = request.ToProof; + } + + if (request.IdentityIds is not null) + { + body["identityIds"] = request.IdentityIds; + } + + if (request.ReversesMoveId is not null) + { + body["reversesMoveId"] = request.ReversesMoveId; + } + + return JsonSerializer.Serialize(body); + } +} diff --git a/src/Bifrost.Web/Features/Heimdall/HeimdallRefusal.cs b/src/Bifrost.Web/Features/Heimdall/HeimdallRefusal.cs index fea10aa..c6bd638 100644 --- a/src/Bifrost.Web/Features/Heimdall/HeimdallRefusal.cs +++ b/src/Bifrost.Web/Features/Heimdall/HeimdallRefusal.cs @@ -11,7 +11,14 @@ public static class HeimdallRefusal /// The error code in the first 4 KiB of a body when it is a short snake_case token, else null. A body that cannot be /// read or parsed yields null: a refusal never becomes a 500, and free text or tokens never reach a log or a page. /// - public static async Task CodeAsync(HttpResponseMessage response, CancellationToken cancellationToken) + public static async Task CodeAsync(HttpResponseMessage response, CancellationToken cancellationToken) => + (await ReadAsync(response, cancellationToken))?.Code; + + /// + /// The error code plus the two qualifiers a move refusal carries (the contract's `proof`, only "from" or "to", and `provider`, a + /// short snake_case token), each kept only when it is that safe shape. Nothing else of the body is read. Null when no code reads. + /// + public static async Task ReadAsync(HttpResponseMessage response, CancellationToken cancellationToken) { try { @@ -24,13 +31,10 @@ public static class HeimdallRefusal } using var body = JsonDocument.Parse(head.AsMemory(0, length)); - if (body.RootElement.ValueKind == JsonValueKind.Object - && body.RootElement.TryGetProperty("error", out var error) - && error.ValueKind == JsonValueKind.String - && error.GetString() is { Length: > 0 and <= 64 } code - && code.All(c => c is (>= 'a' and <= 'z') or (>= '0' and <= '9') or '_')) + if (body.RootElement.ValueKind == JsonValueKind.Object && Token(body.RootElement, "error", 64) is { } code) { - return code; + var proof = body.RootElement.TryGetProperty("proof", out var side) && side.ValueKind == JsonValueKind.String ? side.GetString() : null; + return new HeimdallRefusalBody(code, proof is "from" or "to" ? proof : null, Token(body.RootElement, "provider", 32)); } } catch (Exception) when (!cancellationToken.IsCancellationRequested) @@ -39,4 +43,16 @@ public static class HeimdallRefusal return null; } + + private static string? Token(JsonElement root, string name, int maxLength) => + root.TryGetProperty(name, out var value) + && value.ValueKind == JsonValueKind.String + && value.GetString() is { Length: > 0 } text + && text.Length <= maxLength + && text.All(c => c is (>= 'a' and <= 'z') or (>= '0' and <= '9') or '_') + ? text + : null; } + +/// A Heimdall refusal's safe fields: the code, the proof side ("from"/"to") and the provider, when present and well formed. +public sealed record HeimdallRefusalBody(string Code, string? Proof, string? Provider); diff --git a/src/Bifrost.Web/Program.cs b/src/Bifrost.Web/Program.cs index 48c0f74..2590f77 100644 --- a/src/Bifrost.Web/Program.cs +++ b/src/Bifrost.Web/Program.cs @@ -158,6 +158,13 @@ builder.Services.AddHttpClient((serviceProvider, client) => builder.Services.AddHttpClient((serviceProvider, client) => HeimdallMemberClient.ConfigureClient(client, serviceProvider.GetRequiredService>().Value)) .RedactLoggedHeaders(HeimdallAuthService.RedactsHeader); +// The mover route is an app-secret call like HeimdallAuthService's: same ConfigureClient, same header redaction. +builder.Services.AddHttpClient((serviceProvider, client) => + { + HeimdallAuthService.ConfigureClient(client, serviceProvider.GetRequiredService>().Value); + client.Timeout = HeimdallIdentityMoveClient.Timeout; + }) + .RedactLoggedHeaders(HeimdallAuthService.RedactsHeader); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton();