Files
gott/Middleware/AuthMiddleware.cs
T
Meleki 10da2bd964 feat: Remote-Verbindungen – Windows-Workstations per WebSocket steuern
- RemoteConnectionManager: Singleton-Service verwaltet WebSocket-Verbindungen
  mit Command-Execution (Send/Receive mit Timeout + SemaphoreSlim-Locking)
- RemoteEndpoint: WebSocket-Endpoint /api/remote (auth-frei), REST-APIs für
  Connections/Assign/Unassign, PowerShell-Script-Auslieferung mit dynamischer URL
- remote.ps1: PowerShell-Client für Windows – verbindet per WebSocket,
  sendet Hostname/User/OS, empfängt und führt Befehle aus, Heartbeat
- remote_execute Tool: Neues Agent-Tool, nur verfügbar wenn Session eine
  zugeordnete Remote-Verbindung hat, führt PowerShell-Befehle remote aus
- Frontend: Menüpunkt 'Remote-Verbindungen' im Chat-Dropdown, Modal zeigt
  alle aktiven WebSocket-Verbindungen (Hostname, User, OS, Verbindungszeit),
  Verbinden/Trennen-Buttons pro Verbindung und Session
- Auth: /api/remote und /remote.ps1 von Authentifizierung ausgenommen

Architektur: Windows-Client → WebSocket → GOTT-Server → Agent-Tool
Session-gebunden: Eine Verbindung wird einer Session zugeordnet
2026-03-11 07:04:24 +01:00

65 lines
2.2 KiB
C#

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, IConfiguration config)
{
_next = next;
_kreaApiKey = config["KreaApiKey"] ?? "";
}
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)
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);
}
}