( secret: string, identity: AdminMfaIdentity, purpose: Purpose, token: string | undefined, now: number, )
| 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 | }; |
no test coverage detected