MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / hasValidSession

Function hasValidSession

telemetry-dashboard/src/auth.ts:109–140  ·  view source on GitHub ↗
(env: Env, request: Request)

Source from the content-addressed store, hash-verified

107}
108
109export async function hasValidSession(env: Env, request: Request): Promise<boolean> {
110 if (!env.ADMIN_PASSWORD || !env.SESSION_SECRET) return false;
111
112 const token = readSessionCookie(request);
113 if (!token) return false;
114
115 const dot = token.indexOf('.');
116 if (dot <= 0 || dot === token.length - 1) return false;
117 const encoded = token.slice(0, dot);
118
119 const provided = base64UrlDecode(token.slice(dot + 1));
120 if (!provided) return false;
121 const expected = await sign(env.SESSION_SECRET, encoded);
122 // Length is checked first: timingSafeEqual throws on mismatched lengths, and
123 // the length of an HMAC-SHA256 tag is public anyway.
124 if (provided.byteLength !== expected.byteLength) return false;
125 if (!crypto.subtle.timingSafeEqual(provided, expected)) return false;
126
127 const raw = base64UrlDecode(encoded);
128 if (!raw) return false;
129 let payload: SessionPayload;
130 try {
131 payload = JSON.parse(decoder.decode(raw)) as SessionPayload;
132 } catch {
133 return false;
134 }
135
136 if (payload?.v !== SESSION_VERSION) return false;
137 if (typeof payload.exp !== 'number' || payload.exp <= Math.floor(Date.now() / 1000)) return false;
138 // Rotating ADMIN_PASSWORD signs everyone out.
139 return payload.pw === (await passwordFingerprint(env.ADMIN_PASSWORD));
140}
141
142export function readSessionCookie(request: Request): string | null {
143 const header = request.headers.get('cookie');

Callers 2

handleLoginPageFunction · 0.90
fetchFunction · 0.90

Calls 5

readSessionCookieFunction · 0.85
base64UrlDecodeFunction · 0.85
signFunction · 0.85
passwordFingerprintFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected