Repository Public
1import { requireAdmin } from "@/server/session";2import { getDb } from "@/server/db";3import { listSessions } from "@/server/auth";4import { sha256 } from "@/server/crypto";5import { ActionButton, ActionForm } from "@/components/admin/ActionForm";6import { TotpSetup } from "@/components/admin/TotpSetup";7import { passwordChangeAction, recoveryEmailAction, revokeSessionsAction, totpDisableAction } from "../../../actions";8import { PageTitle, fmtDateTime } from "../../../doc";910export const metadata = { title: "Sécurité" };1112const ACTIONS: Record<string, string> = {13 "login.success": "Connexion réussie",14 "login.failed": "Échec de connexion",15 "login.locked": "Tentative pendant un verrouillage",16 "login.rate_limited": "Trop de tentatives (adresse IP)",17 "login.password_ok_mfa_pending": "Mot de passe accepté, 2e facteur demandé",18 "mfa.failed": "Échec du 2e facteur",19 "mfa.enabled": "2e facteur activé",20 "mfa.disabled": "2e facteur désactivé",21 "mfa.recovery_code_used": "Code de secours utilisé",22 "password.changed": "Mot de passe modifié",23 "password.reset": "Mot de passe réinitialisé",24 logout: "Déconnexion",25 "content.published": "Contenu publié",26 "content.draft_saved": "Brouillon enregistré",27 "content.version_restored": "Version restaurée",28 "admin.bootstrap": "Compte administrateur créé",29 "admin.reset_cli": "Mot de passe réinitialisé (serveur)",30};3132export default async function SecurityPage() {33 const { user, token } = await requireAdmin();34 const sessions = listSessions(user.id);35 const current = sha256(token);36 const log = getDb().prepare("SELECT id, at, action, target, detail, actor FROM audit_log ORDER BY id DESC LIMIT 60").all() as { id: number; at: number; action: string; target: string; detail: string; actor: string }[];37 return (38 <>39 <PageTitle title="Sécurité" intro="Mot de passe, vérification en deux étapes, sessions ouvertes et journal d’audit." />40 <div className="grid gap-6 lg:grid-cols-2">41 <section className="card p-4 sm:p-5">42 <h2 className="text-navy-900 font-serif text-xl">Mot de passe</h2>43 <ActionForm action={passwordChangeAction} submitLabel="Changer le mot de passe" className="mt-4" resetOnOk>44 <div className="flex flex-col gap-4">45 <div>46 <label htmlFor="current" className="field-label">Mot de passe actuel</label>47 <input id="current" name="current" type="password" required autoComplete="current-password" className="field-input" />48 </div>49 <div>50 <label htmlFor="next" className="field-label">Nouveau mot de passe</label>51 <input id="next" name="next" type="password" required minLength={12} autoComplete="new-password" className="field-input" />52 <p className="field-help">12 caractères minimum. Toutes les autres sessions seront fermées.</p>53 </div>54 <div>55 <label htmlFor="confirm" className="field-label">Confirmation</label>56 <input id="confirm" name="confirm" type="password" required autoComplete="new-password" className="field-input" />57 </div>58 </div>59 </ActionForm>60 </section>6162 <section className="card p-4 sm:p-5">63 <h2 className="text-navy-900 font-serif text-xl">64 Vérification en deux étapes {user.totp_enabled ? <span className="badge badge-green ml-1">Active</span> : <span className="badge badge-grey ml-1">Inactive</span>}65 </h2>66 {user.totp_enabled ? (67 <ActionForm action={totpDisableAction} submitLabel="Désactiver" submitClass="abtn abtn-danger" className="mt-4" confirmText="Désactiver la vérification en deux étapes ?">68 <p className="text-ink-700 mb-3 text-sm">Un code de votre application d’authentification est demandé à chaque connexion.</p>69 <label htmlFor="pw-totp" className="field-label">Mot de passe (pour confirmer)</label>70 <input id="pw-totp" name="password" type="password" required autoComplete="current-password" className="field-input" />71 </ActionForm>72 ) : (73 <TotpSetup />74 )}75 </section>7677 <section className="card p-4 sm:p-5">78 <h2 className="text-navy-900 font-serif text-xl">Récupération du compte</h2>79 <p className="text-ink-700 mt-1 text-sm">Avec une adresse de récupération, « Mot de passe oublié » envoie un lien à usage unique (valide 1 heure). Sans adresse, la récupération se fait sur le serveur par la commande <code>pnpm admin:reset</code>.</p>80 <ActionForm action={recoveryEmailAction} className="mt-3">81 <label htmlFor="rec-email" className="field-label">Adresse de récupération</label>82 <input id="rec-email" name="email" type="email" inputMode="email" defaultValue={user.email ?? ""} className="field-input" />83 </ActionForm>84 </section>8586 <section className="card p-4 sm:p-5">87 <h2 className="text-navy-900 font-serif text-xl">Sessions ouvertes</h2>88 <ul className="mt-2 flex flex-col gap-2 text-sm">89 {sessions.map((s) => (90 <li key={s.token_hash} className="flex flex-wrap items-center justify-between gap-2">91 <span className="min-w-0">92 <span className="text-ink-900 block truncate">{s.user_agent.slice(0, 70) || "Appareil inconnu"}</span>93 <span className="text-ink-500 block text-xs">Dernière activité : {fmtDateTime.format(new Date(s.last_seen_at))}</span>94 </span>95 {s.token_hash === current ? <span className="badge badge-green">Cet appareil</span> : <span className="badge">Autre appareil</span>}96 </li>97 ))}98 </ul>99 <div className="mt-3">100 <ActionButton action={revokeSessionsAction} label="Fermer les autres sessions" />101 </div>102 </section>103104 <section className="card p-4 sm:p-5 lg:col-span-2">105 <h2 className="text-navy-900 font-serif text-xl">Journal d’audit</h2>106 <div className="mt-2 overflow-x-auto">107 <table className="atable">108 <thead>109 <tr>110 <th>Date</th>111 <th>Événement</th>112 <th>Détail</th>113 </tr>114 </thead>115 <tbody>116 {log.map((l) => (117 <tr key={l.id}>118 <td className="whitespace-nowrap">{fmtDateTime.format(new Date(l.at))}</td>119 <td>{ACTIONS[l.action] ?? l.action}</td>120 <td className="text-ink-500">{[l.target, l.detail, l.actor].filter(Boolean).join(" · ")}</td>121 </tr>122 ))}123 </tbody>124 </table>125 </div>126 </section>127 </div>128 </>129 );130}131