mfa.ts (closed kinds, TOTP list incl. unverified, enrol, verify, unenrol, assurance); sheets.ts wrappers for species, working copy, account lookup, M7 reads, versions list, drawing download, own grants, with shape parsers. Unit 148, sandbox sheets-admin 5/5 x2.
125 lines
6.0 KiB
TypeScript
125 lines
6.0 KiB
TypeScript
import {
|
|
MfaFailure,
|
|
assuranceLevel,
|
|
classifyMfaError,
|
|
enrollTotp,
|
|
listTotpFactors,
|
|
unenrollFactor,
|
|
verifyTotp,
|
|
type FaunosteoClient,
|
|
} from '../src'
|
|
|
|
/**
|
|
* TOTP wrappers over supabase-js `auth.mfa`: call shapes, the factor list
|
|
* (unverified factors included) and the closed failure kinds. The same
|
|
* functions run against real GoTrue in tests/sandbox/sheets-admin.sandbox.ts.
|
|
*/
|
|
type Result = { data?: unknown; error?: unknown }
|
|
|
|
function stubMfa(results: Record<string, Result> = {}) {
|
|
const calls: Array<[string, unknown]> = []
|
|
const method = (name: string) => async (params?: unknown) => {
|
|
calls.push([name, params])
|
|
return { data: results[name]?.data ?? null, error: results[name]?.error ?? null }
|
|
}
|
|
const client = {
|
|
auth: {
|
|
mfa: {
|
|
listFactors: method('listFactors'),
|
|
enroll: method('enroll'),
|
|
challengeAndVerify: method('challengeAndVerify'),
|
|
unenroll: method('unenroll'),
|
|
getAuthenticatorAssuranceLevel: method('getAuthenticatorAssuranceLevel'),
|
|
},
|
|
},
|
|
} as unknown as FaunosteoClient
|
|
return { client, calls }
|
|
}
|
|
|
|
async function kindOf(promise: Promise<unknown>) {
|
|
try {
|
|
await promise
|
|
return 'no failure'
|
|
} catch (error) {
|
|
return error instanceof MfaFailure ? error.kind : `other: ${String(error)}`
|
|
}
|
|
}
|
|
|
|
const factor = (id: string, status: string, type = 'totp', name?: string) => ({
|
|
id, factor_type: type, status, friendly_name: name, created_at: `2026-11-0${id}T10:00:00Z`, updated_at: 'u',
|
|
})
|
|
|
|
describe('classifyMfaError', () => {
|
|
it('maps the GoTrue codes to the closed kinds', () => {
|
|
expect(classifyMfaError({ code: 'insufficient_aal', status: 403 })).toBe('aal2_required')
|
|
expect(classifyMfaError({ code: 'mfa_verification_failed', status: 422 })).toBe('invalid_code')
|
|
expect(classifyMfaError({ code: 'mfa_verification_rejected', status: 422 })).toBe('invalid_code')
|
|
expect(classifyMfaError({ code: 'mfa_factor_not_found', status: 404 })).toBe('factor_not_found')
|
|
expect(classifyMfaError({ code: 'mfa_challenge_expired', status: 422 })).toBe('challenge_expired')
|
|
expect(classifyMfaError({ code: 'too_many_enrolled_mfa_factors', status: 422 })).toBe('too_many_factors')
|
|
expect(classifyMfaError({ code: 'mfa_factor_name_conflict', status: 422 })).toBe('name_taken')
|
|
expect(classifyMfaError({ code: 'over_request_rate_limit', status: 429 })).toBe('rate_limited')
|
|
expect(classifyMfaError({ code: 'session_not_found', status: 403 })).toBe('session_expired')
|
|
expect(classifyMfaError({ code: 'bad_jwt', status: 401 })).toBe('session_expired')
|
|
})
|
|
|
|
it('reads the client error classes, then the status, and fails closed', () => {
|
|
expect(classifyMfaError({ name: 'AuthSessionMissingError', status: 400 })).toBe('session_expired')
|
|
expect(classifyMfaError({ name: 'AuthRetryableFetchError', status: 0 })).toBe('network')
|
|
expect(classifyMfaError(new TypeError('fetch failed'))).toBe('network')
|
|
expect(classifyMfaError({ status: 429 })).toBe('rate_limited')
|
|
expect(classifyMfaError({ code: 'mfa_totp_enroll_not_enabled', status: 422 })).toBe('unavailable')
|
|
expect(classifyMfaError({ code: 'unexpected_failure', status: 500 })).toBe('unavailable')
|
|
expect(classifyMfaError(undefined)).toBe('unavailable')
|
|
expect(classifyMfaError(new MfaFailure('invalid_code'))).toBe('invalid_code')
|
|
})
|
|
})
|
|
|
|
describe('TOTP wrappers', () => {
|
|
it('lists every TOTP factor, unverified ones included, and no other type', async () => {
|
|
const all = [factor('1', 'verified', 'totp', 'Phone'), factor('2', 'unverified'), factor('3', 'verified', 'phone')]
|
|
const { client } = stubMfa({ listFactors: { data: { all, totp: [all[0]] } } })
|
|
expect(await listTotpFactors(client)).toEqual([
|
|
{ id: '1', friendlyName: 'Phone', status: 'verified', createdAt: '2026-11-01T10:00:00Z' },
|
|
{ id: '2', friendlyName: null, status: 'unverified', createdAt: '2026-11-02T10:00:00Z' },
|
|
])
|
|
})
|
|
|
|
it('enrols a TOTP factor and hands back the QR code, secret and URI', async () => {
|
|
const totp = { qr_code: 'data:image/svg+xml;utf-8,<svg/>', secret: 'S', uri: 'otpauth://totp/x' }
|
|
const { client, calls } = stubMfa({ enroll: { data: { id: 'f1', type: 'totp', totp } } })
|
|
expect(await enrollTotp(client, 'Phone')).toEqual({
|
|
factorId: 'f1', qrCode: totp.qr_code, secret: 'S', uri: totp.uri,
|
|
})
|
|
expect(calls).toEqual([['enroll', { factorType: 'totp', friendlyName: 'Phone' }]])
|
|
})
|
|
|
|
it('verifies with one challenge and removes a factor by id', async () => {
|
|
const { client, calls } = stubMfa({ challengeAndVerify: { data: {} }, unenroll: { data: { id: 'f1' } } })
|
|
await verifyTotp(client, 'f1', '123456')
|
|
await unenrollFactor(client, 'f1')
|
|
expect(calls).toEqual([
|
|
['challengeAndVerify', { factorId: 'f1', code: '123456' }],
|
|
['unenroll', { factorId: 'f1' }],
|
|
])
|
|
})
|
|
|
|
it('answers the assurance levels, null without a session', async () => {
|
|
const level = (currentLevel: unknown, nextLevel: unknown) =>
|
|
stubMfa({ getAuthenticatorAssuranceLevel: { data: { currentLevel, nextLevel, currentAuthenticationMethods: [] } } })
|
|
.client
|
|
expect(await assuranceLevel(level('aal1', 'aal2'))).toEqual({ current: 'aal1', next: 'aal2' })
|
|
expect(await assuranceLevel(level(null, null))).toEqual({ current: null, next: null })
|
|
expect(await assuranceLevel(level('aal3', 'aal2'))).toEqual({ current: null, next: 'aal2' })
|
|
})
|
|
|
|
it('throws the closed kind of a GoTrue refusal', async () => {
|
|
const refused = (name: string, code: string) => stubMfa({ [name]: { error: { code, status: 403 } } }).client
|
|
expect(await kindOf(enrollTotp(refused('enroll', 'insufficient_aal'), 'x'))).toBe('aal2_required')
|
|
expect(await kindOf(verifyTotp(refused('challengeAndVerify', 'mfa_verification_failed'), 'f', '000000')))
|
|
.toBe('invalid_code')
|
|
expect(await kindOf(unenrollFactor(refused('unenroll', 'insufficient_aal'), 'f'))).toBe('aal2_required')
|
|
expect(await kindOf(listTotpFactors(refused('listFactors', 'session_not_found')))).toBe('session_expired')
|
|
})
|
|
})
|