Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
using System.Net;
|
2026-06-08 21:04:51 +01:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Security.Claims;
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text.Json;
|
2026-06-08 21:04:51 +01:00
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using Bifrost.Web.Configuration;
|
|
|
|
|
using Bifrost.Web.Data;
|
|
|
|
|
using Bifrost.Web.Domain;
|
|
|
|
|
using Bifrost.Web.Features.Membership;
|
|
|
|
|
using Bifrost.Web.Features.Shared;
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
2026-06-08 21:04:51 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
namespace Bifrost.Web.Features.Heimdall;
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Heimdall sign-in. Start issues an attempt that lives only in a Data Protection-protected cookie in the
|
|
|
|
|
/// browser that began it. The browser that Heimdall authenticated presents the completion code from its
|
|
|
|
|
/// URL fragment; Bifrost redeems it at Heimdall itself and signs in only when that browser holds the
|
|
|
|
|
/// attempt's cookie and the redeemed return target names the same attempt.
|
|
|
|
|
/// </summary>
|
2026-06-08 21:04:51 +01:00
|
|
|
public sealed class HeimdallAuthService(
|
|
|
|
|
HttpClient httpClient,
|
2026-10-11 02:54:13 +02:00
|
|
|
HeimdallTokenVerifier tokenVerifier,
|
2026-06-08 21:04:51 +01:00
|
|
|
BifrostDbContext dbContext,
|
2026-06-22 15:07:22 +01:00
|
|
|
BifrostIdentityService bifrostIdentityService,
|
2026-06-08 21:04:51 +01:00
|
|
|
IOptions<HeimdallOptions> heimdallOptions,
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
IDataProtectionProvider dataProtectionProvider,
|
2026-06-08 21:04:51 +01:00
|
|
|
TimeProvider timeProvider)
|
|
|
|
|
{
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
public const string ErrorRedirect = "/?auth=heimdall-error";
|
|
|
|
|
public const string AppSecretHeader = "x-heimdall-app-secret";
|
|
|
|
|
|
2026-06-08 21:04:51 +01:00
|
|
|
private static readonly HashSet<string> AllowedProviders = new(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
{
|
|
|
|
|
"discord",
|
2026-06-22 14:08:35 +01:00
|
|
|
"patreon",
|
|
|
|
|
"twitch"
|
2026-06-08 21:04:51 +01:00
|
|
|
};
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
private readonly IDataProtector _protector = dataProtectionProvider.CreateProtector("Bifrost.Heimdall.Attempt.v1");
|
|
|
|
|
|
|
|
|
|
/// <summary>Points the client at Heimdall and, when the app has a secret, identifies Bifrost as the app.</summary>
|
|
|
|
|
public static void ConfigureClient(HttpClient client, HeimdallOptions options)
|
|
|
|
|
{
|
|
|
|
|
client.BaseAddress = new Uri(options.BaseUrl.TrimEnd('/'));
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.AppSecret))
|
|
|
|
|
{
|
|
|
|
|
client.DefaultRequestHeaders.Add(AppSecretHeader, options.AppSecret);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-08 21:04:51 +01:00
|
|
|
public async Task<HeimdallAuthStartResult> StartAsync(
|
|
|
|
|
string provider,
|
|
|
|
|
string callbackBaseUrl,
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
LocalReturnPath returnPath,
|
2026-06-22 14:08:35 +01:00
|
|
|
bool requireMemberAccess,
|
2026-10-11 22:25:24 +02:00
|
|
|
IEnumerable<KeyValuePair<string, string>> requestCookies,
|
2026-06-08 21:04:51 +01:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (!AllowedProviders.Contains(provider))
|
|
|
|
|
{
|
2026-10-11 02:54:13 +02:00
|
|
|
throw new HeimdallAuthException("Unsupported Heimdall provider.");
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var options = heimdallOptions.Value;
|
|
|
|
|
if (!options.IsConfigured)
|
|
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall is not configured.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 14:08:35 +01:00
|
|
|
var entitlementPolicy = requireMemberAccess
|
|
|
|
|
? BuildMemberAccessPolicy(provider, options)
|
|
|
|
|
: null;
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
var nonce = WebEncoders.Base64UrlEncode(RandomNumberGenerator.GetBytes(32));
|
2026-06-08 21:04:51 +01:00
|
|
|
|
|
|
|
|
var request = new HeimdallStartRequest(
|
|
|
|
|
options.AppSlug,
|
|
|
|
|
"sign_in",
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
ToAttemptReturnUrl(callbackBaseUrl, nonce),
|
|
|
|
|
new HeimdallHandoff("browser_completion"),
|
2026-06-08 21:04:51 +01:00
|
|
|
entitlementPolicy);
|
|
|
|
|
|
|
|
|
|
var response = await httpClient.PostAsJsonAsync(
|
|
|
|
|
$"/v1/oauth/{provider}/start",
|
|
|
|
|
request,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
{
|
2026-10-11 02:54:13 +02:00
|
|
|
throw new HeimdallAuthException("Heimdall rejected the auth start.");
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var payload = await response.Content.ReadFromJsonAsync<HeimdallStartResponse>(cancellationToken);
|
|
|
|
|
if (payload is null || string.IsNullOrWhiteSpace(payload.AuthorizationUrl))
|
|
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall auth start returned no authorization URL.");
|
|
|
|
|
}
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
var attempt = new HeimdallAttempt(
|
|
|
|
|
nonce,
|
|
|
|
|
provider,
|
|
|
|
|
requireMemberAccess,
|
|
|
|
|
returnPath,
|
|
|
|
|
timeProvider.GetUtcNow() + HeimdallAttemptCookie.Lifetime);
|
2026-10-11 04:41:36 +02:00
|
|
|
return new HeimdallAuthStartResult(
|
2026-10-11 22:25:24 +02:00
|
|
|
new Uri(payload.AuthorizationUrl, UriKind.Absolute), ChooseSlot(requestCookies), Protect(attempt));
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-11 20:41:41 +02:00
|
|
|
/// <summary>
|
2026-10-11 22:25:24 +02:00
|
|
|
/// The slot the new attempt's cookie takes: the first slot that holds no live attempt, else the one whose
|
|
|
|
|
/// attempt expires first. Parallel starts carry the same request cookies, so they choose the same slot and
|
|
|
|
|
/// overwrite each other; the Cookie header holds at most <see cref="HeimdallAttemptCookie.SlotNames"/> attempt
|
|
|
|
|
/// cookies however many starts a browser makes.
|
2026-10-11 20:41:41 +02:00
|
|
|
/// </summary>
|
2026-10-11 22:25:24 +02:00
|
|
|
private string ChooseSlot(IEnumerable<KeyValuePair<string, string>> requestCookies)
|
2026-10-11 20:41:41 +02:00
|
|
|
{
|
2026-10-11 22:25:24 +02:00
|
|
|
var cookies = requestCookies.ToLookup(x => x.Key, x => x.Value, StringComparer.Ordinal);
|
|
|
|
|
string? oldest = null;
|
|
|
|
|
var oldestExpiry = DateTimeOffset.MaxValue;
|
|
|
|
|
foreach (var name in HeimdallAttemptCookie.SlotNames)
|
2026-10-11 20:41:41 +02:00
|
|
|
{
|
2026-10-11 22:25:24 +02:00
|
|
|
var live = Read(cookies[name].FirstOrDefault());
|
|
|
|
|
if (live is null)
|
2026-10-11 20:41:41 +02:00
|
|
|
{
|
2026-10-11 22:25:24 +02:00
|
|
|
return name;
|
2026-10-11 20:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
2026-10-11 22:25:24 +02:00
|
|
|
if (live.ExpiresAt < oldestExpiry)
|
2026-10-11 20:41:41 +02:00
|
|
|
{
|
2026-10-11 22:25:24 +02:00
|
|
|
oldest = name;
|
|
|
|
|
oldestExpiry = live.ExpiresAt;
|
2026-10-11 20:41:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 22:25:24 +02:00
|
|
|
return oldest!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The slot cookie that holds the attempt for this nonce: its name and value, or null when the browser holds
|
|
|
|
|
/// none (never started here, expired, or overwritten by a later start).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public (string Name, string Value)? FindAttemptCookie(IEnumerable<KeyValuePair<string, string>> requestCookies, string? nonce)
|
|
|
|
|
{
|
|
|
|
|
foreach (var (name, value) in requestCookies)
|
2026-10-11 20:41:41 +02:00
|
|
|
{
|
2026-10-11 22:25:24 +02:00
|
|
|
if (HeimdallAttemptCookie.SlotNames.Contains(name) && Unprotect(value, nonce) is not null)
|
2026-10-11 20:41:41 +02:00
|
|
|
{
|
2026-10-11 22:25:24 +02:00
|
|
|
return (name, value);
|
2026-10-11 20:41:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 22:25:24 +02:00
|
|
|
return null;
|
2026-10-11 20:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
public async Task<HeimdallCompletedSignIn> CompleteAsync(
|
|
|
|
|
string callbackBaseUrl,
|
|
|
|
|
string? nonce,
|
|
|
|
|
string? cookieValue,
|
|
|
|
|
string? code,
|
2026-10-11 02:54:13 +02:00
|
|
|
CancellationToken cancellationToken)
|
2026-06-08 21:04:51 +01:00
|
|
|
{
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
var attempt = Unprotect(cookieValue, nonce)
|
|
|
|
|
?? throw new HeimdallAuthException("Heimdall completion has no attempt for this browser.");
|
|
|
|
|
if (string.IsNullOrWhiteSpace(code))
|
2026-06-08 21:04:51 +01:00
|
|
|
{
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
throw new HeimdallAuthException("Heimdall completion carried no code.");
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
var redeemed = await RedeemAsync(code, cancellationToken);
|
|
|
|
|
if (!string.Equals(redeemed.ReturnTo, ToAttemptReturnUrl(callbackBaseUrl, attempt.Nonce), StringComparison.Ordinal))
|
2026-06-08 21:04:51 +01:00
|
|
|
{
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
throw new HeimdallAuthException("Heimdall completion belongs to another attempt.");
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(redeemed.AccessToken))
|
|
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall completion carried no access token.");
|
|
|
|
|
}
|
2026-06-08 21:04:51 +01:00
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
var identity = await tokenVerifier.VerifyAsync(redeemed.AccessToken, redeemed.Account?.Id, cancellationToken);
|
2026-10-11 02:54:13 +02:00
|
|
|
if (attempt.RequiresMemberAccess && !identity.HasMemberAccess)
|
2026-06-08 21:04:51 +01:00
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall did not grant Bifrost member access.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 02:54:13 +02:00
|
|
|
var userAccount = await UpsertUserAccountAsync(
|
|
|
|
|
identity,
|
|
|
|
|
attempt.Provider,
|
|
|
|
|
attempt.RequiresMemberAccess,
|
|
|
|
|
cancellationToken);
|
2026-06-08 21:04:51 +01:00
|
|
|
return new HeimdallCompletedSignIn(
|
2026-10-11 02:54:13 +02:00
|
|
|
bifrostIdentityService.BuildPrincipal(userAccount, attempt.Provider),
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
attempt.ReturnPath);
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
private async Task<HeimdallRedeemResponse> RedeemAsync(string code, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var response = await httpClient.PostAsJsonAsync(
|
|
|
|
|
$"/v1/apps/{Uri.EscapeDataString(heimdallOptions.Value.AppSlug)}/auth-completions/redeem",
|
|
|
|
|
new HeimdallRedeemRequest(code),
|
|
|
|
|
cancellationToken);
|
|
|
|
|
if (response.StatusCode != HttpStatusCode.Created)
|
|
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall did not redeem the completion code.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await response.Content.ReadFromJsonAsync<HeimdallRedeemResponse>(cancellationToken)
|
|
|
|
|
?? throw new HeimdallAuthException("Heimdall redeem returned no body.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error) when (error is not HeimdallAuthException && !cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall redeem could not be read.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string Protect(HeimdallAttempt attempt) =>
|
|
|
|
|
_protector.Protect(JsonSerializer.Serialize(new AttemptWire(
|
|
|
|
|
attempt.Nonce, attempt.Provider, attempt.RequiresMemberAccess, attempt.ReturnPath.Value, attempt.ExpiresAt)));
|
|
|
|
|
|
2026-10-11 23:02:48 +02:00
|
|
|
/// <summary>The live attempt a cookie value protects when it belongs to this nonce, else null.</summary>
|
2026-10-11 22:25:24 +02:00
|
|
|
private HeimdallAttempt? Unprotect(string? cookieValue, string? nonce) =>
|
2026-10-11 23:02:48 +02:00
|
|
|
Read(cookieValue) is { } attempt && string.Equals(attempt.Nonce, nonce, StringComparison.Ordinal) ? attempt : null;
|
2026-10-11 22:25:24 +02:00
|
|
|
|
2026-10-11 23:02:48 +02:00
|
|
|
/// <summary>The live attempt a cookie value protects, else null.</summary>
|
|
|
|
|
private HeimdallAttempt? Read(string? cookieValue)
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
{
|
2026-10-11 22:25:24 +02:00
|
|
|
if (string.IsNullOrEmpty(cookieValue))
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var wire = JsonSerializer.Deserialize<AttemptWire>(_protector.Unprotect(cookieValue));
|
2026-10-11 23:02:48 +02:00
|
|
|
if (wire is null || wire.ExpiresAt <= timeProvider.GetUtcNow())
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HeimdallAttempt(
|
|
|
|
|
wire.Nonce, wire.Provider, wire.RequiresMemberAccess, LocalReturnPath.Parse(wire.ReturnPath), wire.ExpiresAt);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error) when (error is CryptographicException or JsonException or FormatException)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed record AttemptWire(
|
|
|
|
|
string Nonce,
|
|
|
|
|
string Provider,
|
|
|
|
|
bool RequiresMemberAccess,
|
|
|
|
|
string ReturnPath,
|
|
|
|
|
DateTimeOffset ExpiresAt);
|
|
|
|
|
|
2026-06-08 21:04:51 +01:00
|
|
|
private async Task<UserAccount> UpsertUserAccountAsync(
|
2026-10-11 02:54:13 +02:00
|
|
|
HeimdallVerifiedIdentity identity,
|
|
|
|
|
string provider,
|
2026-06-22 14:08:35 +01:00
|
|
|
bool grantMemberAccess,
|
2026-06-08 21:04:51 +01:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var now = timeProvider.GetUtcNow();
|
2026-10-11 02:54:13 +02:00
|
|
|
var accountId = identity.AccountId;
|
|
|
|
|
var displayName = identity.DisplayName ?? accountId;
|
2026-06-08 21:04:51 +01:00
|
|
|
|
|
|
|
|
var userAccount = await dbContext.UserAccounts
|
|
|
|
|
.Include(x => x.MemberProfile)
|
|
|
|
|
.Include(x => x.Membership!)
|
|
|
|
|
.ThenInclude(x => x.RoleAssignments)
|
|
|
|
|
.Include(x => x.Membership!)
|
|
|
|
|
.ThenInclude(x => x.TierSnapshots)
|
|
|
|
|
.SingleOrDefaultAsync(x => x.HeimdallAccountId == accountId, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (userAccount is null)
|
|
|
|
|
{
|
|
|
|
|
userAccount = new UserAccount
|
|
|
|
|
{
|
|
|
|
|
HeimdallAccountId = accountId,
|
|
|
|
|
DisplayName = displayName,
|
|
|
|
|
CreatedAtUtc = now,
|
|
|
|
|
LastSeenAtUtc = now,
|
|
|
|
|
MemberProfile = new MemberProfile
|
|
|
|
|
{
|
|
|
|
|
Nickname = displayName,
|
2026-10-11 02:54:13 +02:00
|
|
|
Headline = $"Verified through Heimdall {provider}",
|
2026-06-08 21:04:51 +01:00
|
|
|
UpdatedAtUtc = now
|
|
|
|
|
},
|
|
|
|
|
Membership = new Bifrost.Web.Domain.Membership
|
|
|
|
|
{
|
2026-06-22 14:08:35 +01:00
|
|
|
Status = grantMemberAccess ? MembershipStatus.Active : MembershipStatus.Authenticated,
|
2026-06-08 21:04:51 +01:00
|
|
|
CreatedAtUtc = now,
|
2026-06-22 14:08:35 +01:00
|
|
|
ApprovedAtUtc = grantMemberAccess ? now : null,
|
|
|
|
|
Notes = grantMemberAccess
|
|
|
|
|
? "Activated by Heimdall member access claim"
|
2026-10-11 02:54:13 +02:00
|
|
|
: $"Identity verified through Heimdall {provider}"
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
dbContext.UserAccounts.Add(userAccount);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userAccount.DisplayName = displayName;
|
|
|
|
|
userAccount.LastSeenAtUtc = now;
|
|
|
|
|
userAccount.MemberProfile ??= new MemberProfile
|
|
|
|
|
{
|
|
|
|
|
UserAccountId = userAccount.Id,
|
|
|
|
|
Nickname = displayName,
|
2026-10-11 02:54:13 +02:00
|
|
|
Headline = $"Verified through Heimdall {provider}",
|
2026-06-08 21:04:51 +01:00
|
|
|
UpdatedAtUtc = now
|
|
|
|
|
};
|
|
|
|
|
userAccount.Membership ??= new Bifrost.Web.Domain.Membership
|
|
|
|
|
{
|
|
|
|
|
UserAccountId = userAccount.Id,
|
|
|
|
|
CreatedAtUtc = now
|
|
|
|
|
};
|
2026-11-11 08:38:09 +00:00
|
|
|
// A suspension is an admin's decision: the claim neither lifts it nor, absent, imposes it.
|
|
|
|
|
if (grantMemberAccess && userAccount.Membership.Status != MembershipStatus.Suspended)
|
2026-06-22 14:08:35 +01:00
|
|
|
{
|
|
|
|
|
userAccount.Membership.Status = MembershipStatus.Active;
|
|
|
|
|
userAccount.Membership.ApprovedAtUtc ??= now;
|
|
|
|
|
}
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userAccount.MemberProfile!.Nickname = displayName;
|
|
|
|
|
userAccount.MemberProfile.UpdatedAtUtc = now;
|
2026-06-22 15:07:22 +01:00
|
|
|
await bifrostIdentityService.EnsureIdentityAsync(
|
|
|
|
|
userAccount,
|
|
|
|
|
displayName,
|
|
|
|
|
cancellationToken);
|
2026-11-11 08:38:09 +00:00
|
|
|
if (grantMemberAccess && userAccount.Membership!.Status == MembershipStatus.Active)
|
2026-06-22 14:08:35 +01:00
|
|
|
{
|
|
|
|
|
ApplicationBootstrapper.EnsureRole(
|
|
|
|
|
userAccount.Membership!,
|
|
|
|
|
MemberRole.StandardMember,
|
|
|
|
|
null,
|
|
|
|
|
now,
|
|
|
|
|
"Heimdall member access");
|
|
|
|
|
}
|
2026-06-08 21:04:51 +01:00
|
|
|
|
|
|
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
|
return userAccount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static HeimdallEntitlementPolicy BuildDiscordPolicy(HeimdallOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (!options.IsDiscordConfigured)
|
|
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall Discord access is not configured.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HeimdallEntitlementPolicy(
|
|
|
|
|
"discord_role_access",
|
|
|
|
|
options.DiscordGuildId,
|
|
|
|
|
options.DiscordAllowedRoleIds,
|
|
|
|
|
null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static HeimdallEntitlementPolicy BuildPatreonPolicy(HeimdallOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (!options.IsPatreonConfigured)
|
|
|
|
|
{
|
|
|
|
|
throw new HeimdallAuthException("Heimdall Patreon access is not configured.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HeimdallEntitlementPolicy(
|
|
|
|
|
"patreon_membership_access",
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
options.PatreonTierTitle);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 14:08:35 +01:00
|
|
|
private static HeimdallEntitlementPolicy BuildMemberAccessPolicy(string provider, HeimdallOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (provider.Equals("discord", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return BuildDiscordPolicy(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (provider.Equals("patreon", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return BuildPatreonPolicy(options);
|
|
|
|
|
}
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
throw new HeimdallAuthException("Heimdall provider does not support Bifrost member access.");
|
2026-06-22 14:08:35 +01:00
|
|
|
}
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
private static string ToAttemptReturnUrl(string callbackBaseUrl, string nonce)
|
2026-06-08 21:04:51 +01:00
|
|
|
{
|
|
|
|
|
var baseUri = new Uri(callbackBaseUrl.TrimEnd('/') + "/", UriKind.Absolute);
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
return $"{new Uri(baseUri, "auth/heimdall/complete")}?attempt={Uri.EscapeDataString(nonce)}";
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class HeimdallAuthException(string message) : InvalidOperationException(message);
|
|
|
|
|
|
2026-10-11 04:41:36 +02:00
|
|
|
public sealed record HeimdallAuthStartResult(Uri AuthorizationUrl, string CookieName, string CookieValue);
|
2026-10-11 02:54:13 +02:00
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
public sealed record HeimdallCompletedSignIn(ClaimsPrincipal Principal, LocalReturnPath ReturnPath);
|
2026-06-22 14:08:35 +01:00
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
/// <summary>An attempt Start issued. It exists only inside its protected cookie.</summary>
|
|
|
|
|
public sealed record HeimdallAttempt(
|
|
|
|
|
string Nonce,
|
2026-10-11 02:54:13 +02:00
|
|
|
string Provider,
|
|
|
|
|
bool RequiresMemberAccess,
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
LocalReturnPath ReturnPath,
|
|
|
|
|
DateTimeOffset ExpiresAt);
|
2026-06-08 21:04:51 +01:00
|
|
|
|
|
|
|
|
public sealed record HeimdallStartRequest(
|
|
|
|
|
[property: JsonPropertyName("appSlug")] string AppSlug,
|
|
|
|
|
[property: JsonPropertyName("mode")] string Mode,
|
|
|
|
|
[property: JsonPropertyName("returnTo")] string ReturnTo,
|
|
|
|
|
[property: JsonPropertyName("handoff")] HeimdallHandoff Handoff,
|
2026-06-22 14:08:35 +01:00
|
|
|
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
|
|
|
[property: JsonPropertyName("entitlementPolicy")] HeimdallEntitlementPolicy? EntitlementPolicy);
|
2026-06-08 21:04:51 +01:00
|
|
|
|
|
|
|
|
public sealed record HeimdallHandoff(
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
[property: JsonPropertyName("kind")] string Kind);
|
2026-06-08 21:04:51 +01:00
|
|
|
|
|
|
|
|
public sealed record HeimdallEntitlementPolicy(
|
|
|
|
|
[property: JsonPropertyName("kind")] string Kind,
|
|
|
|
|
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
|
|
|
[property: JsonPropertyName("guildId")] string? GuildId,
|
|
|
|
|
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
|
|
|
[property: JsonPropertyName("allowedRoleIds")] string[]? AllowedRoleIds,
|
|
|
|
|
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
|
|
|
[property: JsonPropertyName("requiredTierTitle")] string? RequiredTierTitle);
|
|
|
|
|
|
|
|
|
|
public sealed record HeimdallStartResponse(
|
|
|
|
|
[property: JsonPropertyName("authorizationUrl")] string AuthorizationUrl);
|
|
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
public sealed record HeimdallRedeemRequest(
|
|
|
|
|
[property: JsonPropertyName("completionCode")] string CompletionCode);
|
|
|
|
|
|
|
|
|
|
public sealed record HeimdallRedeemResponse(
|
|
|
|
|
[property: JsonPropertyName("returnTo")] string? ReturnTo,
|
2026-10-11 02:54:13 +02:00
|
|
|
[property: JsonPropertyName("accessToken")] string? AccessToken,
|
|
|
|
|
[property: JsonPropertyName("account")] HeimdallAccount? Account);
|
2026-06-08 21:04:51 +01:00
|
|
|
|
|
|
|
|
public sealed record HeimdallAccount(
|
2026-10-11 02:54:13 +02:00
|
|
|
[property: JsonPropertyName("id")] string Id);
|
2026-06-08 21:04:51 +01:00
|
|
|
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
/// <summary>
|
2026-10-11 22:25:24 +02:00
|
|
|
/// The attempt cookies: a ring of <see cref="SlotNames"/> fixed names, so the Cookie header a browser sends to
|
2026-11-07 18:42:43 +00:00
|
|
|
/// every path is bounded by construction (the __Host- prefix forces Path=/; a worst-case attempt is about 2.4 KB, nginx's line limit is 8 KB).
|
2026-10-11 22:25:24 +02:00
|
|
|
/// A start takes a free slot or overwrites the one expiring first; an overwritten attempt finds no cookie and
|
|
|
|
|
/// is refused. The attempt's nonce lives inside the protected value, never in the cookie name.
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
/// </summary>
|
|
|
|
|
public static class HeimdallAttemptCookie
|
2026-10-11 02:54:13 +02:00
|
|
|
{
|
2026-11-07 18:42:43 +00:00
|
|
|
public const string Prefix = BifrostCookies.HeimdallAttemptPrefix;
|
2026-10-11 22:25:24 +02:00
|
|
|
public const int SlotCount = 2;
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
|
|
|
|
|
public static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(10);
|
|
|
|
|
|
2026-10-11 22:25:24 +02:00
|
|
|
public static readonly IReadOnlyList<string> SlotNames =
|
|
|
|
|
Enumerable.Range(0, SlotCount).Select(x => Prefix + x).ToArray();
|
2026-10-11 02:54:13 +02:00
|
|
|
|
|
|
|
|
public static CookieOptions Options() => new()
|
|
|
|
|
{
|
|
|
|
|
HttpOnly = true,
|
|
|
|
|
Secure = true,
|
|
|
|
|
SameSite = SameSiteMode.Lax,
|
2026-11-07 18:42:43 +00:00
|
|
|
Path = BifrostCookies.Path,
|
Heimdall sign-in: the attempt is a cookie, and a code the browser presents is redeemed at Heimdall
Start puts a per-attempt nonce in the returnTo it sends Heimdall and keeps the attempt (provider,
member flag, local return path, expiry) in a Data Protection cookie named for that nonce. Heimdall's
completion page sends the browser to /auth/heimdall/complete with a code in the URL fragment; the
page posts it back, Bifrost redeems it at Heimdall, requires the redeemed returnTo to name the
cookie's nonce, verifies the access token for the redeemed account, and signs that browser in.
Deleted: the callback route, the wait route, the server-side attempt store, the pending state and
the backend_callback handoff. Added: LocalReturnPath for return targets, Heimdall:AppSecret sent as
x-heimdall-app-secret (member starts are refused without it), a short timeout on the key fetch, and
a key lookup that does not hold the fetch gate for cached keys. Refusals at the completion route all
answer one redirect and log a fixed reason.
Tests are rewritten against a fake Heimdall with start and redeem routes. Not compile-checked by this
commit alone; later commits record the verification.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 04:40:47 +02:00
|
|
|
MaxAge = Lifetime
|
2026-10-11 02:54:13 +02:00
|
|
|
};
|
2026-10-11 08:58:19 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
2026-11-09 11:24:02 +00:00
|
|
|
/// The options a deletion carries: the attempt cookie's Path, which a cookie is matched on, and Secure, without
|
|
|
|
|
/// which a browser ignores a Set-Cookie for a __Host- name. It carries no Max-Age, which would take precedence
|
|
|
|
|
/// over the expiry Delete writes and leave the emptied cookie alive.
|
2026-10-11 08:58:19 +02:00
|
|
|
/// </summary>
|
2026-11-09 11:24:02 +00:00
|
|
|
public static CookieOptions DeleteOptions() => new() { Path = BifrostCookies.Path, Secure = true };
|
2026-10-11 02:54:13 +02:00
|
|
|
}
|