Patch Changes
-
#812
0f64363Thanks @omeraplak! - feat: comprehensive authentication system with JWT, Console Access, and WebSocket supportThe Problem
VoltAgent's authentication system had several critical gaps that made it difficult to secure production deployments:
- No Authentication Support: The framework lacked built-in authentication, forcing developers to implement their own security
- WebSocket Security: WebSocket connections for observability had no authentication, exposing sensitive telemetry data
- Browser Limitations: Browsers cannot send custom headers during WebSocket handshake, making authentication impossible
- Development vs Production: No clear separation between development convenience and production security
- Console Access: No secure way for the VoltAgent Console to access observability endpoints in production
The Solution
JWT Authentication (
@voltagent/server-core,@voltagent/server-hono):- Added pluggable
jwtAuthprovider with configurable secret and options - Implemented
mapUserfunction to transform JWT payloads into user objects - Created flexible route protection with
defaultPrivatemode (opt-out vs opt-in) - Added
publicRoutesconfiguration for fine-grained control
WebSocket Authentication:
- Implemented query parameter authentication for browser WebSocket connections
- Added dual authentication support (headers for servers, query params for browsers)
- Created WebSocket-specific authentication helpers for observability endpoints
- Preserved user context throughout WebSocket connection lifecycle
Console Access System:
- Introduced
VOLTAGENT_CONSOLE_ACCESS_KEYenvironment variable for production Console access - Added
x-console-access-keyheader support for HTTP requests - Implemented query parameter
?key=for WebSocket connections - Created
hasConsoleAccess()utility for unified access checking
Development Experience:
- Enhanced
x-voltagent-devheader to work with both HTTP and WebSocket - Added
isDevRequest()helper that requires both header AND non-production environment - Implemented query parameter
?dev=truefor browser WebSocket connections - Maintained zero-config development mode while ensuring production security
Route Matching Improvements:
- Added wildcard support with
/observability/*pattern for all observability endpoints - Implemented double-star pattern
/api/**for path and all children - Enhanced
pathMatches()function with proper segment matching - Protected all observability, workflow control, and system update endpoints by default
Impact
- ✅ Production Ready: Complete authentication system for securing VoltAgent deployments
- ✅ WebSocket Security: Browser-compatible authentication for real-time observability
- ✅ Console Integration: Secure access for VoltAgent Console in production environments
- ✅ Developer Friendly: Zero-config development with automatic authentication bypass
- ✅ Flexible Security: Choose between opt-in (default) or opt-out authentication modes
- ✅ User Context: Automatic user injection into agent and workflow execution context
Technical Details
Protected Routes (Default):
// Agent/Workflow Execution POST /agents/:id/text POST /agents/:id/stream POST /workflows/:id/run // All Observability Endpoints /observability/* // Traces, logs, memory - all methods // Workflow Control POST /workflows/:id/executions/:executionId/suspend POST /workflows/:id/executions/:executionId/resume // System Updates GET /updates POST /updates/:packageName
Authentication Modes:
// Opt-in mode (default) - Only execution endpoints protected auth: jwtAuth({ secret: process.env.JWT_SECRET, }); // Opt-out mode - Everything protected except specified routes auth: jwtAuth({ secret: process.env.JWT_SECRET, defaultPrivate: true, publicRoutes: ["GET /health", "POST /webhooks/*"], });
WebSocket Authentication Flow:
// Browser WebSocket with query params new WebSocket("ws://localhost:3000/ws/observability?key=console-key"); new WebSocket("ws://localhost:3000/ws/observability?dev=true"); // Server WebSocket with headers ws.connect({ headers: { "x-console-access-key": "console-key", "x-voltagent-dev": "true", }, });
Migration Notes
For Existing Users:
-
No Breaking Changes: Authentication is optional. Existing deployments continue to work without configuration.
-
To Enable Authentication:
import { jwtAuth } from "@voltagent/server-hono"; new VoltAgent({ server: honoServer({ auth: jwtAuth({ secret: process.env.JWT_SECRET, }), }), });
-
For Production Console:
# .env VOLTAGENT_CONSOLE_ACCESS_KEY=your-secure-key NODE_ENV=production -
Generate Secrets:
# JWT Secret openssl rand -hex 32 # Console Access Key openssl rand -hex 32
-
Test Token Generation:
// generate-token.js import jwt from "jsonwebtoken"; const token = jwt.sign({ id: "user-1", email: "test@example.com" }, process.env.JWT_SECRET, { expiresIn: "24h", }); console.log(token);
Documentation
Comprehensive authentication documentation has been added to
/website/docs/api/authentication.mdcovering:- Getting started with three authentication options
- Common use cases with code examples
- Advanced configuration with
mapUserfunction - Console and observability authentication
- Security best practices
- Troubleshooting guide
-
Updated dependencies [
0f64363]:- @voltagent/server-core@1.0.26
- @voltagent/core@1.2.9