@@ -4,16 +4,43 @@ import path from 'path';
44import os from 'os' ;
55import { promises as fs } from 'fs' ;
66import crypto from 'crypto' ;
7- import { apiKeysDb , githubTokensDb } from '../database/db.js' ;
7+ import { userDb , apiKeysDb , githubTokensDb } from '../database/db.js' ;
88import { addProjectManually } from '../projects.js' ;
99import { queryClaudeSDK } from '../claude-sdk.js' ;
1010import { spawnCursor } from '../cursor-cli.js' ;
1111import { Octokit } from '@octokit/rest' ;
1212
1313const 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+ */
1626const 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