feat: protect admin APIs
This commit is contained in:
1 parent
4f027a3728
commit
1e9228e50a
13 files changed
+657
-95
No files matched your search
@@ -1,10 +1,15 @@
|
||||
const STORAGE_KEY = "speaking-lab-state-v1";
|
||||
const ADMIN_CREDENTIALS_KEY = "speaking-lab-admin-credentials-v1";
|
||||
const IS_BROWSER = typeof document !== "undefined";
|
||||
|
||||
const els = {
|
||||
identity: IS_BROWSER ? document.querySelector("#adminIdentity") : null,
|
||||
locked: IS_BROWSER ? document.querySelector("#adminLocked") : null,
|
||||
content: IS_BROWSER ? document.querySelector("#adminContent") : null,
|
||||
adminLoginForm: IS_BROWSER ? document.querySelector("#adminLoginForm") : null,
|
||||
adminUsername: IS_BROWSER ? document.querySelector("#adminUsername") : null,
|
||||
adminPassword: IS_BROWSER ? document.querySelector("#adminPassword") : null,
|
||||
adminLoginMessage: IS_BROWSER ? document.querySelector("#adminLoginMessage") : null,
|
||||
formTitle: IS_BROWSER ? document.querySelector("#adminFormTitle") : null,
|
||||
form: IS_BROWSER ? document.querySelector("#adminCreateForm") : null,
|
||||
submitBtn: IS_BROWSER ? document.querySelector("#adminSubmitBtn") : null,
|
||||
@@ -13,6 +18,7 @@ const els = {
|
||||
speaker: IS_BROWSER ? document.querySelector("#adminSpeaker") : null,
|
||||
description: IS_BROWSER ? document.querySelector("#adminDescription") : null,
|
||||
tags: IS_BROWSER ? document.querySelector("#adminTags") : null,
|
||||
coverFile: IS_BROWSER ? document.querySelector("#adminCoverFile") : null,
|
||||
videoFile: IS_BROWSER ? document.querySelector("#adminVideoFile") : null,
|
||||
videoUrl: IS_BROWSER ? document.querySelector("#adminVideoUrl") : null,
|
||||
subtitleEn: IS_BROWSER ? document.querySelector("#adminSubtitleEn") : null,
|
||||
@@ -31,6 +37,7 @@ const els = {
|
||||
let videos = [];
|
||||
let editingVideo = null;
|
||||
let editableSubtitles = createEmptySubtitleEditState();
|
||||
let adminCredentials = IS_BROWSER ? getStoredAdminCredentials(window.sessionStorage) : null;
|
||||
|
||||
if (IS_BROWSER) initializeAdmin();
|
||||
|
||||
@@ -46,18 +53,8 @@ function loadState() {
|
||||
}
|
||||
}
|
||||
|
||||
function getCurrentUser() {
|
||||
const state = loadState();
|
||||
return state.users.find((user) => user.id === state.currentUserId) || null;
|
||||
}
|
||||
|
||||
function isAdmin(user) {
|
||||
const email = String(user?.email || "").toLowerCase();
|
||||
return user?.role === "admin" || email.includes("admin");
|
||||
}
|
||||
|
||||
async function apiRequest(path, options = {}) {
|
||||
const response = await fetch(path, options);
|
||||
const response = await fetch(path, withAdminAuth(options));
|
||||
if (!response.ok) {
|
||||
let message = "请求失败,请重试。";
|
||||
try {
|
||||
@@ -72,18 +69,7 @@ async function apiRequest(path, options = {}) {
|
||||
}
|
||||
|
||||
function initializeAdmin() {
|
||||
const user = getCurrentUser();
|
||||
if (!isAdmin(user)) {
|
||||
els.identity.textContent = "未登录管理员账号";
|
||||
els.locked.hidden = false;
|
||||
els.content.hidden = true;
|
||||
return;
|
||||
}
|
||||
|
||||
els.identity.textContent = `${user.email} · 管理员`;
|
||||
els.locked.hidden = true;
|
||||
els.content.hidden = false;
|
||||
|
||||
els.adminLoginForm?.addEventListener("submit", loginAdmin);
|
||||
els.form.addEventListener("submit", addVideo);
|
||||
els.refreshBtn.addEventListener("click", loadVideos);
|
||||
els.cancelEditBtn.addEventListener("click", resetFormMode);
|
||||
@@ -91,10 +77,92 @@ function initializeAdmin() {
|
||||
els.processAiSubtitlesBtn?.addEventListener("click", processAiSubtitles);
|
||||
els.generateLocalWhisperSubtitlesBtn?.addEventListener("click", generateLocalWhisperSubtitles);
|
||||
bindFileInputs();
|
||||
if (adminCredentials) {
|
||||
unlockAdmin(adminCredentials.username);
|
||||
verifyStoredAdminCredentials();
|
||||
} else {
|
||||
lockAdmin("请输入管理员账号密码。");
|
||||
}
|
||||
}
|
||||
|
||||
function lockAdmin(message = "") {
|
||||
els.identity.textContent = "未登录管理员账号";
|
||||
els.locked.hidden = false;
|
||||
els.content.hidden = true;
|
||||
if (els.adminLoginMessage) {
|
||||
els.adminLoginMessage.hidden = !message;
|
||||
els.adminLoginMessage.textContent = message;
|
||||
els.adminLoginMessage.classList.toggle("error", Boolean(message));
|
||||
}
|
||||
}
|
||||
|
||||
function unlockAdmin(username) {
|
||||
els.identity.textContent = `${username} · 管理员`;
|
||||
els.locked.hidden = true;
|
||||
els.content.hidden = false;
|
||||
loadAiStatus();
|
||||
loadVideos();
|
||||
}
|
||||
|
||||
async function loginAdmin(event) {
|
||||
event.preventDefault();
|
||||
const username = els.adminUsername.value.trim();
|
||||
const password = els.adminPassword.value;
|
||||
if (!username || !password) return;
|
||||
|
||||
adminCredentials = { username, password };
|
||||
try {
|
||||
const result = await apiRequest("/api/admin/session");
|
||||
setStoredAdminCredentials(window.sessionStorage, username, password);
|
||||
if (els.adminLoginMessage) els.adminLoginMessage.hidden = true;
|
||||
els.adminLoginForm.reset();
|
||||
unlockAdmin(result.username || username);
|
||||
} catch (error) {
|
||||
adminCredentials = null;
|
||||
clearStoredAdminCredentials(window.sessionStorage);
|
||||
lockAdmin(error.message || "管理员账号或密码错误。");
|
||||
}
|
||||
}
|
||||
|
||||
async function verifyStoredAdminCredentials() {
|
||||
try {
|
||||
const result = await apiRequest("/api/admin/session");
|
||||
unlockAdmin(result.username || adminCredentials.username);
|
||||
} catch {
|
||||
adminCredentials = null;
|
||||
clearStoredAdminCredentials(window.sessionStorage);
|
||||
lockAdmin("管理员登录已失效,请重新登录。");
|
||||
}
|
||||
}
|
||||
|
||||
function withAdminAuth(options = {}) {
|
||||
if (!adminCredentials) return options;
|
||||
const headers = new Headers(options.headers || {});
|
||||
headers.set("Authorization", `Basic ${btoa(`${adminCredentials.username}:${adminCredentials.password}`)}`);
|
||||
return { ...options, headers };
|
||||
}
|
||||
|
||||
function getStoredAdminCredentials(storage) {
|
||||
try {
|
||||
const value = JSON.parse(storage.getItem(ADMIN_CREDENTIALS_KEY) || "null");
|
||||
if (!value?.username || !value?.password) return null;
|
||||
return {
|
||||
username: String(value.username),
|
||||
password: String(value.password),
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function setStoredAdminCredentials(storage, username, password) {
|
||||
storage.setItem(ADMIN_CREDENTIALS_KEY, JSON.stringify({ username, password }));
|
||||
}
|
||||
|
||||
function clearStoredAdminCredentials(storage) {
|
||||
storage.removeItem(ADMIN_CREDENTIALS_KEY);
|
||||
}
|
||||
|
||||
function getFileInputState(file, defaultLabel) {
|
||||
if (!file) {
|
||||
return {
|
||||
@@ -112,7 +180,7 @@ function getFileInputState(file, defaultLabel) {
|
||||
}
|
||||
|
||||
function bindFileInputs() {
|
||||
[els.videoFile, els.subtitleEn, els.subtitleZh].forEach((input) => {
|
||||
[els.coverFile, els.videoFile, els.subtitleEn, els.subtitleZh].forEach((input) => {
|
||||
const label = input?.closest(".file-input");
|
||||
if (!label) return;
|
||||
|
||||
@@ -134,7 +202,7 @@ function updateFileInputLabel(input) {
|
||||
}
|
||||
|
||||
function resetFileInputs() {
|
||||
[els.videoFile, els.subtitleEn, els.subtitleZh].forEach((input) => {
|
||||
[els.coverFile, els.videoFile, els.subtitleEn, els.subtitleZh].forEach((input) => {
|
||||
if (input) updateFileInputLabel(input);
|
||||
});
|
||||
}
|
||||
@@ -468,6 +536,7 @@ async function addVideo(event) {
|
||||
formData.append("tags", els.tags.value.trim());
|
||||
formData.append("video_url", els.videoUrl.value.trim());
|
||||
|
||||
if (els.coverFile.files?.[0]) formData.append("cover_file", els.coverFile.files[0]);
|
||||
if (els.videoFile.files?.[0]) formData.append("video_file", els.videoFile.files[0]);
|
||||
if (els.subtitleEn.files?.[0]) formData.append("subtitle_en", els.subtitleEn.files[0]);
|
||||
if (els.subtitleZh.files?.[0]) formData.append("subtitle_zh", els.subtitleZh.files[0]);
|
||||
@@ -508,6 +577,9 @@ if (typeof module !== "undefined" && module.exports) {
|
||||
getLocalWhisperButtonState,
|
||||
shouldConfirmSubtitleOverwrite,
|
||||
pickVideoAfterSave,
|
||||
getStoredAdminCredentials,
|
||||
setStoredAdminCredentials,
|
||||
clearStoredAdminCredentials,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user