|
| 1 | +import { verifyProxyAuth } from 'shared'; |
| 2 | +import type { Env } from 'shared'; |
| 3 | +import { AUTH_REQUIRED_HTML, SERVER_ERROR_HTML } from './templates'; |
| 4 | + |
| 5 | +type AuthMode = 'enforce' | 'log-only' | 'disabled'; |
| 6 | + |
| 7 | +function getAuthMode(request: Request, env: Env): AuthMode { |
| 8 | + const url = new URL(request.url); |
| 9 | + |
| 10 | + // Check query parameter first (for easy testing) |
| 11 | + const queryMode = url.searchParams.get('proxy_auth'); |
| 12 | + if ( |
| 13 | + queryMode === 'enforce' || |
| 14 | + queryMode === 'log-only' || |
| 15 | + queryMode === 'disabled' |
| 16 | + ) { |
| 17 | + return queryMode; |
| 18 | + } |
| 19 | + |
| 20 | + if (env.PROXY_AUTH_MODE) { |
| 21 | + return env.PROXY_AUTH_MODE; |
| 22 | + } |
| 23 | + |
| 24 | + return 'log-only'; |
| 25 | +} |
| 26 | + |
| 27 | +function shouldSkipAuth(request: Request, env: Env): boolean { |
| 28 | + const url = new URL(request.url); |
| 29 | + |
| 30 | + if (url.hostname === 'localhost' || url.hostname === '127.0.0.1') { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + return false; |
| 35 | +} |
| 36 | + |
| 37 | +export async function onRequest(context: { |
| 38 | + request: Request; |
| 39 | + env: Env; |
| 40 | + next: () => Promise<Response>; |
| 41 | +}): Promise<Response> { |
| 42 | + const { request, env, next } = context; |
| 43 | + const url = new URL(request.url); |
| 44 | + |
| 45 | + try { |
| 46 | + const authMode = getAuthMode(request, env); |
| 47 | + console.log(`[Proxy Auth] Mode: ${authMode}, URL: ${url.pathname}`); |
| 48 | + |
| 49 | + if (shouldSkipAuth(request, env)) { |
| 50 | + console.log('[Proxy Auth] Bypassed for development/testing'); |
| 51 | + return await next(); |
| 52 | + } |
| 53 | + |
| 54 | + if (authMode === 'disabled') { |
| 55 | + console.log('[Proxy Auth] Authentication disabled, allowing request'); |
| 56 | + return await next(); |
| 57 | + } |
| 58 | + |
| 59 | + const proxyToken = await verifyProxyAuth(request, env); |
| 60 | + |
| 61 | + if (!proxyToken) { |
| 62 | + console.log('[Proxy Auth] Validation failed - no valid token'); |
| 63 | + |
| 64 | + // In log-only mode, allow the request but log the failure |
| 65 | + if (authMode === 'log-only') { |
| 66 | + console.log( |
| 67 | + '[Proxy Auth] Log-only mode: allowing request despite auth failure', |
| 68 | + ); |
| 69 | + return await next(); |
| 70 | + } |
| 71 | + |
| 72 | + console.log('[Proxy Auth] Enforce mode: blocking request'); |
| 73 | + return new Response(AUTH_REQUIRED_HTML, { |
| 74 | + status: 401, |
| 75 | + headers: { |
| 76 | + 'Content-Type': 'text/html', |
| 77 | + 'Cache-Control': 'no-cache, no-store, must-revalidate', |
| 78 | + }, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + console.log( |
| 83 | + `[Proxy Auth] Validation success for user ${proxyToken.user_id} (app: ${proxyToken.application_id})`, |
| 84 | + ); |
| 85 | + |
| 86 | + return await next(); |
| 87 | + } catch (error) { |
| 88 | + console.error('[Proxy Auth] Middleware error:', error); |
| 89 | + |
| 90 | + // In log-only mode, allow the request despite errors |
| 91 | + const authMode = getAuthMode(request, env); |
| 92 | + if (authMode === 'log-only') { |
| 93 | + console.log('[Proxy Auth] Log-only mode: allowing request despite error'); |
| 94 | + return await next(); |
| 95 | + } |
| 96 | + return new Response(SERVER_ERROR_HTML, { |
| 97 | + status: 500, |
| 98 | + headers: { |
| 99 | + 'Content-Type': 'text/html', |
| 100 | + 'Cache-Control': 'no-cache, no-store, must-revalidate', |
| 101 | + }, |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
0 commit comments