docs: add authentication system documentation

This commit is contained in:
Warren
2026-05-16 17:55:25 +08:00
parent 6e3de0169e
commit af0676c8dd

122
AGENTS.md
View File

@@ -108,10 +108,13 @@ updated_at, sort_order
|路由 |方法 |功能 |server.rs行號 |
|------|------|------|--------------|
| `/api/v2/tree/:user_id` | GET | 取得檔案樹 | 61 |
| `/api/v2/tree/:user_id` | DELETE | 刪除所有節點 | 64 |
| `/api/v2/tree/:user_id/node` | POST | 建立節點 | 62 |
| `/api/v2/tree/:user_id/node/:node_id` | PUT | 更新節點 | 63 |
| `/api/v2/auth/login` | POST | 登入返回token | 1203 |
| `/api/v2/auth/logout` | POST | 登出需Bearer token | 1220 |
| `/api/v2/auth/verify` | GET | 验证token有效性 | 1238 |
| `/api/v2/tree/:user_id` | GET | 取得檔案樹(需認證) | 395 |
| `/api/v2/tree/:user_id` | DELETE | 刪除所有節點(需認證) | 64 |
| `/api/v2/tree/:user_id/node` | POST | 建立節點(需認證) | 62 |
| `/api/v2/tree/:user_id/node/:node_id` | PUT | 更新節點(需認證) | 63 |
| `/api/v2/tree/:user_id/node/:node_id` | DELETE | 刪除節點 | 63 |
| `/api/v2/tree/:user_id/node/:node_id/move` | PUT | 移動節點 | 71 |
| `/api/v2/tree/:user_id/node/:node_id/alias` | PATCH | 更新別名 | 72 |
@@ -122,9 +125,116 @@ updated_at, sort_order
**使用範例:**
```bash
curl http://localhost:11438/api/v2/tree/demo?mode=tree
curl http://localhost:11438/api/v2/tree/demo?mode=list
#登入取得token
curl -X POST http://localhost:11438/api/v2/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"demo","password":"demo123"}'
#返回:{"token":"xxx","expires_at":"...","user_id":"demo"}
#使用token訪問檔案樹
curl http://localhost:11438/api/v2/tree/demo \
-H "Authorization: Bearer <TOKEN>"
```
**認證說明:**
- 所有 `/api/v2/tree/*` 和 `/api/v2/files/*` API 需要認證
- Token有效期24小時
- 默認用戶:`demo` /密碼:`demo123`
````
---
##認證系統Authentication
###認證方式
**Token-BasedAuthentication**
- UUID Token簡單可靠
- 24小時有效期
- Session管理in-memory
**APIEndpoints**
|Endpoint |方法 |功能 |
|---------|------|------|
| `/api/v2/auth/login` | POST | 登入返回token |
| `/api/v2/auth/logout` | POST | 登出需Bearer token |
| `/api/v2/auth/verify` | GET | 验证token |
**使用流程:**
```
1.登入 →取得token
2.使用token訪問APIAuthorization: Bearer <token>
3.Token过期 →重新登入
```
###默認用戶
**測試用戶:**
- Username: `demo`
- Password: `demo123`
- UserID: `demo`
**密碼安全:**
- bcrypt加密DEFAULT_COST
- 密碼不以明文儲存
###認證範例
**登入:**
```bash
curl -X POST http://localhost:11438/api/v2/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"demo","password":"demo123"}'
```
**返回:**
```json
{
"token": "b2060c13-2a2d-4d5c-82e9-985bbdadbea2",
"expires_at": "2026-05-17T09:53:51Z",
"user_id": "demo"
}
```
**使用Token訪問**
```bash
curl http://localhost:11438/api/v2/tree/demo \
-H "Authorization: Bearer b2060c13-2a2d-4d5c-82e9-985bbdadbea2"
```
**验证Token**
```bash
curl http://localhost:11438/api/v2/auth/verify \
-H "Authorization: Bearer <TOKEN>"
```
**登出:**
```bash
curl -X POST http://localhost:11438/api/v2/auth/logout \
-H "Authorization: Bearer <TOKEN>"
```
###保護範圍
**需要認證的API**
- `/api/v2/tree/*` -檔案樹管理
- `/api/v2/files/*` -檔案操作
- `/api/v2/upload/*` -檔案上傳
- `/api/v2/render/*` -檔案渲染
**公開API無需認證**
- `/` -根路徑
- `/api/v2/auth/*` -認證相關
- `/version` -版本信息
- `/api/v2/modes` -顯示模式
###擴展建議
**可擴展功能:**
1. JWT認證已添加依賴未啟用
2. 用戶管理APIcreate_user已實現
3.持久化Session目前in-memory
4. RBAC權限控制
---
### DisplayMode顯示模式