# MarkBase Admin API 使用指南 **版本**:1.0 **日期**:2026-10-26 **狀態**:✅ 已部署 --- ## 概述 MarkBase 提供完整的 Admin API,用於管理用戶、共享和系統設置。所有管理員端點均需要 Bearer Token 認證。 --- ## 認證流程 ### 1. 獲取 Admin Token **端點**:`POST /api/v2/admin/login` **請求**: ```bash curl -X POST http://localhost:11438/api/v2/admin/login \ -H "Content-Type: application/json" \ -d '{ "username": "demo", "password": "demo123" }' ``` **成功響應**(200 OK): ```json { "token": "9dcd7494-6791-4842-9991-fa861ceae578", "expires_at": "2026-10-27T18:07:57Z", "username": "demo" } ``` **失敗響應**(401 Unauthorized): ```json { "error": "Invalid admin credentials" } ``` **說明**: - Token 有效期:24 小時 - Token 用於後續所有管理員 API 調用 - Token 存儲在服務器內存中(重啟後失效) --- ### 2. 驗證 Token **端點**:`GET /api/v2/admin/verify` **請求**: ```bash curl http://localhost:11438/api/v2/admin/verify \ -H "Authorization: Bearer YOUR_TOKEN_HERE" ``` **成功響應**(200 OK): ```json { "ok": true, "username": "demo", "expires_at": "2026-10-27T18:07:57Z" } ``` **失敗響應**(401 Unauthorized): ```json { "ok": false, "error": "Invalid admin token" } ``` --- ## 用戶管理 API ### 1. 列出所有用戶 **端點**:`GET /api/v2/admin/users` **認證**:需要 Bearer Token **請求**: ```bash curl http://localhost:11438/api/v2/admin/users \ -H "Authorization: Bearer YOUR_TOKEN_HERE" ``` **響應**(200 OK): ```json { "total": 6, "users": [ { "username": "demo", "home_dir": "/Users/accusys/momentry/var/sftpgo/data/demo", "permissions": "{\"/\":[\"*\"]}", "status": 1, "created_at": 1778857202346 }, { "username": "accusys", "home_dir": "/Users/accusys/markbase/data/downloads", "permissions": "{\"/\":[\"list\",\"download\",\"upload\",\"overwrite\",\"delete\",\"rename\",\"create_dirs\",\"chmod\"]}", "status": 1, "created_at": 1781171318 } ] } ``` **字段說明**: | 字段 | 類型 | 說明 | |------|------|------| | `username` | string | 用戶名 | | `home_dir` | string | 用戶主目錄 | | `permissions` | string | 權限 JSON 字符串 | | `status` | number | 狀態(1=啟用,0=禁用) | | `created_at` | number | 創建時間(Unix timestamp) | --- ### 2. 創建用戶 **端點**:`POST /api/v2/admin/users` **認證**:需要 Bearer Token **請求**: ```bash curl -X POST http://localhost:11438/api/v2/admin/users \ -H "Authorization: Bearer YOUR_TOKEN_HERE" \ -H "Content-Type: application/json" \ -d '{ "username": "newuser", "password": "password123", "home_dir": "/path/to/home", "permissions": "{\"/\":[\"list\",\"download\",\"upload\"]}" }' ``` **響應**(201 Created): ```json { "success": true, "message": "User created successfully" } ``` --- ### 3. 更新用戶 **端點**:`PUT /api/v2/admin/users/:username` **認證**:需要 Bearer Token **請求**: ```bash curl -X PUT http://localhost:11438/api/v2/admin/users/newuser \ -H "Authorization: Bearer YOUR_TOKEN_HERE" \ -H "Content-Type: application/json" \ -d '{ "home_dir": "/new/path", "permissions": "{\"/\":[\"*\"]}" }' ``` --- ### 4. 刪除用戶 **端點**:`DELETE /api/v2/admin/users/:username` **認證**:需要 Bearer Token **請求**: ```bash curl -X DELETE http://localhost:11438/api/v2/admin/users/newuser \ -H "Authorization: Bearer YOUR_TOKEN_HERE" ``` --- ### 5. 修改密碼 **端點**:`POST /api/v2/admin/users/:username/password` **認證**:需要 Bearer Token **請求**: ```bash curl -X POST http://localhost:11438/api/v2/admin/users/demo/password \ -H "Authorization: Bearer YOUR_TOKEN_HERE" \ -H "Content-Type: application/json" \ -d '{ "new_password": "newpassword123" }' ``` --- ## 共享管理 API ### 列出所有共享 **端點**:`GET /api/v2/admin/shares` **認證**:無需認證(公開端點) **請求**: ```bash curl http://localhost:11438/api/v2/admin/shares ``` **響應**(200 OK): ```json [ { "name": "MarkBase Web", "port": 11438, "protocol": "HTTP/WebDAV", "status": "running", "url": "http://localhost:11438" }, { "name": "SFTP", "port": 2024, "protocol": "SSH/SFTP", "status": "running" }, { "name": "SMB", "port": 4445, "protocol": "SMB", "status": "running" } ] ``` --- ## 設置管理 API ### 1. 讀取設置 **端點**:`GET /api/v2/settings` **認證**:無需認證(公開端點) **請求**: ```bash curl http://localhost:11438/api/v2/settings ``` **響應**(200 OK): ```json { "success": true, "settings": { "server": { "upload_path": "/Users/accusys/momentry/var/sftpgo/data", "port": 11438 }, "database": { "type": "sqlite", "path": "data/auth.sqlite" }, "webdav": { "enabled": true, "parent_path": "/Users/accusys/momentry/var/sftpgo/data", "versioning": true }, "s3": { "enabled": false } } } ``` --- ### 2. 更新設置 **端點**:`POST /api/v2/settings` **認證**:無需認證(公開端點) **請求**: ```bash curl -X POST http://localhost:11438/api/v2/settings \ -H "Content-Type: application/json" \ -d '{ "server": { "upload_path": "/new/path" } }' ``` **成功響應**(200 OK): ```json { "success": true, "message": "Settings updated successfully" } ``` **錯誤響應**(400 Bad Request): ```json { "success": false, "message": "Path does not exist: /nonexistent/path" } ``` --- ### 3. 驗證設置 **端點**:`POST /api/v2/settings/validate` **認證**:無需認證(公開端點) **請求**: ```bash curl -X POST http://localhost:11438/api/v2/settings/validate \ -H "Content-Type: application/json" \ -d '{ "server": { "upload_path": "/path/to/validate" } }' ``` **成功響應**(路徑有效): ```json { "success": true, "valid": true, "message": "Path is valid: /path/to/validate", "details": { "path_exists": true, "path_readable": true, "path_writable": true, "path_is_directory": true, "absolute_path": "/path/to/validate" } } ``` **失敗響應**(路徑無效): ```json { "success": true, "valid": false, "message": "Path does not exist: /nonexistent/path", "details": { "path_exists": false, "path_readable": false, "path_writable": false, "path_is_directory": false, "absolute_path": "/nonexistent/path" } } ``` **驗證項目**: | 項目 | 說明 | |------|------| | **格式檢查** | 路徑不能為空,不能包含非法字符 | | **存在性** | 路徑必須存在 | | **類型檢查** | 必須是目錄,不能是文件 | | **讀權限** | 目錄必須可讀 | | **寫權限** | 目錄必須可寫 | **使用場景**: - 在保存前驗證用戶輸入的路徑 - 提供詳細的錯誤反饋 - 顯示路徑的絕對路徑 --- ## Studio 整合示例 ### 完整認證流程 ```javascript // 1. 登錄獲取 token async function login(username, password) { const response = await fetch('/api/v2/admin/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }); const data = await response.json(); if (response.ok) { // 存儲 token 到 localStorage localStorage.setItem('adminToken', data.token); localStorage.setItem('tokenExpires', data.expires_at); return data; } else { throw new Error(data.error); } } // 2. 使用 token 獲取用戶列表 async function getUsers() { const token = localStorage.getItem('adminToken'); if (!token) { throw new Error('Not authenticated'); } const response = await fetch('/api/v2/admin/users', { headers: { 'Authorization': `Bearer ${token}` } }); if (response.status === 401) { // Token 過期或無效,重新登錄 localStorage.removeItem('adminToken'); throw new Error('Token expired'); } return await response.json(); } // 3. 創建新用戶 async function createUser(userData) { const token = localStorage.getItem('adminToken'); const response = await fetch('/api/v2/admin/users', { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(userData) }); return await response.json(); } // 使用示例 (async () => { try { // 登錄 await login('demo', 'demo123'); console.log('Login successful'); // 獲取用戶列表 const users = await getUsers(); console.log(`Total users: ${users.total}`); // 創建新用戶 await createUser({ username: 'testuser', password: 'test123', home_dir: '/data/testuser', permissions: JSON.stringify({ '/': ['list', 'download'] }) }); console.log('User created'); } catch (error) { console.error('Error:', error.message); } })(); ``` --- ## 錯誤處理 ### 統一錯誤格式 **認證錯誤**: ```json { "ok": false, "error": "Invalid admin token" } ``` **操作錯誤**: ```json { "success": false, "message": "Error description" } ``` ### HTTP 狀態碼 | 狀態碼 | 說明 | |--------|------| | 200 | 成功 | | 201 | 創建成功 | | 400 | 請求錯誤 | | 401 | 未認證 | | 404 | 資源不存在 | | 500 | 服務器錯誤 | --- ## 測試帳號 **管理員帳號**: - Username: `demo` - Password: `demo123` - Permissions: 全部權限(`{"/*": ["*"]}`) --- ## API 端點總覽 | 端點 | 方法 | 認證 | 說明 | |------|------|------|------| | `/api/v2/admin/login` | POST | ❌ | 獲取 admin token | | `/api/v2/admin/verify` | GET | ✅ | 驗證 token | | `/api/v2/admin/users` | GET | ✅ | 用戶列表 | | `/api/v2/admin/users` | POST | ✅ | 創建用戶 | | `/api/v2/admin/users/:username` | PUT | ✅ | 更新用戶 | | `/api/v2/admin/users/:username` | DELETE | ✅ | 刪除用戶 | | `/api/v2/admin/users/:username/password` | POST | ✅ | 修改密碼 | | `/api/v2/admin/shares` | GET | ❌ | 共享列表 | | `/api/v2/settings` | GET | ❌ | 讀取設置 | | `/api/v2/settings` | POST | ❌ | 更新設置 | | `/api/v2/settings/validate` | POST | ❌ | 驗證設置 | --- ## 已知限制 1. **Token 存儲**:內存存儲,服務重啟後失效 2. **單會話**:每個用戶僅一個有效 token 3. **權限檢查**:僅檢查用戶狀態(status=1),無角色區分 --- ## 下一步改進 - [ ] 添加角色權限系統(admin/user) - [ ] Token 持久化(數據庫存儲) - [ ] 多會話支持 - [ ] Token 刷新機制 - [ ] 操作審計日誌 --- **MarkBase 團隊已準備好支持 Studio 整合。**