/** * V1-2.1 acceptance of AR-1 B on the seed owner (contract §16.2 "a stack * upgraded from the DEV state with an owner holding two verified factors keeps * working at aal2 and is refused at aal1; the same upgrade with an owner * holding none stays usable at aal1 for everything but role and rights * management"), with real GoTrue. Meant for the stack of `pnpm db:sandbox up * upgrade` (the DEV path); it holds on a fresh stack too. The seed owner's * factors and sessions made here are removed afterwards (as postgres), so the * owner is back to no factor and the suite runs again on the same stack. */ import { SEED_PASSWORD, confirmedAccount, errorCode, rest, rpc, signIn, sql } from './stack' import { claimsOf, enrolTotp, stepUp, verifyTotp } from './mfa' const OWNER = '10000000-0000-0000-0000-000000000001' const OWNER_EMAIL = 'owner@faunosteo.local' const FACTOR_PREFIX = 'seed-owner-test' const passwordSession = async (): Promise => { const session = await signIn(OWNER_EMAIL, SEED_PASSWORD) return session.body.access_token as string } const refusal = (result: { status: number; body: unknown }) => [result.status, (result.body as { message?: string }).message] describe('AR-1 B on the seed owner (§16.2)', () => { let target = { id: '' } beforeAll(async () => { // Factors left by an interrupted run of this file are removed first. await sql(`delete from auth.mfa_factors where user_id = '${OWNER}' and friendly_name like '${FACTOR_PREFIX}%'`) expect(await sql(`select count(*) from auth.mfa_factors where user_id = '${OWNER}'`)).toBe('0') expect(await sql(`select count(*) from public.profile_roles where user_id = '${OWNER}' and role = 'owner' and revoked_at is null`)).toBe('1') const account = await confirmedAccount('seed-owner-target') await stepUp(account.token) target = account }, 60_000) afterAll(async () => { await sql(`delete from auth.mfa_factors where user_id = '${OWNER}' and friendly_name like '${FACTOR_PREFIX}%'`) await sql(`delete from auth.sessions where user_id = '${OWNER}'`) if (target.id) await sql(`delete from auth.users where id = '${target.id}'`) }) it('without a factor: usable at aal1, refused only for roles and rights', async () => { const token = await passwordSession() expect(claimsOf(token).aal).toBe('aal1') expect((await rest(`/profiles?select=id&id=eq.${OWNER}`, token)).body).toEqual([{ id: OWNER }]) expect(((await rest('/workspaces?select=id', token)).body as unknown[]).length).toBe(1) expect(((await rest('/species?select=id', token)).body as unknown[]).length).toBeGreaterThan(0) expect(refusal(await rpc('grant_platform_role', { target_user_id: target.id, role: 'support' }, token))) .toEqual([403, 'mfa_required']) expect(refusal(await rpc('list_catalog_grants', {}, token))).toEqual([403, 'rights.mfa']) expect(refusal(await rpc('grant_catalog_right', { user_id: target.id, kind: 'catalog_reader', reason: 'x' }, token))) .toEqual([403, 'rights.mfa']) }, 60_000) it('with two verified factors: works at aal2 and is refused at aal1', async () => { // First factor from the aal1 session (no verified factor yet), second from aal2 (§5.7.1, §5.7.3). const aal1 = await passwordSession() const first = await enrolTotp(aal1, `${FACTOR_PREFIX}-1`) expect(first.status).toBe(200) let aal2 = (await verifyTotp(aal1, first.factor!)).body.access_token as string expect(claimsOf(aal2).aal).toBe('aal2') const second = await enrolTotp(aal2, `${FACTOR_PREFIX}-2`) expect(second.status).toBe(200) aal2 = (await verifyTotp(aal2, second.factor!)).body.access_token as string expect(await sql(`select count(*) from auth.mfa_factors where user_id = '${OWNER}' and status = 'verified'`)).toBe('2') const grant = await rpc('grant_platform_role', { target_user_id: target.id, role: 'support' }, aal2) expect(grant.status).toBe(200) expect((await rpc('revoke_platform_role', { target_user_id: target.id, role: 'support', reason: 'test' }, aal2)).status).toBe(200) expect((await rpc('list_catalog_grants', {}, aal2)).status).toBe(200) // A new password sign-in starts at aal1: refused, whatever the factors. const again = await passwordSession() expect(claimsOf(again).aal).toBe('aal1') const refused = await rpc('grant_platform_role', { target_user_id: target.id, role: 'support' }, again) expect([refused.status, errorCode(refused), (refused.body as { message?: string }).message]).toEqual([403, '42501', 'mfa_required']) expect(refusal(await rpc('list_catalog_grants', {}, again))).toEqual([403, 'rights.mfa']) // And that aal1 session cannot add a third factor (§5.7.3). expect((await enrolTotp(again, `${FACTOR_PREFIX}-3`)).status).toBe(403) }, 60_000) })