Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

73.7 KB
Raw Blame
"""
Authentication routes for login, register, and logout.
Uses SQLCipher encrypted databases with browser password manager support.
"""

from fastapi import APIRouter, Depends, Form, HTTPException, Query, Request
from fastapi.responses import JSONResponse, RedirectResponse

from ..dependencies.auth import require_auth
from ..dependencies.flash import flash
from ..dependencies.rate_limit import (
    INTEGRITY_CHECK_RATE_LIMIT,
    LOGIN_RATE_LIMIT,
    PASSWORD_CHANGE_RATE_LIMIT,
    REGISTRATION_RATE_LIMIT,
    VALIDATE_PASSWORD_RATE_LIMIT,
    limiter,
)
from ..dependencies.template_helpers import render_template

import contextvars
import threading
import time
from datetime import datetime, timezone, UTC

from loguru import logger

from ...database.auth_db import auth_db_session
from ...database.encrypted_db import (
    DatabaseInitializationError,
    MissingLoginProofError,
    capture_login_proof,
    db_manager,
)
from ...database.rekey_lock import (
    RekeyIndeterminateError,
    RekeyLockContentionError,
    RekeyOperationalError,
    describe_rekey_fault,
)
from ...database.models.auth import User
from ...database.thread_local_session import thread_cleanup
from sqlalchemy.exc import IntegrityError
from ..auth.session_manager import (
    UNVALIDATED_LOGOUT_STATE_KEY,
    StaleLoginProofError,
    session_manager,
)  # singleton from session_manager module
from ..server_config import load_server_config

import posixpath
from urllib.parse import (
    unquote,
    unquote_plus,
    urljoin,
    urlparse,
    urlsplit,
    urlunsplit,
)

from ...security.url_validator import URLValidator
from ...security.account_lockout import get_account_lockout_manager
from ...security.password_validator import PasswordValidator
from ...security.log_sanitizer import redact_and_bound_for_log
from .notes import _log_value_preview
from typing import Annotated

router = APIRouter(prefix="/auth", tags=["auth"])


@router.get("/csrf-token")
def get_csrf_token(request: Request):
    """Get CSRF token for API requests."""
    from ..dependencies.csrf import (
        AUTH_CHANGED_HEADER,
        generate_csrf_token,
        get_auth_context,
    )

    context = get_auth_context(request.session)
    expected_context = request.headers.get("X-LDR-Auth-Context")
    if expected_context is not None and expected_context != context:
        # A recovery fetch is tied to the page's original login. Ordinary
        # anonymous token fetches (login/register forms) remain supported.
        return JSONResponse(
            {
                "error": "Your sign-in changed. Reload this page before trying again."
                if context
                else "Authentication required"
            },
            status_code=409 if context else 401,
            headers={"Cache-Control": "no-store"}
            | ({AUTH_CHANGED_HEADER: "1"} if context else {}),
        )
    token = generate_csrf_token(request)
    return JSONResponse(
        {"csrf_token": token, "auth_context": context},
        headers={"Cache-Control": "no-store"},
    )


def _rollback_partial_session(request: Request, username: str) -> None:
    """Tear down a half-created login/registration session.

    Session creation sets the cookie keys (session_id/username) and creates a
    server-side session BEFORE the temp-auth / password-store steps. If a later
    step fails, the caller returns a 500 — but SessionMiddleware would still
    write the partially-set cookie on that response, effectively logging the
    user in despite the reported failure (and leaking a server-side session +
    password entry). Clear the cookie and best-effort tear down the server-side
    session + password-store entry so the reported outcome matches reality.

    Best-effort: cleanup must never mask the caller's original error. Ports the
    Flask fix from #5006 (the FastAPI branch creates sessions inline rather than
    via a shared ``_create_user_session`` helper).
    """
    session_id = request.session.get("session_id")
    request.session.clear()
    if session_id:
        try:
            session_manager.destroy_session(session_id)
            from ...database.session_passwords import session_password_store

            session_password_store.clear_session(username, session_id)
        except Exception as error:
            # Type name only, no traceback: this helper runs inside login
            # and register, whose caller frames hold the plaintext password
            # (see _perform_post_login_tasks for when diagnose renders it).
            logger.warning(
                "Failed to roll back a partial user session ({})",
                type(error).__name__,
            )


def _disconnect_user_sockets(username: str) -> None:
    """Disconnect ALL of ``username``'s live sockets and drop subscriptions.

    A Socket.IO connection is authorised once at handshake and then keeps
    delivering that user's events (including ``settings_changed``) for its
    whole lifetime. Used by the
    password-change flow, which destroys every session for the user, so all
    of their sockets must go. For single-session logout use
    ``_disconnect_session_sockets`` instead so other tabs / devices survive.

    Retargeted from main's deleted Flask ``SocketIOService`` (#5535) onto the
    ASGI socket layer. Best-effort: socket teardown must never break the auth
    flow, and a non-web context (tests, CLI) simply has no loop running.
    """
    try:
        from ..services.socketio_asgi import disconnect_user

        disconnect_user(username)
    except Exception:
        logger.exception(
            f"Failed to disconnect sockets for {_log_value_preview(username)}"
        )


def _disconnect_session_sockets(session_id: str) -> None:
    """Disconnect only the sockets belonging to ``session_id``.

    Used by single-session logout: it tears down the sockets of the session
    being logged out without disconnecting the user's other still-valid
    sessions. Best-effort — socket teardown must never break logout.
    """
    try:
        from ..services.socketio_asgi import disconnect_session

        disconnect_session(session_id)
    except Exception:
        logger.exception("Failed to disconnect sockets for session")


# chat.js pre-fills the chat box from this query parameter of a chat URL.
_CHAT_QUERY_PARAM = "q"


def _opens_chat_page(path: str) -> bool:
    """Whether a redirect to ``path`` would load the chat page.

    The browser removes dot segments (``%2e`` included) from a redirect
    target and the server percent-decodes the path before routing, so the
    check uses the decoded path without dot segments. ``/chat`` counts too:
    the router redirects it to ``/chat/`` and keeps the query. Ignoring
    letter case can only make the check match more paths.
    """
    decoded = posixpath.normpath("/" + unquote(path).lstrip("/")).lower()
    return decoded == "/chat" or decoded.startswith("/chat/")


def _drop_chat_query(target: str, base_url: str) -> str:
    """Remove ``q`` from a login ``next`` that leads to the chat page.

    A link from another site can open
    ``/auth/login?next=/chat/?q=<text>``. After the login POST the browser
    follows this app's own redirect, so the chat page sees a same-origin
    navigation, ``_drop_foreign_chat_query`` (chat.py) keeps ``q``, and
    chat.js would pre-fill the linked text. So the login page drops ``q``
    from such a target, both in the form it renders and in the redirect
    after login. The rest of ``next`` is kept as written, and any other
    target is returned unchanged. This runs alongside, not instead of,
    ``URLValidator.get_safe_redirect_path``.
    """
    try:
        parts = urlsplit(target)
        resolved_path = urlsplit(urljoin(base_url, target)).path
    except ValueError:
        # Unparseable (for example "http://["). get_safe_redirect_path
        # raises on the same input, so it never redirects anywhere.
        return target
    if not parts.query or not _opens_chat_page(resolved_path):
        return target
    kept = [
        item
        for item in parts.query.split("&")
        # Decode the name the way chat.js's URLSearchParams does.
        if unquote_plus(item.split("=", 1)[0]) != _CHAT_QUERY_PARAM
    ]
    return urlunsplit(parts._replace(query="&".join(kept)))


@router.get("/login")
def login_page(request: Request, next: Annotated[str, Query()] = ""):
    """Login page (GET only)."""
    config = load_server_config()
    if request.session.get("username"):
        return RedirectResponse(url="/", status_code=302)

    return render_template(
        request,
        "auth/login.html",
        {
            "has_encryption": db_manager.has_encryption,
            "allow_registrations": config.get("allow_registrations", True),
            "next_page": _drop_chat_query(next, str(request.base_url)),
        },
    )


@router.post("/login")
@limiter.limit(LOGIN_RATE_LIMIT)
def login(
    request: Request,
    username: Annotated[str, Form()] = "",
    password: Annotated[str, Form()] = "",  # noqa: S107
    remember: Annotated[str, Form()] = "false",
    next: Annotated[str, Query()] = "",
):
    """Login handler (POST only). Rate limited to prevent brute force."""
    config = load_server_config()
    username = username.strip()
    remember_bool = remember == "true"

    if not username or not password:
        flash(request, "Username and password are required", "error")
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=400,
        )

    # Check account lockout
    lockout_mgr = get_account_lockout_manager()
    if lockout_mgr.is_locked(username):
        logger.warning(
            f"Login attempt for locked account: {redact_and_bound_for_log(username, 50)}"
        )
        flash(
            request,
            "Account is temporarily locked. Please try again later.",
            "error",
        )
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=429,
        )

    # Opening the database is this route's password check only with SQLCipher.
    # The plaintext development fallback has no independent credential check:
    # a successful open there does not prove the caller knows the account's
    # password. A supported shared plaintext mode needs separate authentication.
    # With SQLCipher, distinguish these two failure modes:
    #   - return None  → credentials invalid OR DB missing → 401, count toward lockout
    #   - raise DatabaseInitializationError → credentials valid but schema
    #     can't be brought up (e.g. world-writable migrations dir tripping
    #     the alembic_runner permission check) → 503, do NOT count toward
    #     lockout. The user's password is correct; punishing them with a
    #     lockout for a server-side configuration problem would be wrong.
    # capture_login_proof records the durable rekey epoch the password was
    # verified against, so the session below is bound to THAT epoch rather
    # than to whatever epoch is committed by the time it is created.
    try:
        with capture_login_proof() as login_proof:
            engine = db_manager.open_user_database(username, password)
    except DatabaseInitializationError:
        logger.warning(
            f"Login refused for {redact_and_bound_for_log(username, 50)}: "
            "database initialisation failed (see traceback above). "
            "Lockout counter NOT incremented — credentials are valid."
        )
        flash(
            request,
            "Database initialisation failed. The server is misconfigured — "
            "please check the server logs or contact the administrator.",
            "error",
        )
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=503,
        )
    except RekeyOperationalError as error:
        # Durable rekey state is indeterminate, pending recovery, or the
        # sidecar metadata cannot be read, or another holder (a password
        # change of a large database, a stopped process) kept the sidecar
        # past the open's wait bound (RekeyLockContentionError). Credentials
        # cannot be validated in any of these cases, so fail closed WITHOUT
        # counting toward lockout — the password may well be correct.
        fault_summary = describe_rekey_fault(error)
        logger.warning(
            f"Login refused for {redact_and_bound_for_log(username, 50)}: "
            f"rekey state requires operator attention ({fault_summary})"
        )
        flash(
            request,
            "Your account database is pending maintenance. "
            "Please try again shortly or contact the administrator.",
            "error",
        )
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=503,
        )

    if engine is None:
        lockout_mgr.record_failure(username)
        logger.warning(
            f"Failed login attempt for username: {redact_and_bound_for_log(username, 50)}"
        )
        flash(request, "Invalid username or password", "error")
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=401,
        )

    # The session is bound to the epoch this login PROVED. A successful
    # open always records a proof for this user, so a missing or foreign
    # proof is an internal inconsistency: refuse (fail closed) rather than
    # create a session with the straddle check skipped. Not a guessing
    # failure, so the lockout counter is left alone.
    try:
        proven_epoch = login_proof.proven_epoch_for(username)
    except MissingLoginProofError:
        logger.warning(
            f"Login refused for {redact_and_bound_for_log(username, 50)}: the database "
            "open recorded no epoch proof for this user"
        )
        flash(
            request,
            "Your account database is pending maintenance. "
            "Please try again shortly or contact the administrator.",
            "error",
        )
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=503,
        )

    # Prevent session fixation
    request.session.clear()

    # Create the session atomically: on any post-creation failure below, roll
    # back the half-set cookie + server-side session so the error isn't a
    # silent login (see _rollback_partial_session / #5006).
    try:
        # Create session
        session_id = session_manager.create_session(
            username, remember_bool, proven_epoch=proven_epoch
        )
        request.session["session_id"] = session_id
        request.session["username"] = username
        # RememberMeMiddleware reads this to decide whether to strip
        # Max-Age/Expires from the session cookie (making it a browser
        # session cookie that's discarded on close).
        request.session["_remember_me"] = remember_bool

        # Store password temporarily for post-login database access
        from ...database.temp_auth import temp_auth_store

        auth_token = temp_auth_store.store_auth(username, password)
        request.session["temp_auth_token"] = auth_token

        # Also store in session password store for metrics access
        from ...database.session_passwords import session_password_store

        session_password_store.store_session_password(
            username, session_id, password
        )
    except StaleLoginProofError:
        # The password was right when it was verified, but a rekey
        # (password change, possibly in another worker) committed before
        # the session could be created: it no longer opens the database.
        # Not a guessing failure, so the lockout counter is left alone:
        # neither incremented nor reset (record_success runs only after a
        # session is issued).
        _rollback_partial_session(request, username)
        logger.warning(
            f"Login refused for {redact_and_bound_for_log(username, 50)}: the password "
            "changed while the login was in flight"
        )
        flash(
            request,
            "Your password was changed while you were signing in. "
            "Please sign in with the current password.",
            "error",
        )
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=401,
        )
    except RekeyOperationalError as error:
        # The durable epoch could not be projected, so the session cannot
        # be bound to it: same answer as the open path's rekey failures.
        _rollback_partial_session(request, username)
        logger.warning(
            f"Login refused for {redact_and_bound_for_log(username, 50)}: rekey epoch "
            f"unavailable at session creation ({type(error).__name__})"
        )
        flash(
            request,
            "Your account database is pending maintenance. "
            "Please try again shortly or contact the administrator.",
            "error",
        )
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=503,
        )
    except Exception:
        _rollback_partial_session(request, username)
        raise

    # Success: only now, with a session actually issued, reset the
    # lockout counter. A login refused at session creation (stale proof,
    # unreadable epoch) must neither count as a failure nor clear the
    # failures already recorded.
    lockout_mgr.record_success(username)
    logger.info(f"User {_log_value_preview(username)} logged in successfully")

    # Defer non-critical post-login work to a background thread so the
    # redirect returns immediately (settings migration, library init,
    # news scheduler notify, and backup scheduling are all idempotent
    # and can safely run after the response). Copy the request context
    # so `get_current_username()` inside the worker resolves the user.
    _login_ctx = contextvars.copy_context()

    def _ctx_post_login():
        _login_ctx.run(
            _perform_post_login_tasks, username, password, session_id
        )

    thread = threading.Thread(target=_ctx_post_login, daemon=True)
    thread.start()

    safe_path = URLValidator.get_safe_redirect_path(next, str(request.base_url))
    if safe_path:
        safe_path = _drop_chat_query(
            safe_path.replace("\\", "/"), str(request.base_url)
        )
        parsed = urlparse(safe_path)
        if not parsed.scheme and not parsed.netloc:
            return RedirectResponse(url=safe_path, status_code=302)
    return RedirectResponse(url="/", status_code=302)


@thread_cleanup
def _perform_post_login_tasks(
    username: str, password: str, session_id: str
) -> None:
    """Run non-critical post-login operations in a background thread.

    Each operation is wrapped in its own try/except so that one failure
    does not prevent the others from running. All operations here are
    idempotent and safe to retry on the next login.

    An outer try/except wraps the whole body so any exception that
    escapes the per-step handlers (for example a failure inside a
    ``with`` context manager's __enter__ / __exit__) is logged instead of
    dying silently in the daemon thread.

    Every handler in this worker and in ``register`` logs a WARNING with
    the exception TYPE only -- never ``logger.exception``, ``exc_info`` or
    ``logger.opt(exception=...)``: these frames hold the plaintext
    password, and a rendered traceback prints frame locals whenever a
    sink has loguru ``diagnose`` on. That is not reliably knowable from
    here: ``config_logger`` enables it on the stderr sink when debug is
    on (from the ``app.debug`` setting or the environment) AND
    ``LDR_LOGURU_DIAGNOSE`` is set, and a process that never ran
    ``config_logger`` keeps loguru's default handler, which has
    ``diagnose`` on.
    """
    try:
        _perform_post_login_tasks_body(username, password, session_id)
    except Exception as err:
        logger.warning(
            "Post-login background thread crashed for user {} ({})",
            redact_and_bound_for_log(username, 50),
            type(err).__name__,
        )


def _perform_post_login_tasks_body(
    username: str, password: str, session_id: str
) -> None:
    """Body of _perform_post_login_tasks — split out so the outer
    try/except in the wrapper catches anything the per-step handlers
    miss. See _perform_post_login_tasks for rationale."""
    total_start = time.perf_counter()

    # 1. Settings version check + migration
    #
    # ATOMICITY INVARIANT: the defaults import and the `app.version`
    # marker MUST be written in one `get_user_db_session(...)` scope
    # with a single terminal `db_session.commit()`. SQLite WAL rollback
    # then guarantees either both land or neither does — the only
    # acceptable states for `db_version_matches_package()` to behave
    # correctly on the next login. Splitting into two commits regresses
    # to the "sticky loop": `app.version` stays unwritten, every
    # subsequent login re-runs the ~498-row bulk insert (app.version is
    # not in default_settings.json, only `update_db_version()` writes
    # it). Do not factor these calls into separate sessions or allow
    # the `import_settings`/`update_db_version` calls to commit
    # internally here — both must be called with `commit=False`.
    step_start = time.perf_counter()
    try:
        from ...settings.manager import SettingsManager
        from ...database.session_context import get_user_db_session

        with get_user_db_session(username, password) as db_session:
            settings_manager = SettingsManager(db_session)
            if not settings_manager.db_version_matches_package():
                logger.info(
                    f"Database version mismatch for {_log_value_preview(username)} "
                    "- loading missing default settings"
                )
                # override_locked: this only adds keys the upgrade
                # introduced, so a locked account still needs it. Spent via
                # a direct import_settings call — the
                # load_from_defaults_file wrapper no longer accepts
                # override_locked (#5841).
                settings_manager.import_settings(
                    settings_manager.default_settings,
                    commit=False,
                    overwrite=False,
                    override_locked=True,
                )
                settings_manager.update_db_version(commit=False)
                db_session.commit()
                logger.info(
                    f"Missing default settings loaded and version "
                    f"updated for user {_log_value_preview(username)}"
                )
    except Exception as err:
        logger.warning(
            f"Post-login settings migration failed for {_log_value_preview(username)}"
            f" ({type(err).__name__})"
        )
    _log_step_duration("step 1 (settings version check)", step_start, username)

    # 2. Initialize library system (source types and default collection)
    step_start = time.perf_counter()
    try:
        from ...database.library_init import initialize_library_for_user

        init_results = initialize_library_for_user(username, password)
        if init_results.get("success"):
            logger.info(
                f"Library system initialized for user {_log_value_preview(username)}"
            )
        else:
            logger.warning(
                f"Library initialization issue for {_log_value_preview(username)}: "
                f"{init_results.get('error', 'Unknown error')}"
            )
    except Exception as err:
        logger.warning(
            f"Post-login library init failed for {_log_value_preview(username)}"
            f" ({type(err).__name__})"
        )
    _log_step_duration("step 2 (library init)", step_start, username)

    # Retry a bounded batch of document chunks left behind if a prior
    # post-commit research deletion was interrupted. This runs after the
    # login response in the existing worker, even when RAG indexing is off.
    step_start = time.perf_counter()
    try:
        from ...research_library.deletion.services.document_deletion import (
            DocumentDeletionService,
        )

        DocumentDeletionService(username).reconcile_orphaned_document_chunks(
            password
        )
    except Exception as err:
        # No traceback: this frame holds the plaintext password.
        logger.warning(
            f"Post-login orphan chunk reconciliation failed for "
            f"{_log_value_preview(username)} ({type(err).__name__})"
        )
    _log_step_duration(
        "step 2a (orphan chunk reconciliation)", step_start, username
    )

    # 3. Update last_login in auth DB + notify news scheduler
    step_start = time.perf_counter()
    try:
        with auth_db_session() as auth_db:
            user = auth_db.query(User).filter_by(username=username).first()
            if user:
                user.last_login = datetime.now(UTC)

            try:
                from ...scheduler.background import (
                    get_background_job_scheduler,
                )

                scheduler = get_background_job_scheduler()
                if scheduler.is_running:
                    scheduler.update_user_info(username, password)
                    logger.info(
                        f"Updated scheduler with user info for {_log_value_preview(username)}"
                    )
            except Exception as scheduler_err:
                logger.warning(
                    "Could not update scheduler on login"
                    f" ({type(scheduler_err).__name__})"
                )

            auth_db.commit()
    except Exception as err:
        logger.warning(
            f"Post-login auth DB update failed for {_log_value_preview(username)}"
            f" ({type(err).__name__})"
        )
    _log_step_duration(
        "step 3 (auth DB + scheduler notify)", step_start, username
    )

    # Model cache refresh is handled by POST /settings/api/available-models;
    # ordinary GET reads use its 24h TTL.

    # 4. Schedule background database backup if enabled
    step_start = time.perf_counter()
    try:
        from ...database.backup import get_backup_executor
        from ...settings.manager import SettingsManager
        from ...database.session_context import get_user_db_session

        with get_user_db_session(username, password) as db_session:
            sm = SettingsManager(db_session)
            backup_enabled = sm.get_setting("backup.enabled", True)

            if backup_enabled:
                max_backups = sm.get_setting("backup.max_count", 1)
                max_age_days = sm.get_setting("backup.max_age_days", 7)

                get_backup_executor().submit_backup(
                    username, password, max_backups, max_age_days
                )
                logger.info(
                    f"Background backup scheduled for user {_log_value_preview(username)}"
                )
    except Exception as err:
        logger.warning(
            f"Post-login backup scheduling failed for {_log_value_preview(username)}"
            f" ({type(err).__name__})"
        )
    _log_step_duration("step 4 (schedule backup)", step_start, username)

    # 6. Reconcile orphan IN_PROGRESS research records. If the server was
    # restarted mid-research, those rows point to dead threads and would
    # otherwise appear as "running forever" in the UI with no way to cancel.
    step_start = time.perf_counter()
    try:
        from ...database.session_context import get_user_db_session
        from ..queue.processor_v2 import queue_processor
        from ..research_state import user_research_start_gate

        # Earlier post-login steps reuse this thread-local session. End any
        # read transaction before taking the process-wide gate -> DB order.
        with get_user_db_session(username, password) as ordering_session:
            ordering_session.rollback()

        # A concurrent start holds this same gate until worker registration,
        # so login reconciliation cannot mistake its fresh durable claim for
        # a server-restart orphan.
        with user_research_start_gate(username):
            with get_user_db_session(username, password) as db_session:
                queue_processor.reconcile_orphan_active_research(
                    username, db_session
                )
    except Exception as err:
        logger.warning(
            f"Post-login research reconciliation failed for {_log_value_preview(username)}"
            f" ({type(err).__name__})"
        )
    _log_step_duration(
        "step 6 (reconcile orphan research)", step_start, username
    )

    # 7. Resume queued researches. The queue processor's wake-list
    # (_users_to_check) is in-memory, so after a server restart any QUEUED
    # rows would never dispatch — registration normally happens at queue
    # time (notify_research_queued), and here at login for recovery. Under
    # Flask this was fed by a before_request hook on every authenticated
    # request; login is the FastAPI-native anchor where username,
    # session_id, and the session password are all freshly available. If
    # the user has nothing queued, the first loop tick deregisters them.
    step_start = time.perf_counter()
    try:
        from ..queue.processor_v2 import queue_processor

        queue_processor.notify_user_activity(username, session_id)
    except Exception as err:
        logger.warning(
            f"Post-login queue-resume registration failed for {_log_value_preview(username)}"
            f" ({type(err).__name__})"
        )
    _log_step_duration("step 7 (resume queued research)", step_start, username)

    total_ms = (time.perf_counter() - total_start) * 1000
    if total_ms > 1000:
        logger.info(
            f"Post-login tasks completed for user {_log_value_preview(username)} "
            f"(total: {total_ms:.0f}ms)"
        )
    else:
        logger.info(
            f"Post-login tasks completed for user {_log_value_preview(username)} ({total_ms:.0f}ms)"
        )


def _log_step_duration(step_label: str, start: float, username: str) -> None:
    """Log post-login step duration at INFO if > 100ms, else DEBUG."""
    elapsed_ms = (time.perf_counter() - start) * 1000
    if elapsed_ms > 100:
        logger.info(
            f"Post-login {step_label} for {_log_value_preview(username)} took {elapsed_ms:.0f}ms"
        )
    else:
        logger.opt(lazy=True).debug(
            "Post-login {} for {} took {:.0f}ms",
            lambda: step_label,
            lambda: _log_value_preview(username),
            lambda: elapsed_ms,
        )


@router.post("/validate-password")
@limiter.limit(VALIDATE_PASSWORD_RATE_LIMIT)
def validate_password(
    request: Request,
    password: Annotated[str, Form()] = "",  # noqa: S107
):
    """Validate password strength via API (used by client-side forms).

    Rate-limited to prevent using this endpoint as a complexity oracle
    against unknown candidate passwords.
    """
    errors = PasswordValidator.validate_strength(password)
    return {"valid": len(errors) == 0, "errors": errors}


@router.get("/register")
def register_page(request: Request):
    """
    Registration page (GET only).
    Not rate limited - viewing the page should always work.
    """
    config = load_server_config()
    if not config.get("allow_registrations", True):
        flash(
            request, "New user registrations are currently disabled.", "error"
        )
        return RedirectResponse(url="/auth/login", status_code=302)

    return render_template(
        request,
        "auth/register.html",
        {
            "has_encryption": db_manager.has_encryption,
            "password_requirements": PasswordValidator.get_requirements(),
        },
    )


@router.post("/register")
@limiter.limit(REGISTRATION_RATE_LIMIT)
def register(
    request: Request,
    username: Annotated[str, Form()] = "",
    password: Annotated[str, Form()] = "",  # noqa: S107
    confirm_password: Annotated[str, Form()] = "",  # noqa: S107
    acknowledge: Annotated[str, Form()] = "false",
):
    """Registration handler (POST only)."""
    config = load_server_config()
    if not config.get("allow_registrations", True):
        flash(
            request, "New user registrations are currently disabled.", "error"
        )
        return RedirectResponse(url="/auth/login", status_code=302)

    username = username.strip()
    acknowledge_bool = acknowledge == "true"

    # Validation
    errors = []

    if not username:
        errors.append("Username is required")
    elif len(username) < 3:
        errors.append("Username must be at least 3 characters")
    elif not username.replace("_", "").replace("-", "").isalnum():
        errors.append(
            "Username can only contain letters, numbers, underscores, and hyphens"
        )

    if not password:
        errors.append("Password is required")
    else:
        errors.extend(PasswordValidator.validate_strength(password))

    if password != confirm_password:
        errors.append("Passwords do not match")

    if not acknowledge_bool:
        errors.append(
            "You must acknowledge that password recovery is not possible"
        )

    # Check if user already exists
    # Use generic error message to prevent account enumeration
    # Note: While this creates a minor timing difference, it's acceptable because:
    # 1. Rate limiting prevents automated timing analysis
    # 2. Generic error message prevents content-based enumeration
    # 3. Local database query timing is minimal (no network calls)
    # 4. Better UX with immediate feedback outweighs minor timing risk
    # See: https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html
    if not errors and username and db_manager.user_exists(username):
        errors.append("Registration failed. Please try a different username.")

    if errors:
        for error in errors:
            flash(request, error, "error")
        return render_template(
            request,
            "auth/register.html",
            {
                "has_encryption": db_manager.has_encryption,
                "password_requirements": PasswordValidator.get_requirements(),
            },
            status_code=400,
        )

    # (#6938: the form username has a minimum length but no maximum, so
    # every registration log line below interpolates this bounded preview,
    # never the raw value.)
    log_username = _log_value_preview(username)

    # Create user in auth database
    with auth_db_session() as auth_db:
        try:
            new_user = User(username=username)
            auth_db.add(new_user)
            auth_db.flush()
            # Capture the PK after flush (the INSERT has run and assigned the
            # id) but before commit: reading it here needs no post-commit
            # refresh SELECT (expire_on_commit), and if the commit below fails
            # nothing is persisted, so there is no orphan to clean up. Cleanup
            # deletes by this id rather than re-querying by username, which
            # could race a concurrent registration reusing the same username.
            new_user_id = new_user.id
            auth_db.commit()
        except IntegrityError:
            # Catch duplicate username specifically (race condition case)
            # This handles the edge case where two requests for the same username
            # pass the user_exists() check simultaneously
            logger.warning(f"Duplicate username attempted: {log_username}")
            auth_db.rollback()
            flash(
                request,
                "Registration failed. Please try a different username.",
                "error",
            )
            return render_template(
                request,
                "auth/register.html",
                {
                    "has_encryption": db_manager.has_encryption,
                    "password_requirements": PasswordValidator.get_requirements(),
                },
                status_code=400,
            )
        except Exception as err:
            logger.warning(
                f"Registration failed for {log_username} while creating the "
                f"auth record"
                f" ({type(err).__name__})"
            )
            auth_db.rollback()
            flash(request, "Registration failed. Please try again.", "error")
            return render_template(
                request,
                "auth/register.html",
                {
                    "has_encryption": db_manager.has_encryption,
                    "password_requirements": PasswordValidator.get_requirements(),
                },
                status_code=500,
            )

    # Create encrypted database for user.
    #
    # This is the only step that can leave an orphaned auth row: the User
    # row is already committed, but no encrypted DB exists yet, so the
    # username is taken (blocking re-registration via user_exists()) while
    # login also fails (no DB to open) — a permanently bricked account.
    # The cleanup scope is intentionally narrow: later failures (session
    # setup, library init) must NOT delete the auth row, because by then
    # the user DB exists and the account is legitimate.
    try:
        db_manager.create_user_database(username, password)
    except Exception as err:
        logger.warning(
            f"Registration failed for {log_username} while creating the "
            f"encrypted database"
            f" ({type(err).__name__})"
        )
        # Delete the orphaned auth row by primary key in a fresh session
        # (the original auth_db session has already closed). Deleting by ID
        # avoids racing a concurrent registration that re-uses the username.
        try:
            with auth_db_session() as cleanup_db:
                orphaned_user = (
                    cleanup_db.query(User).filter_by(id=new_user_id).first()
                )
                if orphaned_user is not None:
                    cleanup_db.delete(orphaned_user)
                    cleanup_db.commit()
                    logger.info(
                        f"Cleaned up orphaned auth entry for {log_username}"
                    )
        except Exception as cleanup_err:
            # Surface a stuck orphan so it's diagnosable, but don't mask the
            # original registration failure returned to the user.
            logger.warning(
                f"Failed to clean up orphaned auth entry for {log_username}"
                f" ({type(cleanup_err).__name__})"
            )
        flash(request, "Registration failed. Please try again.", "error")
        return render_template(
            request,
            "auth/register.html",
            {
                "has_encryption": db_manager.has_encryption,
                "password_requirements": PasswordValidator.get_requirements(),
            },
            status_code=500,
        )

    try:
        # Prevent session fixation by clearing old session data
        request.session.clear()

        # Auto-login after registration
        session_id = session_manager.create_session(username, False)
        request.session["session_id"] = session_id
        request.session["username"] = username
        # Registration auto-login is NOT "remember me": issue a
        # browser-session cookie (discarded on close), matching main's
        # non-permanent session. RememberMeMiddleware only strips the
        # 30-day Max-Age when _remember_me is explicitly False — leaving
        # it unset would persist the cookie for 30 days.
        request.session["_remember_me"] = False

        # Store password temporarily for post-registration database access
        from ...database.temp_auth import temp_auth_store

        auth_token = temp_auth_store.store_auth(username, password)
        request.session["temp_auth_token"] = auth_token

        # Also store in session password store for metrics access
        from ...database.session_passwords import session_password_store

        session_password_store.store_session_password(
            username, session_id, password
        )

        # Notify the news scheduler about the new user
        try:
            from ...scheduler.background import (
                get_background_job_scheduler,
            )

            scheduler = get_background_job_scheduler()
            if scheduler.is_running:
                scheduler.update_user_info(username, password)
                logger.info(
                    f"Updated scheduler with new user info for {log_username}"
                )
        except Exception as err:
            logger.warning(
                "Could not update scheduler on registration"
                f" ({type(err).__name__})"
            )

        logger.info(f"New user registered: {log_username}")

        # Initialize library system (source types and default collection)
        from ...database.library_init import initialize_library_for_user

        try:
            init_results = initialize_library_for_user(username, password)
            if init_results.get("success"):
                logger.info(
                    f"Library system initialized for new user {log_username}"
                )
            else:
                logger.warning(
                    f"Library initialization issue for {log_username}: {init_results.get('error', 'Unknown error')}"
                )
        except Exception as err:
            logger.warning(
                f"Error initializing library for new user {log_username}"
                f" ({type(err).__name__})"
            )
            # Don't block registration on library init failure

        return RedirectResponse(url="/", status_code=302)

    except RekeyOperationalError as error:
        # create_session could not project the durable epoch the session
        # must be bound to (a transient sidecar fault, or state that needs
        # the operator): the same retryable 503 the login route answers for
        # it, never a 500. The account and its database exist, so the auth
        # row stays and the user is sent to sign in once the fault clears;
        # the half-set session is rolled back exactly like the 500 below.
        _rollback_partial_session(request, username)
        logger.warning(
            f"Registration auto-login refused for {log_username}: rekey "
            f"epoch unavailable at session creation ({type(error).__name__})"
        )
        flash(
            request,
            "Your account was created, but signing in is temporarily "
            "unavailable. Please sign in again shortly.",
            "error",
        )
        return render_template(
            request,
            "auth/login.html",
            {
                "has_encryption": db_manager.has_encryption,
                "allow_registrations": config.get("allow_registrations", True),
            },
            status_code=503,
        )

    except Exception as err:
        # The encrypted DB already exists at this point, so this failure is in
        # post-creation setup (auto-login / session). The account is created
        # and loginable; do NOT clean up the auth row here (narrow scope).
        # But DO roll back any half-created session so this 500 doesn't leave
        # the user silently logged in (SessionMiddleware would otherwise write
        # the partially-set cookie on this response — see #5006).
        _rollback_partial_session(request, username)
        logger.warning(
            f"Registration failed for {log_username} after database creation "
            f"(post-creation session/login setup)"
            f" ({type(err).__name__})"
        )
        flash(request, "Registration failed. Please try again.", "error")
        return render_template(
            request,
            "auth/register.html",
            {
                "has_encryption": db_manager.has_encryption,
                "password_requirements": PasswordValidator.get_requirements(),
            },
            status_code=500,
        )


@router.post("/logout")
def logout(request: Request):
    """
    Logout handler.
    Clears session and closes database connections.
    POST-only to prevent CSRF-triggered logout via GET (e.g. <img src="/auth/logout">).
    """
    username = request.session.get("username")
    session_id = request.session.get("session_id")

    state = request.scope.get("state")
    if (
        username
        and isinstance(state, dict)
        and state.get(UNVALIDATED_LOGOUT_STATE_KEY)
        and not session_manager.session_belongs_to(session_id, username)
    ):
        # DatabaseMiddleware let this logout through without validating the
        # cookie (the durable epoch was unreadable). The user-wide teardown
        # below runs only for a cookie whose server-side record still binds
        # it to this username; any other cookie just loses itself, which is
        # what the validating middleware would have done anyway.
        request.session.clear()
        return RedirectResponse(url="/auth/login", status_code=302)

    if username:
        # LOGOUT CLEANUP ORDER (order matters):
        # 1. Unregister from news scheduler — removes the password from the
        #    scheduler's user_sessions dict and cancels scheduled jobs.
        # 2. Invalidate the server-side session AND clear every credential
        #    store for this user — BEFORE closing the DB, so the
        #    always-running queue processor / scheduler can't read a stale
        #    password and reopen the DB in the close→clear window. This also
        #    makes the socket connect gate (validate_session at handshake)
        #    reject any NEW socket from this moment on.
        # 3. Disconnect this session's live WebSocket connections — sockets are
        #    authorised once at handshake and never re-checked, so without this
        #    a socket connected before logout keeps receiving the user's events.
        #    Scoped to THIS session so the user's other tabs / devices stay
        #    connected. MUST run AFTER step 2 so the socket connect gate
        #    above already rejects new sockets before we tear down the
        #    existing ones.
        # 4. Close the database connection — unless the user still has research
        #    running (closing then would make the log-queue drain silently drop
        #    that job's logs; the idle sweeper closes it once the job ends).
        # 5. Drop per-user lock-dict entries.
        # 6. Clear the Flask session dict.
        try:
            from ...scheduler.background import (
                get_background_job_scheduler,
            )

            sched = get_background_job_scheduler()
            if sched.is_running:
                sched.unregister_user(username)
        except Exception:
            logger.warning("Could not unregister user from scheduler")

        # Step 2: invalidate the server session and clear EVERY credential
        # store for this user, BEFORE closing the DB. The always-running
        # queue processor / scheduler read the session password store to
        # reopen a user's DB, so clearing only after the close leaves a
        # window where a concurrent tick resurrects the connection — and
        # even resumes a queued research — after logout.
        try:
            if session_id:
                session_manager.destroy_session(session_id)

            # Clear EVERY stored password for this user, not just this
            # session's entry. Re-logging in without an explicit logout
            # replaces the cookie's session_id and orphans the previous
            # entry; because password lookup falls back to a
            # username-wide scan (get_any_session_password), a single
            # orphan lets a captured pre-logout cookie re-open the
            # decrypted DB after logout. Matches change-password, and is
            # broader than main's per-session clear_session for that reason.
            from ...database.session_passwords import (
                session_password_store,
            )

            session_password_store.clear_all_for_user(username)

            # Same reasoning, second store. Ordinary synchronous requests
            # clear their worker-local credential through
            # WorkerCleanupAPIRoute, but logout must also remove entries held
            # by other workers currently acting for this user. One thread
            # cannot clear another thread's ``threading.local`` directly.
            from ...database.thread_local_session import (
                clear_user_credentials,
            )

            clear_user_credentials(username)
        except Exception:
            logger.exception(
                "session cleanup failed during logout — continuing"
            )

        # Step 3: disconnect THIS session's live sockets (#5535). A socket
        # is authorised once at handshake and never re-checked, so without
        # this one connected before logout keeps receiving the user's
        # events. Scoped to this session so other tabs / devices survive.
        # MUST run after step 2: the connect gate already rejects new
        # sockets by now, which closes the reconnect-in-teardown-window
        # race — were sockets torn down first, one connecting in the gap
        # would still pass the gate and survive the logout.
        if session_id:
            _disconnect_session_sockets(session_id)

        # Step 4: close the DB connection — but not while the user still has
        # research running. A running job keeps producing logs, and the
        # log-queue drain no longer reopens a closed DB, so closing here
        # would silently drop that job's log rows. The idle-connection
        # sweeper closes it once the job finishes.
        #
        # The active-research check and the close run under this user's
        # ``user_research_start_gate`` — the SAME per-user gate
        # ``check_and_start_research`` acquires to register a new research
        # thread, and that ``change_password`` holds across its rekey.
        # Holding it here closes two check-then-act races:
        #   (a) a concurrent same-user ``change_password`` rekey (another
        #       tab) could have its in-flight engine disposed by this close
        #       mid-rekey; sharing the gate serialises the two.
        #   (b) a research STARTING between the check and the close could
        #       have its freshly-opened DB closed underneath it and silently
        #       lose logs; holding the gate means a concurrent
        #       ``check_and_start_research`` either registers before our
        #       check (so we see it active and skip the close) or blocks
        #       until after it.
        # Lock ordering is preserved: gate BEFORE ``_lock`` (via
        # ``get_usernames_with_active_research``) and before the DB layer's
        # ``_connections_lock`` (via ``close_user_database``) — the same
        # order used everywhere else, so no deadlock.
        #
        # Guarded so a failure here (e.g. SQLCipher cleanup error) doesn't
        # skip the session-clear below, which would leave the user still
        # authenticated on the client even though we logged a successful
        # logout above.
        try:
            from ..routes.globals import (
                get_usernames_with_active_research,
                user_research_start_gate,
            )

            with user_research_start_gate(username):
                if username not in get_usernames_with_active_research():
                    db_manager.close_user_database(username)
        except Exception:
            logger.exception(
                "close_user_database failed during logout — continuing"
            )

        # Drop per-user lock-dict entries (library-init, backup,
        # queue-processor critical sections). Matches the cleanup
        # done by the idle-connection sweeper; without this, those
        # three module-level dicts accumulate one entry per username
        # across the process lifetime.
        from ..auth.connection_cleanup import (
            _evict_collection_engines_cache,
            _pop_per_user_locks,
        )

        _pop_per_user_locks(username)

        # Same per-user residue as the lock dicts above, dropped on the
        # same teardown paths (this one, password change, the idle
        # sweeper): the user's cached collection names/descriptions
        # (#6609). Best-effort: logout does not stop a running research
        # (the DB-close step above defers to it), and that research can
        # refill the entry afterwards. Such an entry then expires under
        # the TTL like any other.
        _evict_collection_engines_cache(username)

        # Always wipe the cookie session so the browser is genuinely
        # logged out even if earlier cleanup raised.
        request.session.clear()

        logger.info(f"User {_log_value_preview(username)} logged out")
        flash(request, "You have been logged out successfully", "info")

    if "application/json" in request.headers.get("accept", ""):
        return JSONResponse({"success": True})
    return RedirectResponse(url="/auth/login", status_code=302)


@router.get("/check")
def check_auth(request: Request):
    """
    Check if user is authenticated (for AJAX requests).

    Catch the authentication failure locally so this endpoint always answers
    JSON, including for callers without an Accept header. Share the actual
    authentication checks with protected routes, including credential loss
    while a shared database connection is still open.
    """
    try:
        username = require_auth(request)
    except HTTPException as exc:
        if exc.status_code != 401:
            raise
        return JSONResponse({"authenticated": False}, status_code=401)
    return {"authenticated": True, "username": username}


def _revoke_user_credentials_after_rekey(
    request: Request, username: str
) -> None:
    """Revoke everything that carries or was authorised by the old password.

    Shared by a completed password change and one whose outcome is
    indeterminate (the new key may be active). Mirrors the logout
    handler's cleanup, but for every session of the user, not just this
    one. Each step runs on its own: one that fails is logged (without the
    exception text, which could carry request data) and the others still
    run, so a scheduler fault cannot leave other devices signed in.
    """

    def unregister_scheduler() -> None:
        # Removes the scheduler's stored credential.
        from ...scheduler.background import get_background_job_scheduler

        sched = get_background_job_scheduler()
        if sched.is_running:
            sched.unregister_user(username)

    def pop_per_user_locks() -> None:
        # Drop per-user lock-dict entries (matches logout path).
        from ..auth.connection_cleanup import _pop_per_user_locks

        _pop_per_user_locks(username)

    def evict_collection_engines() -> None:
        # Drop the cached collection search engines (matches logout path;
        # every session ends below).
        from ..auth.connection_cleanup import _evict_collection_engines_cache

        _evict_collection_engines_cache(username)

    def destroy_sessions() -> None:
        session_manager.destroy_all_user_sessions(username)

    def clear_session_passwords() -> None:
        from ...database.session_passwords import session_password_store

        session_password_store.clear_all_for_user(username)

    def clear_thread_credentials() -> None:
        # Per-thread cached credentials hold the OLD plaintext password,
        # which is (or may be) invalid now: a pooled worker that kept it
        # would both retain a dead secret and fail to reopen the database.
        # See clear_user_credentials() for why per-request cleanup cannot
        # reach a worker thread under FastAPI.
        from ...database.thread_local_session import clear_user_credentials

        clear_user_credentials(username)

    def disconnect_sockets() -> None:
        # A socket authorised under an old session must stop receiving this
        # user's events (#5535). Every session is destroyed, so this is the
        # all-sessions scope, unlike logout's per-session one.
        _disconnect_user_sockets(username)

    def clear_request_session() -> None:
        request.session.clear()

    for step in (
        unregister_scheduler,
        pop_per_user_locks,
        evict_collection_engines,
        destroy_sessions,
        clear_session_passwords,
        clear_thread_credentials,
        disconnect_sockets,
        clear_request_session,
    ):
        try:
            step()
        except Exception as error:
            logger.warning(
                f"Credential revocation step {step.__name__} failed for "
                f"{_log_value_preview(username)}: {type(error).__name__}"
            )


@router.get("/change-password")
def change_password_page(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Change password page (GET only).
    Not rate limited - viewing the page should always work.

    Auth via Depends(require_auth) — same gate as the POST handler below
    (change_password), including the is_user_connected() staleness check.
    The manual `session.get("username")` check this replaces only verified
    a cookie existed, not that the session still had a live DB connection.
    """
    return render_template(
        request,
        "auth/change_password.html",
        {
            "password_requirements": PasswordValidator.get_requirements(),
        },
    )


@router.post("/change-password")
@limiter.limit(PASSWORD_CHANGE_RATE_LIMIT)
def change_password(
    request: Request,
    username: Annotated[str, Depends(require_auth)],
    current_password: Annotated[str, Form()] = "",  # noqa: S107
    new_password: Annotated[str, Form()] = "",  # noqa: S107
    confirm_password: Annotated[str, Form()] = "",  # noqa: S107
):
    """Change password handler (POST only).

    Auth via Depends(require_auth) — same gate as the rest of the app,
    including any future SessionManager.validate_session() hook. Reading
    request.session["username"] directly bypassed that gate and split
    auth-policy enforcement across two code paths.
    """

    # Validation
    errors = []

    # ``strip()``: a whitespace-only current password is not empty, so it
    # passed this check and reached ``change_password``'s own key validation,
    # which raises ValueError -- a 500 where the form should simply say the
    # field is required (#6375). The new password needs no equivalent: every
    # whitespace-only value fails validate_strength below.
    if not current_password.strip():
        errors.append("Current password is required")

    if not new_password:
        errors.append("New password is required")
    else:
        errors.extend(PasswordValidator.validate_strength(new_password))

    if new_password != confirm_password:
        errors.append("New passwords do not match")

    if current_password == new_password:
        errors.append("New password must be different from current password")

    if errors:
        for error in errors:
            flash(request, error, "error")
        return render_template(
            request,
            "auth/change_password.html",
            {
                "password_requirements": PasswordValidator.get_requirements(),
            },
            status_code=400,
        )

    # Do NOT rekey the encrypted DB while a research job is running. The rekey
    # (close -> PRAGMA rekey -> close) needs exclusive access, and a running
    # job holds a session bound to the pre-rekey engine with the now-invalid
    # old password — it can't recover, and a concurrent rekey risks corrupting
    # the whole encrypted DB. Unlike logout's DB close, the destructive rekey
    # can't be deferred, so reject until research finishes.
    #
    # The check and the rekey run under this user's ``user_research_start_gate``
    # — the SAME per-user gate ``check_and_start_research`` (routes/globals.py)
    # acquires to register and start a new research thread for this user.
    # Without holding it, a job could start in the gap between the check below
    # and the rekey actually happening, undetected by the check but still
    # racing the rekey. Holding the gate across both closes that TOCTOU window:
    # no research can start FOR THIS USER while we hold it.
    #
    # This is a PER-USER gate, NOT the process-global ``_lock`` (routes/globals.py):
    # the SQLCipher ``PRAGMA rekey`` can take seconds, and that global lock is
    # taken on every research progress/log update, so holding it across the
    # rekey would freeze EVERY user's active research for the duration. The
    # per-user gate blocks only this user's new research.
    #
    # RESIDUAL (concurrent same-user writers): the gate serialises the rekey
    # against this user's NEW research starts, and the active-research check
    # below rejects the rekey outright while this user has a research WORKER
    # running (so that worker's metrics/log writers are excluded too). It does
    # NOT serialise same-user background writers that open/write the user's
    # engine WITHOUT going through check_and_start_research — the news-scheduler
    # job (get_user_db_session) and the async log-drain
    # (_write_log_to_database). Those can still INSERT concurrently with the
    # PRAGMA rekey. Fully serialising them would require a per-user read/write
    # lock taken on the metrics/log hot path inside the database layer
    # (encrypted_db.py) — a cross-layer lock the web-layer gate can't provide
    # without a fragile import cycle, and a change with real deadlock/perf risk.
    # Given the narrow window (a manual, rare, single-user action) this is left
    # as a documented residual rather than hardened here.
    from ..routes.globals import (
        get_usernames_with_active_research,
        user_research_start_gate,
    )

    with user_research_start_gate(username):
        if username in get_usernames_with_active_research():
            # Flask signatures here (flash(msg, cat), render_template(name,
            # **ctx), and a `, 409` tuple return) came in with main's #5538
            # hunk. All three raise under FastAPI, so this rejection branch
            # 500'd instead of returning 409 -- the gate still serialised
            # correctly around the crash, but the user got no usable answer.
            flash(
                request,
                "Cannot change your password while research is in progress. "
                "Wait for it to finish or cancel it, then try again.",
                "error",
            )
            return render_template(
                request,
                "auth/change_password.html",
                {
                    "password_requirements": PasswordValidator.get_requirements(),
                },
                status_code=409,
            )

        # Attempt password change (the rekey) — holds only this user's gate.
        outcome_indeterminate = False
        try:
            success = db_manager.change_password(
                username, current_password, new_password
            )
        except ValueError:
            # change_password validates both keys before it rekeys and
            # raises on an invalid one. The form checks above should have
            # caught anything a browser can send, so this is the backstop
            # that keeps a missed shape a 400 rather than a 500 with a
            # traceback. Nothing has been rekeyed on this path.
            #
            # The exception is not logged: its message is built from the
            # password fields this handler holds in plaintext, and the
            # repository's logging guard rejects an exception variable here
            # for exactly that reason. The fact and the user are what an
            # operator needs; the reason is one of the two guards in
            # change_password.
            logger.warning(
                f"Password change rejected for {_log_value_preview(username)}: the rekey path "
                "refused one of the submitted password fields"
            )
            flash(
                request,
                "Password change rejected: check the current and new password "
                "fields and try again.",
                "error",
            )
            return render_template(
                request,
                "auth/change_password.html",
                {
                    "password_requirements": PasswordValidator.get_requirements(),
                },
                status_code=400,
            )
        except RekeyLockContentionError:
            # Another password change (or a gate holder) owns this user's
            # rekey lifecycle right now. Nothing was rekeyed here.
            logger.warning(
                f"Password change contended for {_log_value_preview(username)}: "
                "another rekey lifecycle owns the lock"
            )
            flash(
                request,
                "A password change is already in progress for this account. "
                "Please wait for it to finish and try again.",
                "error",
            )
            return render_template(
                request,
                "auth/change_password.html",
                {
                    "password_requirements": PasswordValidator.get_requirements(),
                },
                status_code=409,
            )
        except RekeyIndeterminateError:
            # The rekey may have taken effect: the database layer could not
            # prove which key is active. Treat every old-password credential
            # as revoked exactly as on success (all sessions on every device,
            # both password stores, the scheduler's copy, live sockets and
            # this request's session), because the old password may no
            # longer be the key. The next login resolves the pending state
            # with whichever password actually opens the database, and that
            # recovery purges backups left under the superseded key
            # (DatabaseManager._purge_superseded_key_backups). No backup is
            # purged or refreshed here: neither key is proven.
            # Handled below, after this user's research-start gate is
            # released, like the success path.
            outcome_indeterminate = True
            success = False
        except RekeyOperationalError:
            # Unavailable (pre-ambiguity): the old key provably stayed
            # active and nothing was rekeyed, or another sidecar fault
            # stopped the change before it started. The user retries.
            logger.warning(
                f"Password change failed for {_log_value_preview(username)}: rekey outcome "
                "unavailable"
            )
            flash(
                request,
                "Password change could not be completed safely. The database "
                "was left fenced; please try again shortly or contact the "
                "administrator if this repeats.",
                "error",
            )
            return render_template(
                request,
                "auth/change_password.html",
                {
                    "password_requirements": PasswordValidator.get_requirements(),
                },
                status_code=503,
            )

    if outcome_indeterminate:
        logger.warning(
            f"Password change outcome indeterminate for "
            f"{_log_value_preview(username)}: revoking all sessions; the "
            "next login resolves the pending rekey"
        )
        _revoke_user_credentials_after_rekey(request, username)
        flash(
            request,
            "Your password change may have completed. Try your NEW "
            "password first; if it does not work, use your old one.",
            "warning",
        )
        return RedirectResponse(url="/auth/login", status_code=302)

    if success:
        # The rekey is the ONLY step needed.  The auth database stores no
        # password hash — login works by attempting to decrypt the user's
        # SQLCipher database.  Do NOT add an auth-DB password-hash update
        # here; it would fail (User model has no set_password method) and
        # is architecturally unnecessary.

        # Clean up stale credentials before clearing session
        # (mirrors logout handler cleanup steps 1–5).

        # 1. No database close here. change_password() already detached and
        # disposed the old-password engine BEFORE the rekey, under the
        # user's rekey sidecar lock that every cold open also takes, so no
        # old-key engine can be cached once it returns True. The only
        # engine that can be cached by now is one a concurrent
        # open_user_database(new_password) published after the rekey
        # committed. That engine is valid, and closing it here would only
        # force the next request to cold-open again.

        # 2. Purge old backups (encrypted with old key) and create
        # a fresh backup with the new key.  Old-key backups are a
        # security risk per NIST SP 800-57 / OWASP A02 — they remain
        # decryptable with the compromised password.
        #
        # The manager reacquires the rekey sidecar and requires the epoch
        # this change committed before deleting anything, so a concurrent
        # (cross-process) change's fresh backup is never classified against
        # a stale epoch. It returns None when it skipped the purge (a later
        # change owns the epoch, the change was not fenced, or the backup
        # service targets another database); the backup status then records
        # that old-key backups may remain.
        try:
            result = db_manager.refresh_backups_after_password_change(
                username, new_password
            )
            if result is None:
                logger.info(
                    f"Backup refresh after password change skipped for "
                    f"{_log_value_preview(username)}; backups under the old "
                    "key may remain until a later purge"
                )
            elif result.success and not result.purge_incomplete:
                logger.info(
                    f"Backups refreshed after password change for {_log_value_preview(username)}"
                )
            elif result.success:
                logger.warning(
                    f"New backup created after password change for "
                    f"{_log_value_preview(username)}, but backups under the "
                    "old key may remain"
                )
            else:
                # Whether the old-key backups were all removed is recorded
                # separately in the backup status (purge_incomplete).
                logger.error(
                    f"Post-password-change backup failed for {_log_value_preview(username)}: "
                    f"{result.error}. See the backup status for whether "
                    "old-key backups remain."
                )
        except RekeyLockContentionError:
            # Same rule as below: no traceback, the frame holds the
            # passwords.
            logger.warning(
                f"Backup refresh after password change for "
                f"{_log_value_preview(username)} could not take the rekey "
                "lock; backups under the old key may remain"
            )
        except Exception as error:
            # Type name only, no traceback: this frame holds
            # current_password, new_password and confirm_password in its
            # locals, and a rendered traceback (loguru diagnose) would
            # print them.
            logger.warning(
                f"Could not refresh backups after password change "
                f"for {_log_value_preview(username)} ({type(error).__name__})"
            )

        # 3. Revoke every old-password credential and session.
        _revoke_user_credentials_after_rekey(request, username)

        logger.info(f"Password changed for user {_log_value_preview(username)}")
        flash(
            request,
            "Password changed successfully. Please login with your new password.",
            "success",
        )
        return RedirectResponse(url="/auth/login", status_code=302)
    flash(request, "Current password is incorrect", "error")
    return render_template(
        request,
        "auth/change_password.html",
        {
            "password_requirements": PasswordValidator.get_requirements(),
        },
        status_code=401,
    )


@router.get("/integrity-check")
@limiter.limit(INTEGRITY_CHECK_RATE_LIMIT)
def integrity_check(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Check database integrity for current user.

    Uses Depends(require_auth) for the auth gate — single source of
    truth, consistent with every other authed endpoint. Rate-limited
    (unlike a plain lookup, this runs SQLite's full `integrity_check`
    scan, not the cheap `quick_check`) so a signed-in user can't
    re-trigger the expensive path back-to-back.
    """
    is_valid = db_manager.check_database_integrity(username)

    return {
        "username": username,
        "integrity": "valid" if is_valid else "corrupted",
        "timestamp": datetime.now(timezone.utc).isoformat(),
    }