feat(auth): add optional HTTP Basic Auth access gate

This commit is contained in:
ji ruo committed 2026-10-09 10:12:27 +08:00
1 parent 979e1fe4eb
commit 04c8c54ff8
14 files changed
+809 -23

No files matched your search

@@ -50,7 +50,7 @@ import { Spinner } from "@/components/ui/spinner";
import { Textarea } from "@/components/ui/textarea";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { useSideQuestions } from "@/hooks/use-side-questions";
import { api, sseUrl } from "@/lib/api";
import { api, openSSE } from "@/lib/api";
import { shouldSubmitOnKey, useChatSendMode } from "@/lib/chat-send-mode";
import { MOCK } from "@/lib/mock/enabled";
import { isBtwCommand } from "@/lib/side-questions";
@@ -969,9 +969,7 @@ export function SessionsTab({ taskId }: { taskId: string }) {
};
});
// Open the single task SSE from the snapshot cursor.
const es = new EventSource(
sseUrl(`/api/exploration/activity/stream?task=${encodeURIComponent(taskId)}&since=${snapshotRef.current}`),
);
const es = openSSE(`/api/exploration/activity/stream?task=${encodeURIComponent(taskId)}&since=${snapshotRef.current}`);
esRef.current = es;
es.onopen = () => setSseLive(true);
es.onerror = () => setSseLive(false); // EventSource auto-reconnects; DB compensates the gap
+2 -2
View File
@@ -3,7 +3,7 @@
import * as React from "react";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { sseUrl } from "@/lib/api";
import { openSSE } from "@/lib/api";
import { MOCK } from "@/lib/mock/enabled";
import type { LogLine } from "@/lib/types";
import { cn } from "@/lib/utils";
@@ -64,7 +64,7 @@ export default function LogsPage() {
setLines(MOCK_LOGS);
return;
}
const es = new EventSource(sseUrl("/api/logs/stream?since=0"));
const es = openSSE("/api/logs/stream?since=0");
es.onmessage = (e) => {
if (pausedRef.current) return;
try {
@@ -0,0 +1,132 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { ShieldCheckIcon } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { Field, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import { api } from "@/lib/api";
export function BasicAuthCard() {
const [enabled, setEnabled] = useState(false);
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [passwordSet, setPasswordSet] = useState(false);
const [loaded, setLoaded] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState("");
const load = useCallback(async () => {
setError("");
try {
const settings = await api.basicAuthSettings();
setEnabled(settings.enabled);
setUsername(settings.username);
setPasswordSet(settings.password_set);
setLoaded(true);
} catch (e) {
setError(`读取失败:${(e as Error).message}`);
}
}, []);
useEffect(() => {
void load();
}, [load]);
const save = async (event: React.FormEvent) => {
event.preventDefault();
setSaving(true);
setError("");
try {
const settings = await api.setBasicAuthSettings({ enabled, username: username.trim(), password });
setEnabled(settings.enabled);
setUsername(settings.username);
setPasswordSet(settings.password_set);
setPassword("");
toast.success(settings.enabled ? "HTTP Basic Auth 已开启,当前浏览器保持访问" : "HTTP Basic Auth 已关闭");
} catch (e) {
setError(`保存失败:${(e as Error).message}`);
} finally {
setSaving(false);
}
};
return (
<Card className="mb-4 break-inside-avoid md:mb-6">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<ShieldCheckIcon className="size-4" />
HTTP Basic Auth
</CardTitle>
<CardDescription>
开启后,访客需先通过浏览器原生账号密码验证,再进入 ARTEX 登录页。默认关闭,保存后立即生效。
</CardDescription>
</CardHeader>
<form onSubmit={save}>
<CardContent>
<FieldGroup>
<Field orientation="horizontal">
<FieldLabel htmlFor="basic-auth-enabled">启用访问验证</FieldLabel>
<Switch
id="basic-auth-enabled"
checked={enabled}
onCheckedChange={setEnabled}
disabled={!loaded || saving}
/>
</Field>
<Field>
<FieldLabel htmlFor="basic-auth-username">验证用户名</FieldLabel>
<Input
id="basic-auth-username"
autoComplete="off"
value={username}
required={enabled}
disabled={!loaded || saving}
onChange={(e) => setUsername(e.target.value)}
placeholder="独立于 ARTEX 登录账号"
/>
<FieldDescription>不能包含冒号,最长 128 字节。</FieldDescription>
</Field>
<Field>
<FieldLabel htmlFor="basic-auth-password">验证密码</FieldLabel>
<Input
id="basic-auth-password"
type="password"
autoComplete="new-password"
value={password}
required={enabled && !passwordSet}
disabled={!loaded || saving}
onChange={(e) => setPassword(e.target.value)}
placeholder={passwordSet ? "已设置,留空保持原密码" : "至少 8 位,最多 72 字节"}
/>
<FieldDescription>与 ARTEX 登录密码独立。公网访问请使用 HTTPS。</FieldDescription>
</Field>
<FieldDescription>
保存不会中断当前浏览器访问。可用无痕窗口检查验证弹窗;浏览器可能缓存凭据。关闭后保留账号密码,便于再次开启。
</FieldDescription>
{error ? (
<p role="alert" className="text-destructive text-sm">
{error}
</p>
) : null}
</FieldGroup>
</CardContent>
<CardFooter className="mt-4 gap-2">
<Button type="submit" disabled={!loaded || saving}>
{saving ? "保存中…" : "保存访问验证"}
</Button>
{!loaded && error ? (
<Button type="button" variant="outline" onClick={load}>
重新加载
</Button>
) : null}
</CardFooter>
</form>
</Card>
);
}
@@ -16,7 +16,7 @@ import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Progress } from "@/components/ui/progress";
import { api, sseUrl } from "@/lib/api";
import { api, openSSE } from "@/lib/api";
import type { UpdateCheck, UpdateProgress } from "@/lib/types";
/** 等待新版本上线的最长时间。一次升级要经过三次进程启动(暂存 → 换装 → 新版),
@@ -100,7 +100,7 @@ export function UpdateCard() {
// 订阅更新进度。SSE 不走 Next 的 /api 重写(那层会缓冲,事件推不出来)。
const openStream = React.useCallback(
(fromVersion: string) => {
const es = new EventSource(sseUrl("/api/update/stream"));
const es = openSSE("/api/update/stream");
es.onmessage = (ev) => {
let p: UpdateProgress;
try {
@@ -16,6 +16,7 @@ import { CHAT_SEND_MODE_OPTIONS, type ChatSendMode, setChatSendMode, useChatSend
import type { Settings } from "@/lib/types";
import { UpdateCard } from "./_components/update-card";
import { BasicAuthCard } from "./_components/basic-auth-card";
export default function SystemSettingsPage() {
const [trafficCapture, setTrafficCapture] = React.useState(false);
@@ -267,6 +268,7 @@ export default function SystemSettingsPage() {
column-gap 只管列间距,行间距要由子元素自己给。 */}
<div className="columns-1 gap-4 md:gap-6 lg:columns-2">
<UpdateCard />
<BasicAuthCard />
<Card className="mb-4 break-inside-avoid md:mb-6">
<CardHeader>