test(supabase): TOTP step guard in the sandbox verify helper

This commit is contained in:
bchanot committed 2026-11-04 18:55:47 +01:00
1 parent 697cefa2dd
commit 2b10471b0a
4 files changed
+39 -34

No files matched your search

+12 -1
View File
@@ -28,6 +28,18 @@ function base32Decode(text: string): Buffer {
return Buffer.from(bytes)
}
/**
* A code the authenticator would show now, never one about to change: a code
* sent in its last seconds may reach GoTrue in the next step.
*/
export async function currentCode(secret: string): Promise<string> {
const step = TOTP_STEP_SECONDS * 1000
const left = step - (Date.now() % step)
if (left < 3000)
await new Promise((resolve) => setTimeout(resolve, left + 200))
return totpCode(secret)
}
/** The 6-digit code of `secret` for the step holding `at` (ms since epoch). */
export function totpCode(secret: string, at = Date.now()): string {
const counter = Buffer.alloc(8)
@@ -37,4 +49,3 @@ export function totpCode(secret: string, at = Date.now()): string {
const value = (mac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000
return value.toString().padStart(6, '0')
}