AuthMiddleware hat /api/telegram/webhook komplett von der Auth ausgeschlossen – unabhängig von der HTTP-Methode. Dadurch hatte DELETE /api/telegram/webhook kein IsAdmin gesetzt → 403. Fix: Nur POST auf diesen Pfad (= eingehender Telegram-Webhook) wird von der Auth ausgenommen.
46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
using System.Threading.Tasks;
|
|
using Gott.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Gott.Middleware;
|
|
|
|
public class AuthMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public AuthMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext ctx, AuthService auth, DatabaseService db)
|
|
{
|
|
var path = ctx.Request.Path.Value ?? "";
|
|
|
|
// Static files, login and telegram webhook don't require auth
|
|
var isTelegramWebhook = path == "/api/telegram/webhook" && ctx.Request.Method == "POST";
|
|
if (!path.StartsWith("/api/") || path == "/api/auth/login" || isTelegramWebhook)
|
|
{
|
|
await _next(ctx);
|
|
return;
|
|
}
|
|
|
|
var token = ctx.Request.Cookies["gott_token"];
|
|
var userId = auth.ValidateToken(token);
|
|
|
|
if (userId == null)
|
|
{
|
|
ctx.Response.StatusCode = 401;
|
|
await ctx.Response.WriteAsJsonAsync(new { error = "Not authenticated" });
|
|
return;
|
|
}
|
|
|
|
ctx.Items["UserId"] = userId;
|
|
|
|
var user = await db.GetUser(userId);
|
|
ctx.Items["IsAdmin"] = user?.IsAdmin ?? false;
|
|
|
|
await _next(ctx);
|
|
}
|
|
}
|