mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 02:09:23 +08:00
The interface ships in French and Ukrainian alongside English and Chinese, and /language pins a reply language for one conversation. Immersive reading organizes collections as colored folders, adds a fullscreen learning mode, and turns Explain / Translate / Guide me on a selection into ordinary messages in the reading chat; "Quiz me on this page" runs one quiz turn without changing the conversation's mode, which retires the reading-openers endpoint and its task model. Images embedded in DOCX, PPTX and PDF are extracted in place and reach vision models. Office files preview as paged PDFs through LibreOffice (now in the Docker image), falling back to the in-browser renderers without it. Web sources re-sync on a durable schedule, linked folders sync on demand, .bib files index, and a zotero_search tool queries Zotero. Settings gains Learning progress and credential-free presets; admins import users from CSV; split deployments can hand a session to a public device. Grok CLI joins the subagents, Unifically the providers, ChatGPT exports the importers. Gate cleanup: mypy/bandit findings in the new launcher, audio-overview, web-source, reading-store and textbook modules; the task-model copy test follows the retired reading_openers kind. See assets/releases/ver1-6-11.md for the full notes, and the README Releases row plus its eleven translations.
91 lines
3.4 KiB
TypeScript
91 lines
3.4 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 HANDOFF_PATH = "/handoff";
|
|
export const COOKIE_NAME = "dt_token";
|
|
export const CODEX_CALLBACK_PATH = "/auth/callback";
|
|
export const CODEX_CALLBACK_API_PATH = "/api/auth/openai-codex/callback";
|
|
const RETIRED_PAGE_PATHS = new Set(["/partners/groups"]);
|
|
|
|
export function isCodexCallbackPath(pathname: string): boolean {
|
|
return pathname === CODEX_CALLBACK_PATH;
|
|
}
|
|
|
|
/** Exact retired pages that would otherwise collide with a dynamic route. */
|
|
export function isRetiredPagePath(pathname: string): boolean {
|
|
return RETIRED_PAGE_PATHS.has(pathname);
|
|
}
|
|
|
|
// 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/...` or `.../ws`) and let the rewrite
|
|
// bridge the origin gap.
|
|
export function isBackendPath(pathname: string): boolean {
|
|
return (
|
|
pathname.startsWith("/api/") ||
|
|
isWebSocketPath(pathname) ||
|
|
pathname.startsWith("/files/")
|
|
);
|
|
}
|
|
|
|
export function isWebSocketPath(pathname: string): boolean {
|
|
return pathname === "/ws" || 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|wasm)$/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 === HANDOFF_PATH ||
|
|
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";
|
|
}
|