Files
DeepTutor/web/lib/proxy-policy.ts
Bingxi Zhao (Frank) f2aa49f42e release: v1.5.6
Maintenance release on top of v1.5.5, finishing the Codex sign-in it
shipped and adding a connected knowledge engine.

- Tencent IMA joins as a connected knowledge engine: a KB bound to `ima`
  is a connection pointer, not an index, following the lightrag-server
  shape. Documents are added in IMA and retrieval calls IMA's OpenAPI;
  credentials (client_id + api_key) and the library id live per-KB, so
  each user on a shared deployment retrieves through their own account.
- The activity panel splits what a conversation produced from what you
  uploaded: generated files get their own card above attachments, with a
  size and a hover title derived from the artifact URL, so no absolute
  server path is persisted or sent to the browser.
- Remote Codex OAuth completes (#716): the loopback listener stays
  private while the exact callback path is forwarded through the Web
  origin to a state-validating broker, with the real callback address,
  the SSH tunnel command, an authorize action, and a reload-surviving
  countdown surfaced in the settings card and CLI.
- That callback is delivered by OAuth state rather than the request's
  user, since the browser arrives through the user's own tunnel with no
  session cookie -- without it remote sign-in returned login_not_active
  for every non-administrator. Partner scopes resolve Codex credentials
  to the owner's root (#713) instead of the partner's synthetic
  workspace.
- parse_language() no longer collapses every non-English language to
  Chinese (#712), which is why a Japanese book came back with Chinese
  quizzes; prompt fallbacks now try [code, base locale, "en"].
- Assorted: book creation and block regeneration run over WebSocket to
  dodge proxy timeouts with non-streaming spine synthesis (#707, routed
  through the agent seam so image attachments, trace metadata, and
  per-agent routing survive), deeptutor start installs web/node_modules
  when missing (#710), admin user management is localized (#718), and
  the service lazy loader uses literal import paths (#717).

README prose, the ten translated READMEs, release notes, and the version
bump ride along in this commit.
2026-07-29 01:56:12 +08:00

75 lines
3.0 KiB
TypeScript

// Pure request-routing policy for the Next.js middleware (web/proxy.ts).
//
// This module deliberately carries NO dependency on `next/server`: it answers
// "what should happen to this request?" as plain, side-effect-free functions,
// while proxy.ts stays a thin adapter that turns those answers into
// NextResponse objects. Keeping the policy pure means the routing/auth rules
// can be unit-tested in the node harness without booting the Next runtime.
export const LOGIN_PATH = "/login";
export const COOKIE_NAME = "dt_token";
export const CODEX_CALLBACK_PATH = "/auth/callback";
export const CODEX_CALLBACK_API_PATH = "/api/v1/auth/openai-codex/callback";
export function isCodexCallbackPath(pathname: string): boolean {
return pathname === CODEX_CALLBACK_PATH;
}
// Paths whose responses come from the backend, not the Next app. The middleware
// rewrites these to DEEPTUTOR_API_BASE_URL so the browser can use frontend-
// relative URLs (e.g. `:3782/api/v1/...` or `.../ws`) and let the rewrite
// bridge the origin gap.
export function isBackendPath(pathname: string): boolean {
return pathname.startsWith("/api/") || pathname.startsWith("/ws/");
}
// Static assets served straight out of `web/public` (logos, favicons, fonts,
// provider icons, …). These must bypass the auth gate even in multi-user mode:
// the Next image optimizer re-fetches a referenced public image over a
// server-side loopback request that carries NO auth cookie, so gating the path
// bounces that fetch to /login and the `<Image>` renders as a broken icon
// (issue #599 — broken logo/banner after login). Public assets are
// non-sensitive by design, so allowing them through is safe.
const STATIC_ASSET =
/\.(?:png|jpe?g|gif|svg|ico|webp|avif|woff2?|ttf|otf|txt|json|map|css|js)$/i;
// Paths the auth gate must never block: the auth pages themselves, Next.js
// internals, and public static assets (see STATIC_ASSET above).
export function isAuthExempt(pathname: string): boolean {
return (
pathname.startsWith(LOGIN_PATH) ||
pathname.startsWith("/register") ||
pathname.startsWith("/_next") ||
pathname.startsWith("/favicon") ||
STATIC_ASSET.test(pathname)
);
}
export type TokenState = "missing" | "malformed" | "expired" | "valid";
// Classify the auth cookie WITHOUT trusting its signature — the middleware is a
// cheap front-line gate, not the authority (the backend does real verification
// on every API call). `nowMs` is injected rather than read from the clock so
// the classifier stays pure and testable.
export function classifyToken(
token: string | undefined,
nowMs: number,
): TokenState {
if (!token) return "missing";
// Expect a JWT: header.payload.signature
const parts = token.split(".");
if (parts.length !== 3) return "malformed";
try {
const payload = JSON.parse(
Buffer.from(parts[1], "base64url").toString("utf-8"),
);
if (payload.exp && nowMs >= payload.exp * 1000) return "expired";
} catch {
return "malformed";
}
return "valid";
}