diff --git a/app/core/auth.py b/app/core/auth.py index a4c8735..e17804c 100644 --- a/app/core/auth.py +++ b/app/core/auth.py @@ -61,7 +61,7 @@ async def verify_app_key( """ 验证后台登录密钥(app_key)。 - 如果未配置 app_key,则跳过验证。 + app_key 必须配置,否则拒绝登录。 """ app_key = get_config("app.app_key", "") diff --git a/app/static/common/batch-sse.js b/app/static/common/batch-sse.js index 1d953d9..5f20299 100644 --- a/app/static/common/batch-sse.js +++ b/app/static/common/batch-sse.js @@ -1,8 +1,14 @@ (function (global) { + function normalizeApiKey(apiKey) { + if (!apiKey) return ''; + const trimmed = String(apiKey).trim(); + return trimmed.startsWith('Bearer ') ? trimmed.slice(7).trim() : trimmed; + } + function openBatchStream(taskId, apiKey, handlers = {}) { if (!taskId) return null; - // Strip "Bearer " prefix if present - query param expects raw key - const rawKey = apiKey && apiKey.startsWith('Bearer ') ? apiKey.slice(7) : apiKey; + // Query param expects raw key + const rawKey = normalizeApiKey(apiKey); const url = `/api/v1/admin/batch/${taskId}/stream?api_key=${encodeURIComponent(rawKey || '')}`; const es = new EventSource(url); @@ -31,9 +37,10 @@ async function cancelBatchTask(taskId, apiKey) { if (!taskId) return; try { + const rawKey = normalizeApiKey(apiKey); await fetch(`/api/v1/admin/batch/${taskId}/cancel`, { method: 'POST', - headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : undefined + headers: rawKey ? { Authorization: `Bearer ${rawKey}` } : undefined }); } catch { // ignore diff --git a/app/static/config/config.js b/app/static/config/config.js index bef6667..84c985c 100644 --- a/app/static/config/config.js +++ b/app/static/config/config.js @@ -31,8 +31,8 @@ const NUMERIC_FIELDS = new Set([ const LOCALE_MAP = { "app": { "label": "应用设置", - "api_key": { title: "API 密钥", desc: "调用 Grok2API 服务所需的 Bearer Token,请妥善保管。" }, - "app_key": { title: "后台密码", desc: "登录 Grok2API 服务管理后台的密码,请妥善保管。" }, + "api_key": { title: "API 密钥", desc: "调用 Grok2API 服务的 Token(可留空,留空将不校验;不要包含 Bearer 前缀)。" }, + "app_key": { title: "后台密码", desc: "登录 Grok2API 管理后台的密码(必填,默认 grok2api)。" }, "app_url": { title: "应用地址", desc: "当前 Grok2API 服务的外部访问 URL,用于文件链接访问。" }, "image_format": { title: "图片格式", desc: "生成的图片格式(url 或 base64)。" }, "video_format": { title: "视频格式", desc: "生成的视频格式(html 或 url,url 为处理后的链接)。" } @@ -335,7 +335,7 @@ async function saveConfig() { } else if (input.dataset.type === 'json') { try { val = JSON.parse(val); } catch (e) { throw new Error(`无效的 JSON: ${getText(s, k).title}`); } } else if (k === 'app_key' && val.trim() === '') { - throw new Error('后台密码不能为空'); + throw new Error('app_key 不能为空(后台密码)'); } else if (NUMERIC_FIELDS.has(k)) { if (val.trim() !== '' && !Number.isNaN(Number(val))) { val = Number(val); diff --git a/docs/README.en.md b/docs/README.en.md index 39d2ff3..7dc41ac 100644 --- a/docs/README.en.md +++ b/docs/README.en.md @@ -154,8 +154,8 @@ Config file: `data/config.toml` | Module | Field | Key | Description | Default | | :--- | :--- | :--- | :--- | :--- | | **app** | `app_url` | App URL | External access URL for Grok2API (used for file links). | `http://127.0.0.1:8000` | -| | `app_key` | Admin password | Password for the Grok2API admin panel. | `grok2api` | -| | `api_key` | API key | Bearer token required to call Grok2API. | `""` | +| | `app_key` | Admin password | Password for the Grok2API admin panel (required). | `grok2api` | +| | `api_key` | API key | Token for calling Grok2API (optional). | `""` | | | `image_format` | Image format | Output image format (`url` or `base64`). | `url` | | | `video_format` | Video format | Output video format (html tag or processed url). | `html` | | **grok** | `temporary` | Temporary chat | Enable temporary conversation mode. | `true` | diff --git a/readme.md b/readme.md index 71b00ea..ac0dfa9 100644 --- a/readme.md +++ b/readme.md @@ -187,8 +187,8 @@ curl http://localhost:8000/v1/images/generations \ | 模块 | 字段 | 配置名 | 说明 | 默认值 | | :-------------------- | :--------------------------- | :----------- | :--------------------------------------------------- | :-------------------------------------------------------- | | **app** | `app_url` | 应用地址 | 当前 Grok2API 服务的外部访问 URL,用于文件链接访问。 | `http://127.0.0.1:8000` | -| | `app_key` | 后台密码 | 登录 Grok2API 服务管理后台的密码,请妥善保管。 | `grok2api` | -| | `api_key` | API 密钥 | 调用 Grok2API 服务所需的 Bearer Token,请妥善保管。 | `grok2api` | +| | `app_key` | 后台密码 | 登录 Grok2API 管理后台的密码(必填)。 | `grok2api` | +| | `api_key` | API 密钥 | 调用 Grok2API 服务的 Token(可选)。 | `""` | | | `image_format` | 图片格式 | 生成的图片格式(url 或 base64)。 | `url` | | | `video_format` | 视频格式 | 生成的视频格式(html 或 url,url 为处理后的链接)。 | `html` | | **grok** | `temporary` | 临时对话 | 是否启用临时对话模式。 | `true` |