feat: API-Key-Auth für Krea-Endpoints (Automation ohne User-Kontext)

- AuthMiddleware: X-Api-Key Header-Prüfung für /api/images/* Routen
- Neues Config-Feld KreaApiKey in appsettings.json
- Bei gültigem API-Key wird Request ohne Cookie-Auth durchgelassen
- Normaler User-Auth funktioniert weiterhin als Fallback
- ARCHITECTURE.md + ARCHITECTURE_QUICK.md aktualisiert
This commit is contained in:
Meleki committed 2026-03-06 15:00:11 +01:00
1 parent 1b9cc29421
commit 20f96f69d0
4 files changed
+30 -8

No files matched your search

+7 -2
View File
@@ -368,10 +368,14 @@ Alle Endpoints folgen dem **Minimal API**-Pattern mit Extension Methods (`MapXxx
Ausnahmen (kein Auth nötig):
- Statische Dateien (nicht `/api/`)
- `POST /api/auth/login`
- `GET /api/auth/autologin` (Token-basierter Autologin via Link)
- `POST /api/telegram/webhook/{sessionId}` (authentifiziert via Secret-Header)
- `GET /api/shared/{token}` (öffentlicher Zugriff auf geteilte Nachrichten)
Bei Erfolg werden `UserId` und `IsAdmin` in `HttpContext.Items` gesetzt.
**API-Key Auth** (ohne User-Kontext):
- `/api/images/*`-Routen akzeptieren einen `X-Api-Key`-Header. Wenn der Key mit `KreaApiKey` aus der Config übereinstimmt, wird der Request ohne User-Authentifizierung durchgelassen. Normaler Cookie-Auth funktioniert weiterhin als Fallback.
Bei Erfolg werden `UserId` und `IsAdmin` in `HttpContext.Items` gesetzt (bei API-Key-Auth sind diese nicht gesetzt).
### Models
@@ -1062,7 +1066,8 @@ Die Anwendung läuft als systemd Service (`gott.service`), Kestrel auf Port 1900
"MaxIterations": 100, // Max Tool-Loop Iterationen pro Chat
"PayloadLogging": false, // API-Payloads loggen (Debug)
"PayloadLoggingGzip": false, // Payload-Logs als .gz komprimieren
"KreaToken": "" // Krea API Token (optional)
"KreaToken": "", // Krea API Token (optional)
"KreaApiKey": "" // API-Key für Krea-Endpoints ohne User-Auth (optional)
}
```
+6 -4
View File
@@ -194,9 +194,10 @@ User-Input → POST /api/chat (SSE)
1. `POST /api/auth/login` → Token generiert, als `gott_token` HttpOnly Cookie gesetzt
2. Jeder `/api/`-Request → `AuthMiddleware` prüft Cookie → setzt `UserId` + `IsAdmin`
3. Ausnahmen: Login, Telegram-Webhook und `GET /api/shared/{token}` brauchen kein Auth
4. **Impersonate**: Admin kann via `POST /api/admin/users/{id}/impersonate` als anderer User einloggen
5. **DefaultPath**: Pro User ein Basis-Ordner (nur vom Admin setzbar), Session-Workspaces sind relativ dazu
3. Ausnahmen: Login, Telegram-Webhook, `GET /api/shared/{token}` und Autologin brauchen kein Auth
4. **Krea API-Key**: `/api/images/`-Routen akzeptieren `X-Api-Key`-Header (Config: `KreaApiKey`) für Automation ohne User-Kontext
5. **Impersonate**: Admin kann via `POST /api/admin/users/{id}/impersonate` als anderer User einloggen
6. **DefaultPath**: Pro User ein Basis-Ordner (nur vom Admin setzbar), Session-Workspaces sind relativ dazu
---
@@ -208,7 +209,8 @@ User-Input → POST /api/chat (SSE)
"MaxIterations": 100, // Max Tool-Loop Iterationen pro Chat
"PayloadLogging": false, // API-Payloads loggen (Debug)
"PayloadLoggingGzip": false, // Payload-Logs als .gz komprimieren
"KreaToken": "" // Krea API Token (optional)
"KreaToken": "", // Krea API Token (optional)
"KreaApiKey": "" // API-Key für Krea-Endpoints ohne User-Auth (optional)
}
```
+15 -1
View File
@@ -1,16 +1,19 @@
using System.Threading.Tasks;
using Gott.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace Gott.Middleware;
public class AuthMiddleware
{
private readonly RequestDelegate _next;
private readonly string _kreaApiKey;
public AuthMiddleware(RequestDelegate next)
public AuthMiddleware(RequestDelegate next, IConfiguration config)
{
_next = next;
_kreaApiKey = config["KreaApiKey"] ?? "";
}
public async Task InvokeAsync(HttpContext ctx, AuthService auth, DatabaseService db)
@@ -27,6 +30,17 @@ public class AuthMiddleware
return;
}
// Krea API-Key auth (for automation without user context)
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);
+2 -1
View File
@@ -10,5 +10,6 @@
"MaxIterations": 100,
"PayloadLogging": false,
"PayloadLoggingGzip": false,
"KreaToken": ""
"KreaToken": "",
"KreaApiKey": ""
}