|
| 1 | +const JSON_HEADERS = { |
| 2 | + 'Cache-Control': 'no-store', |
| 3 | + 'Content-Type': 'application/json; charset=utf-8', |
| 4 | +}; |
| 5 | + |
| 6 | +const DEFAULT_RETURN_PATH = '/auth/session'; |
| 7 | + |
| 8 | +function getAccessContext(request) { |
| 9 | + const email = request.headers.get('Cf-Access-Authenticated-User-Email'); |
| 10 | + const jwt = request.headers.get('Cf-Access-Jwt-Assertion'); |
| 11 | + |
| 12 | + return { |
| 13 | + authenticated: Boolean(email || jwt), |
| 14 | + email: email || '', |
| 15 | + hasJwtAssertion: Boolean(jwt), |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function getSafeReturnTo(requestUrl) { |
| 20 | + const returnTo = requestUrl.searchParams.get('returnTo'); |
| 21 | + |
| 22 | + if (!returnTo) return `${requestUrl.origin}${DEFAULT_RETURN_PATH}`; |
| 23 | + |
| 24 | + try { |
| 25 | + const absolute = new URL(returnTo, requestUrl.origin); |
| 26 | + if (absolute.protocol === 'http:' || absolute.protocol === 'https:') return absolute.toString(); |
| 27 | + } catch (e) { |
| 28 | + // no-op |
| 29 | + } |
| 30 | + |
| 31 | + return `${requestUrl.origin}${DEFAULT_RETURN_PATH}`; |
| 32 | +} |
| 33 | + |
| 34 | +function getCorsHeaders(request) { |
| 35 | + const origin = request.headers.get('Origin'); |
| 36 | + return origin ? { |
| 37 | + 'Access-Control-Allow-Credentials': 'true', |
| 38 | + 'Access-Control-Allow-Headers': 'Content-Type', |
| 39 | + 'Access-Control-Allow-Methods': 'GET, OPTIONS', |
| 40 | + 'Access-Control-Allow-Origin': origin, |
| 41 | + Vary: 'Origin', |
| 42 | + } : {}; |
| 43 | +} |
| 44 | + |
| 45 | +function json(request, body, init = {}) { |
| 46 | + return new Response(JSON.stringify(body, null, 2), { |
| 47 | + ...init, |
| 48 | + headers: { |
| 49 | + ...JSON_HEADERS, |
| 50 | + ...getCorsHeaders(request), |
| 51 | + ...(init.headers || {}), |
| 52 | + }, |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +function redirect(location) { |
| 57 | + const headers = { Location: location }; |
| 58 | + return new Response(null, { |
| 59 | + status: 302, |
| 60 | + headers, |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +export default { |
| 65 | + async fetch(request) { |
| 66 | + const url = new URL(request.url); |
| 67 | + const { pathname } = url; |
| 68 | + const access = getAccessContext(request); |
| 69 | + |
| 70 | + if (request.method === 'OPTIONS') { |
| 71 | + return new Response(null, { |
| 72 | + status: 204, |
| 73 | + headers: { |
| 74 | + ...getCorsHeaders(request), |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + if (request.method !== 'GET') { |
| 80 | + return json(request, { error: 'Method not allowed' }, { |
| 81 | + status: 405, |
| 82 | + headers: { Allow: 'GET' }, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + if (pathname === '/auth/login') { |
| 87 | + return redirect(getSafeReturnTo(url)); |
| 88 | + } |
| 89 | + |
| 90 | + if (pathname === '/auth/logout') { |
| 91 | + const logoutUrl = new URL('/cdn-cgi/access/logout', url.origin); |
| 92 | + return redirect(logoutUrl.toString()); |
| 93 | + } |
| 94 | + |
| 95 | + if (pathname === '/auth/session') { |
| 96 | + return json(request, { |
| 97 | + ...access, |
| 98 | + path: pathname, |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + return json(request, { |
| 103 | + error: 'Not found', |
| 104 | + availablePaths: ['/auth/login', '/auth/logout', '/auth/session'], |
| 105 | + }, { status: 404 }); |
| 106 | + }, |
| 107 | +}; |
0 commit comments