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

+5 -18
View File
@@ -10,13 +10,11 @@ import type { Dict } from '@/i18n/types'
import type { Locator, Page } from '@playwright/test'
import {
enrolTotp,
refusal,
verifyTotp,
} from '../../../../packages/supabase/tests/sandbox/mfa'
import { sql, stack } from '../../../../packages/supabase/tests/sandbox/stack'
import {
totpCode,
TOTP_STEP_SECONDS,
} from '../../../../packages/supabase/tests/sandbox/totp'
import { currentCode } from '../../../../packages/supabase/tests/sandbox/totp'
import { expect } from './browser'
import type { SandboxAccount } from './sandbox'
@@ -69,25 +67,14 @@ export async function enrolVerifiedFactor(
): Promise<string> {
const token = await passwordToken(account)
const enrolment = await enrolTotp(token, name)
if (!enrolment.factor)
throw new Error(`sandbox enrol: HTTP ${enrolment.status}`)
if (!enrolment.factor) throw new Error(`sandbox enrol: ${refusal(enrolment)}`)
const verified = await verifyTotp(token, enrolment.factor)
if (verified.status !== 200)
throw new Error(`sandbox verify: HTTP ${verified.status}`)
throw new Error(`sandbox verify: ${refusal(verified)}`)
return enrolment.factor.secret
}
/**
* A code the authenticator would show now, never one about to change: a code
* typed 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)
}
export { currentCode }
/** Types a code from `secret` into the TOTP form inside `scope` and sends it. */
export async function enterCode(
+5 -2
View File
@@ -12,6 +12,7 @@ import type { Dict } from '@/i18n/types'
import type { Browser, BrowserContext, Page } from '@playwright/test'
import {
enrolTotp,
refusal,
verifyTotp,
} from '../../../../packages/supabase/tests/sandbox/mfa'
import {
@@ -50,10 +51,12 @@ export async function makeActor(
for (const kind of grants) await seedGrant(account, kind)
const token = await passwordToken(account)
const enrolment = await enrolTotp(token, 'Téléphone')
if (!enrolment.factor) throw new Error('editor fixture: enrol refused')
if (!enrolment.factor)
throw new Error(`editor fixture: enrol ${refusal(enrolment)}`)
const verified = await verifyTotp(token, enrolment.factor)
const aal2 = verified.body.access_token
if (typeof aal2 !== 'string') throw new Error('editor fixture: no aal2')
if (typeof aal2 !== 'string')
throw new Error(`editor fixture: no aal2 ${refusal(verified)}`)
return { account, secret: enrolment.factor.secret, aal2 }
}
+17 -13
View File
@@ -5,7 +5,7 @@
* in memory and are never printed.
*/
import { stack, type Account } from './stack'
import { totpCode } from './totp'
import { currentCode } from './totp'
export interface Factor {
id: string
@@ -73,21 +73,26 @@ export async function verifyTotp(
if (challenge.status !== 200) return challenge
return authRequest('POST', `/factors/${factor.id}/verify`, token, {
challenge_id: challenge.body.id,
code: totpCode(factor.secret),
code: await currentCode(factor.secret),
})
}
/** The HTTP status and GoTrue's error code of an answer: never a body, a token or a secret. */
export function refusal(result: {
status: number
body: Record<string, unknown>
}): string {
const code = result.body.error_code ?? result.body.code
return `HTTP ${result.status}${typeof code === 'string' ? ` ${code}` : ''}`
}
/** Enrols and verifies a TOTP factor from `token`, returning the new aal2 access token. */
export async function stepUp(
token: string,
name = 'device',
): Promise<string> {
export async function stepUp(token: string, name = 'device'): Promise<string> {
const enrol = await enrolTotp(token, name)
if (!enrol.factor)
throw new Error(`enrol failed: ${JSON.stringify(enrol.body)}`)
if (!enrol.factor) throw new Error(`enrol failed: ${refusal(enrol)}`)
const verified = await verifyTotp(token, enrol.factor)
const aal2 = verified.body.access_token as string
if (!aal2) throw new Error(`verify failed: ${JSON.stringify(verified.body)}`)
if (!aal2) throw new Error(`verify failed: ${refusal(verified)}`)
return aal2
}
@@ -103,8 +108,7 @@ export async function steppedUp(account: Account): Promise<SteppedAccount> {
/** The claims of an access token (payload only; the signature is not checked here). */
export function claimsOf(token: string): Record<string, unknown> {
const payload = token.split('.')[1] ?? ''
return JSON.parse(Buffer.from(payload, 'base64url').toString('utf8')) as Record<
string,
unknown
>
return JSON.parse(
Buffer.from(payload, 'base64url').toString('utf8'),
) as Record<string, unknown>
}
+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')
}