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

+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>
}