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

Function handler

apps/cloud/src/auth/admin-mfa-routes.ts:29–235  ·  view source on GitHub ↗
(action: "status" | "start" | "verify" | "cancel" | "lock")

Source from the content-addressed store, hash-verified

27 HttpServerResponse.jsonUnsafe(body, { status, headers: { "cache-control": "no-store" } });
28
29const handler = (action: "status" | "start" | "verify" | "cancel" | "lock") =>
30 Effect.gen(function* () {
31 const request = yield* HttpServerRequest.HttpServerRequest;
32 const webRequest = yield* HttpServerRequest.toWeb(request);
33 if (action !== "status" && request.headers.origin !== new URL(webRequest.url).origin) {
34 return json({ message: "This request must come from Executor." }, 403);
35 }
36 const workos = yield* WorkOSClient;
37 const session = yield* workos.authenticateRequest(webRequest);
38 if (!session) return json({ message: "Sign in to continue." }, 401);
39 const response = yield* Effect.gen(function* () {
40 const selector = request.headers[ORG_SELECTOR_HEADER];
41 const org = selector ? yield* authorizeOrganizationSelector(session.userId, selector) : null;
42 if (!org) return json({ message: "Select an organization to continue." }, 403);
43 if (action === "status") {
44 const proof = yield* readAdminMfaProof(
45 env.WORKOS_COOKIE_PASSWORD,
46 { userId: session.userId, sessionId: session.sessionId },
47 "verified",
48 request.cookies[ADMIN_MFA_COOKIE],
49 yield* Clock.currentTimeMillis,
50 );
51 return json(
52 proof !== null ? { state: "verified", expiresAt: proof.exp } : { state: "required" },
53 );
54 }
55
56 if (action === "lock") {
57 return json({ canceled: true }).pipe(
58 HttpServerResponse.setCookieUnsafe(ADMIN_MFA_COOKIE, "", {
59 ...cookieOptions,
60 maxAge: Duration.seconds(0),
61 }),
62 HttpServerResponse.setCookieUnsafe(ADMIN_MFA_CHALLENGE_COOKIE, "", {
63 ...cookieOptions,
64 maxAge: Duration.seconds(0),
65 }),
66 );
67 }
68 if (action === "cancel") {
69 return HttpServerResponse.setCookieUnsafe(
70 json({ canceled: true }),
71 ADMIN_MFA_CHALLENGE_COOKIE,
72 "",
73 {
74 ...cookieOptions,
75 maxAge: Duration.seconds(0),
76 },
77 );
78 }
79
80 // Applies across new challenges too, so starting over cannot reset the attempt budget.
81 const rateLimit = env.ADMIN_MFA_RATE_LIMITER;
82 if (!rateLimit) return json({ message: "Verification is temporarily unavailable." }, 503);
83 const allowed = yield* Effect.tryPromise({
84 try: () => rateLimit.limit({ key: session.userId }),
85 catch: (cause) => new RateLimitError({ cause }),
86 });

Callers 15

installFetchFunction · 0.50
configureServerFunction · 0.50
fetchFunction · 0.50
configureServerFunction · 0.50
boot.test.tsFile · 0.50
getFunction · 0.50
signUpFunction · 0.50

Calls 7

readAdminMfaProofFunction · 0.90
signAdminMfaProofFunction · 0.90
limitMethod · 0.80
decodeMethod · 0.80
jsonFunction · 0.70
setMethod · 0.65

Tested by 15

installFetchFunction · 0.40
getFunction · 0.40
signUpFunction · 0.40
signUpFunction · 0.40
orgIdOfFunction · 0.40
addIntegrationFunction · 0.40
createConnectionFunction · 0.40
connectionAddressesFunction · 0.40
runCodeFunction · 0.40
mcpFunction · 0.40
seedIdentityFunction · 0.40
listConnectionAddressesFunction · 0.40