- Neuer ServerSettingsService: zentrale Settings in SQLite mit In-Memory-Cache - Auto-Migration: bestehende appsettings.Production.json wird einmalig in DB übernommen - Alle Services umgestellt: IConfiguration → ServerSettingsService (dynamisch, kein Neustart) - Port bleibt als einziges Setting in appsettings.Production.json (erfordert Neustart) - ServerSettingsEndpoint: liest/schreibt DB statt JSON-Datei, Port als Sonder-Setting - IConfiguration komplett entfernt aus allen Services (ChatService, ToolService, AnthropicService, OpenAiService, KreaService, TelegramService, KeyScannerService, GitHubEventScannerService) - AuthMiddleware: KreaApiKey dynamisch aus DB - appsettings.json bereinigt: nur noch Port und Logging - Dokumentation aktualisiert (ARCHITECTURE.md, ARCHITECTURE_QUICK.md, README.md)
65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
using System.Threading.Tasks;
|
|
using Gott.Services;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Gott.Middleware;
|
|
|
|
public class AuthMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
private readonly ServerSettingsService _settings;
|
|
|
|
public AuthMiddleware(RequestDelegate next, ServerSettingsService settings)
|
|
{
|
|
_next = next;
|
|
_settings = settings;
|
|
}
|
|
|
|
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.StartsWith("/api/telegram/webhook/") && ctx.Request.Method == "POST";
|
|
var isSharedGet = path.StartsWith("/api/shared/") && ctx.Request.Method == "GET";
|
|
var isAutologin = path == "/api/auth/autologin" && ctx.Request.Method == "GET";
|
|
var isImageFile = path.StartsWith("/api/images/file/") && ctx.Request.Method == "GET";
|
|
var isRemoteWs = path == "/api/remote";
|
|
var isRemoteScript = path == "/remote.ps1";
|
|
if (!path.StartsWith("/api/") || path == "/api/auth/login" || isTelegramWebhook || isSharedGet || isAutologin || isImageFile || isRemoteWs || isRemoteScript)
|
|
{
|
|
await _next(ctx);
|
|
return;
|
|
}
|
|
|
|
// Krea API-Key auth (for automation without user context)
|
|
var kreaApiKey = _settings.Get("KreaApiKey");
|
|
if (path.StartsWith("/api/images/") && !string.IsNullOrEmpty(kreaApiKey))
|
|
{
|
|
var apiKey = ctx.Request.Headers["X-Api-Key"].ToString();
|
|
if (!string.IsNullOrEmpty(apiKey) && apiKey == kreaApiKey)
|
|
{
|
|
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);
|
|
}
|
|
}
|