checkpoint
This commit is contained in:
+23
-6
@@ -15,12 +15,13 @@ namespace StatiKosev;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public const string AdminScheme = "Admin";
|
||||
|
||||
public static async Task Main(string[] arguments)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(arguments);
|
||||
var databaseConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||
var messageRateLimitSeconds = builder.Configuration.GetValue("MessageRateLimitSeconds", 10);
|
||||
var adminEmail = builder.Configuration["AdminEmail"]; // this account sees the Inbox
|
||||
|
||||
builder.Services.AddLocalization(o => o.ResourcesPath = "LanguagesValues");
|
||||
builder.Services.AddRazorPages();
|
||||
@@ -44,9 +45,18 @@ public class Program
|
||||
o.LoginPath = "/Login";
|
||||
o.AccessDeniedPath = "/";
|
||||
});
|
||||
//the admin has no account: the AdminPassword from appsettings.json, entered on /Inbox, signs this separate cookie in
|
||||
builder.Services.AddAuthentication().AddCookie(AdminScheme, o =>
|
||||
{
|
||||
o.Cookie.Name = ".StatiKosev.Admin";
|
||||
o.LoginPath = "/Inbox";
|
||||
o.AccessDeniedPath = "/Inbox";
|
||||
o.ExpireTimeSpan = TimeSpan.FromHours(8);
|
||||
o.SlidingExpiration = true;
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorizationBuilder()
|
||||
.AddPolicy("AdminOnly", p => p.RequireRole("Admin"));
|
||||
.AddPolicy("AdminOnly", p => p.AddAuthenticationSchemes(AdminScheme).RequireRole("Admin"));
|
||||
//rate limiting for messages
|
||||
builder.Services.AddRateLimiter(o =>
|
||||
{
|
||||
@@ -65,6 +75,15 @@ public class Program
|
||||
return ValueTask.CompletedTask;
|
||||
};
|
||||
|
||||
//password guessing on /Inbox: 5 tries per 15 minutes per IP
|
||||
o.AddPolicy("admin-login", ctx =>
|
||||
RateLimitPartition.GetFixedWindowLimiter("ip:" + ctx.Connection.RemoteIpAddress, _ => new FixedWindowRateLimiterOptions
|
||||
{
|
||||
PermitLimit = 5,
|
||||
Window = TimeSpan.FromMinutes(15),
|
||||
QueueLimit = 0
|
||||
}));
|
||||
|
||||
o.AddPolicy("contact", ctx => // ← the policy [EnableRateLimiting("contact")] uses
|
||||
{
|
||||
var userId = ctx.User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
@@ -87,7 +106,7 @@ public class Program
|
||||
Console.WriteLine($"Database connection string: {databaseConnectionString}");
|
||||
|
||||
var app = builder.Build();
|
||||
await DbSeeder.SeedAsync(app.Services, adminEmail);
|
||||
await DbSeeder.SeedAsync(app.Services);
|
||||
app.UseAntiforgery();
|
||||
app.UseRateLimiter();
|
||||
|
||||
@@ -120,8 +139,6 @@ public class Program
|
||||
if (!res.Succeeded)
|
||||
return Results.LocalRedirect(LoginPage("register-failed", back));
|
||||
await users.AddToRoleAsync(user, DbSeeder.UserRole);
|
||||
if (DbSeeder.IsAdminEmail(email, adminEmail))
|
||||
await users.AddToRoleAsync(user, DbSeeder.AdminRole);
|
||||
await signIn.SignInAsync(user, isPersistent: true);
|
||||
return Results.LocalRedirect(back);
|
||||
});
|
||||
@@ -166,4 +183,4 @@ public class Program
|
||||
app.Logger.LogInformation("Application started!");
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user