@insapp-dev/inslog 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +26 -0
- package/README.md +66 -2
- package/config/database.example.json +5 -0
- package/config/locale.example.json +3 -0
- package/locales/en.json +476 -0
- package/locales/zh-CN.json +1 -0
- package/next-env.d.ts +7 -0
- package/next.config.ts +10 -0
- package/package.json +62 -4
- package/public/apple-touch-icon.png +0 -0
- package/public/favicon.ico +0 -0
- package/public/icon-color.png +0 -0
- package/public/icon-color.svg +5 -0
- package/public/icon-dark.svg +5 -0
- package/public/icon.svg +5 -0
- package/scripts/_db.mjs +67 -0
- package/scripts/export-backup.mjs +104 -0
- package/scripts/release.sh +70 -0
- package/scripts/seed-demo.mjs +184 -0
- package/scripts/test-connection.mjs +26 -0
- package/src/app/api/auth/logout/route.ts +9 -0
- package/src/app/api/auth/passkey/add-request/route.ts +35 -0
- package/src/app/api/auth/passkey/login/options/route.ts +40 -0
- package/src/app/api/auth/passkey/login/verify/route.ts +58 -0
- package/src/app/api/auth/passkey/register/options/route.ts +46 -0
- package/src/app/api/auth/passkey/register/verify/route.ts +63 -0
- package/src/app/api/auth/passkey/remove-request/route.ts +44 -0
- package/src/app/api/auth/recovery/route.ts +45 -0
- package/src/app/api/auth/totp/disable/route.ts +42 -0
- package/src/app/api/auth/totp/enable/route.ts +75 -0
- package/src/app/api/auth/totp/login/route.ts +47 -0
- package/src/app/api/auth/totp/setup/route.ts +20 -0
- package/src/app/api/setup/complete/route.ts +27 -0
- package/src/app/api/setup/email/route.ts +41 -0
- package/src/app/api/upload/local/route.ts +73 -0
- package/src/app/api/upload/route.ts +73 -0
- package/src/app/api/upload/s3/route.ts +73 -0
- package/src/app/confirm/[token]/page.tsx +181 -0
- package/src/app/globals.css +952 -0
- package/src/app/layout.tsx +107 -0
- package/src/app/login/login-form.tsx +325 -0
- package/src/app/login/page.tsx +76 -0
- package/src/app/not-found.tsx +26 -0
- package/src/app/page.tsx +179 -0
- package/src/app/posts/[slug]/page.tsx +100 -0
- package/src/app/robots.txt/route.ts +25 -0
- package/src/app/rss.xml/route.ts +60 -0
- package/src/app/search/page.tsx +94 -0
- package/src/app/settings/actions.ts +92 -0
- package/src/app/settings/image-field.tsx +143 -0
- package/src/app/settings/language-actions.ts +28 -0
- package/src/app/settings/language-select.tsx +56 -0
- package/src/app/settings/page.tsx +97 -0
- package/src/app/settings/passkey-manager.tsx +194 -0
- package/src/app/settings/site-form.tsx +323 -0
- package/src/app/settings/theme-toggle.tsx +51 -0
- package/src/app/settings/totp-manager.tsx +239 -0
- package/src/app/setup/auth-config-panel.tsx +132 -0
- package/src/app/setup/config-actions.ts +90 -0
- package/src/app/setup/page.tsx +80 -0
- package/src/app/setup/secrets-actions.ts +49 -0
- package/src/app/setup/setup-data.ts +133 -0
- package/src/app/setup/setup-wizard.tsx +338 -0
- package/src/app/sitemap.xml/route.ts +46 -0
- package/src/app/uploads/[...path]/route.ts +64 -0
- package/src/app/write/actions.ts +85 -0
- package/src/app/write/page.tsx +48 -0
- package/src/app/write/write-client.tsx +254 -0
- package/src/components/BlockEditor.tsx +102 -0
- package/src/components/PostBody.tsx +51 -0
- package/src/components/SiteFooter.tsx +41 -0
- package/src/components/WriteFab.tsx +38 -0
- package/src/components/i18n-provider.tsx +22 -0
- package/src/components/install-hint.tsx +31 -0
- package/src/components/service-config-fields.tsx +110 -0
- package/src/lib/auth.ts +41 -0
- package/src/lib/change-request.ts +184 -0
- package/src/lib/config-keys.ts +300 -0
- package/src/lib/credentials.ts +171 -0
- package/src/lib/env-config.ts +39 -0
- package/src/lib/env-store.ts +72 -0
- package/src/lib/i18n-core.ts +27 -0
- package/src/lib/i18n.ts +72 -0
- package/src/lib/image-compress.ts +59 -0
- package/src/lib/mail.ts +121 -0
- package/src/lib/mongodb.ts +236 -0
- package/src/lib/passkey.ts +73 -0
- package/src/lib/pending.ts +142 -0
- package/src/lib/post-utils.ts +56 -0
- package/src/lib/posts.ts +283 -0
- package/src/lib/rate-limit.ts +91 -0
- package/src/lib/recovery-hints.ts +16 -0
- package/src/lib/safe-path.ts +0 -0
- package/src/lib/secret-box.ts +46 -0
- package/src/lib/service-config.ts +25 -0
- package/src/lib/service-features.ts +72 -0
- package/src/lib/session-constants.ts +5 -0
- package/src/lib/session.ts +51 -0
- package/src/lib/settings-file.ts +120 -0
- package/src/lib/settings.ts +199 -0
- package/src/lib/site-url.ts +15 -0
- package/src/lib/slug.ts +53 -0
- package/src/lib/storage.ts +226 -0
- package/src/lib/time.ts +15 -0
- package/src/lib/totp.ts +88 -0
- package/src/lib/types.ts +72 -0
- package/src/lib/upload-client.ts +83 -0
- package/src/lib/xml.ts +12 -0
- package/src/proxy.ts +24 -0
- package/tsconfig.json +41 -0
package/src/lib/auth.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { cache } from "react";
|
|
2
|
+
import { cookies } from "next/headers";
|
|
3
|
+
import {
|
|
4
|
+
COOKIE_OPTIONS,
|
|
5
|
+
SESSION_COOKIE,
|
|
6
|
+
SESSION_MAX_AGE,
|
|
7
|
+
signSessionToken,
|
|
8
|
+
verifySessionToken,
|
|
9
|
+
} from "./session";
|
|
10
|
+
import { getAuth } from "./credentials";
|
|
11
|
+
|
|
12
|
+
/** 登录成功后写会话 cookie(带上当前会话版本号) */
|
|
13
|
+
export async function startSession(): Promise<void> {
|
|
14
|
+
const auth = await getAuth();
|
|
15
|
+
const token = await signSessionToken(auth.sessionVersion);
|
|
16
|
+
(await cookies()).set(SESSION_COOKIE, token, {
|
|
17
|
+
...COOKIE_OPTIONS,
|
|
18
|
+
maxAge: SESSION_MAX_AGE,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 当前登录状态。
|
|
24
|
+
*
|
|
25
|
+
* 除了验签,还要比对 token 里的 sv 与库里的 sessionVersion:
|
|
26
|
+
* 换过验证器后版本号 +1,别处留着的旧 cookie 立刻失效,不用等 30 天过期。
|
|
27
|
+
* 用 cache 包一层,同一次渲染里首页 / layout / 页脚都调用也只查一次库。
|
|
28
|
+
*/
|
|
29
|
+
export const isLoggedIn = cache(async (): Promise<boolean> => {
|
|
30
|
+
const token = (await cookies()).get(SESSION_COOKIE)?.value;
|
|
31
|
+
if (!token) return false;
|
|
32
|
+
const payload = await verifySessionToken(token);
|
|
33
|
+
if (!payload) return false;
|
|
34
|
+
try {
|
|
35
|
+
const auth = await getAuth();
|
|
36
|
+
return payload.sv === auth.sessionVersion;
|
|
37
|
+
} catch {
|
|
38
|
+
// 读不到版本号就无法确认会话仍然有效,按未登录处理
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { createPendingChange, deletePendingChange, type ChangePurpose } from "./pending";
|
|
2
|
+
import { sendMail, isMailConfigured } from "./mail";
|
|
3
|
+
import { getT } from "./i18n";
|
|
4
|
+
import { getSettings } from "./settings";
|
|
5
|
+
import { formatDateTime } from "./time";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 凭据变更的确认信:只写待确认记录 + 发信,这一刻凭据一个字都没改。
|
|
9
|
+
* 真正写库发生在确认页的 POST(见 /confirm/[token])。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** 邮件确认可用的前提:服务端配了发信能力,且设置里填了收件邮箱 */
|
|
13
|
+
export async function mailReady(): Promise<boolean> {
|
|
14
|
+
if (!(await isMailConfigured())) return false;
|
|
15
|
+
const settings = await getSettings();
|
|
16
|
+
return settings.notifyEmail.trim().length > 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* HTML 转义。
|
|
21
|
+
*
|
|
22
|
+
* 邮件里有几处内容来自请求头(来源 IP、User-Agent)和站点配置,
|
|
23
|
+
* 直接拼进 HTML 就等于允许注入链接、标签和追踪图片——
|
|
24
|
+
* 而这封信是发到站长邮箱的,攻击者能在里面放一个仿冒的「确认」链接。
|
|
25
|
+
*/
|
|
26
|
+
function escapeHtml(value: string): string {
|
|
27
|
+
return value
|
|
28
|
+
.replace(/&/g, "&")
|
|
29
|
+
.replace(/</g, "<")
|
|
30
|
+
.replace(/>/g, ">")
|
|
31
|
+
.replace(/"/g, """)
|
|
32
|
+
.replace(/'/g, "'");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function sendConfirmMail(input: {
|
|
36
|
+
purpose: ChangePurpose;
|
|
37
|
+
payload: Record<string, unknown>;
|
|
38
|
+
actionText: string;
|
|
39
|
+
consequence: string;
|
|
40
|
+
linkOrigin: string;
|
|
41
|
+
ip: string;
|
|
42
|
+
ua: string;
|
|
43
|
+
/**
|
|
44
|
+
* 收件人。不给就用设置里已存的通知邮箱。
|
|
45
|
+
* 安装向导第一次填邮箱时,设置里还空着,得显式指定。
|
|
46
|
+
*/
|
|
47
|
+
to?: string;
|
|
48
|
+
}): Promise<{ ok: true } | { error: string }> {
|
|
49
|
+
const t = await getT();
|
|
50
|
+
const settings = await getSettings();
|
|
51
|
+
const to = (input.to ?? settings.notifyEmail).trim();
|
|
52
|
+
if (!to) return { error: t("设置页还没填通知邮箱") };
|
|
53
|
+
|
|
54
|
+
const created = await createPendingChange({
|
|
55
|
+
purpose: input.purpose,
|
|
56
|
+
payload: input.payload,
|
|
57
|
+
ip: input.ip,
|
|
58
|
+
ua: input.ua,
|
|
59
|
+
});
|
|
60
|
+
if ("error" in created) return created;
|
|
61
|
+
|
|
62
|
+
const origin = input.linkOrigin.replace(/\/+$/, "");
|
|
63
|
+
const url = `${origin}/confirm/${created.token}`;
|
|
64
|
+
const when = formatDateTime(new Date());
|
|
65
|
+
const device = input.ua.slice(0, 120) || t("未知");
|
|
66
|
+
|
|
67
|
+
const text = [
|
|
68
|
+
`有人在 inslog 请求${input.actionText}。`,
|
|
69
|
+
"",
|
|
70
|
+
t("确认地址(10 分钟内有效,只能用一次):"),
|
|
71
|
+
url,
|
|
72
|
+
"",
|
|
73
|
+
`申请时间:${when}`,
|
|
74
|
+
`来源 IP:${input.ip}`,
|
|
75
|
+
`设备:${device}`,
|
|
76
|
+
"",
|
|
77
|
+
input.consequence,
|
|
78
|
+
t("如果不是你本人操作,忽略这封邮件即可,凭据不会改变。"),
|
|
79
|
+
].join("\n");
|
|
80
|
+
|
|
81
|
+
const html = `<div style="font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif;font-size:15px;line-height:1.7;color:#111">
|
|
82
|
+
<p>有人在 inslog 请求${escapeHtml(input.actionText)}。</p>
|
|
83
|
+
<p><a href="${escapeHtml(url)}" style="color:#1a4fa0">确认${escapeHtml(input.actionText)}</a></p>
|
|
84
|
+
<p style="color:#888;font-size:13px">链接 10 分钟内有效,只能用一次。</p>
|
|
85
|
+
<p style="color:#888;font-size:13px">${escapeHtml(input.consequence)}</p>
|
|
86
|
+
<p style="color:#888;font-size:13px;border-top:1px solid #e6e6e6;padding-top:12px;margin-top:20px">
|
|
87
|
+
申请时间:${escapeHtml(when)}<br>
|
|
88
|
+
来源 IP:${escapeHtml(input.ip)}<br>
|
|
89
|
+
设备:${escapeHtml(device)}
|
|
90
|
+
</p>
|
|
91
|
+
<p style="color:#888;font-size:13px">如果不是你本人操作,忽略这封邮件即可,凭据不会改变。</p>
|
|
92
|
+
</div>`;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
await sendMail({ to, subject: `inslog:确认${input.actionText}`, text, html });
|
|
96
|
+
} catch (error) {
|
|
97
|
+
// 邮件没发出去就把那条待确认记录删掉:
|
|
98
|
+
// 留着的话它白占「10 分钟 3 封」的配额,用户重试会立刻被限流,
|
|
99
|
+
// 而那条记录对应的链接根本没送到任何人手里。
|
|
100
|
+
await deletePendingChange(created.token).catch(() => undefined);
|
|
101
|
+
return {
|
|
102
|
+
error: error instanceof Error ? error.message : t("确认邮件发送失败"),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return { ok: true };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export async function requestTotpChange(input: {
|
|
109
|
+
action: "set" | "disable";
|
|
110
|
+
sealedSecret?: string;
|
|
111
|
+
linkOrigin: string;
|
|
112
|
+
ip: string;
|
|
113
|
+
ua: string;
|
|
114
|
+
}): Promise<{ ok: true } | { error: string }> {
|
|
115
|
+
const t = await getT();
|
|
116
|
+
const replacing = input.action === "set";
|
|
117
|
+
return sendConfirmMail({
|
|
118
|
+
purpose: replacing ? "totp-set" : "totp-disable",
|
|
119
|
+
payload: replacing ? { sealedSecret: input.sealedSecret } : {},
|
|
120
|
+
actionText: replacing ? t("更换登录验证器") : t("解绑登录验证器"),
|
|
121
|
+
consequence: replacing
|
|
122
|
+
? t("确认后旧验证器立刻失效,所有设备都需要重新登录。")
|
|
123
|
+
: t("确认后账号会少一把钥匙,所有设备都需要重新登录。"),
|
|
124
|
+
linkOrigin: input.linkOrigin,
|
|
125
|
+
ip: input.ip,
|
|
126
|
+
ua: input.ua,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 安装时确认通知邮箱。
|
|
132
|
+
*
|
|
133
|
+
* 装站点第一步只填邮箱,填完发这封信;点信里的链接才把邮箱写进设置,
|
|
134
|
+
* 然后接着往下装。这样做有两个好处:
|
|
135
|
+
* - 顺带验证了发信通道确实是通的(后面绑定验证器、换绑都靠它)
|
|
136
|
+
* - 邮箱是站长自己点链接确认过的,不存在填错收不到的情况
|
|
137
|
+
*/
|
|
138
|
+
export async function requestSetupEmail(input: {
|
|
139
|
+
email: string;
|
|
140
|
+
linkOrigin: string;
|
|
141
|
+
ip: string;
|
|
142
|
+
ua: string;
|
|
143
|
+
}): Promise<{ ok: true } | { error: string }> {
|
|
144
|
+
const t = await getT();
|
|
145
|
+
const email = input.email.trim();
|
|
146
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
147
|
+
return { error: t("邮箱格式不对") };
|
|
148
|
+
}
|
|
149
|
+
return sendConfirmMail({
|
|
150
|
+
purpose: "setup-email",
|
|
151
|
+
payload: { email },
|
|
152
|
+
// 不带「确认」二字:邮件里链接文字是「确认{actionText}」,
|
|
153
|
+
// 写成「确认安装邮箱」会拼出「确认确认安装邮箱」
|
|
154
|
+
actionText: t("安装邮箱"),
|
|
155
|
+
consequence: t("确认后这个邮箱会存进站点设置,用来接收验证器变更等确认信。"),
|
|
156
|
+
linkOrigin: input.linkOrigin,
|
|
157
|
+
ip: input.ip,
|
|
158
|
+
ua: input.ua,
|
|
159
|
+
to: email,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function requestPasskeyChange(input: {
|
|
164
|
+
action: "add" | "remove";
|
|
165
|
+
credentialId?: string;
|
|
166
|
+
linkOrigin: string;
|
|
167
|
+
ip: string;
|
|
168
|
+
ua: string;
|
|
169
|
+
}): Promise<{ ok: true } | { error: string }> {
|
|
170
|
+
const t = await getT();
|
|
171
|
+
const adding = input.action === "add";
|
|
172
|
+
return sendConfirmMail({
|
|
173
|
+
purpose: adding ? "passkey-add" : "passkey-remove",
|
|
174
|
+
payload: adding ? {} : { credentialId: input.credentialId },
|
|
175
|
+
actionText: adding ? t("绑定一把新的 Passkey") : t("删除一把 Passkey"),
|
|
176
|
+
consequence: adding
|
|
177
|
+
? t("确认后 10 分钟内可以回设置页完成设备绑定,过期需要重新申请。")
|
|
178
|
+
: t("确认后这把 Passkey 立刻失效,所有设备都需要重新登录。"),
|
|
179
|
+
linkOrigin: input.linkOrigin,
|
|
180
|
+
ip: input.ip,
|
|
181
|
+
ua: input.ua,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 可以引导配置的变量清单。
|
|
3
|
+
*
|
|
4
|
+
* 分两组:
|
|
5
|
+
* - auth:身份验证与启动必需(数据库、会话密钥、紧急密钥、Passkey 域名、站点地址、
|
|
6
|
+
* 站点设置存在哪儿)。只在安装 / 重置引导里设置,设置页不提供修改入口——改了等于换锁。
|
|
7
|
+
* - service:邮件与图片存储的凭据。同样只在安装 / 重置引导里设置。
|
|
8
|
+
*
|
|
9
|
+
* 安装向导只负责把两组值生成到同一个 .env 文件里,由用户自行保存到服务器
|
|
10
|
+
* 或导入平台后台;程序不会写服务器上的环境变量。
|
|
11
|
+
*
|
|
12
|
+
* service 组里再按「功能块」分:图片存储、邮件。每个功能块有一个 provider 选择器,
|
|
13
|
+
* 选了哪个 provider 就只显示它的那几个字段——不选的那套不参与,也不必填。
|
|
14
|
+
*
|
|
15
|
+
* label / hint 都是中文原文,页面上再走语言包翻译。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type ConfigGroup = "auth" | "service";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 传给前端的配置项。
|
|
22
|
+
*
|
|
23
|
+
* 已配置的项只显示「已配置」,不把值回显到前端——这些是数据库密码和密钥,
|
|
24
|
+
* 显示出来等于公开。
|
|
25
|
+
*/
|
|
26
|
+
export type ConfigField = {
|
|
27
|
+
key: string;
|
|
28
|
+
label: string;
|
|
29
|
+
hint: string;
|
|
30
|
+
secret: boolean;
|
|
31
|
+
configured: boolean;
|
|
32
|
+
/** 枚举型配置:给候选项,渲染成下拉 */
|
|
33
|
+
options?: { value: string; label: string }[];
|
|
34
|
+
/** 当前生效的值;下拉的初始选中项用它 */
|
|
35
|
+
value?: string;
|
|
36
|
+
/** 由程序生成,不用手填 */
|
|
37
|
+
generated?: boolean;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** service 组里的功能块 */
|
|
41
|
+
export type ConfigFeature = "storage" | "mail";
|
|
42
|
+
|
|
43
|
+
export type ConfigKey = {
|
|
44
|
+
key: string;
|
|
45
|
+
label: string;
|
|
46
|
+
hint: string;
|
|
47
|
+
/** 敏感值:不回显到前端 */
|
|
48
|
+
secret: boolean;
|
|
49
|
+
group: ConfigGroup;
|
|
50
|
+
/** 属于哪个功能块(只对 service 组有意义) */
|
|
51
|
+
feature?: ConfigFeature;
|
|
52
|
+
/** 该字段属于哪个 provider;provider 选择器本身没有这个值 */
|
|
53
|
+
provider?: string;
|
|
54
|
+
/** 标记它是 provider 的下拉选择器本身 */
|
|
55
|
+
isProviderChoice?: boolean;
|
|
56
|
+
/** 由程序生成,不用手填 */
|
|
57
|
+
generated?: boolean;
|
|
58
|
+
/** 下拉选择器的候选项(枚举型配置用) */
|
|
59
|
+
options?: { value: string; label: string }[];
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const CONFIG_KEYS: ConfigKey[] = [
|
|
63
|
+
{
|
|
64
|
+
key: "MONGODB_URI",
|
|
65
|
+
group: "auth",
|
|
66
|
+
label: "数据库连接串",
|
|
67
|
+
hint: "任何 MongoDB 都行:Atlas、自建、Railway、容器里的都行。改了会立刻重连数据库,页面可能短暂打不开。",
|
|
68
|
+
secret: true,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
key: "MONGODB_DB_NAME",
|
|
72
|
+
group: "auth",
|
|
73
|
+
label: "库名",
|
|
74
|
+
hint: "必须指定数据库,集群中没有时会自动创建",
|
|
75
|
+
secret: false,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
key: "AUTH_SECRET",
|
|
79
|
+
group: "auth",
|
|
80
|
+
label: "会话密钥",
|
|
81
|
+
hint: "用来签名登录状态。改了之后所有设备都要重新登录,已绑定的验证器也要重新绑定。",
|
|
82
|
+
secret: true,
|
|
83
|
+
generated: true,
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
key: "RECOVERY_TOKEN",
|
|
87
|
+
group: "auth",
|
|
88
|
+
label: "紧急密钥",
|
|
89
|
+
hint: "两把钥匙都丢了时的救急入口。程序生成,请立刻保存下来。",
|
|
90
|
+
secret: true,
|
|
91
|
+
generated: true,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
key: "RP_ID",
|
|
95
|
+
group: "auth",
|
|
96
|
+
label: "Passkey 域名主体",
|
|
97
|
+
hint: "域名,请不要带 http:// 或 https://",
|
|
98
|
+
secret: false,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
key: "ORIGIN",
|
|
102
|
+
group: "auth",
|
|
103
|
+
label: "站点地址",
|
|
104
|
+
hint: "站点的完整地址,例如 https://inslog.example.com。",
|
|
105
|
+
secret: false,
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
key: "SETTINGS_STORE",
|
|
109
|
+
group: "auth",
|
|
110
|
+
label: "站点设置存哪",
|
|
111
|
+
hint: "mongodb 或 file。file 是服务器上的 config/settings.json,只在文件系统可写时可用。",
|
|
112
|
+
secret: false,
|
|
113
|
+
options: [
|
|
114
|
+
{ value: "mongodb", label: "MongoDB(跟着数据库一起备份)" },
|
|
115
|
+
{ value: "file", label: "配置文件(config/settings.json)" },
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
// ── 图片存储 ────────────────────────────────
|
|
120
|
+
{
|
|
121
|
+
key: "STORAGE_PROVIDER",
|
|
122
|
+
group: "service",
|
|
123
|
+
feature: "storage",
|
|
124
|
+
isProviderChoice: true,
|
|
125
|
+
label: "文件存储方式",
|
|
126
|
+
hint: "选一个。Vercel 部署用 Vercel Blob 最省事;自建服务器可以用本地磁盘或任何 S3 兼容服务。",
|
|
127
|
+
secret: false,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
key: "BLOB_READ_WRITE_TOKEN",
|
|
131
|
+
group: "service",
|
|
132
|
+
feature: "storage",
|
|
133
|
+
provider: "vercel-blob",
|
|
134
|
+
label: "文件存储令牌",
|
|
135
|
+
hint: "Vercel Blob 的读写令牌。在 Storage 里连上 Blob store 时勾上「Add a read-write token env var」,Vercel 会自动注入,不用手填。浏览器直传图片需要它。",
|
|
136
|
+
secret: true,
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
key: "S3_ENDPOINT",
|
|
140
|
+
group: "service",
|
|
141
|
+
feature: "storage",
|
|
142
|
+
provider: "s3",
|
|
143
|
+
label: "S3 接入地址",
|
|
144
|
+
hint: "留空就是 AWS S3 本身。Cloudflare R2 填 https://<账号>.r2.cloudflarestorage.com,MinIO 填 http://<主机>:9000。",
|
|
145
|
+
secret: false,
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
key: "S3_REGION",
|
|
149
|
+
group: "service",
|
|
150
|
+
feature: "storage",
|
|
151
|
+
provider: "s3",
|
|
152
|
+
label: "区域",
|
|
153
|
+
hint: "AWS 是 us-east-1 之类;R2 填 auto;MinIO 留空即可。",
|
|
154
|
+
secret: false,
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
key: "S3_BUCKET",
|
|
158
|
+
group: "service",
|
|
159
|
+
feature: "storage",
|
|
160
|
+
provider: "s3",
|
|
161
|
+
label: "存储桶名",
|
|
162
|
+
hint: "图片放进哪个桶。",
|
|
163
|
+
secret: false,
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
key: "S3_ACCESS_KEY_ID",
|
|
167
|
+
group: "service",
|
|
168
|
+
feature: "storage",
|
|
169
|
+
provider: "s3",
|
|
170
|
+
label: "访问密钥 ID",
|
|
171
|
+
hint: "S3 兼容服务的访问密钥。",
|
|
172
|
+
secret: true,
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
key: "S3_SECRET_ACCESS_KEY",
|
|
176
|
+
group: "service",
|
|
177
|
+
feature: "storage",
|
|
178
|
+
provider: "s3",
|
|
179
|
+
label: "访问密钥",
|
|
180
|
+
hint: "S3 兼容服务的密钥。只在服务端用,不会显示在前端。",
|
|
181
|
+
secret: true,
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
key: "S3_PUBLIC_URL",
|
|
185
|
+
group: "service",
|
|
186
|
+
feature: "storage",
|
|
187
|
+
provider: "s3",
|
|
188
|
+
label: "公开访问地址",
|
|
189
|
+
hint: "桶的公开访问前缀,例如 https://cdn.example.com。桶需要允许匿名读取。",
|
|
190
|
+
secret: false,
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
key: "LOCAL_UPLOAD_DIR",
|
|
194
|
+
group: "service",
|
|
195
|
+
feature: "storage",
|
|
196
|
+
provider: "local",
|
|
197
|
+
label: "本地存放目录",
|
|
198
|
+
hint: "相对项目根目录,默认 uploads。服务器上要可写;多实例部署需要共享这个目录。",
|
|
199
|
+
secret: false,
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
// ── 邮件 ────────────────────────────────────
|
|
203
|
+
{
|
|
204
|
+
key: "MAIL_PROVIDER",
|
|
205
|
+
group: "service",
|
|
206
|
+
feature: "mail",
|
|
207
|
+
isProviderChoice: true,
|
|
208
|
+
label: "发信方式",
|
|
209
|
+
hint: "选一个。没配好就不能更换或解绑验证器(那是保护你账号的最后一层)。",
|
|
210
|
+
secret: false,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
key: "RESEND_API_KEY",
|
|
214
|
+
group: "service",
|
|
215
|
+
feature: "mail",
|
|
216
|
+
provider: "resend",
|
|
217
|
+
label: "发信 API key",
|
|
218
|
+
hint: "Resend 后台创建的密钥。没配就发不出验证器变更确认信。",
|
|
219
|
+
secret: true,
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
key: "RESEND_FROM",
|
|
223
|
+
group: "service",
|
|
224
|
+
feature: "mail",
|
|
225
|
+
provider: "resend",
|
|
226
|
+
label: "发件人",
|
|
227
|
+
hint: "形如 inslog <no-reply@你的域名>,域名要先在 Resend 验证过。",
|
|
228
|
+
secret: false,
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
key: "SMTP_HOST",
|
|
232
|
+
group: "service",
|
|
233
|
+
feature: "mail",
|
|
234
|
+
provider: "smtp",
|
|
235
|
+
label: "SMTP 服务器",
|
|
236
|
+
hint: "QQ 邮箱是 smtp.qq.com,Gmail 是 smtp.gmail.com,自建 Postfix 填自己的主机名。",
|
|
237
|
+
secret: false,
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
key: "SMTP_PORT",
|
|
241
|
+
group: "service",
|
|
242
|
+
feature: "mail",
|
|
243
|
+
provider: "smtp",
|
|
244
|
+
label: "SMTP 端口",
|
|
245
|
+
hint: "一般是 465(隐式 TLS)或 587(STARTTLS)。",
|
|
246
|
+
secret: false,
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
key: "SMTP_USER",
|
|
250
|
+
group: "service",
|
|
251
|
+
feature: "mail",
|
|
252
|
+
provider: "smtp",
|
|
253
|
+
label: "SMTP 账号",
|
|
254
|
+
hint: "登录用的账号。QQ / Gmail 一般就是邮箱地址;本地 relay 不需要登录就留空。",
|
|
255
|
+
secret: false,
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
key: "SMTP_PASSWORD",
|
|
259
|
+
group: "service",
|
|
260
|
+
feature: "mail",
|
|
261
|
+
provider: "smtp",
|
|
262
|
+
label: "SMTP 密码或授权码",
|
|
263
|
+
hint: "QQ / Gmail 要用「授权码」而不是登录密码。只在服务端用。",
|
|
264
|
+
secret: true,
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
key: "SMTP_FROM",
|
|
268
|
+
group: "service",
|
|
269
|
+
feature: "mail",
|
|
270
|
+
provider: "smtp",
|
|
271
|
+
label: "发件人",
|
|
272
|
+
hint: "形如 inslog <no-reply@你的域名>。",
|
|
273
|
+
secret: false,
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
key: "SMTP_SECURE",
|
|
277
|
+
group: "service",
|
|
278
|
+
feature: "mail",
|
|
279
|
+
provider: "smtp",
|
|
280
|
+
label: "用隐式 TLS",
|
|
281
|
+
hint: "填 1 表示端口 465 那种直连加密。587 一般留空。",
|
|
282
|
+
secret: false,
|
|
283
|
+
},
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
export const AUTH_KEYS = CONFIG_KEYS.filter((item) => item.group === "auth");
|
|
287
|
+
export const SERVICE_KEYS = CONFIG_KEYS.filter((item) => item.group === "service");
|
|
288
|
+
|
|
289
|
+
/** 图片存储的可选方案 */
|
|
290
|
+
export const STORAGE_PROVIDERS = [
|
|
291
|
+
{ value: "vercel-blob", label: "Vercel Blob(Vercel 部署最省事)" },
|
|
292
|
+
{ value: "s3", label: "S3 兼容(R2 / MinIO / S3 / OSS…)" },
|
|
293
|
+
{ value: "local", label: "本地磁盘(自建服务器)" },
|
|
294
|
+
];
|
|
295
|
+
|
|
296
|
+
/** 发信的可选方案 */
|
|
297
|
+
export const MAIL_PROVIDERS = [
|
|
298
|
+
{ value: "resend", label: "Resend(HTTP API)" },
|
|
299
|
+
{ value: "smtp", label: "SMTP(QQ / Gmail / 自建)" },
|
|
300
|
+
];
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { getDb } from "./mongodb";
|
|
2
|
+
import { openValue } from "./secret-box";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 凭据存储:只存「怎么登录」这件事本身。
|
|
6
|
+
*
|
|
7
|
+
* 现在只有两把钥匙:
|
|
8
|
+
* - Passkey(WebAuthn,凭据在 passkeys 集合,见 lib/passkey.ts)
|
|
9
|
+
* - TOTP 验证器(6 位码,secret 加密存在这里)
|
|
10
|
+
*
|
|
11
|
+
* 没有密码了。密码是最弱的一环:能猜、能撞、能忘,而它换来的安全性还不如一把 Passkey。
|
|
12
|
+
* 万一两把钥匙都没了(手机丢了、设备重置),走 RECOVERY_TOKEN 紧急入口,
|
|
13
|
+
* 那是环境变量里的一把临时钥匙,用完就该删掉。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const AUTH_ID = "admin";
|
|
17
|
+
|
|
18
|
+
interface AuthDoc {
|
|
19
|
+
_id: string;
|
|
20
|
+
/** TOTP secret 的密文(AES-256-GCM,见 secret-box.ts) */
|
|
21
|
+
totpSecret?: string;
|
|
22
|
+
/** 最近一次验证成功的时间步,同一个步长的码不能用第二次 */
|
|
23
|
+
totpLastStep?: number;
|
|
24
|
+
/** 邮件确认后开启的「可以绑新 Passkey」窗口,10 分钟 */
|
|
25
|
+
passkeyAddUntil?: Date;
|
|
26
|
+
sessionVersion: number;
|
|
27
|
+
updatedAt: Date;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface AuthState {
|
|
31
|
+
/** 已解密的 TOTP secret;没绑验证器、或密钥换了解不开,就是 null */
|
|
32
|
+
totpSecret: string | null;
|
|
33
|
+
/**
|
|
34
|
+
* 数据库里是否确实存着密文。
|
|
35
|
+
* 跟 totpSecret 分开:解不开不等于没绑过,
|
|
36
|
+
* 当成「没绑过」会让换绑绕过邮件确认。
|
|
37
|
+
*/
|
|
38
|
+
totpBound: boolean;
|
|
39
|
+
totpLastStep: number | null;
|
|
40
|
+
sessionVersion: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 当前凭据状态。
|
|
45
|
+
*
|
|
46
|
+
* 数据库读不到时直接抛错:调用方是 isLoggedIn(),它把异常当成未登录。
|
|
47
|
+
* 宁可让一次连接抖动把人踢下线,也不要在读不到状态时误判成「已登录」。
|
|
48
|
+
*/
|
|
49
|
+
export async function getAuth(): Promise<AuthState> {
|
|
50
|
+
const db = await getDb();
|
|
51
|
+
const doc = await db.collection<AuthDoc>("auth").findOne({ _id: AUTH_ID });
|
|
52
|
+
if (!doc) {
|
|
53
|
+
return {
|
|
54
|
+
totpSecret: null,
|
|
55
|
+
totpBound: false,
|
|
56
|
+
totpLastStep: null,
|
|
57
|
+
sessionVersion: 0,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
// totpBound 表示「数据库里确实有密文」,totpSecret 是「能不能解开」。
|
|
61
|
+
// 两者必须分开:密钥换过或密文损坏时 openValue 会返回 null,
|
|
62
|
+
// 但那不等于「没绑过」——当成没绑过的话,换绑就能绕过邮件确认,
|
|
63
|
+
// 任何登录用户都能静默替换掉站长的验证器。
|
|
64
|
+
const totpBound = Boolean(doc.totpSecret);
|
|
65
|
+
return {
|
|
66
|
+
totpSecret: doc.totpSecret ? openValue(doc.totpSecret) : null,
|
|
67
|
+
totpBound,
|
|
68
|
+
totpLastStep: doc.totpLastStep ?? null,
|
|
69
|
+
sessionVersion: doc.sessionVersion ?? 0,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 写入或清除验证器,并把会话版本 +1(所有设备立刻失效)。
|
|
75
|
+
* 返回新的版本号,调用方拿它给当前浏览器重签会话。
|
|
76
|
+
*/
|
|
77
|
+
export async function setTotpSecret(sealed: string | null): Promise<number> {
|
|
78
|
+
const col = (await getDb()).collection<AuthDoc>("auth");
|
|
79
|
+
const current = await col.findOne({ _id: AUTH_ID });
|
|
80
|
+
const sessionVersion = (current?.sessionVersion ?? 0) + 1;
|
|
81
|
+
|
|
82
|
+
if (sealed) {
|
|
83
|
+
await col.updateOne(
|
|
84
|
+
{ _id: AUTH_ID },
|
|
85
|
+
{
|
|
86
|
+
$set: {
|
|
87
|
+
totpSecret: sealed,
|
|
88
|
+
totpLastStep: 0,
|
|
89
|
+
sessionVersion,
|
|
90
|
+
updatedAt: new Date(),
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
{ upsert: true }
|
|
94
|
+
);
|
|
95
|
+
} else {
|
|
96
|
+
await col.updateOne(
|
|
97
|
+
{ _id: AUTH_ID },
|
|
98
|
+
{
|
|
99
|
+
$set: { sessionVersion, updatedAt: new Date() },
|
|
100
|
+
$unset: { totpSecret: "", totpLastStep: "" },
|
|
101
|
+
},
|
|
102
|
+
{ upsert: true }
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return sessionVersion;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 记下已经用掉的时间步。
|
|
110
|
+
* 用 $max 而不是 $set:并发请求下只会往前推,不会被旧值覆盖回去。
|
|
111
|
+
*/
|
|
112
|
+
export async function recordTotpStep(step: number): Promise<void> {
|
|
113
|
+
const col = (await getDb()).collection<AuthDoc>("auth");
|
|
114
|
+
await col.updateOne(
|
|
115
|
+
{ _id: AUTH_ID },
|
|
116
|
+
{ $max: { totpLastStep: step } },
|
|
117
|
+
{ upsert: true }
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 只递增版本号,不碰凭据本身(删 Passkey 这类场合)。
|
|
123
|
+
*
|
|
124
|
+
* 用 $inc 原子自增,而不是「读出来 +1 再写回去」:
|
|
125
|
+
* 后者在并发下会丢增量——两个请求读到同一个旧值,各自算出 +1,
|
|
126
|
+
* 写回去的是同一个数,等于只加了一次,而页面宣称「所有设备都失效了」。
|
|
127
|
+
*
|
|
128
|
+
* 另外要 upsert:只用 Passkey、没绑过验证器的站点 auth 集合是空的,
|
|
129
|
+
* 不 upsert 的话版本号根本没写进去,旧 cookie 照样有效。
|
|
130
|
+
*/
|
|
131
|
+
export async function bumpSessionVersion(): Promise<number> {
|
|
132
|
+
const col = (await getDb()).collection<AuthDoc>("auth");
|
|
133
|
+
await col.updateOne(
|
|
134
|
+
{ _id: AUTH_ID },
|
|
135
|
+
{ $inc: { sessionVersion: 1 }, $set: { updatedAt: new Date() } },
|
|
136
|
+
{ upsert: true }
|
|
137
|
+
);
|
|
138
|
+
const after = await col.findOne({ _id: AUTH_ID });
|
|
139
|
+
return after?.sessionVersion ?? 1;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 邮件确认通过后,开一个 10 分钟的窗口允许绑定新 Passkey */
|
|
143
|
+
export async function authorizePasskeyAdd(): Promise<void> {
|
|
144
|
+
const col = (await getDb()).collection<AuthDoc>("auth");
|
|
145
|
+
await col.updateOne(
|
|
146
|
+
{ _id: AUTH_ID },
|
|
147
|
+
{
|
|
148
|
+
$set: {
|
|
149
|
+
passkeyAddUntil: new Date(Date.now() + 10 * 60 * 1000),
|
|
150
|
+
updatedAt: new Date(),
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
{ upsert: true }
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 绑定窗口是否还有效(读操作,不消耗) */
|
|
158
|
+
export async function passkeyAddAllowed(): Promise<boolean> {
|
|
159
|
+
const col = (await getDb()).collection<AuthDoc>("auth");
|
|
160
|
+
const doc = await col.findOne({ _id: AUTH_ID });
|
|
161
|
+
return !!doc?.passkeyAddUntil && doc.passkeyAddUntil.getTime() > Date.now();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 绑定成功后关掉窗口,避免一次授权绑很多把 */
|
|
165
|
+
export async function clearPasskeyAddAuthorization(): Promise<void> {
|
|
166
|
+
const col = (await getDb()).collection<AuthDoc>("auth");
|
|
167
|
+
await col.updateOne(
|
|
168
|
+
{ _id: AUTH_ID },
|
|
169
|
+
{ $unset: { passkeyAddUntil: "" }, $set: { updatedAt: new Date() } }
|
|
170
|
+
);
|
|
171
|
+
}
|