优化鉴权系统
This commit is contained in:
@@ -5,8 +5,9 @@
|
||||
## 代码结构
|
||||
|
||||
- `routes.py`:集中定义所有 HTTP 路由。
|
||||
- `auth.py`:校验 API key 白名单并识别当前用户。
|
||||
- `auth.py`:根据 `user_id` 识别当前用户,校验请求 `X-API-Key`。
|
||||
- `schemas.py`:集中定义 API 请求和响应结构。
|
||||
- `users.py`:基于 SQLite 的用户注册、登录与 API Key 持久化。
|
||||
- `domain.py`:定义会话、消息和工具调用的持久化模型。
|
||||
- `tools.py`:集中定义工具注册表、参数结构和工具函数。
|
||||
- `service.py`:处理对话、Agent 循环和业务规则。
|
||||
@@ -23,12 +24,27 @@ uv run uvicorn main:app --reload
|
||||
|
||||
服务默认运行在 `http://127.0.0.1:8000`,交互式 API 文档位于 `/docs`。
|
||||
|
||||
服务会自动加载项目根目录的 `.env`,已有系统环境变量优先级更高。可配置 `DEEPSEEK_BASE_URL`、`DEEPSEEK_MODEL`、`DEFAULT_SYSTEM_PROMPT`、`CHAT_DATA_DIR` 和工具调用限制,完整示例见 `.env.example`。第一版应只使用一个 Uvicorn worker。
|
||||
服务会自动加载项目根目录的 `.env`,已有系统环境变量优先级更高。可配置 `DEEPSEEK_BASE_URL`、`DEEPSEEK_MODEL`、`DEFAULT_SYSTEM_PROMPT`、`CHAT_DATA_DIR`、`USER_DB_PATH` 和工具调用限制,完整示例见 `.env.example`。第一版应只使用一个 Uvicorn worker。
|
||||
|
||||
所有业务接口都必须通过 `X-API-Key` 请求头提供访问密钥。白名单使用用户到密钥的 JSON 映射:
|
||||
用户名、密码哈希、UUID 及 API Key 保存在 SQLite 中(默认路径 `USER_DB_PATH=data/users.db`,随服务启动自动建表)。所有业务接口都必须通过 `X-API-Key` 请求头提供访问密钥,服务端从数据库中查找比对以识别当前用户。
|
||||
|
||||
```text
|
||||
API_KEY_WHITELIST={"11111111-1111-4111-8111-111111111111":"replace-with-a-secret-key"}
|
||||
## 注册与登录
|
||||
|
||||
注册用户(用户名已存在返回 `409`):
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/auth/register \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"username":"小明","password":"your-password"}'
|
||||
```
|
||||
|
||||
用用户名、密码换取持久化的 API Key(凭据错误返回 `401`):
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/auth/login \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"username":"小明","password":"your-password"}'
|
||||
# => {"user_id":"...","api_key":"YOUR_API_KEY"}
|
||||
```
|
||||
|
||||
## 使用
|
||||
@@ -37,7 +53,7 @@ API_KEY_WHITELIST={"11111111-1111-4111-8111-111111111111":"replace-with-a-secret
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/sessions \
|
||||
-H 'X-API-Key: replace-with-a-secret-key' \
|
||||
-H 'X-API-Key: YOUR_API_KEY' \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"system_prompt":"你是一个简洁的中文助手。"}'
|
||||
```
|
||||
@@ -46,7 +62,7 @@ curl -X POST http://127.0.0.1:8000/sessions \
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8000/sessions/SESSION_ID/messages \
|
||||
-H 'X-API-Key: replace-with-a-secret-key' \
|
||||
-H 'X-API-Key: YOUR_API_KEY' \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"content":"你好,请记住我的名字是小明。"}'
|
||||
```
|
||||
@@ -63,14 +79,14 @@ API 返回及存储的每条消息都包含 UTC `created_at`。只有模型完
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8000/sessions/SESSION_ID/messages \
|
||||
-H 'X-API-Key: replace-with-a-secret-key'
|
||||
-H 'X-API-Key: YOUR_API_KEY'
|
||||
```
|
||||
|
||||
查询当前用户的累计 API 调用次数和 token 用量:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8000/usage \
|
||||
-H 'X-API-Key: replace-with-a-secret-key'
|
||||
-H 'X-API-Key: YOUR_API_KEY'
|
||||
```
|
||||
|
||||
每个会话 JSON 顶层保存所属 `user_id`、成功聊天次数及累计 token。`/usage` 会扫描并汇总当前用户的全部会话,不使用独立统计文件。创建会话、查询历史和查询用量不计入 `api_calls`。
|
||||
|
||||
Reference in New Issue
Block a user