All files / src/routes portal.js

92.15% Statements 235/255
86.92% Branches 133/153
100% Functions 18/18
91.86% Lines 226/246

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 6715x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x     5x           5x   5x                 5x 5x 5x                         67x 17x   50x         25x                 26x 26x 3x   23x         20x 20x 19x               14x 14x 14x         14x         22x 22x 22x                           5x 33x 33x     33x     33x 1x     32x     32x 34x 1x       32x 32x 1x       32x 32x 3x   2x 1x 1x               32x 12x                           20x 20x     20x 20x 5x   1x       4x 4x   3x 3x 3x 3x           1x   3x         16x                                               5x 17x 17x     17x 2x       15x 15x 1x       14x     14x 14x               14x     14x             14x                         5x 29x 29x 29x     29x 29x     23x 23x 1x     22x     22x 3x       19x 1x       18x           18x     18x 18x 1x       17x 17x 2x       15x           15x 15x 15x   15x                           5x 18x 18x 18x     18x 18x     13x 1x       12x 12x 11x     11x 2x       9x 1x       7x             7x   7x   1x 1x                       5x 16x 16x 16x     16x 16x     11x 11x 10x     10x 1x       9x 1x       8x 8x     8x   8x                         5x 4x 4x 4x                           5x 5x 5x 5x     5x 4x 4x   3x 3x 2x   2x       2x 1x       2x 2x                     5x 5x     3x                   5x 9x 9x     9x 2x       7x 1x       6x       6x 6x     1x       5x 5x   1x       4x           4x       4x     4x 4x 1x       3x 3x             3x 3x     3x                         5x 12x 12x     12x 1x   11x 1x       10x     10x 10x   10x 2x     8x     8x 1x       7x 1x       6x           6x 1x       5x     5x     5x           5x 5x     5x     5x       5x     5x                   5x  
const router = require('express').Router();
const crypto = require('node:crypto');
const { authMiddlewareStrict } = require('../middleware/auth');
const { db, auth } = require('../utils/firebase');
const log = require('../utils/log');
const { sendEmail } = require('../utils/email');
const { buildOtpEmail } = require('../utils/email-templates');
const { encryptSecret, decryptSecret } = require('../utils/totp-crypto');
const { generateSecret, generateURI, verifySync } = require('otplib/functional');
const { NobleCryptoPlugin } = require('@otplib/plugin-crypto-noble');
const { ScureBase32Plugin } = require('@otplib/plugin-base32-scure');
 
// Instantiate otplib plugins once at module load
const otplibPlugins = { crypto: new NobleCryptoPlugin(), base32: new ScureBase32Plugin() };
 
// All portal routes use authMiddlewareStrict except totp-recovery (unauthenticated)
// Individual endpoints will be added in subsequent tasks
 
// Avatar URLs must be served from our CDN or localhost (local dev)
const TRUSTED_AVATAR_HOSTS = ['images.shytalk.shyden.co.uk', 'localhost', '127.0.0.1'];
 
const SUSPENSION_REASONS_ALLOWLIST = [
  'Spamming',
  'Harassment',
  'Inappropriate content',
  'Ban evasion',
  'Terms violation',
  'Other',
];
 
const TOTP_MAX_AGE_MS = 24 * 60 * 60 * 1000; // 24 hours
const TOTP_REPLAY_WINDOW_MS = 30_000;
const TOTP_CODE_FORMAT_RE = /^\d{6}$/;
 
// ═══════════════════════════════════════════════════════════════════
// Private helpers — shared TOTP lifecycle operations
//
// All helpers that can "fail" return { status, error } for the caller
// to forward via res.status(status).json({ error }), or null on success.
// Callers remain in full control of the response, matching the Express
// idiom used elsewhere in this codebase.
// ═══════════════════════════════════════════════════════════════════
 
/** Returns { status, error } if the code is not exactly 6 digits, else null. */
function validateCodeFormat(code) {
  if (!code || typeof code !== 'string' || !TOTP_CODE_FORMAT_RE.test(code)) {
    return { status: 400, error: 'Code must be exactly 6 digits' };
  }
  return null;
}
 
/** Returns true if this code was used within the replay window. */
function isReplay(totpData, code) {
  return (
    totpData.lastUsedCode === code &&
    totpData.lastUsedAt &&
    Date.now() - totpData.lastUsedAt < TOTP_REPLAY_WINDOW_MS
  );
}
 
/** Reads the enrolled TOTP doc. Returns { data } on hit, { status, error } on miss. */
async function readEnrolledTotp(uniqueId) {
  const snap = await db.doc(`users/${uniqueId}/private/totp`).get();
  if (!snap.exists) {
    return { status: 403, error: 'TOTP not enrolled' };
  }
  return { data: snap.data() };
}
 
/** Decrypts the stored secret and verifies the code via otplib. */
function verifyTotpCode(totpData, code) {
  const secret = decryptSecret(totpData.encryptedSecret);
  const result = verifySync({ token: code, secret, ...otplibPlugins });
  return result.valid;
}
 
/**
 * Clears totpVerified/totpVerifiedAt claims (preserving other claims like `admin`)
 * and revokes all refresh tokens. Used by sign-out, revoke-all-sessions, and DELETE /portal/totp.
 */
async function clearTotpSessionAndRevoke(uid) {
  const userRecord = await auth.getUser(uid);
  const existingClaims = userRecord.customClaims || {};
  await auth.setCustomUserClaims(uid, {
    ...existingClaims,
    totpVerified: false,
    totpVerifiedAt: null,
  });
  await auth.revokeRefreshTokens(uid);
}
 
/** Sets totpVerified:true with a fresh timestamp, preserving other claims. */
async function setTotpVerifiedClaim(uid) {
  const userRecord = await auth.getUser(uid);
  const existingClaims = userRecord.customClaims || {};
  await auth.setCustomUserClaims(uid, {
    ...existingClaims,
    totpVerified: true,
    totpVerifiedAt: Date.now(),
  });
}
 
/**
 * GET /portal/me — Returns the authenticated user's portal-relevant data.
 *
 * This is the main portal entry point. Every portal page load calls this
 * endpoint first. It determines what the user sees (dashboard, TOTP prompt,
 * enrollment, suspension screen, or "download the app").
 */
router.get('/portal/me', authMiddlewareStrict, async (req, res) => {
  try {
    const { uniqueId, token } = req.auth;
 
    // 1. Read user doc from Firestore
    const userSnap = await db.doc('users/' + uniqueId).get();
 
    // 2. If user doc doesn't exist → 404
    if (!userSnap.exists) {
      return res.status(404).json({ error: 'User not found' });
    }
 
    const userData = userSnap.data();
 
    // 3. Avatar URL validation: only allow trusted domains
    let avatarUrl = userData.avatarUrl || null;
    if (avatarUrl && !TRUSTED_AVATAR_HOSTS.some((host) => avatarUrl.includes(host))) {
      avatarUrl = null;
    }
 
    // 4. Suspension reason sanitization
    let suspensionReason = userData.suspensionReason || null;
    if (suspensionReason && !SUSPENSION_REASONS_ALLOWLIST.includes(suspensionReason)) {
      suspensionReason = null;
    }
 
    // 5. Convert suspensionEndDate to ISO string
    let suspensionEndDate = null;
    if (userData.suspensionEndDate) {
      if (typeof userData.suspensionEndDate.toDate === 'function') {
        // Firestore Timestamp
        suspensionEndDate = userData.suspensionEndDate.toDate().toISOString();
      } else if (userData.suspensionEndDate instanceof Date) {
        suspensionEndDate = userData.suspensionEndDate.toISOString();
      } else Eif (typeof userData.suspensionEndDate === 'string') {
        suspensionEndDate = userData.suspensionEndDate;
      }
    }
 
    // 6. Suspension check first — return early with suspension data
    //    (do NOT check TOTP — suspension takes precedence so user can see appeal screen)
    if (userData.isSuspended) {
      return res.status(200).json({
        uniqueId,
        displayName: userData.displayName || '',
        avatarUrl,
        userType: userData.userType || 'MEMBER',
        isAdmin: token.admin === true,
        isSuspended: true,
        suspensionReason,
        suspensionEndDate,
        totpEnrolled: false, // Don't reveal TOTP status for suspended users
      });
    }
 
    // 7. Read TOTP doc
    const totpSnap = await db.doc('users/' + uniqueId + '/private/totp').get();
    const totpEnrolled = totpSnap.exists;
 
    // 8. TOTP enforcement for password users
    const signInProvider = token.firebase?.sign_in_provider;
    if (signInProvider === 'password' && totpEnrolled) {
      if (!token.totpVerified) {
        // Not verified at all
        return res.status(403).json({ error: 'MFA required' });
      }
 
      // Check if totpVerifiedAt is missing or expired (> 24h)
      const totpVerifiedAt = token.totpVerifiedAt;
      if (!totpVerifiedAt || Date.now() - totpVerifiedAt > TOTP_MAX_AGE_MS) {
        // Clear the expired claim
        try {
          const userRecord = await auth.getUser(req.auth.uid);
          const existingClaims = userRecord.customClaims || {};
          await auth.setCustomUserClaims(req.auth.uid, {
            ...existingClaims,
            totpVerified: false,
            totpVerifiedAt: null,
          });
        } catch (err) {
          log.error('portal', 'Failed to clear expired TOTP claims', { error: err.message });
        }
        return res.status(403).json({ error: 'Re-verify TOTP' });
      }
    }
 
    // 9. Return success response
    return res.status(200).json({
      uniqueId,
      displayName: userData.displayName || '',
      avatarUrl,
      userType: userData.userType || 'MEMBER',
      isAdmin: token.admin === true,
      isSuspended: false,
      suspensionReason: null,
      suspensionEndDate: null,
      totpEnrolled,
    });
  } catch (err) {
    log.error('portal', 'GET /portal/me failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
/**
 * POST /portal/totp/setup — Begin TOTP enrollment.
 *
 * Generates a TOTP secret, stores it encrypted in a pending doc,
 * and returns the secret + otpauth URI for QR code rendering.
 * Password provider only. Overwrites any existing pending doc (page refresh).
 */
router.post('/portal/totp/setup', authMiddlewareStrict, async (req, res) => {
  try {
    const { uniqueId, token } = req.auth;
 
    // 1. Verify password provider
    if (token.firebase?.sign_in_provider !== 'password') {
      return res.status(400).json({ error: 'Password provider required' });
    }
 
    // 2. Check if already enrolled
    const totpSnap = await db.doc(`users/${uniqueId}/private/totp`).get();
    if (totpSnap.exists) {
      return res.status(409).json({ error: 'Already enrolled' });
    }
 
    // 3. Generate TOTP secret (length: 20 bytes → 32 BASE32 chars)
    const secret = generateSecret({ length: 20, ...otplibPlugins });
 
    // 4. Generate otpauth URI (email is URI-encoded in the label by generateURI)
    const userEmail = token.email || '';
    const qrCodeUrl = generateURI({
      issuer: 'ShyTalk',
      label: userEmail,
      secret,
      type: 'totp',
    });
 
    // 5. Encrypt the secret
    const encryptedSecret = encryptSecret(secret);
 
    // 6. Store in totp-pending (overwrites if already exists — handles page refresh)
    await db.doc(`users/${uniqueId}/private/totp-pending`).set({
      encryptedSecret,
      expiresAt: Date.now() + 600000, // 10 minutes
      attempts: 0,
    });
 
    // 7. Return secret and QR code URL
    return res.status(200).json({ secret, qrCodeUrl });
  } catch (err) {
    log.error('portal', 'POST /portal/totp/setup failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
/**
 * POST /portal/totp/confirm-setup — Confirm TOTP enrollment with a code.
 *
 * Validates the TOTP code against the pending secret, then promotes
 * the secret to permanent storage and sets MFA claims.
 */
router.post('/portal/totp/confirm-setup', authMiddlewareStrict, async (req, res) => {
  try {
    const { uniqueId, uid } = req.auth;
    const { code } = req.body || {};
 
    // 1. Validate code format
    const formatErr = validateCodeFormat(code);
    if (formatErr) return res.status(formatErr.status).json({ error: formatErr.error });
 
    // 2. Read pending doc
    const pendingSnap = await db.doc(`users/${uniqueId}/private/totp-pending`).get();
    if (!pendingSnap.exists) {
      return res.status(400).json({ error: 'No pending setup session' });
    }
 
    const pendingData = pendingSnap.data();
 
    // 3. Check expiry
    if (pendingData.expiresAt < Date.now()) {
      return res.status(400).json({ error: 'Setup session expired' });
    }
 
    // 4. Check attempts (BEFORE incrementing)
    if (pendingData.attempts >= 5) {
      return res.status(429).json({ error: 'Too many attempts' });
    }
 
    // 5. Increment attempts
    await db.doc(`users/${uniqueId}/private/totp-pending`).set({
      ...pendingData,
      attempts: pendingData.attempts + 1,
    });
 
    // 6. Decrypt the pending secret (used both for verify and for re-encrypting on promote)
    const secret = decryptSecret(pendingData.encryptedSecret);
 
    // 7. Replay prevention against any pre-existing enrolled doc
    const existingSnap = await db.doc(`users/${uniqueId}/private/totp`).get();
    if (existingSnap.exists && isReplay(existingSnap.data(), code)) {
      return res.status(401).json({ error: 'Code already used' });
    }
 
    // 8. Verify code (uses pending secret, not enrolled)
    const result = verifySync({ token: code, secret, ...otplibPlugins });
    if (!result.valid) {
      return res.status(401).json({ error: 'Invalid code' });
    }
 
    // 9. Valid: store permanent doc (freshly encrypted), delete pending, set claim, revoke tokens
    await db.doc(`users/${uniqueId}/private/totp`).set({
      encryptedSecret: encryptSecret(secret),
      createdAt: Date.now(),
      lastUsedCode: code,
      lastUsedAt: Date.now(),
    });
    await db.doc(`users/${uniqueId}/private/totp-pending`).delete();
    await setTotpVerifiedClaim(uid);
    await auth.revokeRefreshTokens(uid);
 
    return res.status(200).json({ success: true });
  } catch (err) {
    log.error('portal', 'POST /portal/totp/confirm-setup failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
/**
 * POST /portal/totp/verify — Verify TOTP code after sign-in.
 *
 * Called when a password user with TOTP enrolled signs in and needs
 * to prove possession of their authenticator app. On success, sets
 * the totpVerified custom claim so portal/me returns full dashboard data.
 */
router.post('/portal/totp/verify', authMiddlewareStrict, async (req, res) => {
  try {
    const { uniqueId, uid, token } = req.auth;
    const { code } = req.body || {};
 
    // 1. Validate code format
    const formatErr = validateCodeFormat(code);
    if (formatErr) return res.status(formatErr.status).json({ error: formatErr.error });
 
    // 2. Verify password provider
    if (token.firebase?.sign_in_provider !== 'password') {
      return res.status(400).json({ error: 'Password provider required' });
    }
 
    // 3. Read enrolled TOTP doc
    const enrolled = await readEnrolledTotp(uniqueId);
    if (enrolled.error) return res.status(enrolled.status).json({ error: enrolled.error });
    const totpData = enrolled.data;
 
    // 4. Replay prevention
    if (isReplay(totpData, code)) {
      return res.status(401).json({ error: 'Code already used' });
    }
 
    // 5. Decrypt + verify code
    if (!verifyTotpCode(totpData, code)) {
      return res.status(401).json({ error: 'Invalid code' });
    }
 
    // 6. Update TOTP doc with lastUsedCode/lastUsedAt
    await db.doc(`users/${uniqueId}/private/totp`).set({
      ...totpData,
      lastUsedCode: code,
      lastUsedAt: Date.now(),
    });
 
    // 7. Set totpVerified claim
    await setTotpVerifiedClaim(uid);
 
    return res.status(200).json({ success: true });
  } catch (err) {
    log.error('portal', 'POST /portal/totp/verify failed', { error: err.message });
    return res.status(503).json({ error: 'Internal server error' });
  }
});
 
/**
 * DELETE /portal/totp — Remove TOTP enrollment (re-enrollment flow).
 *
 * A logged-in user who wants to switch their authenticator app. Requires
 * verifying their current TOTP code first. On success, deletes the TOTP doc,
 * clears the totpVerified claim, and revokes all refresh tokens so the user
 * must re-authenticate and re-enroll.
 */
router.delete('/portal/totp', authMiddlewareStrict, async (req, res) => {
  try {
    const { uniqueId, uid } = req.auth;
    const { totpCode } = req.body || {};
 
    // 1. Validate code format
    const formatErr = validateCodeFormat(totpCode);
    if (formatErr) return res.status(formatErr.status).json({ error: formatErr.error });
 
    // 2. Read enrolled TOTP doc
    const enrolled = await readEnrolledTotp(uniqueId);
    if (enrolled.error) return res.status(enrolled.status).json({ error: enrolled.error });
    const totpData = enrolled.data;
 
    // 3. Replay prevention
    if (isReplay(totpData, totpCode)) {
      return res.status(401).json({ error: 'Code already used' });
    }
 
    // 4. Decrypt + verify code
    if (!verifyTotpCode(totpData, totpCode)) {
      return res.status(401).json({ error: 'Invalid code' });
    }
 
    // 5. Delete enrollment and any pending setup
    await db.doc(`users/${uniqueId}/private/totp`).delete();
    await db.doc(`users/${uniqueId}/private/totp-pending`).delete();
 
    // 6. Clear session claim and revoke tokens
    await clearTotpSessionAndRevoke(uid);
 
    return res.status(200).json({ success: true });
  } catch (err) {
    log.error('portal', 'DELETE /portal/totp failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
/**
 * POST /portal/sign-out — Server-side sign-out.
 *
 * Clears the TOTP session claim and revokes all refresh tokens so the
 * client is fully signed out. Other custom claims (e.g. admin) are preserved.
 */
router.post('/portal/sign-out', authMiddlewareStrict, async (req, res) => {
  try {
    await clearTotpSessionAndRevoke(req.auth.uid);
    return res.status(200).json({ success: true });
  } catch (err) {
    log.error('portal', 'POST /portal/sign-out failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
/**
 * POST /portal/revoke-all-sessions — Sign out of all devices.
 *
 * Password users must provide their current TOTP code to prevent
 * unauthorised session revocation with a stolen token.
 * OAuth users bypass TOTP (their provider handles 2FA).
 */
router.post('/portal/revoke-all-sessions', authMiddlewareStrict, async (req, res) => {
  try {
    const { uniqueId, uid, token } = req.auth;
    const { totpCode } = req.body || {};
 
    // Password users must re-prove ownership via TOTP; OAuth users can proceed directly.
    if (token.firebase?.sign_in_provider === 'password') {
      const formatErr = validateCodeFormat(totpCode);
      if (formatErr) return res.status(formatErr.status).json({ error: formatErr.error });
 
      const enrolled = await readEnrolledTotp(uniqueId);
      if (enrolled.error) return res.status(enrolled.status).json({ error: enrolled.error });
      const totpData = enrolled.data;
 
      Iif (isReplay(totpData, totpCode)) {
        return res.status(401).json({ error: 'Code already used' });
      }
 
      if (!verifyTotpCode(totpData, totpCode)) {
        return res.status(401).json({ error: 'Invalid code' });
      }
    }
 
    await clearTotpSessionAndRevoke(uid);
    return res.status(200).json({ success: true });
  } catch (err) {
    log.error('portal', 'POST /portal/revoke-all-sessions failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
// ═══════════════════════════════════════════════════════════════════
// TOTP Recovery Routes (UNAUTHENTICATED)
// ═══════════════════════════════════════════════════════════════════
 
const RECOVERY_OTP_EXPIRY_MS = 10 * 60 * 1000; // 10 minutes
const RECOVERY_MAX_ATTEMPTS = 3;
 
function generateRecoveryOtp() {
  return crypto.randomInt(100000, 999999).toString();
}
 
/**
 * POST /portal/totp-recovery/send — Send a recovery OTP to email.
 *
 * UNAUTHENTICATED. For users who have lost their TOTP device.
 * Always returns 200 regardless of whether the email exists or TOTP
 * is enrolled (anti-enumeration).
 */
router.post('/portal/totp-recovery/send', async (req, res) => {
  try {
    const { email } = req.body || {};
 
    // 1. Validate email field
    if (!email || typeof email !== 'string' || email.trim().length === 0) {
      return res.status(400).json({ error: 'Email is required' });
    }
 
    // 2. Validate email length
    if (email.trim().length > 254) {
      return res.status(400).json({ error: 'Email is too long' });
    }
 
    // 3. Normalize email
    const normalizedEmail = email.trim().toLowerCase();
 
    // 4. Look up Firebase user by email
    let firebaseUser;
    try {
      firebaseUser = await auth.getUserByEmail(normalizedEmail);
    } catch (_err) {
      // User not found → return 200 (anti-enumeration)
      return res.status(200).json({ message: 'Recovery code sent' });
    }
 
    // 5. Check if user has password provider
    const providers = (firebaseUser.providerData || []).map((p) => p.providerId);
    if (!providers.includes('password')) {
      // No password provider → return 200 (same response, no email sent)
      return res.status(200).json({ message: 'Recovery code sent' });
    }
 
    // 6. Resolve uniqueId from Firebase UID
    const snap = await db
      .collection('users')
      .where('firebaseUid', '==', firebaseUser.uid)
      .limit(1)
      .get();
 
    Iif (snap.empty) {
      return res.status(200).json({ message: 'Recovery code sent' });
    }
 
    const uniqueId = snap.docs[0].data().uniqueId;
 
    // 7. Check if TOTP is enrolled
    const totpSnap = await db.doc(`users/${uniqueId}/private/totp`).get();
    if (!totpSnap.exists) {
      return res.status(200).json({ message: 'Recovery code sent' });
    }
 
    // 8. Generate OTP and store recovery code
    const code = generateRecoveryOtp();
    await db.doc(`totpRecoveryCodes/${normalizedEmail}`).set({
      code,
      expiresAt: Date.now() + RECOVERY_OTP_EXPIRY_MS,
      attempts: 0,
    });
 
    // 9. Send email with recovery code
    const template = buildOtpEmail(code);
    await sendEmail(normalizedEmail, template.subject, template.html);
 
    // 10. Return success
    return res.status(200).json({ message: 'Recovery code sent' });
  } catch (err) {
    log.error('portal', 'POST /portal/totp-recovery/send failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
/**
 * POST /portal/totp-recovery/verify — Verify recovery OTP and remove TOTP.
 *
 * UNAUTHENTICATED. On success, removes TOTP enrollment so the user
 * can sign in with just their password and re-enroll.
 */
router.post('/portal/totp-recovery/verify', async (req, res) => {
  try {
    const { email, code } = req.body || {};
 
    // 1. Validate fields
    if (!email || typeof email !== 'string' || email.trim().length === 0) {
      return res.status(400).json({ error: 'Email is required' });
    }
    if (!code || typeof code !== 'string' || code.trim().length === 0) {
      return res.status(400).json({ error: 'Code is required' });
    }
 
    // 2. Normalize email
    const normalizedEmail = email.trim().toLowerCase();
 
    // 3. Read recovery code doc
    const recoveryRef = db.doc(`totpRecoveryCodes/${normalizedEmail}`);
    const recoverySnap = await recoveryRef.get();
 
    if (!recoverySnap.exists) {
      return res.status(401).json({ error: 'Invalid or expired code' });
    }
 
    const recoveryData = recoverySnap.data();
 
    // 4. Check expiry
    if (recoveryData.expiresAt < Date.now()) {
      return res.status(401).json({ error: 'Invalid or expired code' });
    }
 
    // 5. Check attempts (rate limit per code)
    if (recoveryData.attempts >= RECOVERY_MAX_ATTEMPTS) {
      return res.status(429).json({ error: 'Too many attempts' });
    }
 
    // 6. Increment attempts
    await recoveryRef.set({
      ...recoveryData,
      attempts: recoveryData.attempts + 1,
    });
 
    // 7. Check code
    if (recoveryData.code !== code) {
      return res.status(401).json({ error: 'Invalid or expired code' });
    }
 
    // 8. Code matches — consume it
    await recoveryRef.delete();
 
    // 9. Look up Firebase user by email
    const firebaseUser = await auth.getUserByEmail(normalizedEmail);
 
    // 10. Resolve uniqueId from Firebase UID
    const snap = await db
      .collection('users')
      .where('firebaseUid', '==', firebaseUser.uid)
      .limit(1)
      .get();
 
    Eif (!snap.empty) {
      const uniqueId = snap.docs[0].data().uniqueId;
 
      // 11. Delete TOTP enrollment
      await db.doc(`users/${uniqueId}/private/totp`).delete();
 
      // 12. Delete pending TOTP (cleanup)
      await db.doc(`users/${uniqueId}/private/totp-pending`).delete();
    }
 
    // 13. Revoke all refresh tokens
    await auth.revokeRefreshTokens(firebaseUser.uid);
 
    // 14. Return success
    return res.status(200).json({
      success: true,
      message: 'Authenticator removed. Sign in and set up a new one.',
    });
  } catch (err) {
    log.error('portal', 'POST /portal/totp-recovery/verify failed', { error: err.message });
    return res.status(500).json({ error: 'Internal server error' });
  }
});
 
module.exports = router;