Skip to content

Commit 1f4cd16

Browse files
committed
fix: change agent mode for platform
1 parent 09688a0 commit 1f4cd16

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

server/routes/agent.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,43 @@ import path from 'path';
44
import os from 'os';
55
import { promises as fs } from 'fs';
66
import crypto from 'crypto';
7-
import { apiKeysDb, githubTokensDb } from '../database/db.js';
7+
import { userDb, apiKeysDb, githubTokensDb } from '../database/db.js';
88
import { addProjectManually } from '../projects.js';
99
import { queryClaudeSDK } from '../claude-sdk.js';
1010
import { spawnCursor } from '../cursor-cli.js';
1111
import { Octokit } from '@octokit/rest';
1212

1313
const router = express.Router();
1414

15-
// Middleware to validate API key for external requests
15+
/**
16+
* Middleware to authenticate agent API requests.
17+
*
18+
* Supports two authentication modes:
19+
* 1. Platform mode (VITE_IS_PLATFORM=true): For managed/hosted deployments where
20+
* authentication is handled by an external proxy. Requests are trusted and
21+
* the default user context is used.
22+
*
23+
* 2. API key mode (default): For self-hosted deployments where users authenticate
24+
* via API keys created in the UI. Keys are validated against the local database.
25+
*/
1626
const validateExternalApiKey = (req, res, next) => {
27+
// Platform mode: Authentication is handled externally (e.g., by a proxy layer).
28+
// Trust the request and use the default user context.
29+
if (process.env.VITE_IS_PLATFORM === 'true') {
30+
try {
31+
const user = userDb.getFirstUser();
32+
if (!user) {
33+
return res.status(500).json({ error: 'Platform mode: No user found in database' });
34+
}
35+
req.user = user;
36+
return next();
37+
} catch (error) {
38+
console.error('Platform mode error:', error);
39+
return res.status(500).json({ error: 'Platform mode: Failed to fetch user' });
40+
}
41+
}
42+
43+
// Self-hosted mode: Validate API key from header or query parameter
1744
const apiKey = req.headers['x-api-key'] || req.query.apiKey;
1845

1946
if (!apiKey) {

0 commit comments

Comments
 (0)