118 lines
2.9 KiB
C#
118 lines
2.9 KiB
C#
using Lunar.Exchange.Lapis.Data;
|
|
using Lunar.Exchange.Lapis.Models;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
var lapisConfiguration = new LapisOptions();
|
|
builder.Configuration.Bind("Lapis", lapisConfiguration);
|
|
|
|
builder.Services.AddDbContext<LapisContext>(
|
|
options => options.UseSqlite(
|
|
builder.Configuration.GetConnectionString("LapisContext")
|
|
)
|
|
);
|
|
|
|
builder.Services
|
|
.AddAntiforgery(options =>
|
|
{
|
|
options.HeaderName = "X-LAPIS-XSRF-TOKEN";
|
|
options.Cookie.Name = "__Host-X-LAPIS-XSRF-TOKEN";
|
|
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
|
|
options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always;
|
|
})
|
|
.AddIdentityCore<IdentityUser>(o =>
|
|
{
|
|
o.Stores.MaxLengthForKeys = 128;
|
|
})
|
|
.AddSignInManager()
|
|
.AddDefaultTokenProviders()
|
|
.AddEntityFrameworkStores<LapisContext>();
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("Public", policy =>
|
|
{
|
|
policy
|
|
.AllowAnyOrigin()
|
|
.WithMethods("GET")
|
|
.WithExposedHeaders();
|
|
});
|
|
});
|
|
|
|
builder.Services
|
|
.AddAuthentication("Identity.Application")
|
|
.AddCookie("Identity.Application", options =>
|
|
{
|
|
options.AccessDeniedPath = null;
|
|
options.LoginPath = null;
|
|
options.LogoutPath = null;
|
|
options.Events.OnRedirectToAccessDenied = redirectContext =>
|
|
{
|
|
redirectContext.Response.StatusCode = 403;
|
|
return Task.CompletedTask;
|
|
};
|
|
options.Events.OnRedirectToLogin = redirectContext =>
|
|
{
|
|
Console.WriteLine($"{redirectContext}");
|
|
redirectContext.Response.StatusCode = 401;
|
|
return Task.CompletedTask;
|
|
};
|
|
});
|
|
|
|
builder.Services
|
|
.AddControllers();
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
{
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
|
});
|
|
|
|
{
|
|
using var scope = app.Services.CreateScope();
|
|
using var context = scope.ServiceProvider.GetRequiredService<LapisContext>();
|
|
await context.Database.MigrateAsync();
|
|
}
|
|
|
|
app.Use(async (context, next) =>
|
|
{
|
|
if (context.Request.Method != "GET")
|
|
{
|
|
await next(context);
|
|
return;
|
|
}
|
|
|
|
var path = context.Request.Path.Value;
|
|
if (path is not null && path.EndsWith("/"))
|
|
path = path[..^1];
|
|
|
|
var redirect = path switch
|
|
{
|
|
"/forgot" or "/login" or "/logout" or "/register" => true,
|
|
"/authorize" or "/indieauth" => true,
|
|
_ => false
|
|
};
|
|
|
|
if (redirect)
|
|
context.Request.Path = "/";
|
|
|
|
await next(context);
|
|
});
|
|
|
|
app.UseDefaultFiles();
|
|
app.UseStaticFiles();
|
|
|
|
app.UseCors();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapDefaultControllerRoute();
|
|
|
|
app.Run();
|