( userId: string )
| 69 | }); |
| 70 | |
| 71 | const enrollViaService = async ( |
| 72 | userId: string |
| 73 | ): Promise<{ secretBase32: string; recoveryCodes: string[] }> => { |
| 74 | const setup = await mfaService.setup(userId, PASSWORD); |
| 75 | const staged = await cacheService.get<{ secretEncrypted: string }>( |
| 76 | MFA_CACHE_KEYS.setup(userId) |
| 77 | ); |
| 78 | |
| 79 | if (staged === null) { |
| 80 | throw new Error("setup did not stage a secret"); |
| 81 | } |
| 82 | |
| 83 | const secretBase32 = decryptString(staged.secretEncrypted); |
| 84 | |
| 85 | await mfaService.verifySetup(userId, totpFor(secretBase32)); |
| 86 | |
| 87 | /* |
| 88 | * Verifying setup writes the current TOTP step into mfaLastTotpStep |
| 89 | * for replay protection. The route tests then immediately try to log |
| 90 | * in inside that same 30-second window, which the replay guard would |
| 91 | * otherwise reject. Clearing the step is safe — a real user picks |
| 92 | * up the same protection on their next verify. |
| 93 | */ |
| 94 | await db |
| 95 | .update(users) |
| 96 | .set({ mfaLastTotpStep: null }) |
| 97 | .where(eq(users.id, userId)); |
| 98 | |
| 99 | return { secretBase32, recoveryCodes: setup.recoveryCodes }; |
| 100 | }; |
| 101 | |
| 102 | describe("MFA routes", () => { |
| 103 | beforeEach(async () => { |
no test coverage detected