2026-07-16 21:19:16 +00:00
|
|
|
using System.Security.Claims;
|
2026-07-16 19:09:28 +00:00
|
|
|
using Heimdall.Web.Components;
|
2026-07-16 21:19:16 +00:00
|
|
|
using Heimdall.Web.Services;
|
2026-07-18 20:59:38 +00:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2026-07-16 21:19:16 +00:00
|
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
|
using MudBlazor.Services;
|
2026-07-16 19:09:28 +00:00
|
|
|
|
2026-07-18 20:31:37 +00:00
|
|
|
// ── Sentry (GlitchTip) — DSN from env var, with hardcoded fallback ─
|
|
|
|
|
var sentryDsn = Environment.GetEnvironmentVariable("SENTRY_DSN")
|
|
|
|
|
?? "https://db94b9fc2f36425f9eb4b9f6f347dc73@glitchtip.tordais.cc/6";
|
2026-07-18 19:41:57 +00:00
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2026-07-18 19:41:57 +00:00
|
|
|
// ── Sentry ────────────────────────────────────────────────────────
|
|
|
|
|
builder.WebHost.UseSentry(options =>
|
|
|
|
|
{
|
2026-07-18 20:31:37 +00:00
|
|
|
options.Dsn = sentryDsn;
|
2026-07-18 19:41:57 +00:00
|
|
|
options.MinimumBreadcrumbLevel = LogLevel.Debug;
|
|
|
|
|
options.MinimumEventLevel = LogLevel.Error;
|
|
|
|
|
options.TracesSampleRate = 0.1;
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
builder.Services.AddRazorComponents()
|
|
|
|
|
.AddInteractiveServerComponents();
|
|
|
|
|
|
2026-07-18 20:59:38 +00:00
|
|
|
// ── Authentication (Microsoft Identity pattern: cookie auth) ──────
|
|
|
|
|
// The app uses JWT auth via the Heimdall API at the application level.
|
|
|
|
|
// At the ASP.NET Core middleware level, we use cookie authentication
|
|
|
|
|
// to persist the user session across requests.
|
|
|
|
|
// The cookie is created after successful API login (see /_auth/signin).
|
|
|
|
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
|
|
|
|
{
|
|
|
|
|
options.LoginPath = "/login";
|
|
|
|
|
options.LogoutPath = "/_auth/signout";
|
|
|
|
|
options.AccessDeniedPath = "/not-found";
|
|
|
|
|
options.Cookie.Name = ".Heimdall.Auth";
|
|
|
|
|
options.Cookie.HttpOnly = true;
|
|
|
|
|
options.Cookie.SameSite = SameSiteMode.Lax;
|
|
|
|
|
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
options.ExpireTimeSpan = TimeSpan.FromDays(7);
|
|
|
|
|
options.SlidingExpiration = true;
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 21:19:16 +00:00
|
|
|
builder.Services.AddAuthorizationCore();
|
2026-07-18 18:58:20 +00:00
|
|
|
builder.Services.AddCascadingAuthenticationState();
|
2026-07-16 21:19:16 +00:00
|
|
|
|
2026-07-18 20:59:38 +00:00
|
|
|
// Blazor auth state provider (reads JWT from ProtectedLocalStorage)
|
2026-07-18 21:08:08 +00:00
|
|
|
// Register the concrete type so @inject HeimdallAuthStateProvider works in pages,
|
|
|
|
|
// then make the base AuthenticationStateProvider resolve to the same instance.
|
|
|
|
|
builder.Services.AddScoped<HeimdallAuthStateProvider>();
|
|
|
|
|
builder.Services.AddScoped<AuthenticationStateProvider>(
|
|
|
|
|
sp => sp.GetRequiredService<HeimdallAuthStateProvider>());
|
2026-07-18 20:59:38 +00:00
|
|
|
|
|
|
|
|
// ── MudBlazor ─────────────────────────────────────────────────────
|
2026-07-16 21:19:16 +00:00
|
|
|
builder.Services.AddMudServices(config =>
|
|
|
|
|
{
|
|
|
|
|
config.SnackbarConfiguration.PositionClass = "mud-alert-center-top";
|
|
|
|
|
config.SnackbarConfiguration.PreventDuplicates = true;
|
|
|
|
|
config.SnackbarConfiguration.ShowCloseIcon = true;
|
|
|
|
|
config.SnackbarConfiguration.VisibleStateDuration = 4000;
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-18 20:59:38 +00:00
|
|
|
// ── HTTP client for Heimdall API ──────────────────────────────────
|
2026-07-16 21:19:16 +00:00
|
|
|
builder.Services.AddScoped<HeimdallApiClient>();
|
2026-07-17 13:22:02 +00:00
|
|
|
var apiBaseUrl = builder.Configuration.GetValue<string>("ApiBaseUrl")
|
|
|
|
|
?? Environment.GetEnvironmentVariable("API_BASE_URL")
|
|
|
|
|
?? "http://localhost:5000";
|
2026-07-16 21:19:16 +00:00
|
|
|
builder.Services.AddHttpClient("HeimdallApi", client =>
|
|
|
|
|
{
|
2026-07-17 13:22:02 +00:00
|
|
|
client.BaseAddress = new Uri(apiBaseUrl.TrimEnd('/') + "/");
|
2026-07-16 21:19:16 +00:00
|
|
|
client.Timeout = TimeSpan.FromSeconds(30);
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-18 20:59:38 +00:00
|
|
|
// Auth state (shared between services and components)
|
2026-07-16 21:19:16 +00:00
|
|
|
builder.Services.AddScoped<AuthState>();
|
|
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2026-07-18 20:59:38 +00:00
|
|
|
// ── Middleware pipeline ────────────────────────────────────────────
|
2026-07-16 19:09:28 +00:00
|
|
|
if (!app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
|
|
|
app.UseHsts();
|
|
|
|
|
}
|
2026-07-16 21:19:16 +00:00
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
app.UseAntiforgery();
|
|
|
|
|
app.MapStaticAssets();
|
2026-07-18 20:59:38 +00:00
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
app.MapRazorComponents<App>()
|
|
|
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
|
|
2026-07-18 20:59:38 +00:00
|
|
|
// ── Cookie auth endpoints (called from Blazor via JS fetch) ───────
|
|
|
|
|
// /_auth/signin — Creates an authentication cookie after successful API login
|
|
|
|
|
// /_auth/signout — Clears the authentication cookie on logout
|
|
|
|
|
|
|
|
|
|
app.MapPost("/_auth/signin", async (HttpContext httpContext, SignInRequest request) =>
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(request.Id) || string.IsNullOrEmpty(request.Email))
|
|
|
|
|
return Results.BadRequest(new { error = "Id and Email are required" });
|
|
|
|
|
|
|
|
|
|
var claims = new List<Claim>
|
|
|
|
|
{
|
|
|
|
|
new(ClaimTypes.NameIdentifier, request.Id),
|
|
|
|
|
new(ClaimTypes.Email, request.Email),
|
|
|
|
|
new(ClaimTypes.Name, request.DisplayName ?? request.Email),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (request.Roles is not null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var role in request.Roles)
|
|
|
|
|
claims.Add(new Claim(ClaimTypes.Role, role));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
var principal = new ClaimsPrincipal(identity);
|
|
|
|
|
|
|
|
|
|
await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
|
|
|
|
|
return Results.Ok();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.MapPost("/_auth/signout", async (HttpContext httpContext) =>
|
|
|
|
|
{
|
|
|
|
|
await httpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
return Results.Ok();
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
app.Run();
|
2026-07-18 20:59:38 +00:00
|
|
|
|
|
|
|
|
// ─── DTO for the cookie sign-in request ───────────────────────────
|
|
|
|
|
|
|
|
|
|
internal sealed record SignInRequest(
|
|
|
|
|
string Id,
|
|
|
|
|
string Email,
|
|
|
|
|
string? DisplayName,
|
|
|
|
|
IList<string>? Roles);
|