feat(auth): 支持自托管访问鉴权与笔记同步#378
Open
techotaku39 wants to merge 5 commits into
Open
Conversation
11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改动概述
为开源自托管部署补齐可选访问鉴权和后端笔记历史同步能力。
开启鉴权后,Web 前端和浏览器插件都需要使用同一个自托管访问密码登录,才能访问受保护的后端接口;同时 Web 前端和插件会从后端同步已生成笔记历史,让不同设备访问同一个自托管服务时能看到一致的笔记列表。
这个 PR 面向的是个人 / 单用户自托管场景:解决公网部署没有访问保护、以及后端已有笔记但不同设备历史不同步的问题。它不引入注册、账号管理、团队空间或多用户数据隔离,也不改变 BiliNote Pro 的多用户产品定位。
为什么
原项目在自托管场景下有两个比较明显的问题:
公网部署缺少访问保护
BiliNote 自托管服务默认没有鉴权。如果直接部署到服务器并暴露访问地址,任何知道地址的人都可以打开 Web 页面、调用后端接口、消耗模型额度或查看生成内容。
反代层加 Basic Auth / Cloudflare Access 虽然能保护页面,但浏览器插件需要直接请求后端 API,现有插件并不会自动携带这些外部鉴权信息,因此容易出现 Web 能保护、插件不可用或 API 仍裸露的问题。
笔记内容在后端,但历史列表主要依赖本地状态
后端会把生成结果写入
backend/note_results,但 Web 前端历史和插件任务记录主要依赖浏览器本地状态。结果是:同一个自托管服务中,设备 A 生成的笔记已经存在后端,设备 B 打开同一个服务时却看不到这篇笔记历史,需要重新生成或手动找任务。
因此这个 PR 选择做一个相对克制的开源自托管方案:
这样可以解决自托管用户最直接的安全和同步问题,又尽量减少对现有架构、默认本地使用流程和 Pro 产品边界的影响。
做了什么
后端:可选单用户鉴权
BILINOTE_AUTH_ENABLEDBILINOTE_AUTH_PASSWORDBILINOTE_AUTH_TOKEN_EXPIRE_DAYSGET /api/auth/statusPOST /api/auth/loginPOST /api/auth/logout/api/*、/static/*、/uploads/*后端:笔记历史同步来源
backend/app/services/note_storage.py,从后端note_results读取可同步的笔记历史。_markdown / _audio / _request / _transcript等缓存产物任务,避免同步历史里出现删不掉的伪等待任务。Web 前端:登录与历史同步
AuthGate,在后端开启鉴权时显示访问密码登录。浏览器插件:自托管后端鉴权与同步
3015localhost:3015http://host:3015/api文档与配置
.env.examplebackend/.env.example相比原项目的改进
_markdown、_audio等伪任务出现在历史列表的问题。测试方式
pnpm build(Web 前端)通过pnpm build(浏览器插件)通过pnpm typecheck(浏览器插件)通过(先运行 build 生成插件项目 gitignored 的 auto-import 类型文件)python -m py_compile backend/app/services/auth.py backend/app/services/note_storage.py backend/app/routers/auth.py backend/app/middlewares/auth.py backend/main.py通过3015后可解析为http://localhost:3015并连通后端/api/notes不再返回_markdown / _audio / _request / _transcript缓存产物伪任务回归风险
Checklist
feature/auth-self-host-sync)develop)type(scope): subject格式README.md/ env example).env/ 大型二进制