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:
1 parent
99488d71d9
commit
4744e28986
38 files changed
+1818
-55
No files matched your search
@@ -0,0 +1,15 @@
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import TotpCodeForm from './TotpCodeForm'
|
||||
import type { StepUpChallenge } from '@/auth/useStepUp'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** GoTrue asked for `aal2`: the reason, then the challenge in place; the action resumes once it succeeds. */
|
||||
export default function InlineStepUp({ challenge }: { challenge: StepUpChallenge }) {
|
||||
const t = useT()
|
||||
return (
|
||||
<div className="space-y-3 rounded-lg border border-[var(--color-border)] p-4">
|
||||
<Notice tone="info">{t('mfa.step_up.required')}</Notice>
|
||||
<TotpCodeForm {...challenge} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { useId, useState, type FormEvent } from 'react'
|
||||
import { verifyTotp, type TotpFactor } from '@faunosteo/supabase'
|
||||
import Button from '@/components/ui/Button'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Input from '@/components/ui/Input'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import Select from '@/components/ui/Select'
|
||||
import { fieldErrors, totpCodeSchema, type FieldErrors } from '@/auth/validation'
|
||||
import { useMfaFailureMessage } from '@/auth/useMfaFailureMessage'
|
||||
import { getSupabaseClient } from '@/lib/supabase'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** A factor the code may come from: the account's verified ones, or the one being enrolled. */
|
||||
export type CodeFactor = Pick<TotpFactor, 'id' | 'friendlyName'>
|
||||
|
||||
export interface TotpCodeFormProps {
|
||||
/** At least one; a choice is offered when there are several. */
|
||||
factors: CodeFactor[]
|
||||
/** Called once GoTrue accepted the code: the session is `aal2` from now on. */
|
||||
onVerified: () => void
|
||||
onCancel?: () => void
|
||||
}
|
||||
|
||||
/** The name a factor is listed under, or the generic one when it has none. */
|
||||
export function factorLabel(factor: CodeFactor, fallback: string): string {
|
||||
return factor.friendlyName ?? fallback
|
||||
}
|
||||
|
||||
/**
|
||||
* A 6-digit TOTP code, challenged and verified by GoTrue: the step-up
|
||||
* challenge (contract §5.6) with a verified factor, or the verification of a
|
||||
* factor being enrolled. Success upgrades this session to `aal2`
|
||||
* (supabase-js then emits the new session to the store). The code is never
|
||||
* kept once sent.
|
||||
*/
|
||||
export default function TotpCodeForm({ factors, onVerified, onCancel }: TotpCodeFormProps) {
|
||||
const t = useT()
|
||||
const describe = useMfaFailureMessage()
|
||||
const id = useId()
|
||||
const [factorId, setFactorId] = useState(factors[0]?.id ?? '')
|
||||
const [code, setCode] = useState('')
|
||||
const [errors, setErrors] = useState<FieldErrors<{ code: string }>>({})
|
||||
const [failure, setFailure] = useState<string | null>(null)
|
||||
const [busy, setBusy] = useState(false)
|
||||
|
||||
async function submit(event: FormEvent) {
|
||||
event.preventDefault()
|
||||
const client = getSupabaseClient()
|
||||
const parsed = totpCodeSchema.safeParse({ code })
|
||||
if (!client) return
|
||||
if (!parsed.success) {
|
||||
setErrors(fieldErrors(parsed.error))
|
||||
return
|
||||
}
|
||||
setErrors({})
|
||||
setFailure(null)
|
||||
setBusy(true)
|
||||
try {
|
||||
await verifyTotp(client, factorId, parsed.data.code)
|
||||
setCode('')
|
||||
onVerified()
|
||||
} catch (error) {
|
||||
setCode('')
|
||||
setFailure(describe(error))
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
const codeId = `${id}-code`
|
||||
return (
|
||||
<form onSubmit={submit} noValidate className="space-y-4">
|
||||
{factors.length > 1 && (
|
||||
<FormField id={`${id}-factor`} label={t('mfa.step_up.factor')}>
|
||||
<Select
|
||||
id={`${id}-factor`}
|
||||
value={factorId}
|
||||
onChange={(e) => setFactorId(e.target.value)}
|
||||
>
|
||||
{factors.map((factor) => (
|
||||
<option key={factor.id} value={factor.id}>
|
||||
{factorLabel(factor, t('mfa.factor.unnamed'))}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</FormField>
|
||||
)}
|
||||
<FormField
|
||||
id={codeId}
|
||||
label={t('mfa.code.label')}
|
||||
hint={t('mfa.code.hint')}
|
||||
error={errors.code && t(errors.code)}
|
||||
>
|
||||
<Input
|
||||
id={codeId}
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value)}
|
||||
inputMode="numeric"
|
||||
autoComplete="one-time-code"
|
||||
maxLength={7}
|
||||
aria-invalid={Boolean(errors.code)}
|
||||
aria-describedby={fieldDescribedBy(codeId, 'hint', errors.code)}
|
||||
/>
|
||||
</FormField>
|
||||
{failure && <Notice tone="error">{failure}</Notice>}
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button type="submit" disabled={busy}>
|
||||
{busy ? t('mfa.code.verifying') : t('mfa.code.verify')}
|
||||
</Button>
|
||||
{onCancel && (
|
||||
<Button type="button" variant="ghost" onClick={onCancel} disabled={busy}>
|
||||
{t('common.cancel')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user