2026-07-16 20:53:56 +00:00
|
|
|
using System.Text;
|
2026-07-17 06:57:00 +00:00
|
|
|
using Microsoft.AspNetCore.RateLimiting;
|
2026-07-16 20:53:56 +00:00
|
|
|
using Heimdall.Api.Filters;
|
|
|
|
|
using Heimdall.Application;
|
|
|
|
|
using Heimdall.Infrastructure;
|
|
|
|
|
using Heimdall.Infrastructure.Data;
|
|
|
|
|
using Heimdall.Infrastructure.Security;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2026-07-17 06:57:00 +00:00
|
|
|
// ── Rate Limiting (Polly/System.Threading.RateLimiting) ───────────
|
|
|
|
|
builder.Services.AddRateLimiter(options =>
|
|
|
|
|
{
|
|
|
|
|
options.RejectionStatusCode = 429;
|
|
|
|
|
|
|
|
|
|
options.AddFixedWindowLimiter("Auth", opt =>
|
|
|
|
|
{
|
|
|
|
|
opt.PermitLimit = 10;
|
|
|
|
|
opt.Window = TimeSpan.FromMinutes(1);
|
|
|
|
|
opt.QueueLimit = 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.AddFixedWindowLimiter("Api", opt =>
|
|
|
|
|
{
|
|
|
|
|
opt.PermitLimit = 200;
|
|
|
|
|
opt.Window = TimeSpan.FromMinutes(1);
|
|
|
|
|
opt.QueueLimit = 0;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
// ── Services ──────────────────────────────────────────────────────
|
|
|
|
|
|
2026-07-16 21:19:16 +00:00
|
|
|
if (!builder.Environment.IsEnvironment("Testing"))
|
2026-07-16 20:53:56 +00:00
|
|
|
{
|
2026-07-16 21:19:16 +00:00
|
|
|
builder.Services.AddControllers(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Filters.Add<ExceptionFilter>();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
}
|
2026-07-16 19:09:28 +00:00
|
|
|
|
|
|
|
|
builder.Services.AddOpenApi();
|
|
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
// ── Application layer ─────────────────────────────────────────────
|
|
|
|
|
builder.Services.AddApplication();
|
|
|
|
|
|
|
|
|
|
// ── Infrastructure layer ──────────────────────────────────────────
|
2026-07-16 21:19:16 +00:00
|
|
|
builder.Services.AddInfrastructure(builder.Configuration, builder.Environment.EnvironmentName);
|
2026-07-16 20:53:56 +00:00
|
|
|
|
|
|
|
|
// ── Authentication ────────────────────────────────────────────────
|
|
|
|
|
var jwtKey = builder.Configuration["Jwt:Key"]
|
2026-07-16 21:19:16 +00:00
|
|
|
?? Environment.GetEnvironmentVariable("JWT__KEY")
|
2026-07-16 20:53:56 +00:00
|
|
|
?? throw new InvalidOperationException("JWT:Key is not configured");
|
2026-07-16 21:19:16 +00:00
|
|
|
var jwtIssuer = builder.Configuration["Jwt:Issuer"] ?? Environment.GetEnvironmentVariable("JWT__ISSUER") ?? "heimdall";
|
|
|
|
|
var jwtAudience = builder.Configuration["Jwt:Audience"] ?? Environment.GetEnvironmentVariable("JWT__AUDIENCE") ?? "heimdall-api";
|
2026-07-16 20:53:56 +00:00
|
|
|
|
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
})
|
|
|
|
|
.AddJwtBearer("JwtBearer", options =>
|
|
|
|
|
{
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)),
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidIssuer = jwtIssuer,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidAudience = jwtAudience,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ClockSkew = TimeSpan.Zero
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>("ApiKey", null);
|
|
|
|
|
|
|
|
|
|
// ── Authorization policies ───────────────────────────────────────
|
|
|
|
|
builder.Services.AddAuthorization(options =>
|
|
|
|
|
{
|
|
|
|
|
var allPermissions = AppPermissions.RolePermissions.Values
|
|
|
|
|
.SelectMany(p => p).Distinct();
|
|
|
|
|
|
|
|
|
|
foreach (var permission in allPermissions)
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy(permission, policy =>
|
|
|
|
|
policy.RequireClaim("permission", permission));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ── CORS ──────────────────────────────────────────────────────────
|
|
|
|
|
var allowedOrigins = builder.Configuration["Cors:AllowedOrigins"]?.Split(',')
|
|
|
|
|
?? ["https://localhost:5001"];
|
|
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
2026-07-16 21:19:16 +00:00
|
|
|
var isWildcard = allowedOrigins.Length == 1 && allowedOrigins[0] == "*";
|
|
|
|
|
if (isWildcard)
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("ApiPolicy", policy =>
|
|
|
|
|
policy.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowAnyMethod());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("ApiPolicy", policy =>
|
|
|
|
|
policy.WithOrigins(allowedOrigins)
|
|
|
|
|
.AllowCredentials()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.WithMethods("GET", "POST", "PUT", "DELETE", "PATCH"));
|
|
|
|
|
}
|
2026-07-16 20:53:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ── Pipeline ──────────────────────────────────────────────────────
|
|
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2026-07-16 21:19:16 +00:00
|
|
|
// Apply migrations on startup (skip in Testing with InMemory)
|
|
|
|
|
if (!app.Environment.IsEnvironment("Testing"))
|
2026-07-16 20:53:56 +00:00
|
|
|
{
|
2026-07-16 21:19:16 +00:00
|
|
|
using (var scope = app.Services.CreateScope())
|
2026-07-16 20:53:56 +00:00
|
|
|
{
|
2026-07-16 21:19:16 +00:00
|
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
|
|
|
if (db.Database.GetPendingMigrations().Any())
|
|
|
|
|
{
|
|
|
|
|
db.Database.Migrate();
|
|
|
|
|
}
|
2026-07-16 20:53:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Seed roles
|
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var roleManager = scope.ServiceProvider
|
|
|
|
|
.GetRequiredService<Microsoft.AspNetCore.Identity.RoleManager<Heimdall.Domain.Entities.AppRole>>();
|
2026-07-16 21:19:16 +00:00
|
|
|
var userManager = scope.ServiceProvider
|
|
|
|
|
.GetRequiredService<Microsoft.AspNetCore.Identity.UserManager<Heimdall.Domain.Entities.AppUser>>();
|
2026-07-16 20:53:56 +00:00
|
|
|
|
|
|
|
|
foreach (var roleName in AppRoles.All)
|
|
|
|
|
{
|
|
|
|
|
if (!await roleManager.RoleExistsAsync(roleName))
|
|
|
|
|
{
|
|
|
|
|
await roleManager.CreateAsync(new Heimdall.Domain.Entities.AppRole(roleName));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-16 21:19:16 +00:00
|
|
|
|
|
|
|
|
// In Testing, seed an admin user for integration tests
|
|
|
|
|
if (app.Environment.IsEnvironment("Testing"))
|
|
|
|
|
{
|
|
|
|
|
var testAdmin = await userManager.FindByEmailAsync("testadmin@heimdall.test");
|
|
|
|
|
if (testAdmin is null)
|
|
|
|
|
{
|
|
|
|
|
testAdmin = new Heimdall.Domain.Entities.AppUser
|
|
|
|
|
{
|
|
|
|
|
UserName = "testadmin@heimdall.test",
|
|
|
|
|
Email = "testadmin@heimdall.test",
|
|
|
|
|
DisplayName = "Test Admin"
|
|
|
|
|
};
|
|
|
|
|
await userManager.CreateAsync(testAdmin, "TestAdmin123!");
|
|
|
|
|
await userManager.AddToRoleAsync(testAdmin, AppRoles.Admin);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-16 20:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-16 19:09:28 +00:00
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.MapOpenApi();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 20:53:56 +00:00
|
|
|
app.UseCors("ApiPolicy");
|
2026-07-16 19:09:28 +00:00
|
|
|
app.UseHttpsRedirection();
|
2026-07-17 06:57:00 +00:00
|
|
|
app.UseRateLimiter();
|
2026-07-16 20:53:56 +00:00
|
|
|
app.UseAuthentication();
|
2026-07-16 19:09:28 +00:00
|
|
|
app.UseAuthorization();
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
app.Run();
|