mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-06 21:11:50 +08:00
feat(auth): add optional HTTP Basic Auth access gate
This commit is contained in:
1 parent
979e1fe4eb
commit
04c8c54ff8
14 files changed
+809
-23
No files matched your search
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user