-
-
Notifications
You must be signed in to change notification settings - Fork 427
feat: add authNext and deprecate legacy auth #883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| --- | ||
| "@voltagent/server-core": patch | ||
| "@voltagent/server-hono": patch | ||
| --- | ||
|
|
||
| feat: add authNext and deprecate legacy auth | ||
|
|
||
| Add a new `authNext` policy that splits routes into public, console, and user access. All routes are protected by default; use `publicRoutes` to opt out. | ||
|
|
||
| AuthNext example: | ||
|
|
||
| ```ts | ||
| import { jwtAuth } from "@voltagent/server-core"; | ||
| import { honoServer } from "@voltagent/server-hono"; | ||
|
|
||
| const server = honoServer({ | ||
| authNext: { | ||
| provider: jwtAuth({ secret: process.env.JWT_SECRET! }), | ||
| publicRoutes: ["GET /health"], | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Behavior summary: | ||
|
|
||
| - When `authNext` is set, all routes are private by default. | ||
| - Console endpoints (agents, workflows, tools, docs, observability, updates) require a Console Access Key. | ||
| - Execution endpoints require a user token (JWT). | ||
|
|
||
| Console access uses `VOLTAGENT_CONSOLE_ACCESS_KEY`: | ||
|
|
||
| ```bash | ||
| VOLTAGENT_CONSOLE_ACCESS_KEY=your-console-key | ||
| ``` | ||
|
|
||
| ```bash | ||
| curl http://localhost:3141/agents \ | ||
| -H "x-console-access-key: your-console-key" | ||
| ``` | ||
|
|
||
| Legacy `auth` remains supported but is deprecated. Use `authNext` for new integrations. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { resolveAuthNextAccess } from "./next"; | ||
| import type { AuthProvider } from "./types"; | ||
|
|
||
| const mockProvider: AuthProvider = { | ||
| type: "jwt", | ||
| verifyToken: async () => ({ id: "user-1" }), | ||
| publicRoutes: ["GET /provider-public"], | ||
| }; | ||
|
|
||
| describe("resolveAuthNextAccess", () => { | ||
| it("treats explicit publicRoutes as public", () => { | ||
| const config = { provider: mockProvider, publicRoutes: ["GET /health"] }; | ||
| expect(resolveAuthNextAccess("GET", "/health", config)).toBe("public"); | ||
| }); | ||
|
|
||
| it("treats provider publicRoutes as public", () => { | ||
| const config = { provider: mockProvider }; | ||
| expect(resolveAuthNextAccess("GET", "/provider-public", config)).toBe("public"); | ||
| }); | ||
|
|
||
| it("treats default console routes as console", () => { | ||
| const config = { provider: mockProvider }; | ||
| expect(resolveAuthNextAccess("GET", "/agents", config)).toBe("console"); | ||
| }); | ||
|
|
||
| it("treats non-console routes as user", () => { | ||
| const config = { provider: mockProvider }; | ||
| expect(resolveAuthNextAccess("POST", "/agents/test-agent/text", config)).toBe("user"); | ||
| }); | ||
|
|
||
| it("treats websocket test connection as console", () => { | ||
| const config = { provider: mockProvider }; | ||
| expect(resolveAuthNextAccess("WS", "/ws", config)).toBe("console"); | ||
| }); | ||
|
|
||
| it("allows publicRoutes to override console routes", () => { | ||
| const config = { provider: mockProvider, publicRoutes: ["GET /agents"] }; | ||
| expect(resolveAuthNextAccess("GET", "/agents", config)).toBe("public"); | ||
| }); | ||
|
|
||
| it("uses custom consoleRoutes when provided", () => { | ||
| const config = { provider: mockProvider, consoleRoutes: ["GET /custom-console"] }; | ||
| expect(resolveAuthNextAccess("GET", "/custom-console", config)).toBe("console"); | ||
| expect(resolveAuthNextAccess("GET", "/agents", config)).toBe("user"); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { DEFAULT_CONSOLE_ROUTES, pathMatches } from "./defaults"; | ||
| import type { AuthProvider } from "./types"; | ||
|
|
||
| export type AuthNextAccess = "public" | "console" | "user"; | ||
|
|
||
| export interface AuthNextRoutesConfig { | ||
| publicRoutes?: string[]; | ||
| consoleRoutes?: string[]; | ||
| } | ||
|
|
||
| export interface AuthNextConfig<TRequest = any> extends AuthNextRoutesConfig { | ||
| provider: AuthProvider<TRequest>; | ||
| } | ||
|
|
||
| export function isAuthNextConfig<TRequest>( | ||
| value: AuthProvider<TRequest> | AuthNextConfig<TRequest>, | ||
| ): value is AuthNextConfig<TRequest> { | ||
| return typeof (value as AuthNextConfig<TRequest>).provider !== "undefined"; | ||
| } | ||
|
|
||
| export function normalizeAuthNextConfig<TRequest>( | ||
| value: AuthProvider<TRequest> | AuthNextConfig<TRequest>, | ||
| ): AuthNextConfig<TRequest> { | ||
| return isAuthNextConfig(value) ? value : { provider: value }; | ||
| } | ||
|
|
||
| function routeMatches(method: string, path: string, routePattern: string): boolean { | ||
| const parts = routePattern.split(" "); | ||
| if (parts.length === 2) { | ||
| const [routeMethod, routePath] = parts; | ||
| if (method.toUpperCase() !== routeMethod.toUpperCase()) { | ||
| return false; | ||
| } | ||
| return pathMatches(path, routePath); | ||
| } | ||
|
|
||
| return pathMatches(path, routePattern); | ||
| } | ||
|
|
||
| function matchesAnyRoute(method: string, path: string, routes?: string[]): boolean { | ||
| if (!routes || routes.length === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| return routes.some((route) => routeMatches(method, path, route)); | ||
| } | ||
|
|
||
| export function resolveAuthNextAccess<TRequest>( | ||
| method: string, | ||
| path: string, | ||
| authNext: AuthNextConfig<TRequest> | AuthProvider<TRequest>, | ||
| ): AuthNextAccess { | ||
| const config = normalizeAuthNextConfig(authNext); | ||
| const publicRoutes = [...(config.publicRoutes ?? []), ...(config.provider.publicRoutes ?? [])]; | ||
|
|
||
| if (matchesAnyRoute(method, path, publicRoutes)) { | ||
| return "public"; | ||
| } | ||
|
|
||
| const consoleRoutes = config.consoleRoutes ?? DEFAULT_CONSOLE_ROUTES; | ||
| if (matchesAnyRoute(method, path, consoleRoutes)) { | ||
| return "console"; | ||
| } | ||
|
|
||
| return "user"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,9 @@ export function isDevRequest(req: Request): boolean { | |
| * // Production with console key | ||
| * NODE_ENV=production + x-console-access-key=valid-key → true | ||
| * | ||
| * // Production with console key in query param | ||
| * NODE_ENV=production + ?key=valid-key → true | ||
| * | ||
| * // Production without key | ||
| * NODE_ENV=production + no key → false | ||
| * | ||
|
|
@@ -68,9 +71,11 @@ export function hasConsoleAccess(req: Request): boolean { | |
|
|
||
| // 2. Console Access Key check (for production) | ||
| const consoleKey = req.headers.get("x-console-access-key"); | ||
| const url = new URL(req.url, "http://localhost"); | ||
| const queryKey = url.searchParams.get("key"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Security: Passing access keys in URL query parameters exposes credentials to logging, browser history, Referer headers, and proxy caches. Sensitive authentication tokens should only be transmitted via headers (like the existing Prompt for AI agents |
||
| const configuredKey = process.env.VOLTAGENT_CONSOLE_ACCESS_KEY; | ||
|
|
||
| if (configuredKey && consoleKey === configuredKey) { | ||
| if (configuredKey && (consoleKey === configuredKey || queryKey === configuredKey)) { | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Inconsistent route configuration behavior:
publicRoutesmerges config with provider routes, butconsoleRoutescompletely replaces defaults when provided. If a user setsconsoleRoutes: ["/my-admin"], they'll lose all DEFAULT_CONSOLE_ROUTES (like "WS /ws", "GET /api/logs", etc.). Consider merging instead:const consoleRoutes = [...DEFAULT_CONSOLE_ROUTES, ...(config.consoleRoutes ?? [])];Prompt for AI agents