MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / verifySealedSessionLocally

Function verifySealedSessionLocally

apps/cloud/src/auth/workos.ts:232–325  ·  view source on GitHub ↗
(
  sessionData: string,
  cookiePassword: string,
  jwks: CachedRemoteJWKSet,
)

Source from the content-addressed store, hash-verified

230 );
231
232const verifySealedSessionLocally = (
233 sessionData: string,
234 cookiePassword: string,
235 jwks: CachedRemoteJWKSet,
236): Effect.Effect<LocalSessionVerification, ServiceAdapterError> =>
237 Effect.gen(function* () {
238 // Phase timings, not just child spans. `local_verify` is a leaf in
239 // production traces, so a ~3.3s verify has nothing under it to blame —
240 // and it stayed 3.3s after the JWKS fetch was eliminated entirely
241 // (jwks.fetch_count == 0), so the cost is one of the phases below. Under
242 // workerd `Date.now()` only advances at I/O boundaries, which is exactly
243 // what makes a raw span duration misleading here: recording each phase
244 // explicitly says which await the wall-clock actually crossed.
245 const verifyStartedAt = Date.now();
246
247 const unsealStartedAt = Date.now();
248 const unsealed = yield* Effect.tryPromise({
249 try: () => unsealWorkOSSession(sessionData, cookiePassword),
250 catch: (cause) => new LocalSessionCookieError({ cause }),
251 }).pipe(
252 Effect.catchTag("LocalSessionCookieError", () => Effect.succeed(null as unknown | null)),
253 Effect.withSpan("workos.session.unseal"),
254 );
255 const unsealMs = Date.now() - unsealStartedAt;
256 yield* Effect.annotateCurrentSpan({ "verify.unseal_ms": unsealMs });
257 if (!unsealed) return { _tag: "InvalidCookie" };
258
259 const decodeStartedAt = Date.now();
260 const session = Option.match(decodeSealedSessionPayload(unsealed), {
261 onNone: (): SealedSessionPayload | null => null,
262 onSome: (payload) => payload,
263 });
264 yield* Effect.annotateCurrentSpan({ "verify.decode_ms": Date.now() - decodeStartedAt });
265 if (!session) return { _tag: "InvalidCookie" };
266
267 // Snapshot the JWKS cache around the verify so the `local_verify` span
268 // says whether THIS verify was a warm-cache signature check or paid for a
269 // live upstream JWKS fetch. The Aug 2026 latency regression was the cache
270 // silently missing on most verifies, and no span attribute distinguished
271 // the two paths.
272 const jwksBefore = jwks.inspect();
273 // Entry-state annotation goes on BEFORE the verify so a failing verify
274 // (the case worth debugging) still records whether the cache was warm.
275 yield* Effect.annotateCurrentSpan({
276 "jwks.cache_populated_at_start": jwksBefore.hasJwks,
277 ...(jwksBefore.fetchedAt === null
278 ? {}
279 : { "jwks.cache_age_ms": Date.now() - jwksBefore.fetchedAt }),
280 });
281 const jwtStartedAt = Date.now();
282 const verified = yield* verifyJwtWithRefreshRetry(session.accessToken, jwks).pipe(
283 Effect.withSpan("workos.session.jwt_verify"),
284 Effect.onExit(() => {
285 const jwksAfter = jwks.inspect();
286 const finishedAt = Date.now();
287 return Effect.annotateCurrentSpan({
288 "verify.jwt_ms": finishedAt - jwtStartedAt,
289 "verify.total_ms": finishedAt - verifyStartedAt,

Callers 1

Calls 2

unsealWorkOSSessionFunction · 0.85

Tested by

no test coverage detected