feat(app): admin and catalogue entries, step-up guard, MFA in Mon compte (V1-2.5 E, F)

Nav hint from own grants and roles (never a control); RequireCatalog, RequireAdmin (no admin read before aal2), Accès non autorisé page, placeholder routes; TOTP factor list, enrolment with QR and one-time secret, removal, inline step-up for aal2 actions, step-up in password reset; auth kind aal2_required. App 601, e2e:ui 227.
This commit is contained in:
bchanot committed 2026-11-02 16:49:37 +01:00
1 parent 99488d71d9
commit 4744e28986
38 files changed
+1818 -55

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
import type { OwnCatalogGrant, PlatformRole } from '@faunosteo/supabase'
export interface NavigationHint {
/** « Fiches anatomiques »: an active `catalog_reader` grant or any active capability (§5.3). */
catalog: boolean
/** « Administration »: an active capability or the owner role (§11.1, BDR-094). */
admin: boolean
}
const CAPABILITIES: ReadonlySet<OwnCatalogGrant['kind']> = new Set([
'sheet_editor',
'sheet_reviewer',
'sheet_publisher',
])
/** Not revoked and not past its expiry, on the device clock. */
function isActive(grant: OwnCatalogGrant, now: number): boolean {
if (grant.revoked_at) return false
return grant.expires_at === null || Date.parse(grant.expires_at) > now
}
/**
* Which navigation entries to show, from the caller's own grant rows and
* platform roles. A display hint only: a hidden entry protects nothing, and
* a shown one grants nothing. Every page acts on the server's answer (RLS,
* the capability and `aal2` checks), never on this.
*/
export function navigationHint(
grants: readonly OwnCatalogGrant[],
roles: readonly PlatformRole[],
now: number = Date.now(),
): NavigationHint {
const active = grants.filter((grant) => isActive(grant, now))
const capability = active.some((grant) => CAPABILITIES.has(grant.kind))
return {
catalog: capability || active.some((grant) => grant.kind === 'catalog_reader'),
admin: capability || roles.includes('owner'),
}
}
+24
View File
@@ -0,0 +1,24 @@
import {
assuranceLevel,
listTotpFactors,
type FaunosteoClient,
type TotpFactor,
} from '@faunosteo/supabase'
/** The account's verified TOTP factors: the ones a step-up can use. */
export async function verifiedFactors(client: FaunosteoClient): Promise<TotpFactor[]> {
const factors = await listTotpFactors(client)
return factors.filter((factor) => factor.status === 'verified')
}
/**
* What raising the session to `aal2` needs (contract §5.6): null when the
* session is `aal2` already, else the account's verified factors, empty when
* it has none (a factor must be added first). The level read is a hint for
* the screens; GoTrue and the RPCs check the token themselves.
*/
export async function stepUpFactors(client: FaunosteoClient): Promise<TotpFactor[] | null> {
const level = await assuranceLevel(client)
if (level.current === 'aal2') return null
return verifiedFactors(client)
}
@@ -0,0 +1,8 @@
import { classifyMfaError } from '@faunosteo/supabase'
import { useT } from '@/i18n/useT'
/** One sentence per MFA failure kind (`mfa.failure.<kind>`); GoTrue's own message never shows. */
export function useMfaFailureMessage(): (error: unknown) => string {
const t = useT()
return (error) => t(`mfa.failure.${classifyMfaError(error)}`)
}
+59
View File
@@ -0,0 +1,59 @@
import { useState } from 'react'
import { AuthFailure, MfaFailure, type TotpFactor } from '@faunosteo/supabase'
import { getSupabaseClient } from '@/lib/supabase'
import { verifiedFactors } from './stepUp'
interface PendingStepUp {
factors: TotpFactor[]
resume: () => void
}
/** The open challenge: its factors, and what verifying or cancelling it does. */
export interface StepUpChallenge {
factors: TotpFactor[]
onVerified: () => void
onCancel: () => void
}
/** GoTrue refused because the session is below `aal2` (IDENTITY.md §4.1). */
export function isAal2Required(error: unknown): boolean {
return (
(error instanceof MfaFailure || error instanceof AuthFailure) &&
error.kind === 'aal2_required'
)
}
/**
* An action GoTrue may refuse below `aal2` (a password change, a second
* factor, the removal of a verified one): `open` shows the challenge for one
* of the account's verified factors and keeps the action to resume once the
* session is `aal2`; it answers false when the account has no verified factor
* (nothing to step up with) or the factors cannot be read. Never a bypass:
* the retried action goes to GoTrue again.
*/
export function useStepUp() {
const [pending, setPending] = useState<PendingStepUp | null>(null)
async function open(resume: () => void): Promise<boolean> {
const client = getSupabaseClient()
if (!client) return false
try {
const factors = await verifiedFactors(client)
if (factors.length === 0) return false
setPending({ factors, resume })
return true
} catch {
return false
}
}
const challenge: StepUpChallenge | null = pending && {
factors: pending.factors,
onVerified: () => {
setPending(null)
pending.resume()
},
onCancel: () => setPending(null),
}
return { open, challenge }
}
+19
View File
@@ -50,6 +50,25 @@ export const newPasswordSchema = z
path: ['passwordConfirm'],
})
/** A TOTP code as authenticator apps show it: six digits, spaces tolerated. */
export const totpCodeSchema = z.object({
code: z
.string()
.transform((value) => value.replace(/\s/g, ''))
.pipe(z.string().regex(/^\d{6}$/, 'mfa.error.code_format')),
})
/** The name a factor is listed under (GoTrue's friendly_name, unique per account). */
export const FACTOR_NAME_MAX_LENGTH = 64
export const factorNameSchema = z.object({
name: z
.string()
.trim()
.min(1, 'mfa.error.name_required')
.max(FACTOR_NAME_MAX_LENGTH, 'mfa.error.name_too_long'),
})
export type FieldErrors<T> = Partial<Record<keyof T & string, string>>
/** First issue per field, as an i18n key. */