Worker flow
Implementation: apps/cosmos/digital-services/workers/auth-gateway.js, image-optimizer.js, and wrangler.*.toml.
GET /api/auth/session
Section titled “GET /api/auth/session”- Browser calls
GET /api/auth/session(with credentials / Access cookies as applicable). - Gateway reads
cf-access-jwt-assertion, orCF_Authorization/cf_authorizationcookies, orAuthorization: Bearer. - Gateway
POSTs a JSON body{ token, tokenType }toAUTH_WORKER_VERIFY_URL(default in code:https://auth.cosmos-platform.com/api/v1/verify; overridden per env inwrangler.gateway.toml). - If the auth worker returns
successwith auser, gateway responds with JSONsuccess: trueand a public user object (id, email, name, roles, permissions). - On failure, gateway returns JSON
success: falsewith an appropriate status (401, 502, etc.).
GET /api/auth/logout
Section titled “GET /api/auth/logout”- Browser calls
GET /api/auth/logout. - Gateway responds with 302 to
/cdn-cgi/access/logouton the same origin so Cloudflare Access clears the session.
Protected routes
Section titled “Protected routes”- Request path matches
PROTECTED_PATH_PREFIXES(default/portal,/admin; comma-separated in Wrangler). - Gateway runs the same verification as session. If verification fails, 302 to
/login?redirect=<original path>. - If verification succeeds, gateway
fetch(request)continues to the static site (workersitebucket).
Wrangler routes must include the host patterns for /portal* and /admin* (see workers/wrangler.gateway.toml); otherwise the worker never runs and protection is ineffective.
Image optimization
Section titled “Image optimization”- Routes are declared in
wrangler.image.tomlby file extension (e.g.*.jpg,*.webp) per hostname/environment. image-optimizer.jscallshandleImageResizefrom@cosmos/edge-utils/image-optimizerwhen the path matches an image extension and the query string includeswidth; otherwise it falls through withfetch(request).ENABLE_IMAGE_RESIZINGinwrangler.image.tomlis the worker-side feature toggle string.
Fallback and /api/*
Section titled “Fallback and /api/*”- After auth session/logout handling and protected-path checks, paths starting with
/api/are handled byhandleApiRequestimported from@cosmos/edge-utils/image-optimizer(shared package behavior—not Astro routes). - Other requests
return fetch(request)to the static asset pipeline.
Source anchors
Section titled “Source anchors”apps/cosmos/digital-services/workers/auth-gateway.jsapps/cosmos/digital-services/workers/image-optimizer.jsapps/cosmos/digital-services/workers/wrangler.gateway.tomlapps/cosmos/digital-services/workers/wrangler.image.toml