checkpoint

This commit is contained in:
2026-10-28 14:22:27 +02:00
parent 33dad8ca5a
commit bd22fa86b7
11 changed files with 161 additions and 52 deletions
+41 -2
View File
@@ -1,4 +1,7 @@
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
@@ -12,6 +15,7 @@ namespace StatiKosev.Controllers;
public class PrivateMessageController(
MessageService messagesService,
IStringLocalizer<StringValues> strings,
IConfiguration config,
ILogger<PrivateMessageController> logger) : Controller
{
//form post from _MessageMe, redirects back to the page it came from
@@ -45,8 +49,43 @@ public class PrivateMessageController(
return LocalRedirect(back);
}
//JSON for the Inbox page (AJAX)
[HttpGet("")]
//password form on the /Inbox page; the admin has no account, only AdminPassword in appsettings.json
[HttpPost("login")]
[EnableRateLimiting("admin-login")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login([FromForm] string? password)
{
var adminPassword = config["AdminPassword"];
//empty AdminPassword = nobody gets in; hashing both sides gives a constant-time compare of equal-length values
var ok = !string.IsNullOrEmpty(adminPassword) && !string.IsNullOrEmpty(password) &&
CryptographicOperations.FixedTimeEquals(
SHA256.HashData(Encoding.UTF8.GetBytes(password)),
SHA256.HashData(Encoding.UTF8.GetBytes(adminPassword)));
if (!ok)
{
logger.LogWarning("Failed admin login from {Ip}", HttpContext.Connection.RemoteIpAddress);
TempData["Status"] = strings["AdminPasswordWrong"].Value;
TempData["StatusType"] = "error";
return LocalRedirect("/Inbox");
}
var admin = new ClaimsPrincipal(new ClaimsIdentity(
[new Claim(ClaimTypes.Name, "admin"), new Claim(ClaimTypes.Role, "Admin")], Program.AdminScheme));
await HttpContext.SignInAsync(Program.AdminScheme, admin);
return LocalRedirect("/Inbox");
}
[HttpPost("logout")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(Program.AdminScheme);
return LocalRedirect("/");
}
//JSON for the /Inbox page (AJAX)
[HttpGet("list")]
[Authorize(Policy = "AdminOnly")]
public async Task<IActionResult> Inbox(bool unreadOnly = false) =>
Ok(await messagesService.GetInboxAsync(unreadOnly));