feat(auth): authentication module with multi-tenant isolation (RFC-001)

Introduce an always-on auth layer with auto-created admin on first boot,
multi-tenant isolation for threads/stores, and a full setup/login flow.

Backend
- JWT access tokens with `ver` field for stale-token rejection; bump on
  password/email change
- Password hashing, HttpOnly+Secure cookies (Secure derived from request
  scheme at runtime)
- CSRF middleware covering both REST and LangGraph routes
- IP-based login rate limiting (5 attempts / 5-min lockout) with bounded
  dict growth and X-Forwarded-For bypass fix
- Multi-worker-safe admin auto-creation (single DB write, WAL once)
- needs_setup + token_version on User model; SQLite schema migration
- Thread/store isolation by owner; orphan thread migration on first admin
  registration
- thread_id validated as UUID to prevent log injection
- CLI tool to reset admin password
- Decorator-based authz module extracted from auth core

Frontend
- Login and setup pages with SSR guard for needs_setup flow
- Account settings page (change password / email)
- AuthProvider + route guards; skips redirect when no users registered
- i18n (en-US / zh-CN) for auth surfaces
- Typed auth API client; parseAuthError unwraps FastAPI detail envelope

Infra & tooling
- Unified `serve.sh` with gateway mode + auto dep install
- Public PyPI uv.toml pin for CI compatibility
- Regenerated uv.lock with public index

Tests
- HTTP vs HTTPS cookie security tests
- Auth middleware, rate limiter, CSRF, setup flow coverage
This commit is contained in:
greatmengqi
2026-04-08 00:31:43 +08:00
parent 636053fb6d
commit 27b66d6753
214 changed files with 18830 additions and 1065 deletions
+45 -13
View File
@@ -10,12 +10,24 @@ function getInternalServiceURL(envKey, fallbackURL) {
? configured.replace(/\/+$/, "")
: fallbackURL;
}
import nextra from "nextra";
const withNextra = nextra({});
/** @type {import("next").NextConfig} */
const config = {
i18n: {
locales: ["en", "zh"],
defaultLocale: "en",
},
devIndicators: false,
allowedDevOrigins: process.env.NEXT_DEV_ALLOWED_ORIGINS
? process.env.NEXT_DEV_ALLOWED_ORIGINS.split(",")
.map((s) => s.trim())
.filter(Boolean)
: [],
async rewrites() {
const rewrites = [];
const beforeFiles = [];
const langgraphURL = getInternalServiceURL(
"DEER_FLOW_INTERNAL_LANGGRAPH_BASE_URL",
"http://127.0.0.1:2024",
@@ -26,29 +38,49 @@ const config = {
);
if (!process.env.NEXT_PUBLIC_LANGGRAPH_BASE_URL) {
rewrites.push({
beforeFiles.push({
source: "/api/langgraph",
destination: langgraphURL,
});
rewrites.push({
beforeFiles.push({
source: "/api/langgraph/:path*",
destination: `${langgraphURL}/:path*`,
});
}
// Auth endpoints: explicit v1/auth prefix only (deny-by-default)
beforeFiles.push({
source: "/api/v1/auth/:path*",
destination: `${gatewayURL}/api/v1/auth/:path*`,
});
// LangGraph-compat: handled by route handler at /api/langgraph-compat/[...path]
// with allowlist, header sanitization, and timeout — no rewrite needed.
if (!process.env.NEXT_PUBLIC_BACKEND_BASE_URL) {
rewrites.push({
source: "/api/agents",
destination: `${gatewayURL}/api/agents`,
});
rewrites.push({
source: "/api/agents/:path*",
destination: `${gatewayURL}/api/agents/:path*`,
});
// Explicit gateway API prefixes (deny-by-default, no catch-all)
const GATEWAY_PREFIXES = [
"agents",
"models",
"threads",
"memory",
"skills",
"mcp",
];
for (const prefix of GATEWAY_PREFIXES) {
beforeFiles.push({
source: `/api/${prefix}`,
destination: `${gatewayURL}/api/${prefix}`,
});
beforeFiles.push({
source: `/api/${prefix}/:path*`,
destination: `${gatewayURL}/api/${prefix}/:path*`,
});
}
}
return rewrites;
return { beforeFiles, afterFiles: [], fallback: [] };
},
};
export default config;
export default withNextra(config);