From d266c98e48ce95a0f42170f1db18d37b62d077a0 Mon Sep 17 00:00:00 2001 From: webdevcody Date: Mon, 19 Jan 2026 17:41:55 -0500 Subject: [PATCH] feat: add option to disable authentication for local/trusted networks - Implemented a mechanism to disable authentication when the environment variable AUTOMAKER_DISABLE_AUTH is set to 'true'. - Updated authMiddleware to bypass authentication checks for requests from trusted networks. - Modified getAuthStatus and isRequestAuthenticated functions to reflect the authentication status based on the new configuration. This enhancement allows for easier development and testing in trusted environments by simplifying access control. --- apps/server/src/lib/auth.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/apps/server/src/lib/auth.ts b/apps/server/src/lib/auth.ts index 1deef0db..60cb2d58 100644 --- a/apps/server/src/lib/auth.ts +++ b/apps/server/src/lib/auth.ts @@ -23,6 +23,13 @@ const SESSION_COOKIE_NAME = 'automaker_session'; const SESSION_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000; // 30 days const WS_TOKEN_MAX_AGE_MS = 5 * 60 * 1000; // 5 minutes for WebSocket connection tokens +/** + * Check if an environment variable is set to 'true' + */ +function isEnvTrue(envVar: string | undefined): boolean { + return envVar === 'true'; +} + // Session store - persisted to file for survival across server restarts const validSessions = new Map(); @@ -134,8 +141,8 @@ const API_KEY = ensureApiKey(); const BOX_CONTENT_WIDTH = 67; // Print API key to console for web mode users (unless suppressed for production logging) -if (process.env.AUTOMAKER_HIDE_API_KEY !== 'true') { - const autoLoginEnabled = process.env.AUTOMAKER_AUTO_LOGIN === 'true'; +if (!isEnvTrue(process.env.AUTOMAKER_HIDE_API_KEY)) { + const autoLoginEnabled = isEnvTrue(process.env.AUTOMAKER_AUTO_LOGIN); const autoLoginStatus = autoLoginEnabled ? 'enabled (auto-login active)' : 'disabled'; // Build box lines with exact padding @@ -375,6 +382,12 @@ function checkAuthentication( * 5. Session cookie (for web mode) */ export function authMiddleware(req: Request, res: Response, next: NextFunction): void { + // Allow disabling auth for local/trusted networks + if (isEnvTrue(process.env.AUTOMAKER_DISABLE_AUTH)) { + next(); + return; + } + const result = checkAuthentication( req.headers as Record, req.query as Record, @@ -420,9 +433,10 @@ export function isAuthEnabled(): boolean { * Get authentication status for health endpoint */ export function getAuthStatus(): { enabled: boolean; method: string } { + const disabled = isEnvTrue(process.env.AUTOMAKER_DISABLE_AUTH); return { - enabled: true, - method: 'api_key_or_session', + enabled: !disabled, + method: disabled ? 'disabled' : 'api_key_or_session', }; } @@ -430,6 +444,7 @@ export function getAuthStatus(): { enabled: boolean; method: string } { * Check if a request is authenticated (for status endpoint) */ export function isRequestAuthenticated(req: Request): boolean { + if (isEnvTrue(process.env.AUTOMAKER_DISABLE_AUTH)) return true; const result = checkAuthentication( req.headers as Record, req.query as Record, @@ -447,5 +462,6 @@ export function checkRawAuthentication( query: Record, cookies: Record ): boolean { + if (isEnvTrue(process.env.AUTOMAKER_DISABLE_AUTH)) return true; return checkAuthentication(headers, query, cookies).authenticated; }