Files
gott/Middleware/AuthMiddleware.cs
T
Meleki 0dd7c38751 feat: Telegram Bot Integration
- TelegramService: Non-Streaming Claude Chat mit Tool-Loop,
  Commands (/start, /link, /session, /new, /clear, /status, /unlink),
  Nachrichten-Splitting bei >4096 Zeichen, Typing-Indicator,
  History-Aufbau aus DB, Token-Usage-Tracking
- TelegramEndpoint: Webhook (POST, no-auth), Link-Code-Generierung
  (POST, auth), Webhook-Registrierung + Info (Admin)
- DatabaseService: telegram_links Tabelle mit Link-Code-Flow
  (6-stelliger Code, 5 Min Ablauf), GetUserSessions
- AuthMiddleware: /api/telegram/webhook von Auth ausgenommen
- Session-Verknüpfung via Link-Code (Web-UI generiert Code,
  Telegram /link CODE löst ein)
- TelegramLinkRecord Model
2026-03-03 06:30:05 +01:00

45 lines
1.1 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
if (!path.StartsWith("/api/") || path == "/api/auth/login" || path == "/api/telegram/webhook")
{
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);
}
}