|
| 1 | +import { Data, Effect, Option, Schema } from "effect"; |
| 2 | +import { SignJWT, jwtVerify } from "jose"; |
| 3 | + |
| 4 | +/** HttpOnly cookies used only for the administrative verification flow. */ |
| 5 | +export const ADMIN_MFA_COOKIE = "__Host-executor-admin-mfa"; |
| 6 | +/** The pending challenge is bound to the same user and WorkOS session. */ |
| 7 | +export const ADMIN_MFA_CHALLENGE_COOKIE = "__Host-executor-admin-challenge"; |
| 8 | +/** Verification is session-bound, with a seven-day maximum matching the login cookie. */ |
| 9 | +export const ADMIN_MFA_TTL_SECONDS = 7 * 24 * 60 * 60; |
| 10 | + |
| 11 | +/** A verified WorkOS session, supplied by the authentication adapter. */ |
| 12 | +export interface AdminMfaIdentity { |
| 13 | + readonly userId: string; |
| 14 | + readonly sessionId: string; |
| 15 | +} |
| 16 | + |
| 17 | +const Proof = Schema.Struct({ |
| 18 | + factorId: Schema.String, |
| 19 | + challengeId: Schema.String, |
| 20 | + mode: Schema.Literals(["enroll", "challenge"]), |
| 21 | + exp: Schema.Number, |
| 22 | +}); |
| 23 | +const decodeProof = Schema.decodeUnknownOption(Proof); |
| 24 | + |
| 25 | +/** Signing failures are server failures; invalid input cookies are simply refused. */ |
| 26 | +export class AdminMfaProofError extends Data.TaggedError("AdminMfaProofError")<{ |
| 27 | + readonly cause: unknown; |
| 28 | +}> {} |
| 29 | + |
| 30 | +type Purpose = "challenge" | "verified"; |
| 31 | +const issuer = (purpose: Purpose) => `executor:admin-mfa:${purpose}`; |
| 32 | +const key = (secret: string) => new TextEncoder().encode(secret); |
| 33 | + |
| 34 | +/** Sign a purpose-specific, session-bound proof with an explicit expiration. */ |
| 35 | +export const signAdminMfaProof = ( |
| 36 | + secret: string, |
| 37 | + identity: AdminMfaIdentity, |
| 38 | + purpose: Purpose, |
| 39 | + proof: typeof Proof.Type, |
| 40 | + now: number, |
| 41 | +) => |
| 42 | + Effect.tryPromise({ |
| 43 | + try: () => |
| 44 | + new SignJWT({ factorId: proof.factorId, challengeId: proof.challengeId, mode: proof.mode }) |
| 45 | + .setProtectedHeader({ alg: "HS256" }) |
| 46 | + .setIssuer(issuer(purpose)) |
| 47 | + .setSubject(identity.userId) |
| 48 | + .setAudience(identity.sessionId) |
| 49 | + .setIssuedAt(Math.floor(now / 1000)) |
| 50 | + .setExpirationTime(proof.exp) |
| 51 | + .sign(key(secret)), |
| 52 | + catch: (cause) => new AdminMfaProofError({ cause }), |
| 53 | + }); |
| 54 | + |
| 55 | +/** Reject expired, tampered, cross-user, cross-session, and wrong-purpose proofs. */ |
| 56 | +export const readAdminMfaProof = ( |
| 57 | + secret: string, |
| 58 | + identity: AdminMfaIdentity, |
| 59 | + purpose: Purpose, |
| 60 | + token: string | undefined, |
| 61 | + now: number, |
| 62 | +) => { |
| 63 | + if (!token) return Effect.succeed(null); |
| 64 | + return Effect.tryPromise({ |
| 65 | + try: () => |
| 66 | + jwtVerify(token, key(secret), { |
| 67 | + algorithms: ["HS256"], |
| 68 | + issuer: issuer(purpose), |
| 69 | + subject: identity.userId, |
| 70 | + audience: identity.sessionId, |
| 71 | + requiredClaims: ["exp", "iat", "sub", "aud"], |
| 72 | + maxTokenAge: purpose === "challenge" ? 300 : ADMIN_MFA_TTL_SECONDS, |
| 73 | + currentDate: new Date(now), |
| 74 | + }), |
| 75 | + catch: (cause) => new AdminMfaProofError({ cause }), |
| 76 | + }).pipe( |
| 77 | + Effect.map(({ payload }) => { |
| 78 | + const maxAge = purpose === "challenge" ? 300 : ADMIN_MFA_TTL_SECONDS; |
| 79 | + if ( |
| 80 | + typeof payload.iat !== "number" || |
| 81 | + typeof payload.exp !== "number" || |
| 82 | + payload.exp > payload.iat + maxAge |
| 83 | + ) |
| 84 | + return null; |
| 85 | + return Option.getOrNull(decodeProof(payload)); |
| 86 | + }), |
| 87 | + Effect.catchTag("AdminMfaProofError", () => Effect.succeed(null)), |
| 88 | + ); |
| 89 | +}; |
0 commit comments