2026-06-08 21:04:51 +01:00
|
|
|
namespace Bifrost.Web.Configuration;
|
|
|
|
|
|
|
|
|
|
public sealed class HeimdallOptions
|
|
|
|
|
{
|
|
|
|
|
public const string SectionName = "Heimdall";
|
|
|
|
|
|
|
|
|
|
public string BaseUrl { get; init; } = "https://heimdall.gamecult.org";
|
|
|
|
|
|
|
|
|
|
public string AppSlug { get; init; } = "bifrost";
|
|
|
|
|
|
|
|
|
|
public string DiscordGuildId { get; init; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
public string[] DiscordAllowedRoleIds { get; init; } = [];
|
|
|
|
|
|
|
|
|
|
public string PatreonTierTitle { get; init; } = "Inner Sanctum";
|
|
|
|
|
|
2026-11-12 13:24:59 +00:00
|
|
|
/// <summary>Deprecated: rides in the process environment. Use <see cref="AppSecretFile"/>; the file wins when both are set.</summary>
|
|
|
|
|
public string AppSecret { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
/// <summary>Path of a file holding the app secret (systemd <c>LoadCredential</c> style: <c>%d/heimdall-app-secret</c>).</summary>
|
|
|
|
|
public string AppSecretFile { get; init; } = string.Empty;
|
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-06-09 17:16:27 +01:00
|
|
|
public string PatronSupportIntakeSecret { get; init; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
public bool EnablePatronSupportIntake { get; init; } = true;
|
|
|
|
|
|
2026-06-08 21:04:51 +01:00
|
|
|
public bool IsConfigured =>
|
|
|
|
|
!string.IsNullOrWhiteSpace(BaseUrl) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(AppSlug);
|
|
|
|
|
|
|
|
|
|
public bool IsDiscordConfigured =>
|
|
|
|
|
IsConfigured &&
|
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
|
|
|
!string.IsNullOrWhiteSpace(AppSecret) &&
|
2026-06-08 21:04:51 +01:00
|
|
|
!string.IsNullOrWhiteSpace(DiscordGuildId) &&
|
|
|
|
|
DiscordAllowedRoleIds.Length > 0;
|
|
|
|
|
|
|
|
|
|
public bool IsPatreonConfigured =>
|
|
|
|
|
IsConfigured &&
|
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
|
|
|
!string.IsNullOrWhiteSpace(AppSecret) &&
|
2026-06-08 21:04:51 +01:00
|
|
|
!string.IsNullOrWhiteSpace(PatreonTierTitle);
|
2026-06-09 17:16:27 +01:00
|
|
|
|
|
|
|
|
public bool IsPatronSupportIntakeConfigured =>
|
|
|
|
|
IsConfigured &&
|
|
|
|
|
EnablePatronSupportIntake &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(PatronSupportIntakeSecret);
|
2026-11-12 13:24:59 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves <see cref="AppSecret"/> once, when the options are built. A set <see cref="AppSecretFile"/> must be a
|
|
|
|
|
/// readable, non-empty file, or startup fails; the message names the option and path, never a secret.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void ResolveAppSecret(HeimdallOptions options, ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.AppSecret))
|
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Heimdall:AppSecret is deprecated: it rides in the process environment. Use Heimdall:AppSecretFile, which wins when both are set.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.AppSecretFile))
|
|
|
|
|
{
|
|
|
|
|
string secret;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
secret = File.ReadAllText(options.AppSecretFile).TrimEnd('\r', '\n');
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error) when (error is IOException or UnauthorizedAccessException or ArgumentException or NotSupportedException)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"Heimdall:AppSecretFile must name a readable file; '{options.AppSecretFile}' could not be read ({error.GetType().Name}).");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(secret))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"Heimdall:AppSecretFile '{options.AppSecretFile}' is empty.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.AppSecret = secret;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-08 21:04:51 +01:00
|
|
|
}
|