Files
hughhuangandClaude Sonnet 4.5 903dc1e61f refactor: 更新认证方式为密码验证
简化认证流程,移除用户名,只需要输入访问密码:

## 后端更改
- 移除 JWT_SECRET、ADMIN_USERNAME、ADMIN_PASSWORD 配置
- 添加 ACCESS_PASSWORD 配置
- 更新认证接口:POST /api/auth/verify-password
- 简化 JWT payload,只包含认证状态和时间戳
- 移除用户管理相关代码
- 更新认证中间件

## 前端更改
- 登录页面只显示密码输入框
- 移除用户名输入和相关状态
- 更新 Pinia store 为 verifyPassword 方法
- 移除 User 类型定义
- 更新布局和设置页面

## 文档更改
- 更新 README.md 说明新的认证方式
- 更新 DEVELOPMENT_GUIDE.md 环境变量配置
- 更新 API.md 认证接口文档
- 更新 .env.example 配置模板

## 安全改进
- 使用 ACCESS_PASSWORD 作为 JWT 签名密钥
- 自动清理过期会话(7天)
- 简化配置,更易于部署

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-02 23:04:49 +08:00

414 lines
7.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CC Cloud API 文档
本文档详细描述 CC Cloud 后端 API 接口。
## 基础信息
- **Base URL**: `http://localhost:3000/api`
- **认证方式**: JWT Bearer Token
- **Content-Type**: `application/json`
## 认证
### 验证密码
验证访问密码并获取令牌。
**请求**
```http
POST /auth/verify-password
Content-Type: application/json
{
"password": "string"
}
```
**响应**
```json
{
"success": true,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
```
### 验证 Token
验证当前 Token 是否有效。
**请求**
```http
GET /auth/verify
Authorization: Bearer <token>
```
**响应**
```json
{
"valid": true,
"authenticated": true
}
```
## 实例管理
### 创建实例
创建新的 Claude Code 实例。
**请求**
```http
POST /instances/start
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "string", // 实例名称
"config": {
"model": "string", // Claude 模型名称
"apiKey": "string", // API Key(可选)
"workingDir": "string", // 工作目录路径
"port": 4000, // 端口(可选,自动分配)
"maxMemory": 512, // 最大内存(MB,可选)
"env": { // 环境变量(可选)
"KEY": "value"
}
}
}
```
**响应**
```json
{
"instance": {
"id": "string",
"name": "string",
"status": "stopped",
"config": { ... },
"stats": {
"memory": 0,
"cpu": 0,
"restarts": 0,
"status": "offline"
},
"createdAt": "2024-01-01T00:00:00.000Z"
}
}
```
### 列出所有实例
获取所有实例列表。
**请求**
```http
GET /instances
Authorization: Bearer <token>
```
**响应**
```json
{
"instances": [
{
"id": "string",
"name": "string",
"status": "running",
"config": { ... },
"stats": { ... },
"createdAt": "2024-01-01T00:00:00.000Z",
"lastStarted": "2024-01-01T01:00:00.000Z"
}
]
}
```
### 获取实例详情
获取指定实例的详细信息。
**请求**
```http
GET /instances/:instanceId
Authorization: Bearer <token>
```
**响应**
```json
{
"instance": { ... }
}
```
### 启动实例
启动已停止的实例。
**请求**
```http
POST /instances/:instanceId/start
Authorization: Bearer <token>
```
**响应**
```json
{
"success": true,
"message": "实例启动成功"
}
```
### 停止实例
停止正在运行的实例。
**请求**
```http
POST /instances/:instanceId/stop
Authorization: Bearer <token>
```
**响应**
```json
{
"success": true,
"message": "实例停止成功"
}
```
### 删除实例
删除指定实例(会先停止实例)。
**请求**
```http
DELETE /instances/:instanceId
Authorization: Bearer <token>
```
**响应**
```json
{
"success": true,
"message": "实例删除成功"
}
```
### 获取实例状态
获取实例的实时状态信息。
**请求**
```http
GET /instances/:instanceId/status
Authorization: Bearer <token>
```
**响应**
```json
{
"status": "running",
"stats": {
"pid": 12345,
"port": 4000,
"uptime": 3600,
"memory": 134217728,
"cpu": 5.2,
"restarts": 0,
"status": "online"
}
}
```
## 数据模型
### Instance
实例对象结构:
```typescript
interface Instance {
id: string; // 实例 ID (UUID)
name: string; // 实例名称
status: 'running' | 'stopped' | 'errored' | 'offline';
config: InstanceConfig; // 实例配置
stats: InstanceStats; // 实时统计
createdAt: Date; // 创建时间
lastStarted?: Date; // 最后启动时间
}
```
### InstanceConfig
实例配置:
```typescript
interface InstanceConfig {
model: string; // Claude 模型名称
apiKey?: string; // Anthropic API Key(可选)
workingDir: string; // 工作目录路径
port?: number; // HTTP 服务端口
maxMemory?: number; // 最大内存限制(MB)
env?: Record<string, string>; // 环境变量
}
```
### InstanceStats
实例统计信息:
```typescript
interface InstanceStats {
pid?: number; // 进程 ID
port?: number; // HTTP 服务端口
uptime?: number; // 运行时长(秒)
memory: number; // 内存使用(字节)
cpu: number; // CPU 使用率(%)
restarts: number; // 重启次数
status: 'online' | 'offline'; // 进程状态
}
```
## 错误响应
所有错误响应遵循统一格式:
```json
{
"error": "错误描述",
"message": "详细错误信息(可选)"
}
```
### HTTP 状态码
- `200` - 成功
- `201` - 创建成功
- `400` - 请求错误
- `401` - 未授权
- `404` - 资源不存在
- `500` - 服务器错误
## WebSocket 事件
### 连接
```javascript
import { io } from 'socket.io-client';
const socket = io('http://localhost:3000', {
auth: {
token: 'your-jwt-token'
}
});
```
### 加入实例房间
```javascript
socket.emit('join-instance', 'instance-id');
```
### 离开实例房间
```javascript
socket.emit('leave-instance', 'instance-id');
```
### 监听事件(待实现)
```javascript
// 实例状态变化
socket.on('instance-status-changed', (data) => {
console.log('Status:', data.status);
});
// 日志更新
socket.on('log-update', (data) => {
console.log('Log:', data.log);
});
// 监控数据更新
socket.on('monitor-update', (data) => {
console.log('Stats:', data.stats);
});
```
## 使用示例
### 创建并启动实例
```bash
# 1. 登录获取 token
TOKEN=$(curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}' \
| jq -r '.token')
# 2. 创建实例
INSTANCE=$(curl -X POST http://localhost:3000/api/instances/start \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "My Instance",
"config": {
"model": "claude-sonnet-4-5",
"workingDir": "/path/to/project"
}
}' | jq -r '.instance.id')
# 3. 启动实例
curl -X POST http://localhost:3000/api/instances/$INSTANCE/start \
-H "Authorization: Bearer $TOKEN"
# 4. 查看状态
curl http://localhost:3000/api/instances/$INSTANCE/status \
-H "Authorization: Bearer $TOKEN"
```
### JavaScript 示例
```javascript
import axios from 'axios';
const API = 'http://localhost:3000/api';
let token = '';
// 登录
async function login(username, password) {
const response = await axios.post(`${API}/auth/login`, {
username,
password
});
token = response.data.token;
return response.data;
}
// 创建实例
async function createInstance(name, config) {
const response = await axios.post(`${API}/instances/start`, {
name,
config
}, {
headers: { Authorization: `Bearer ${token}` }
});
return response.data.instance;
}
// 启动实例
async function startInstance(instanceId) {
const response = await axios.post(`${API}/instances/${instanceId}/start`, {}, {
headers: { Authorization: `Bearer ${token}` }
});
return response.data;
}
// 使用示例
await login('admin', 'admin123');
const instance = await createInstance('My Instance', {
model: 'claude-sonnet-4-5',
workingDir: '/path/to/project'
});
await startInstance(instance.id);
```