After resolving the issue of 2fa forget1

This commit is contained in:
Your Name 2026-08-23 20:35:25 +02:00
parent 8ea0bc11f9
commit b826b05ce3
6 changed files with 55 additions and 4 deletions

View File

@ -766,10 +766,21 @@ export async function getSelfProfileForUser(userId: string): Promise<SelfProfile
`SELECT u.id, u.first_name, u.middle_name, u.last_name, u.date_of_birth,
u.email_encrypted, u.phone_encrypted, u.gender,
u.country_of_residence, u.nationality,
r.name AS role_name,
COALESCE(r.name, (
SELECT r2.name FROM user_roles ur
JOIN roles r2 ON r2.id = ur.role_id
WHERE ur.user_id = u.id
ORDER BY CASE r2.name
WHEN 'super_admin' THEN 0
WHEN 'admin' THEN 1
WHEN 'moderator' THEN 2
ELSE 3
END
LIMIT 1
), 'user') AS role_name,
u.email_verified, u.totp_enabled, u.created_at
FROM users u
JOIN roles r ON r.id = u.role_id
LEFT JOIN roles r ON r.id = u.role_id
WHERE u.id = $1`,
[userId]
);

View File

@ -542,14 +542,28 @@ export async function getProfile(userId: string): Promise<{
row.created_at instanceof Date
? row.created_at.toISOString()
: String(row.created_at);
// PII decrypt must not block /me (e.g. after ENCRYPTION_KEY rotation): otherwise
// Super Admins cannot re-enable 2FA from the profile page after break-glass.
const safeDecrypt = (ciphertext: string | null, field: string): string | null => {
if (!ciphertext) return null;
try {
return decrypt(ciphertext);
} catch (e) {
const detail = e instanceof Error ? e.message : String(e);
console.error(`[profile] Failed to decrypt ${field} for user ${userId}: ${detail}`);
return null;
}
};
return {
id: row.id,
firstName: row.first_name ?? null,
middleName: row.middle_name ?? null,
lastName: row.last_name ?? null,
dateOfBirth: dob != null ? (typeof dob === 'string' ? dob : (dob as Date).toISOString().slice(0, 10)) : null,
email: row.email_encrypted ? decrypt(row.email_encrypted) : null,
phone: row.phone_encrypted ? decrypt(row.phone_encrypted) : null,
email: safeDecrypt(row.email_encrypted, 'email'),
phone: safeDecrypt(row.phone_encrypted, 'phone'),
countryOfResidence: row.country_of_residence ?? null,
nationality: row.nationality ?? null,
gender: (row.gender as UserGender | null) ?? null,

View File

@ -655,6 +655,14 @@ npm run phase-b:break-glass-reset-2fa -- \
2. Profil → réactiver le 2FA → sauvegarder les **nouveaux** recovery codes.
3. Créer un second Super Admin si ce n’est pas déjà fait.
Si `/profile` affiche « Failed to load profile » alors que le login fonctionne, l’e-mail chiffré en base ne se déchiffre plus (souvent après changement d’`ENCRYPTION_KEY`). Mettez l’ancienne clé dans :
```env
ENCRYPTION_KEY_PREVIOUS=<ancienne_cle_64_hex>
```
puis redémarrez le backend, ou déployez le correctif qui laisse `/me` charger même si le decrypt PII échoue (pour pouvoir réactiver le 2FA). Ensuite `npm run phase-b:…` / rebuild backend (+ rebuild `prediqo-web` si vous tirez le fix UI).
#### Sécurité
| À faire | À éviter |

View File

@ -296,6 +296,20 @@ export function ProfilePage() {
onSuccess={setToast}
onError={setToast}
/>
) : profileQ.isError ? (
<Stack spacing={1.5}>
<Alert severity="warning">
{t('profile.loadError')} {t('profile.twoFactorRecoveryHint')}
</Alert>
<TwoFactorPanel
totpEnabled={false}
onSuccess={(msg) => {
setToast(msg);
void profileQ.refetch();
}}
onError={setToast}
/>
</Stack>
) : (
<Skeleton height={120} />
)}

View File

@ -538,6 +538,8 @@ export const en = {
twoFactorAuth: 'Two-factor authentication',
twoFactorHint:
'Protect your account with an authenticator app. Required for sensitive operations when enabled.',
twoFactorRecoveryHint:
'You can still set up 2FA below. If email fields stay empty, ENCRYPTION_KEY may have changed — set ENCRYPTION_KEY_PREVIOUS to the old key, or update your email after 2FA is enabled.',
securityNotifications: {
title: 'Security activity',
subtitle: 'Recent security events on your account (new sign-ins, password changes, 2FA updates).',

View File

@ -548,6 +548,8 @@ export const fr = {
twoFactorAuth: 'Authentification à deux facteurs',
twoFactorHint:
'Protégez votre compte avec une application d’authentification. Requis pour les opérations sensibles lorsqu’elle est activée.',
twoFactorRecoveryHint:
'Vous pouvez quand même configurer le 2FA ci-dessous. Si l’e-mail reste vide, ENCRYPTION_KEY a peut-être changé — définissez ENCRYPTION_KEY_PREVIOUS avec l’ancienne clé, ou mettez à jour l’e-mail après avoir activé le 2FA.',
securityNotifications: {
title: 'Activité de sécurité',
subtitle: